Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

feat: Support Links in Exported SVG (#4791)

Jesse Jurman46e43baad1d1
diff --git a/src/renderer/renderElement.ts b/src/renderer/renderElement.ts
index 126d667771..6db858d9a3 100644
--- a/src/renderer/renderElement.ts
+++ b/src/renderer/renderElement.ts
@@ -836,6 +836,18 @@ export const renderElementToSvg = (
const cy = (y2 - y1) / 2 - (element.y - y1);
const degree = (180 * element.angle) / Math.PI;
const generator = rsvg.generator;
+
+ // element to append node to, most of the time svgRoot
+ let root = svgRoot;
+
+ // if the element has a link, create an anchor tag and make that the new root
+ if (element.link) {
+ const anchorTag = svgRoot.ownerDocument!.createElementNS(SVG_NS, "a");
+ anchorTag.setAttribute("href", element.link);
+ root.appendChild(anchorTag);
+ root = anchorTag;
+ }
+
switch (element.type) {
case "selection": {
// Since this is used only during editing experience, which is canvas based,
@@ -863,7 +875,7 @@ export const renderElementToSvg = (
offsetY || 0
}) rotate(${degree} ${cx} ${cy})`,
);
- svgRoot.appendChild(node);
+ root.appendChild(node);
break;
}
case "line":
@@ -898,7 +910,7 @@ export const renderElementToSvg = (
}
group.appendChild(node);
});
- svgRoot.appendChild(group);
+ root.appendChild(group);
break;
}
case "freedraw": {
@@ -923,7 +935,7 @@ export const renderElementToSvg = (
path.setAttribute("fill", element.strokeColor);
path.setAttribute("d", getFreeDrawSvgPath(element));
node.appendChild(path);
- svgRoot.appendChild(node);
+ root.appendChild(node);
break;
}
case "image": {
@@ -944,7 +956,7 @@ export const renderElementToSvg = (
symbol.appendChild(image);
- svgRoot.prepend(symbol);
+ root.prepend(symbol);
}
const use = svgRoot.ownerDocument!.createElementNS(SVG_NS, "use");
@@ -965,7 +977,7 @@ export const renderElementToSvg = (
}) rotate(${degree} ${cx} ${cy})`,
);
- svgRoot.appendChild(use);
+ root.appendChild(use);
}
break;
}
@@ -1012,7 +1024,7 @@ export const renderElementToSvg = (
text.setAttribute("direction", direction);
node.appendChild(text);
}
- svgRoot.appendChild(node);
+ root.appendChild(node);
} else {
// @ts-ignore
throw new Error(`Unimplemented type ${element.type}`);
diff --git a/src/tests/fixtures/elementFixture.ts b/src/tests/fixtures/elementFixture.ts
index 0b84d0afa5..eca71256d1 100644
--- a/src/tests/fixtures/elementFixture.ts
+++ b/src/tests/fixtures/elementFixture.ts
@@ -37,3 +37,8 @@ export const diamondFixture: ExcalidrawElement = {
...elementBase,
type: "diamond",
};
+export const rectangleWithLinkFixture: ExcalidrawElement = {
+ ...elementBase,
+ type: "rectangle",
+ link: "excalidraw.com",
+};
diff --git a/src/tests/scene/__snapshots__/export.test.ts.snap b/src/tests/scene/__snapshots__/export.test.ts.snap
index c75f2b2f30..c0290f7570 100644
--- a/src/tests/scene/__snapshots__/export.test.ts.snap
+++ b/src/tests/scene/__snapshots__/export.test.ts.snap
@@ -71,6 +71,25 @@ exports[`exportToSvg with default arguments 1`] = `
</svg>
`;
+exports[`exportToSvg with elements that have a link 1`] = `
+"
+ <!-- svg-source:excalidraw -->
+
+ <defs>
+ <style>
+ @font-face {
+ font-family: \\"Virgil\\";
+ src: url(\\"https://excalidraw.com/Virgil.woff2\\");
+ }
+ @font-face {
+ font-family: \\"Cascadia\\";
+ src: url(\\"https://excalidraw.com/Cascadia.woff2\\");
+ }
+ </style>
+ </defs>
+ <a href=\\"excalidraw.com\\"><g stroke-linecap=\\"round\\" transform=\\"translate(10 10) rotate(0 107 107)\\"><path d=\\"M0 0 C0 0, 0 0, 0 0 M0 0 C0 0, 0 0, 0 0 M-0.81 6.83 C1.42 4.82, 3.76 1.52, 5.21 0.19 M-0.15 6.66 C1.8 4.06, 3.47 2.78, 5.22 0.73 M-0.29 10.96 C2.44 8.61, 6.86 4.4, 10.93 -0.91 M0.52 11.4 C3.45 8.64, 6.67 5.66, 10.55 0.52 M-0.73 16.51 C7.35 10.86, 13.5 3, 17.44 0.95 M0.2 18.45 C5.73 11.79, 11.18 5.08, 16.02 -0.18 M0.75 25.53 C5.82 17, 10.04 11.07, 20.53 0.11 M0.63 24.62 C6.15 16.7, 12.35 9.33, 21.67 -0.92 M-0.46 31.29 C10.67 19.56, 20.95 6.14, 26.3 0.97 M0.19 30.33 C6.15 22.94, 12.8 16.86, 26.48 0.56 M0.06 38.68 C9.21 29.05, 14.84 21.35, 32.64 0.53 M0.13 37.62 C11.7 22.94, 22.8 8.94, 31.86 0.17 M1.97 42.03 C14.26 29.52, 23.97 12.83, 36.26 1.93 M0.5 42.42 C7.49 34.15, 14.87 25.57, 36.32 -0.12 M-1.55 47.05 C9.52 36.22, 20.47 27.66, 42.25 0.23 M0.07 48.87 C15.17 30.66, 32.72 11.39, 42.55 -0.3 M1 53.34 C15.85 39.16, 30.2 22.36, 47.03 0.12 M0.83 54.24 C13.32 41.61, 24.32 26.03, 48.09 -0.05 M-1.51 60.18 C18.44 42.68, 33.89 21.16, 51.91 1.7 M0.58 61.04 C19.82 38.42, 41.63 15.34, 52.4 -0.74 M-2 68.97 C17.08 50.31, 33.78 31.07, 58 -1.79 M-0.57 67.89 C12.79 53.01, 23.57 38.56, 57.54 0.78 M-0.94 73.26 C17.3 51.99, 37.15 29.16, 63.79 1.79 M-0.34 73.88 C13.12 57.81, 25.23 44.29, 63.24 0.24 M-0.02 80.75 C25.47 53.17, 50.04 23.22, 68.13 1.24 M-0.31 79 C13.15 62.83, 27.89 46.91, 69.58 0.16 M-1.36 83.81 C22.43 62.66, 42.64 36.29, 75.7 -0.26 M0.49 84.94 C20.19 61.39, 38.84 39.35, 74.08 -0.14 M-0.44 93.19 C29.84 56.59, 58.57 21.34, 80.05 0.63 M0.23 91.29 C16.75 73.26, 34.07 51.65, 80.34 -0.29 M-1.32 96.52 C29.7 64.08, 56.68 31.83, 85.06 1.15 M-1.23 98.35 C28.8 63.78, 59.61 29.8, 85.15 -0.85 M1.68 104.37 C24.69 72.49, 52 43.21, 91.99 -1.81 M-0.48 103.54 C31.96 64.64, 64.99 27.22, 89.13 0.87 M-1.77 108.18 C36.54 67.37, 72.5 22.54, 96.92 -1.99 M-0.63 109.78 C22.47 82.36, 45.16 56.53, 95.84 -1.07 M-1.12 114.79 C38.49 70.57, 74.74 30, 101.81 -1.93 M1.19 116.33 C38.03 73.29, 76.06 29.97, 100.42 0.88 M2 120.2 C36.47 78.86, 73.59 37.62, 107.95 0.68 M0.39 120.97 C29.7 88.16, 58.99 53.35, 106.73 -0.13 M-1.32 128.06 C41.12 75.45, 86.81 26, 110.99 -1.18 M-0.57 128.82 C35.82 86.5, 72.74 44.1, 111.91 0.79 M-1.02 135.64 C43.91 82.98, 87.98 32.05, 117.79 -1.37 M-0.27 134.74 C26.27 106.76, 50.22 76.9, 116.93 1.16 M-1.8 139.9 C35.21 104.38, 65.53 62.78, 120.66 1.37 M-0.43 139.71 C38.55 95.61, 78.01 50.56, 122.46 -0.53 M1.76 147.91 C29.11 117.4, 55.37 85.99, 127.68 1.48 M0.53 145.69 C46.44 94.62, 91.7 39.92, 126.74 -0.41 M0.17 150.85 C33.17 115.83, 61.4 80.26, 131.64 0.9 M0.15 151.84 C53.25 90.23, 106.14 30.74, 132.16 -0.36 M-1.84 157.7 C37.59 114.01, 76.78 70.61, 136.63 -0.45 M0.35 158.99 C30.36 122.85, 60.89 88, 137.61 0.03 M-0.23 166.11 C29.43 128.1, 58.73 93.84, 143.89 -0.53 M-0.32 164.8 C28.62 130.08, 57.48 96.97, 144 -0.28 M0.58 169.48 C50.77 111.65, 102.4 55.59, 147.57 0.38 M0.18 171.37 C41.33 123.45, 81.86 77.61, 148.81 0.27 M-0.43 175.5 C42.41 129.13, 83.17 82.4, 153.98 0.49 M-0.38 176.58 C56.14 112.56, 110.58 48.82, 153.05 0 M0.7 184.05 C39.63 137.27, 77.83 89.07, 159.24 -1.15 M-0.04 183.65 C61.15 113.46, 120.25 43.45, 159.1 0.14 M0.78 189.08 C63.86 116.64, 126.59 42.93, 165.06 -0.83 M-0.1 189.24 C43.16 138.18, 87.83 86.83, 164.16 0.56 M1.24 193.84 C36.84 154.67, 72.19 113.57, 170.32 -1.11 M-0.39 194.15 C64.21 119.86, 128.3 45.54, 169.93 0.91 M-0.29 202.73 C49.19 146.43, 96.87 92.17, 176.44 -0.47 M0.13 200.67 C52.92 138.41, 108.19 74.96, 174.6 -0.26 M1.16 206.55 C59.57 139.38, 118.88 71.01, 179.33 -0.46 M-0.07 207.43 C53.03 145.16, 107.35 82.74, 180.4 0.42 M0.22 213.68 C60.95 141.62, 123.88 71.22, 187.21 0.77 M0.6 214.05 C46.1 157.63, 93.55 102.73, 185.97 0.53 M1.26 216.37 C45.53 168.31, 87.98 118.47, 190.76 -1.23 M3.04 216.09 C67.52 139.61, 132.76 63.4, 190.6 -0.35 M7.95 217.48 C67.06 150.79, 124.44 83.38, 196.32 1.03 M7.93 216.35 C53.89 161.92, 100.46 109.21, 196.09 -0.51 M12.62 217.33 C58.21 163, 105.7 107.92, 202.43 -0.59 M13.54 216.66 C71.71 147.52, 130.4 81.02, 201.49 0.82 M18.27 217.27 C66.39 160.5, 115.21 104.07, 208.36 0.27 M18.01 215.72 C58.01 168.33, 99.25 121.93, 206.76 -0.82 M24.9 216.54 C74.66 158.72, 125.24 98.12, 212.06 0.05 M23.39 216.76 C89.76 140.49, 156.61 64.23, 211.91 -0.02 M28.87 215.01 C94.99 140.87, 162.5 64.39, 216.64 2.5 M28.76 215.55 C79.01 159.87, 128.27 103.38, 215.68 1.25 M32.95 216.11 C83.19 160.31, 131.85 106.05, 216.52 8.63 M33.94 217.11 C75.45 168.61, 117.47 121.55, 216.54 8.14 M39.78 217.56 C92.26 159.75, 140.57 102.37, 215.58 12.68 M40.11 216.11 C90.9 157.88, 140.49 99.7, 215.33 14 M43.47 216.97 C104.46 148.92, 163.67 79.54, 215.48 18.1 M44.41 216.74 C89.24 164.57, 133.58 113.45, 215.55 19.53 M50.78 215.1 C93.5 164.92, 139.86 112.9, 214.41 26.53 M49.78 215.99 C110.29 149.66, 168.81 81.69, 215.73 26.76 M57.12 215.58 C107.36 155.81, 157.92 97.78, 215.19 31.92 M54.97 216.03 C113.66 149.14, 173.56 80.85, 215.38 32.06 M61.3 215.31 C95.1 175.75, 132.1 134.8, 216.98 38.89 M61.19 216.77 C109.52 160.05, 158.35 103.94, 216.2 38.35 M67.47 216.77 C115.11 158.21, 167.71 97.2, 216.63 45.21 M66.77 216.96 C102.08 174.52, 137.46 133.67, 215.1 44.3 M70.35 215.08 C103.39 179.45, 138.17 141.98, 216.94 49.42 M72.29 216.59 C103.33 179.45, 132.1 146.13, 215.3 50.78 M77.48 217.53 C112.79 176.25, 148.92 136.21, 214.87 56.96 M76.92 216.34 C126.01 159.84, 176.06 104.61, 216.09 56.89 M83.65 216.59 C109.9 184.76, 137.67 151.42, 216.82 64.24 M81.86 216.8 C115.56 177.43, 148.43 139.03, 215.35 63.31 M86.45 216.6 C117.96 179.57, 150.68 145.05, 215.64 67.24 M86.76 215.76 C133.59 164.04, 179.87 111.6, 216.32 67.66 M92.5 217.18 C130.08 175.3, 164.37 136.62, 217.67 75.31 M93.31 216.93 C121.12 183.96, 149.71 151.9, 216.67 74.37 M97.31 214.67 C122.26 188.24, 148.41 160.47, 217.8 78.71 M97.27 217.16 C133.41 175.31, 167.83 132.89, 216.35 79.87 M102.05 215.76 C128.77 182.73, 159.47 152.36, 217.64 85.73 M103.92 216.03 C140.06 172.81, 177.77 130.15, 216.44 86.54 M108.77 215.29 C145.82 169.8, 184.54 127.33, 214.48 93.3 M108.6 216.14 C152.4 169.17, 193.64 120.21, 215.53 93.61 M113.85 217.99 C138.74 189.67, 160.26 165.2, 215.6 100.97 M113.85 215.19 C144.51 180.53, 176.69 143.07, 215.68 99.56 M119.06 216.26 C139.68 192.15, 161.93 164.32, 216.54 104.97 M120.04 216.13 C141.97 189.71, 164.98 164.4, 216.3 104.47 M124.31 216.62 C147.59 192.15, 170.4 161.51, 217.92 112.68 M123.84 216.01 C151.85 183.74, 178.95 152.19, 216.63 110.91 M130.04 217.74 C153.81 189.32, 176.55 163.35, 216.49 116.35 M130.09 216.38 C146.84 196.06, 166.21 175.33, 216.35 118.08 M135.41 216.47 C160.47 188.02, 182.14 159.88, 213.93 123.66 M135.78 217.64 C155.48 194.76, 172.71 172.66, 216.06 124.66 M139.2 218.28 C157.86 195.92, 177.77 173.69, 215.14 127.67 M140.43 215.93 C161.4 191.88, 182.03 169.94, 215.2 129.17 M146.79 215.06 C162.71 194.09, 180.79 177.42, 214.55 136.84 M144.79 217.28 C160.2 199.12, 174.78 183.05, 215.91 136.41 M151.66 214.81 C171.28 196.29, 187.11 175.48, 216.92 141.52 M151.91 215.67 C176.44 188.45, 199.88 160.32, 215.56 142.26 M154.38 215.47 C172.69 198.01, 192.07 175.16, 216.36 147.74 M155.82 217.21 C174.84 196.04, 193.76 174.8, 215.32 147.85 M160.14 214.61 C179.82 197.41, 195.96 177.17, 217.2 155.66 M162.6 216.26 C174.04 201, 186.86 187.65, 214.67 154.44 M165.72 215.2 C184.79 196.83, 203.71 176.89, 216.23 161.56 M167.04 217.53 C178.06 204.41, 186.16 194.48, 215.28 159.73 M173.01 215.35 C189.21 198.76, 204.39 179.52, 216.99 167.92 M171.74 216.2 C188.55 197.71, 202.6 180.81, 214.93 166.35 M176 218.1 C188.62 206.57, 198.94 193.03, 214.63 173.83 M177.77 216.11 C190.55 201.92, 202.86 186.55, 215.97 172.6 M182.26 217.09 C193.97 201.5, 207.69 185.56, 214.91 176.8 M183.75 215.8 C191.36 207.78, 198.34 197.48, 215.95 178.7 M187.99 218.4 C199.34 205.7, 207.8 191.99, 215.03 183.22 M188.53 216.69 C197.17 205.82, 204.83 195.35, 216.1 185.58 M195.49 214.23 C197.72 209.17, 202.2 205.99, 217.9 190.4 M194.25 215.18 C202.02 207.92, 209.16 198.59, 215.2 191.28 M200.15 216.71 C206.55 209.28, 212.55 200.97, 214.47 196.53 M199.03 216.79 C205.41 210.03, 212.23 201.38, 215.53 197.34 M203.99 215.64 C207.37 213.36, 210.04 206.68, 216.48 203.13 M204.08 216.24 C207 212.46, 211.61 208.42, 215.41 201.74 M209.93 216.23 C210.58 213.66, 212.88 213.24, 215.79 209.03 M209.77 216.09 C211.32 214.48, 213.4 211.72, 215.64 208.76\\" stroke=\\"#15aabf\\" stroke-width=\\"0.5\\" fill=\\"none\\"></path><path d=\\"M-0.39 -0.88 C55.46 -0.97, 113.94 -0.3, 214.26 0.61 M-0.49 0.4 C84.22 -0.65, 169.11 -0.38, 213.87 0.66 M212.35 -0.69 C216.14 81.33, 216.66 160.36, 212.82 212.88 M213.44 -0.28 C214.34 52.63, 212.93 106.93, 213.51 214 M215.36 213.92 C153.36 215.01, 94.72 215.24, 0.66 215.17 M214.59 213.78 C136.22 216.39, 57.75 216.64, 0.1 213.77 M-1.71 215.7 C0.69 160.6, -0.19 109.87, -0.18 1.75 M0.58 213.93 C-0.62 147.47, -0.5 79.36, 0.52 0\\" stroke=\\"#000000\\" stroke-width=\\"1\\" fill=\\"none\\"></path></g></a>"
+`;
+
exports[`exportToSvg with exportEmbedScene 1`] = `
"
<!-- svg-source:excalidraw -->
diff --git a/src/tests/scene/export.test.ts b/src/tests/scene/export.test.ts
index 3267c91192..1b6f72113b 100644
--- a/src/tests/scene/export.test.ts
+++ b/src/tests/scene/export.test.ts
@@ -1,6 +1,10 @@
import { NonDeletedExcalidrawElement } from "../../element/types";
import * as exportUtils from "../../scene/export";
-import { diamondFixture, ellipseFixture } from "../fixtures/elementFixture";
+import {
+ diamondFixture,
+ ellipseFixture,
+ rectangleWithLinkFixture,
+} from "../fixtures/elementFixture";
describe("exportToSvg", () => {
const ELEMENT_HEIGHT = 100;
@@ -112,4 +116,13 @@ describe("exportToSvg", () => {
);
expect(svgElement.innerHTML).toMatchSnapshot();
});
+
+ it("with elements that have a link", async () => {
+ const svgElement = await exportUtils.exportToSvg(
+ [rectangleWithLinkFixture],
+ DEFAULT_OPTIONS,
+ null,
+ );
+ expect(svgElement.innerHTML).toMatchSnapshot();
+ });
});