@takodotid/circuit 0.34.3 → 0.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -14
- package/dist/chunks/index-20kcjxjg.js +174 -0
- package/dist/chunks/index-2r4bbhac.js +977 -0
- package/dist/chunks/index-n7w3tg5y.js +149 -0
- package/dist/chunks/new-w3zys5y4.js +172 -0
- package/dist/chunks/server-gzsf5me5.js +259 -0
- package/dist/cli/index.js +11345 -9024
- package/dist/helpers/index.js +594 -0
- package/dist/index.js +9 -28
- package/dist/types/core/{addresses.d.ts → address-space.d.ts} +23 -7
- package/dist/types/core/address.d.ts +44 -0
- package/dist/types/core/cabling.d.ts +41 -0
- package/dist/types/core/cross-checks.d.ts +28 -0
- package/dist/types/core/define.d.ts +25 -3
- package/dist/types/core/project-files.d.ts +12 -0
- package/dist/types/core/schema-docs.d.ts +13 -4
- package/dist/types/core/secrets.d.ts +62 -21
- package/dist/types/core/{graph.d.ts → topology.d.ts} +32 -12
- package/dist/types/core/utils.d.ts +8 -0
- package/dist/types/core/validator.d.ts +19 -0
- package/dist/types/helpers/bgp.d.ts +97 -0
- package/dist/types/helpers/checks.d.ts +32 -0
- package/dist/types/helpers/exchange.d.ts +49 -0
- package/dist/types/helpers/filters.d.ts +51 -0
- package/dist/types/helpers/index.d.ts +10 -0
- package/dist/types/helpers/route-server.d.ts +52 -0
- package/dist/types/index.d.ts +5 -3
- package/dist/types/integrations/librenms.d.ts +60 -0
- package/dist/types/integrations/netbox.d.ts +89 -0
- package/dist/types/integrations/peeringdb.d.ts +24 -0
- package/dist/types/integrations/prometheus.d.ts +24 -0
- package/dist/types/integrations/registries.d.ts +8 -0
- package/dist/types/platforms/bgp-wait.d.ts +18 -0
- package/dist/types/platforms/catalog.d.ts +109 -0
- package/dist/types/platforms/derive.d.ts +17 -0
- package/dist/types/platforms/fortios/firewall.d.ts +45 -0
- package/dist/types/platforms/fortios/index.d.ts +134 -0
- package/dist/types/platforms/fortios/names.d.ts +52 -0
- package/dist/types/platforms/fortios/plan.d.ts +104 -0
- package/dist/types/platforms/fortios/render.d.ts +75 -0
- package/dist/types/platforms/fortios/routing.d.ts +56 -0
- package/dist/types/platforms/fortios/tree.d.ts +37 -0
- package/dist/types/platforms/index.d.ts +13 -0
- package/dist/types/platforms/junos/bgp.d.ts +44 -0
- package/dist/types/platforms/junos/index.d.ts +114 -0
- package/dist/types/platforms/junos/render.d.ts +154 -0
- package/dist/types/platforms/line-diff.d.ts +67 -0
- package/dist/types/platforms/platform.d.ts +120 -0
- package/dist/types/platforms/raisecom-ros/index.d.ts +71 -0
- package/dist/types/platforms/raisecom-ros/render.d.ts +41 -0
- package/dist/types/platforms/routeros/export.d.ts +59 -0
- package/dist/types/platforms/routeros/index.d.ts +96 -0
- package/dist/types/{adapters/devices → platforms}/routeros/models.d.ts +4 -0
- package/dist/types/platforms/routeros/plan.d.ts +54 -0
- package/dist/types/platforms/routeros/render.d.ts +240 -0
- package/dist/types/platforms/vrp/index.d.ts +83 -0
- package/dist/types/platforms/vrp/render.d.ts +96 -0
- package/dist/types/schema/common.d.ts +12 -6
- package/dist/types/schema/index.d.ts +1 -0
- package/dist/types/schema/interfaces.d.ts +14 -6
- package/dist/types/schema/policy.d.ts +18 -10
- package/dist/types/schema/routing.d.ts +14 -1
- package/dist/types/schema/system.d.ts +16 -6
- package/dist/types/schema/time-zones.d.ts +2 -0
- package/dist/types/transport/console.d.ts +79 -23
- package/dist/types/transport/path-check.d.ts +32 -0
- package/dist/types/transport/ssh.d.ts +17 -15
- package/package.json +17 -11
- package/src/cli/graph-format.ts +91 -0
- package/src/cli/index.ts +1074 -484
- package/src/cli/new.ts +208 -175
- package/src/cli/rename.ts +97 -91
- package/src/core/address-space.ts +320 -0
- package/src/core/address.ts +202 -0
- package/src/core/cabling.ts +154 -0
- package/src/core/cross-checks.ts +364 -0
- package/src/core/define.ts +26 -3
- package/src/core/project-files.ts +35 -0
- package/src/core/schema-docs.ts +138 -130
- package/src/core/secrets.ts +170 -76
- package/src/core/topology.ts +656 -0
- package/src/core/utils.ts +37 -0
- package/src/core/validator.ts +407 -0
- package/src/helpers/bgp.ts +301 -0
- package/src/helpers/checks.ts +178 -0
- package/src/helpers/exchange.ts +117 -0
- package/src/helpers/filters.ts +107 -0
- package/src/helpers/index.ts +12 -0
- package/src/helpers/route-server.ts +212 -0
- package/src/index.ts +6 -3
- package/src/integrations/librenms.ts +291 -0
- package/src/integrations/netbox.ts +620 -0
- package/src/integrations/peeringdb.ts +173 -0
- package/src/integrations/prometheus.ts +90 -0
- package/src/integrations/registries.ts +25 -0
- package/src/platforms/bgp-wait.ts +41 -0
- package/src/platforms/catalog.ts +69 -0
- package/src/platforms/derive.ts +117 -0
- package/src/{adapters/devices → platforms}/fortios/TRAPS.md +6 -0
- package/src/platforms/fortios/firewall.ts +377 -0
- package/src/platforms/fortios/index.ts +1060 -0
- package/src/platforms/fortios/names.ts +130 -0
- package/src/platforms/fortios/plan.ts +929 -0
- package/src/platforms/fortios/render.ts +779 -0
- package/src/platforms/fortios/routing.ts +476 -0
- package/src/platforms/fortios/tree.ts +129 -0
- package/src/platforms/index.ts +36 -0
- package/src/{adapters/devices → platforms}/junos/TRAPS.md +8 -0
- package/src/platforms/junos/bgp.ts +293 -0
- package/src/platforms/junos/index.ts +1006 -0
- package/src/platforms/junos/render.ts +967 -0
- package/src/platforms/line-diff.ts +244 -0
- package/src/platforms/platform.ts +122 -0
- package/src/platforms/raisecom-ros/TRAPS.md +40 -0
- package/src/platforms/raisecom-ros/index.ts +731 -0
- package/src/platforms/raisecom-ros/render.ts +328 -0
- package/src/{adapters/devices → platforms}/routeros/TRAPS.md +16 -2
- package/src/platforms/routeros/export.ts +342 -0
- package/src/platforms/routeros/index.ts +636 -0
- package/src/{adapters/devices → platforms}/routeros/models.ts +2 -0
- package/src/platforms/routeros/plan.ts +444 -0
- package/src/platforms/routeros/render.ts +1483 -0
- package/src/platforms/vrp/TRAPS.md +45 -0
- package/src/platforms/vrp/index.ts +652 -0
- package/src/platforms/vrp/render.ts +615 -0
- package/src/schema/common.ts +15 -6
- package/src/schema/index.ts +1 -0
- package/src/schema/interfaces.ts +15 -5
- package/src/schema/policy.ts +22 -10
- package/src/schema/routing.ts +16 -1
- package/src/schema/system.ts +16 -6
- package/src/schema/time-zones.ts +482 -0
- package/src/studio/data.ts +172 -168
- package/src/studio/git.ts +95 -90
- package/src/studio/public/app.js +161 -150
- package/src/studio/public/compare.js +219 -212
- package/src/studio/public/diff.js +77 -75
- package/src/studio/public/dom.js +118 -108
- package/src/studio/public/evaluate.js +584 -545
- package/src/studio/public/export.js +120 -118
- package/src/studio/public/graph.js +1343 -1266
- package/src/studio/public/net.js +115 -109
- package/src/studio/public/tables.js +1135 -1083
- package/src/studio/public/tools.js +561 -529
- package/src/studio/server.ts +202 -196
- package/src/transport/console.ts +363 -271
- package/src/transport/path-check.ts +91 -0
- package/src/transport/ssh.ts +98 -94
- package/templates/colocation/checks.ts +11 -6
- package/templates/colocation/edge-01.ts +9 -9
- package/templates/colocation/routing.ts +2 -2
- package/templates/colocation/tenancy.ts +3 -3
- package/templates/colocation/tor-01.ts +3 -3
- package/templates/edge-router/checks.ts +4 -4
- package/templates/edge-router/edge-01.ts +5 -5
- package/templates/edge-router/routing.ts +2 -2
- package/templates/project/AGENTS.md +5 -1
- package/templates/single-site/checks.ts +2 -2
- package/dist/chunks/index-47debt16.js +0 -956
- package/dist/chunks/index-v8zgr7wm.js +0 -128
- package/dist/chunks/new-ny89s7y7.js +0 -166
- package/dist/chunks/server-tjbb3rqc.js +0 -251
- package/dist/presets/index.js +0 -394
- package/dist/types/adapters/devices/catalog.d.ts +0 -86
- package/dist/types/adapters/devices/derive.d.ts +0 -7
- package/dist/types/adapters/devices/fortios/firewall.d.ts +0 -23
- package/dist/types/adapters/devices/fortios/index.d.ts +0 -58
- package/dist/types/adapters/devices/fortios/names.d.ts +0 -48
- package/dist/types/adapters/devices/fortios/plan.d.ts +0 -28
- package/dist/types/adapters/devices/fortios/render.d.ts +0 -38
- package/dist/types/adapters/devices/fortios/routing.d.ts +0 -32
- package/dist/types/adapters/devices/fortios/tree.d.ts +0 -33
- package/dist/types/adapters/devices/index.d.ts +0 -3
- package/dist/types/adapters/devices/junos/bgp.d.ts +0 -11
- package/dist/types/adapters/devices/junos/index.d.ts +0 -42
- package/dist/types/adapters/devices/junos/render.d.ts +0 -52
- package/dist/types/adapters/devices/lines.d.ts +0 -56
- package/dist/types/adapters/devices/raisecom-ros/index.d.ts +0 -28
- package/dist/types/adapters/devices/routeros/export.d.ts +0 -39
- package/dist/types/adapters/devices/routeros/index.d.ts +0 -30
- package/dist/types/adapters/devices/routeros/plan.d.ts +0 -4
- package/dist/types/adapters/devices/routeros/render.d.ts +0 -100
- package/dist/types/adapters/devices/types.d.ts +0 -40
- package/dist/types/adapters/devices/vrp/index.d.ts +0 -27
- package/dist/types/adapters/registries/index.d.ts +0 -3
- package/dist/types/adapters/registries/librenms.d.ts +0 -15
- package/dist/types/adapters/registries/peeringdb.d.ts +0 -16
- package/dist/types/adapters/registries/prometheus.d.ts +0 -16
- package/dist/types/core/across.d.ts +0 -4
- package/dist/types/core/addr.d.ts +0 -34
- package/dist/types/core/files.d.ts +0 -6
- package/dist/types/core/merge.d.ts +0 -4
- package/dist/types/core/time.d.ts +0 -2
- package/dist/types/core/validate.d.ts +0 -2
- package/dist/types/presets/index.d.ts +0 -183
- package/src/adapters/devices/catalog.ts +0 -41
- package/src/adapters/devices/derive.ts +0 -59
- package/src/adapters/devices/fortios/firewall.ts +0 -362
- package/src/adapters/devices/fortios/index.ts +0 -923
- package/src/adapters/devices/fortios/names.ts +0 -115
- package/src/adapters/devices/fortios/plan.ts +0 -888
- package/src/adapters/devices/fortios/render.ts +0 -768
- package/src/adapters/devices/fortios/routing.ts +0 -444
- package/src/adapters/devices/fortios/tree.ts +0 -121
- package/src/adapters/devices/index.ts +0 -9
- package/src/adapters/devices/junos/bgp.ts +0 -269
- package/src/adapters/devices/junos/index.ts +0 -862
- package/src/adapters/devices/junos/render.ts +0 -872
- package/src/adapters/devices/lines.ts +0 -212
- package/src/adapters/devices/raisecom-ros/TRAPS.md +0 -26
- package/src/adapters/devices/raisecom-ros/index.ts +0 -700
- package/src/adapters/devices/routeros/export.ts +0 -321
- package/src/adapters/devices/routeros/index.ts +0 -346
- package/src/adapters/devices/routeros/plan.ts +0 -398
- package/src/adapters/devices/routeros/render.ts +0 -1490
- package/src/adapters/devices/types.ts +0 -38
- package/src/adapters/devices/vrp/TRAPS.md +0 -26
- package/src/adapters/devices/vrp/index.ts +0 -865
- package/src/adapters/registries/index.ts +0 -19
- package/src/adapters/registries/librenms.ts +0 -92
- package/src/adapters/registries/peeringdb.ts +0 -169
- package/src/adapters/registries/prometheus.ts +0 -71
- package/src/cli/graph.ts +0 -89
- package/src/core/across.ts +0 -350
- package/src/core/addr.ts +0 -174
- package/src/core/addresses.ts +0 -316
- package/src/core/files.ts +0 -32
- package/src/core/graph.ts +0 -640
- package/src/core/merge.ts +0 -19
- package/src/core/time.ts +0 -15
- package/src/core/validate.ts +0 -348
- package/src/presets/index.ts +0 -579
- /package/dist/types/{adapters/devices → platforms}/fortios/models.d.ts +0 -0
- /package/dist/types/{adapters/devices → platforms}/junos/models.d.ts +0 -0
- /package/dist/types/{adapters/devices → platforms}/raisecom-ros/models.d.ts +0 -0
- /package/dist/types/{adapters/devices → platforms}/vrp/models.d.ts +0 -0
- /package/src/{adapters/devices → platforms}/fortios/models.ts +0 -0
- /package/src/{adapters/devices → platforms}/junos/models.ts +0 -0
- /package/src/{adapters/devices → platforms}/raisecom-ros/models.ts +0 -0
- /package/src/{adapters/devices → platforms}/vrp/models.ts +0 -0
|
@@ -2,127 +2,129 @@
|
|
|
2
2
|
/// <reference lib="dom" />
|
|
3
3
|
// Saving what Studio shows: a table as CSV, and the graph as SVG or PNG, drawn the way it looks on screen.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* A table as CSV: its header, then each row, every cell as the text it shows.
|
|
39
|
-
*
|
|
40
|
-
* @param {HTMLTableElement} table
|
|
41
|
-
*/
|
|
42
|
-
export function csvOf(table) {
|
|
43
|
-
/** @param {string} text */
|
|
44
|
-
const field = (text) => (/[",\n]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text);
|
|
45
|
-
const rows = [...table.querySelectorAll(":scope > thead > tr, :scope > tbody > tr")];
|
|
46
|
-
|
|
47
|
-
return rows.map((row) => [...row.children].map((cell) => field(cell.textContent?.trim() ?? "")).join(",")).join("\n") + "\n";
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The drawing as an SVG file of its own: the drawn parts, framed around what is drawn, with each element's look written onto it.
|
|
52
|
-
*
|
|
53
|
-
* @param {SVGElement} svg
|
|
54
|
-
* @param {SVGGraphicsElement} viewport
|
|
55
|
-
* @returns {{ text: string, width: number, height: number }}
|
|
56
|
-
*/
|
|
57
|
-
export function svgOf(svg, viewport) {
|
|
58
|
-
const box = viewport.getBBox();
|
|
59
|
-
const margin = 24;
|
|
60
|
-
const width = Math.ceil(box.width + margin * 2);
|
|
61
|
-
const height = Math.ceil(box.height + margin * 2);
|
|
62
|
-
|
|
63
|
-
const copy = /** @type {SVGElement} */ (svg.cloneNode(true));
|
|
64
|
-
const originals = [svg, ...svg.querySelectorAll("*")];
|
|
65
|
-
const copies = [copy, ...copy.querySelectorAll("*")];
|
|
66
|
-
|
|
67
|
-
// The dots travelling the cables are left out: a still image would show each one stopped at the drawing's corner.
|
|
68
|
-
const moving = [];
|
|
69
|
-
|
|
70
|
-
for (const [index, original] of originals.entries()) {
|
|
71
|
-
const target = copies[index];
|
|
72
|
-
if (!(target instanceof SVGElement)) continue;
|
|
73
|
-
if (original.classList.contains("signal")) moving.push(target);
|
|
74
|
-
|
|
75
|
-
const look = getComputedStyle(original);
|
|
76
|
-
target.setAttribute("style", DRAWN.map((name) => `${name}:${look.getPropertyValue(name)}`).join(";"));
|
|
77
|
-
target.removeAttribute("class");
|
|
5
|
+
export class Export {
|
|
6
|
+
/** The properties that make the drawing look as it does, copied onto each element, since a saved SVG has no stylesheet. */
|
|
7
|
+
static #DRAWN = [
|
|
8
|
+
"fill",
|
|
9
|
+
"stroke",
|
|
10
|
+
"stroke-width",
|
|
11
|
+
"stroke-dasharray",
|
|
12
|
+
"stroke-linejoin",
|
|
13
|
+
"paint-order",
|
|
14
|
+
"opacity",
|
|
15
|
+
"font-family",
|
|
16
|
+
"font-size",
|
|
17
|
+
"font-weight",
|
|
18
|
+
"text-anchor",
|
|
19
|
+
"dominant-baseline",
|
|
20
|
+
"display",
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Hand the browser a file to save.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} name
|
|
27
|
+
* @param {Blob} blob
|
|
28
|
+
*/
|
|
29
|
+
static save(name, blob) {
|
|
30
|
+
const url = URL.createObjectURL(blob);
|
|
31
|
+
const anchor = document.createElement("a");
|
|
32
|
+
anchor.href = url;
|
|
33
|
+
anchor.download = name;
|
|
34
|
+
anchor.click();
|
|
35
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
78
36
|
}
|
|
79
37
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
background.setAttribute("width", String(width));
|
|
93
|
-
background.setAttribute("height", String(height));
|
|
94
|
-
background.setAttribute("fill", getComputedStyle(document.body).backgroundColor);
|
|
95
|
-
copy.prepend(background);
|
|
38
|
+
/**
|
|
39
|
+
* A table as CSV: its header, then each row, every cell as the text it shows.
|
|
40
|
+
*
|
|
41
|
+
* @param {HTMLTableElement} table
|
|
42
|
+
*/
|
|
43
|
+
static csv(table) {
|
|
44
|
+
/** @param {string} text */
|
|
45
|
+
const field = (text) => (/[",\n]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text);
|
|
46
|
+
const rows = [...table.querySelectorAll(":scope > thead > tr, :scope > tbody > tr")];
|
|
47
|
+
|
|
48
|
+
return rows.map((row) => [...row.children].map((cell) => field(cell.textContent?.trim() ?? "")).join(",")).join("\n") + "\n";
|
|
49
|
+
}
|
|
96
50
|
|
|
97
|
-
|
|
98
|
-
|
|
51
|
+
/**
|
|
52
|
+
* The drawing as an SVG file of its own: the drawn parts, framed around what is drawn, with each element's look written onto it.
|
|
53
|
+
*
|
|
54
|
+
* @param {SVGElement} svg
|
|
55
|
+
* @param {SVGGraphicsElement} viewport
|
|
56
|
+
* @returns {{ text: string, width: number, height: number }}
|
|
57
|
+
*/
|
|
58
|
+
static svg(svg, viewport) {
|
|
59
|
+
const box = viewport.getBBox();
|
|
60
|
+
const margin = 24;
|
|
61
|
+
const width = Math.ceil(box.width + margin * 2);
|
|
62
|
+
const height = Math.ceil(box.height + margin * 2);
|
|
63
|
+
|
|
64
|
+
const copy = /** @type {SVGElement} */ (svg.cloneNode(true));
|
|
65
|
+
const originals = [svg, ...svg.querySelectorAll("*")];
|
|
66
|
+
const copies = [copy, ...copy.querySelectorAll("*")];
|
|
67
|
+
|
|
68
|
+
// The dots travelling the cables are left out: a still image would show each one stopped at the drawing's corner.
|
|
69
|
+
const moving = [];
|
|
70
|
+
|
|
71
|
+
for (const [index, original] of originals.entries()) {
|
|
72
|
+
const target = copies[index];
|
|
73
|
+
if (!(target instanceof SVGElement)) continue;
|
|
74
|
+
if (original.classList.contains("signal")) moving.push(target);
|
|
75
|
+
|
|
76
|
+
const look = getComputedStyle(original);
|
|
77
|
+
target.setAttribute("style", Export.#DRAWN.map((name) => `${name}:${look.getPropertyValue(name)}`).join(";"));
|
|
78
|
+
target.removeAttribute("class");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
for (const dot of moving) dot.remove();
|
|
82
|
+
|
|
83
|
+
// The drawing's own place on screen is left out: the file is framed around what is drawn.
|
|
84
|
+
copy.querySelector("#viewport")?.removeAttribute("transform");
|
|
85
|
+
copy.setAttribute("xmlns", "http://www.w3.org/2000/svg");
|
|
86
|
+
copy.setAttribute("viewBox", `${box.x - margin} ${box.y - margin} ${width} ${height}`);
|
|
87
|
+
copy.setAttribute("width", String(width));
|
|
88
|
+
copy.setAttribute("height", String(height));
|
|
89
|
+
|
|
90
|
+
const background = document.createElementNS("http://www.w3.org/2000/svg", "rect");
|
|
91
|
+
background.setAttribute("x", String(box.x - margin));
|
|
92
|
+
background.setAttribute("y", String(box.y - margin));
|
|
93
|
+
background.setAttribute("width", String(width));
|
|
94
|
+
background.setAttribute("height", String(height));
|
|
95
|
+
background.setAttribute("fill", getComputedStyle(document.body).backgroundColor);
|
|
96
|
+
copy.prepend(background);
|
|
97
|
+
|
|
98
|
+
return { text: new XMLSerializer().serializeToString(copy), width, height };
|
|
99
|
+
}
|
|
99
100
|
|
|
100
|
-
/**
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
101
|
+
/**
|
|
102
|
+
* An SVG drawn into a PNG, at twice its size so it stays sharp.
|
|
103
|
+
*
|
|
104
|
+
* @param {{ text: string, width: number, height: number }} drawing
|
|
105
|
+
* @returns {Promise<Blob>}
|
|
106
|
+
*/
|
|
107
|
+
static png(drawing) {
|
|
108
|
+
const scale = 2;
|
|
109
|
+
const image = new Image();
|
|
110
|
+
const url = URL.createObjectURL(new Blob([drawing.text], { type: "image/svg+xml" }));
|
|
111
|
+
|
|
112
|
+
return new Promise((done, fail) => {
|
|
113
|
+
image.addEventListener("load", () => {
|
|
114
|
+
const canvas = document.createElement("canvas");
|
|
115
|
+
canvas.width = drawing.width * scale;
|
|
116
|
+
canvas.height = drawing.height * scale;
|
|
117
|
+
|
|
118
|
+
const context = canvas.getContext("2d");
|
|
119
|
+
if (!context) return fail(new Error("this browser cannot draw a PNG"));
|
|
120
|
+
|
|
121
|
+
context.scale(scale, scale);
|
|
122
|
+
context.drawImage(image, 0, 0);
|
|
123
|
+
URL.revokeObjectURL(url);
|
|
124
|
+
canvas.toBlob((blob) => (blob ? done(blob) : fail(new Error("the PNG could not be made"))), "image/png");
|
|
125
|
+
});
|
|
126
|
+
image.addEventListener("error", () => fail(new Error("the drawing could not be read back")));
|
|
127
|
+
image.src = url;
|
|
124
128
|
});
|
|
125
|
-
|
|
126
|
-
image.src = url;
|
|
127
|
-
});
|
|
129
|
+
}
|
|
128
130
|
}
|