@drawtonomy/sdk 0.5.0 → 0.6.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 CHANGED
@@ -120,6 +120,12 @@ http://localhost:3000/?ext=http://localhost:3001/manifest.json
120
120
  | `getBoundingBox(points)` | Get bounding box |
121
121
  | `distanceToSegment(point, a, b)` | Point-to-segment distance |
122
122
 
123
+ ### Snapshot
124
+
125
+ | Function | Description |
126
+ |----------|-------------|
127
+ | `parseDrawtonomySvg(svg)` | Read a `.drawtonomy.svg` source string and return the embedded `DrawtonomySnapshot`, or `null` if absent / malformed |
128
+
123
129
  ### Exporter Module
124
130
 
125
131
  Convert a `DrawtonomySnapshot` into target-format strings (OpenDRIVE,
package/dist/index.d.ts CHANGED
@@ -2,5 +2,6 @@ export * from './types';
2
2
  export * from './helpers';
3
3
  export * from './geometry';
4
4
  export { ExtensionClient } from './ExtensionClient';
5
+ export { parseDrawtonomySvg } from './snapshot';
5
6
  export * as exporter from './exporter';
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAEnD,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAE/C,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAA"}
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ export * from './types';
3
3
  export * from './helpers';
4
4
  export * from './geometry';
5
5
  export { ExtensionClient } from './ExtensionClient';
6
+ export { parseDrawtonomySvg } from './snapshot';
6
7
  // Exporter sub-module is also available via "@drawtonomy/sdk/exporter".
7
8
  export * as exporter from './exporter';
8
9
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,wEAAwE;AACxE,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAC/C,wEAAwE;AACxE,OAAO,KAAK,QAAQ,MAAM,YAAY,CAAA"}
@@ -0,0 +1,9 @@
1
+ import type { DrawtonomySnapshot } from './types';
2
+ /**
3
+ * Parse a `.drawtonomy.svg` source string and return the embedded
4
+ * DrawtonomySnapshot. Returns `null` if the file is not a drawtonomy SVG
5
+ * (e.g. plain SVG without an embedded snapshot) or the embedded payload is
6
+ * malformed.
7
+ */
8
+ export declare function parseDrawtonomySvg(svgContent: string): DrawtonomySnapshot | null;
9
+ //# sourceMappingURL=snapshot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAkCjD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,kBAAkB,GAAG,IAAI,CAgBhF"}
@@ -0,0 +1,60 @@
1
+ // Parse a `.drawtonomy.svg` file (a regular SVG with a base64-encoded
2
+ // snapshot embedded in `data-drawtonomy-snapshot`) back into a
3
+ // DrawtonomySnapshot. The legacy attribute name `data-drawauto-snapshot`
4
+ // is also accepted for backwards compatibility.
5
+ /**
6
+ * Decode a base64-encoded UTF-8 string. Mirrors the encoder used when the
7
+ * editor produces .drawtonomy.svg files: `btoa(unescape(encodeURIComponent(json)))`.
8
+ */
9
+ function base64DecodeUtf8(encoded) {
10
+ if (typeof atob === 'function') {
11
+ return decodeURIComponent(escape(atob(encoded)));
12
+ }
13
+ // Node fallback (Buffer is global in Node 18+).
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ const buf = globalThis.Buffer;
16
+ if (buf) {
17
+ return buf.from(encoded, 'base64').toString('utf-8');
18
+ }
19
+ throw new Error('No base64 decoder available in this environment');
20
+ }
21
+ /**
22
+ * Extract the value of an attribute from the root `<svg>` element of an SVG
23
+ * source string. Used instead of DOMParser so the parser works in plain
24
+ * Node without jsdom.
25
+ */
26
+ function extractRootSvgAttribute(svg, attrName) {
27
+ // Match the opening <svg ...> tag (allow line breaks inside).
28
+ const openTagMatch = svg.match(/<svg\b[^>]*>/);
29
+ if (!openTagMatch)
30
+ return null;
31
+ const openTag = openTagMatch[0];
32
+ const attrRegex = new RegExp(`\\b${attrName}="([^"]*)"`);
33
+ const m = openTag.match(attrRegex);
34
+ return m ? m[1] : null;
35
+ }
36
+ /**
37
+ * Parse a `.drawtonomy.svg` source string and return the embedded
38
+ * DrawtonomySnapshot. Returns `null` if the file is not a drawtonomy SVG
39
+ * (e.g. plain SVG without an embedded snapshot) or the embedded payload is
40
+ * malformed.
41
+ */
42
+ export function parseDrawtonomySvg(svgContent) {
43
+ if (!svgContent || typeof svgContent !== 'string')
44
+ return null;
45
+ const encoded = extractRootSvgAttribute(svgContent, 'data-drawtonomy-snapshot') ??
46
+ extractRootSvgAttribute(svgContent, 'data-drawauto-snapshot');
47
+ if (!encoded)
48
+ return null;
49
+ try {
50
+ const jsonString = base64DecodeUtf8(encoded);
51
+ const parsed = JSON.parse(jsonString);
52
+ if (!parsed || !Array.isArray(parsed.shapes))
53
+ return null;
54
+ return parsed;
55
+ }
56
+ catch {
57
+ return null;
58
+ }
59
+ }
60
+ //# sourceMappingURL=snapshot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.js","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,+DAA+D;AAC/D,yEAAyE;AACzE,gDAAgD;AAIhD;;;GAGG;AACH,SAAS,gBAAgB,CAAC,OAAe;IACvC,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE,CAAC;QAC/B,OAAO,kBAAkB,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;IAClD,CAAC;IACD,gDAAgD;IAChD,8DAA8D;IAC9D,MAAM,GAAG,GAAI,UAAkB,CAAC,MAAM,CAAA;IACtC,IAAI,GAAG,EAAE,CAAC;QACR,OAAO,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAA;IACtD,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAA;AACpE,CAAC;AAED;;;;GAIG;AACH,SAAS,uBAAuB,CAAC,GAAW,EAAE,QAAgB;IAC5D,8DAA8D;IAC9D,MAAM,YAAY,GAAG,GAAG,CAAC,KAAK,CAAC,cAAc,CAAC,CAAA;IAC9C,IAAI,CAAC,YAAY;QAAE,OAAO,IAAI,CAAA;IAC9B,MAAM,OAAO,GAAG,YAAY,CAAC,CAAC,CAAC,CAAA;IAC/B,MAAM,SAAS,GAAG,IAAI,MAAM,CAAC,MAAM,QAAQ,YAAY,CAAC,CAAA;IACxD,MAAM,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;IAClC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;AACxB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,UAAkB;IACnD,IAAI,CAAC,UAAU,IAAI,OAAO,UAAU,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAA;IAE9D,MAAM,OAAO,GACX,uBAAuB,CAAC,UAAU,EAAE,0BAA0B,CAAC;QAC/D,uBAAuB,CAAC,UAAU,EAAE,wBAAwB,CAAC,CAAA;IAC/D,IAAI,CAAC,OAAO;QAAE,OAAO,IAAI,CAAA;IAEzB,IAAI,CAAC;QACH,MAAM,UAAU,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAA;QAC5C,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAuB,CAAA;QAC3D,IAAI,CAAC,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC;YAAE,OAAO,IAAI,CAAA;QACzD,OAAO,MAAM,CAAA;IACf,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAA;IACb,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drawtonomy/sdk",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "SDK for building drawtonomy extensions",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",