Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove dependency on svg-as-symbol-loader #427

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions manual-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ declare module '*.svg' {
export default SVG;
}

declare module '*?raw' {
const content: string;
export default content;
}

declare module '*.css' {
const content: any;
export default content;
Expand Down
95 changes: 0 additions & 95 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@
"postcss-loader": "8.1.1",
"postcss-variables-loader": "6.0.0",
"prettier": "3.3.3",
"raw-loader": "4.0.2",
"react": "18.3.1",
"react-dom": "18.3.1",
"style-loader": "4.0.0",
"svg-as-symbol-loader": "1.2.4",
"ts-loader": "9.5.1",
"ts-node": "10.9.2",
"typescript": "5.5.4",
Expand Down
2 changes: 1 addition & 1 deletion src/components/icons/deprecated-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/components/icons/relay-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 34 additions & 11 deletions src/graph/svg-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// eslint-disable-next-line import/no-unresolved
import DeprecatedIconSvg from '../components/icons/deprecated-icon.svg?raw';
// eslint-disable-next-line import/no-unresolved
import RelayIconSvg from '../components/icons/relay-icon.svg?raw';
import { stringToSvg } from '../utils/dom-helpers';
import { getDot } from './dot';
import { VizWorker } from './graphviz-worker';
Expand All @@ -12,22 +16,21 @@ export async function renderSvg(typeGraph: TypeGraph) {
return svg;
}

// eslint-disable-next-line @typescript-eslint/no-require-imports
const RelayIconSvg = require('!!svg-as-symbol-loader?id=RelayIcon!../components/icons/relay-icon.svg');
// eslint-disable-next-line @typescript-eslint/no-require-imports
const DeprecatedIconSvg = require('!!svg-as-symbol-loader?id=DeprecatedIcon!../components/icons/deprecated-icon.svg');

const svgNS = 'http://www.w3.org/2000/svg';
const xlinkNS = 'http://www.w3.org/1999/xlink';

function preprocessVizSVG(svgString: string) {
//Add Relay and Deprecated icons
// eslint-disable-next-line @typescript-eslint/no-base-to-string
svgString = svgString.replace(/<svg [^>]*>/, '$&' + RelayIconSvg);
svgString = svgString.replace(/<svg [^>]*>/, '$&' + DeprecatedIconSvg);

function preprocessVizSVG(svgString: string): string {
const svg = stringToSvg(svgString);

//Add Relay and Deprecated icons
let defs = svg.querySelector('defs');
if (!defs) {
defs = document.createElementNS(svgNS, 'defs');
svg.insertBefore(defs, svg.firstChild);
}
defs.appendChild(svgToSymbol(DeprecatedIconSvg, 'DeprecatedIcon'));
defs.appendChild(svgToSymbol(RelayIconSvg, 'RelayIcon'));

for (const $a of svg.querySelectorAll('a')) {
const $g = $a.parentNode!;

Expand Down Expand Up @@ -118,3 +121,23 @@ function preprocessVizSVG(svgString: string) {
const serializer = new XMLSerializer();
return serializer.serializeToString(svg);
}

function svgToSymbol(svg: string, id: string): SVGSymbolElement {
const $svg = stringToSvg(svg);
const $symbol = document.createElementNS(svgNS, 'symbol');

// Transfer supported attributes <svg> to the <symbol>.
const attributes = ['viewBox', 'height', 'width', 'preserveAspectRatio'];
attributes.forEach(function (attr) {
const value = $svg.getAttribute(attr);
if (value != null) {
$symbol.setAttribute(attr, value);
}
});
$symbol.setAttribute('id', id);

// Move all child nodes from <svg> to the <symbol>
$symbol.append(...$svg.children);

return $symbol;
}
20 changes: 20 additions & 0 deletions tests/demo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ test('use custom SDL with custom directives', async ({ page }) => {
);
});

/* FIXME: need a way to disable "Skip deprecated" (uncheck it in UI) in tests
test('use custom SDL with deprecated fields', async ({ page }) => {
const voyagerPage = await gotoVoyagerPage(page);
// TODO: test deprecated args and input fields
await voyagerPage.submitSDL(`
type Query {
foo: String @deprecated
bar: Int @deprecated(reason: "Just because")
}
`);

expect
.soft(await voyagerPage.getGraphSVG())
.toMatchSnapshot('custom-sdl-with-deprecated-graph.svg');
await expect(voyagerPage.page).toHaveScreenshot(
'display-sdl-with-deprecated.png',
);
});
*/

test('use custom introspection', async ({ page }) => {
test.slow();

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 10 additions & 16 deletions tests/demo.spec.ts-snapshots/custom-sdl-graph-Demo-linux.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading