@open-artifact/dc 0.2.2 → 0.9.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-artifact/dc",
3
- "version": "0.2.2",
3
+ "version": "0.9.1",
4
4
  "description": "The Design Component (.dc.html) format: parser, template addressing, edits, checks, React renderer and the in-frame board runtime.",
5
5
  "type": "module",
6
6
  "exports": {
package/src/document.d.ts CHANGED
@@ -6,6 +6,8 @@
6
6
  import { type PropsSpec } from './props.ts';
7
7
  /** The line every artboard's `<head>` must carry; the editor swaps it for the runtime. */
8
8
  export declare const SUPPORT_SCRIPT_TAG = "<script src=\"./support.js\"></script>";
9
+ /** Finds the runtime tag in the head. Nested boards can address the project root. */
10
+ export declare function supportScriptTag(source: string): string | null;
9
11
  /** A range of the source. */
10
12
  export interface SourceRange {
11
13
  start: number;
package/src/document.js CHANGED
@@ -7,6 +7,11 @@ import { decodeEntities } from "./html.js";
7
7
  import { parseProps } from "./props.js";
8
8
  /** The line every artboard's `<head>` must carry; the editor swaps it for the runtime. */
9
9
  export const SUPPORT_SCRIPT_TAG = '<script src="./support.js"></script>';
10
+ /** Finds the runtime tag in the head. Nested boards can address the project root. */
11
+ export function supportScriptTag(source) {
12
+ const head = /<head(?:\s[^>]*)?>([\s\S]*?)<\/head>/i.exec(source)?.[1];
13
+ return head?.match(/<script src="(?:\.\/|(?:\.\.\/)+)support\.js"><\/script>/)?.[0] ?? null;
14
+ }
10
15
  /** Finds the `<x-dc>` open tag and the last `</x-dc>`. */
11
16
  function findTemplate(source) {
12
17
  const open = /<x-dc(\s[^>]*)?>/i.exec(source);
@@ -81,7 +86,7 @@ export function parseDcDocument(source) {
81
86
  propsError,
82
87
  title: titleMatch ? decodeEntities(titleMatch[1].trim()) : null,
83
88
  lang: langMatch ? langMatch[1] : null,
84
- hasSupportScript: source.includes(SUPPORT_SCRIPT_TAG),
89
+ hasSupportScript: supportScriptTag(source) !== null,
85
90
  head,
86
91
  };
87
92
  }
package/src/protocol.d.ts CHANGED
@@ -259,6 +259,11 @@ export interface BoardApi {
259
259
  declare global {
260
260
  interface Window {
261
261
  __dc?: BoardApi;
262
+ __dcExport?: {
263
+ path: string;
264
+ source: string;
265
+ files: Record<string, string>;
266
+ };
262
267
  }
263
268
  interface HTMLIFrameElement {
264
269
  __dcBoot?: BoardBoot;
@@ -453,7 +453,7 @@ class Board {
453
453
  event.stopPropagation();
454
454
  return;
455
455
  }
456
- if (this.mode !== 'edit')
456
+ if (this.mode !== 'edit' && event.key !== 'Escape')
457
457
  return;
458
458
  const target = event.target;
459
459
  if (target && (target.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName)))
@@ -468,16 +468,34 @@ class Board {
468
468
  /* -------------------------------------------------------------------------- */
469
469
  /** Boot config when the page runs on its own: fetch our own source, siblings by URL. */
470
470
  async function standaloneBoot() {
471
+ const embedded = window.__dcExport;
472
+ if (embedded)
473
+ return {
474
+ path: embedded.path, source: embedded.source, mode: 'play',
475
+ load: async (target) => {
476
+ if (Object.hasOwn(embedded.files, target))
477
+ return embedded.files[target];
478
+ throw new Error(`Missing from export: ${target}`);
479
+ },
480
+ report: event => { if (window.parent !== window && event.type === 'navigate')
481
+ window.parent.postMessage({ __dc: event }, '*'); },
482
+ };
471
483
  const source = await fetch(location.href).then((response) => response.text());
472
- const path = decodeURIComponent(location.pathname.split('/').pop() ?? 'index.dc.html');
473
- const base = location.href.replace(/[^/]*$/, '');
484
+ // Keep the served folder in the path. A sibling link or import can climb out
485
+ // of a nested board folder, including when the ZIP is served under a prefix.
486
+ const path = decodeURIComponent(location.pathname.slice(1));
487
+ const urlOf = (target) => new URL('/' + target.split('/').map(encodeURIComponent).join('/'), location.href);
474
488
  return {
475
489
  path,
476
490
  source,
477
491
  mode: 'play',
478
- // The page's own path is its base name, so children resolve next to it.
479
- load: (target) => fetch(new URL(target, base)).then((r) => (r.ok ? r.text() : Promise.reject(new Error(`HTTP ${r.status}`)))),
480
- report: (event) => window.parent !== window && window.parent.postMessage({ __dc: event }, '*'),
492
+ load: (target) => fetch(urlOf(target)).then((r) => (r.ok ? r.text() : Promise.reject(new Error(`HTTP ${r.status}`)))),
493
+ report: event => {
494
+ if (event.type === 'navigate')
495
+ location.assign(urlOf(event.path).href);
496
+ else if (window.parent !== window)
497
+ window.parent.postMessage({ __dc: event }, '*');
498
+ },
481
499
  };
482
500
  }
483
501
  /** Starts the runtime: hides the raw template, waits for the DOM, then renders. */