Skip to content

Commit

Permalink
remove dependency on svg-as-symbol-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Oct 5, 2024
1 parent d30c3eb commit b9f24e3
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 217 deletions.
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.
35 changes: 27 additions & 8 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,15 +16,10 @@ 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) {
function preprocessVizSVG(svgString: string): string {
const svg = stringToSvg(svgString);

//Add Relay and Deprecated icons
Expand All @@ -29,8 +28,8 @@ function preprocessVizSVG(svgString: string) {
defs = document.createElementNS(svgNS, 'defs');
svg.insertBefore(defs, svg.firstChild);
}
defs.appendChild(stringToSvg(DeprecatedIconSvg));
defs.appendChild(stringToSvg(RelayIconSvg));
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 @@ -122,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.
12 changes: 2 additions & 10 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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 2 additions & 10 deletions tests/demo.spec.ts-snapshots/demo-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.
12 changes: 2 additions & 10 deletions tests/demo.spec.ts-snapshots/github-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

0 comments on commit b9f24e3

Please sign in to comment.