@chengzhiyi/pi-excalidraw 0.1.2 → 0.1.3

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
@@ -1,5 +1,8 @@
1
1
  # @chengzhiyi/pi-excalidraw
2
2
 
3
+ See [frontend architecture and UI negotiation](https://github.com/chengzhiyi/pi-extensions/blob/main/packages/excalidraw/FRONTEND.md) for host-owned cards,
4
+ page-level parameters, lifecycle/compatibility and the Shadow DOM feasibility probe.
5
+
3
6
  Hosted v2 Excalidraw package. Migrated from `pi-webapp-excalidraw` diagrams at source HEAD `d87f24e`, including `b01b6bb` persistence/grouping changes. No host-private imports, shared React, direct checkpoint writes, workspace filesystem access, account, collaboration, image attachments or external embeds.
4
7
 
5
8
  ## Install and update in pi-webapp
package/dist/frame.css CHANGED
@@ -42,6 +42,12 @@
42
42
  border-radius: 12px;
43
43
  overflow: hidden;
44
44
  }
45
+ [data-host-tool-card] .diagrams_artifact {
46
+ box-sizing: border-box;
47
+ margin: 0;
48
+ border: 0;
49
+ border-radius: 0;
50
+ }
45
51
  .diagrams_toolHeader {
46
52
  display: flex;
47
53
  align-items: center;
@@ -6263,6 +6269,43 @@ body.excalidraw-cursor-resize * {
6263
6269
  left: -5px;
6264
6270
  }
6265
6271
 
6272
+ /* src/client/ui.module.css */
6273
+ .ui_dialog {
6274
+ box-sizing: border-box;
6275
+ width: min(760px, calc(100% - 32px));
6276
+ max-height: calc(100dvh - 32px);
6277
+ padding: 20px;
6278
+ border: 1px solid var(--dsw-alias-border-l2);
6279
+ border-radius: 12px;
6280
+ color: var(--dsw-alias-label-primary);
6281
+ background: var(--dsw-alias-bg-layer-1);
6282
+ }
6283
+ .ui_dialog::backdrop {
6284
+ background: #0006;
6285
+ }
6286
+ .ui_header {
6287
+ display: flex;
6288
+ align-items: center;
6289
+ justify-content: space-between;
6290
+ gap: 16px;
6291
+ margin-bottom: 16px;
6292
+ }
6293
+ .ui_header h2 {
6294
+ margin: 0;
6295
+ font-size: 16px;
6296
+ }
6297
+ .ui_header button {
6298
+ border: 1px solid var(--dsw-alias-border-l2);
6299
+ border-radius: 6px;
6300
+ padding: 6px 10px;
6301
+ color: inherit;
6302
+ background: transparent;
6303
+ }
6304
+ .ui_content {
6305
+ overflow: auto;
6306
+ max-height: calc(100dvh - 140px);
6307
+ }
6308
+
6266
6309
  /* src/client/frame.css */
6267
6310
  html,
6268
6311
  body,
@@ -6304,3 +6347,9 @@ input {
6304
6347
  button {
6305
6348
  cursor: pointer;
6306
6349
  }
6350
+ body[data-mount-kind=tool],
6351
+ body[data-mount-kind=tool] #root {
6352
+ height: auto;
6353
+ min-height: 0;
6354
+ display: flow-root;
6355
+ }
package/dist/frame.js CHANGED
@@ -449,41 +449,65 @@ function NativeDiagramEditor(p) {
449
449
  }
450
450
 
451
451
  // src/client/DiagramPreview.tsx
452
+ var import_react4 = __toESM(require_react(), 1);
453
+
454
+ // src/client/ui.tsx
452
455
  var import_react3 = __toESM(require_react(), 1);
453
456
 
457
+ // src/client/ui.module.css
458
+ var ui_default = {
459
+ dialog: "ui_dialog",
460
+ header: "ui_header",
461
+ content: "ui_content"
462
+ };
463
+
454
464
  // src/client/ui.tsx
455
465
  var import_jsx_runtime2 = __toESM(require_jsx_runtime(), 1);
456
466
  function ExcalidrawIcon({ size = 18 }) {
457
467
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("svg", { width: size, height: size, viewBox: "0 0 24 24", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("path", { d: "M4 4h16v16H4zM7 16l9-9", fill: "none", stroke: "currentColor", strokeWidth: "2" }) });
458
468
  }
459
469
  function Modal(p) {
460
- return p.open ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { role: "dialog", "aria-modal": "true", "aria-label": p.title, className: p.className, onKeyDown: (e) => {
461
- if (e.key === "Escape") p.onClose();
470
+ const ref = (0, import_react3.useRef)(null);
471
+ (0, import_react3.useEffect)(() => {
472
+ if (!p.open || !ref.current) return;
473
+ const dialog = ref.current, previous = document.activeElement;
474
+ dialog.showModal();
475
+ return () => {
476
+ dialog.close();
477
+ if (previous instanceof HTMLElement && previous.isConnected) previous.focus();
478
+ };
479
+ }, [p.open]);
480
+ return p.open ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("dialog", { ref, "aria-modal": "true", "aria-label": p.title, className: `${ui_default.dialog} ${p.className ?? ""}`, onCancel: (e) => {
481
+ e.preventDefault();
482
+ p.onClose();
462
483
  }, children: [
463
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { autoFocus: true, onClick: p.onClose, children: p.closeLabel }),
464
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: p.contentClassName, children: p.children })
484
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("header", { className: ui_default.header, children: [
485
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h2", { children: p.title }),
486
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { autoFocus: true, type: "button", onClick: p.onClose, children: p.closeLabel })
487
+ ] }),
488
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: `${ui_default.content} ${p.contentClassName ?? ""}`, children: p.children })
465
489
  ] }) : null;
466
490
  }
467
491
 
468
492
  // src/client/DiagramPreview.tsx
469
493
  var import_jsx_runtime3 = __toESM(require_jsx_runtime(), 1);
470
494
  function useThumbnailUrl() {
471
- const [url, setUrl] = (0, import_react3.useState)("");
472
- (0, import_react3.useEffect)(() => () => {
495
+ const [url, setUrl] = (0, import_react4.useState)("");
496
+ (0, import_react4.useEffect)(() => () => {
473
497
  if (url) URL.revokeObjectURL(url);
474
498
  }, [url]);
475
499
  return [url, setUrl];
476
500
  }
477
501
  function DiagramToolView(p) {
478
502
  const session = p.session;
479
- const [collapsed, setCollapsed] = (0, import_react3.useState)(false), [details, setDetails] = (0, import_react3.useState)(false);
480
- const bodyId = (0, import_react3.useId)();
481
- const parametersButton = (0, import_react3.useRef)(null), restoreParametersFocus = (0, import_react3.useRef)(false);
503
+ const [collapsed, setCollapsed] = (0, import_react4.useState)(false), [details, setDetails] = (0, import_react4.useState)(false);
504
+ const bodyId = (0, import_react4.useId)();
505
+ const parametersButton = (0, import_react4.useRef)(null), restoreParametersFocus = (0, import_react4.useRef)(false);
482
506
  const closeDetails = () => {
483
507
  restoreParametersFocus.current = true;
484
508
  setDetails(false);
485
509
  };
486
- (0, import_react3.useEffect)(() => {
510
+ (0, import_react4.useEffect)(() => {
487
511
  if (!details && restoreParametersFocus.current) {
488
512
  parametersButton.current?.focus();
489
513
  restoreParametersFocus.current = false;
@@ -491,7 +515,7 @@ function DiagramToolView(p) {
491
515
  }, [details]);
492
516
  const t = (zh, en) => p.locale === "zh" ? zh : en;
493
517
  const output = p.result?.blocks.filter((b) => b.kind === "text").map((b) => b.text).join("\n");
494
- const header = (unavailable2 = false) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("header", { className: diagrams_default.toolHeader, children: [
518
+ const header = (unavailable2 = false) => p.hostToolCard ? null : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("header", { className: diagrams_default.toolHeader, children: [
495
519
  /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("button", { type: "button", className: diagrams_default.previewToggle, "aria-expanded": !collapsed, "aria-controls": bodyId, "aria-label": t("\u5C55\u5F00\u6216\u6536\u8D77\u56FE\u7A3F", "Toggle drawing preview"), onClick: () => setCollapsed((value) => !value), children: [
496
520
  /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExcalidrawIcon, { size: 18 }),
497
521
  /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: diagrams_default.toolTitle, children: "create view" }),
@@ -514,7 +538,7 @@ function DiagramToolView(p) {
514
538
  ] });
515
539
  else if (session && result && typeof result.id === "string" && /^[\w-]{1,80}$/.test(result.id)) content = /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(DiagramPreview, { ...disclosure, sessionId: session.sessionId, id: result.id, title: typeof result.title === "string" ? result.title : "Excalidraw", locale: p.locale, open: () => p.openPanel(`diagram:${result.id}`) });
516
540
  else content = /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(StreamingDrawing, { ...disclosure, input: p.call.text, locale: p.locale, running: p.running });
517
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [
541
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { "data-host-tool-card": p.hostToolCard || void 0, children: [
518
542
  content,
519
543
  /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Modal, { open: details, onClose: closeDetails, title: t("\u7ED8\u56FE\u5DE5\u5177\u53C2\u6570", "Drawing tool parameters"), closeLabel: t("\u5173\u95ED", "Close"), className: diagrams_default.parametersDialog, contentClassName: diagrams_default.parametersContent, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: diagrams_default.toolDetails, children: [
520
544
  /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("h3", { children: t("\u8F93\u5165\u53C2\u6570", "Input") }),
@@ -528,10 +552,10 @@ function DiagramToolView(p) {
528
552
  }
529
553
  function StreamingDrawing({ input, locale, running, header, collapsed, bodyId }) {
530
554
  const theme = useDiagramTheme();
531
- const latest = (0, import_react3.useRef)(input);
555
+ const latest = (0, import_react4.useRef)(input);
532
556
  latest.current = input;
533
557
  const [image, setImage] = useThumbnailUrl();
534
- (0, import_react3.useEffect)(() => {
558
+ (0, import_react4.useEffect)(() => {
535
559
  let active = true, busy2 = false, prior2 = "";
536
560
  const render2 = async () => {
537
561
  if (collapsed || busy2 || !active || latest.current === prior2) return;
@@ -569,19 +593,19 @@ function StreamingDrawing({ input, locale, running, header, collapsed, bodyId })
569
593
  }
570
594
  function DiagramPreview({ sessionId, id, title, locale, open, header, collapsed, bodyId }) {
571
595
  const theme = useDiagramTheme();
572
- const ref = (0, import_react3.useRef)(null);
573
- const [visible, setVisible] = (0, import_react3.useState)(false);
596
+ const ref = (0, import_react4.useRef)(null);
597
+ const [visible, setVisible] = (0, import_react4.useState)(false);
574
598
  const [image, setImage] = useThumbnailUrl();
575
- const [error, setError] = (0, import_react3.useState)(""), [renderedKey, setRenderedKey] = (0, import_react3.useState)("");
576
- (0, import_react3.useEffect)(() => {
599
+ const [error, setError] = (0, import_react4.useState)(""), [renderedKey, setRenderedKey] = (0, import_react4.useState)("");
600
+ (0, import_react4.useEffect)(() => {
577
601
  const observer = new IntersectionObserver(([entry]) => setVisible(!!entry?.isIntersecting), { rootMargin: "160px" });
578
602
  if (ref.current) observer.observe(ref.current);
579
603
  return () => observer.disconnect();
580
604
  }, []);
581
605
  const model = diagramModel(sessionId, id);
582
- const state = (0, import_react3.useSyncExternalStore)(model.subscribe, model.getSnapshot);
606
+ const state = (0, import_react4.useSyncExternalStore)(model.subscribe, model.getSnapshot);
583
607
  const t = (zh, en) => locale === "zh" ? zh : en;
584
- (0, import_react3.useEffect)(() => {
608
+ (0, import_react4.useEffect)(() => {
585
609
  if (!visible || state.unavailable) return;
586
610
  const abort = new AbortController();
587
611
  const refresh = async () => {
@@ -604,7 +628,7 @@ function DiagramPreview({ sessionId, id, title, locale, open, header, collapsed,
604
628
  const candidate = state.draft?.candidate;
605
629
  const initial = !!candidate && state.draft?.revision === 0 && !state.draft.scene.elements.length && !candidate.patch;
606
630
  const stale = !!candidate && (candidate.baseRevision !== state.draft?.revision || state.unsynced);
607
- (0, import_react3.useEffect)(() => {
631
+ (0, import_react4.useEffect)(() => {
608
632
  if (!visible || !state.scene) return;
609
633
  let active = true;
610
634
  const render2 = async () => {
@@ -663,6 +687,9 @@ var tool = {};
663
687
  var frozen = false;
664
688
  var disposed = false;
665
689
  var composing = false;
690
+ var resizeObserver;
691
+ var resizeFrame = 0;
692
+ var lastHeight = -1;
666
693
  var pointers = /* @__PURE__ */ new Set();
667
694
  var contribution = {
668
695
  entry: { label: "Excalidraw", description: { zh: "\u624B\u7ED8\u56FE\u7A3F", en: "Editable drawings" }, kind: "workspace" },
@@ -676,10 +703,28 @@ function render() {
676
703
  document.body.toggleAttribute("data-ds-dark-theme", mount.theme === "dark");
677
704
  document.getElementById("root").inert = frozen;
678
705
  const p = { session: { sessionId: mount.sessionId }, contentId: mount.contentId ?? "", locale: mount.locale, visible: mount.visible !== false };
679
- root.render(mount.kind === "controller" ? null : mount.kind === "tool" ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(DiagramToolView, { ...p, call: tool.call ?? { text: "" }, result: tool.result, running: tool.running ?? false, openPanel: (alias) => {
706
+ root.render(mount.kind === "controller" ? null : mount.kind === "tool" ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(DiagramToolView, { ...p, hostToolCard: !!mount.hostUi?.toolCard, call: tool.call ?? { text: "" }, result: tool.result, running: tool.running ?? false, openPanel: (alias) => {
680
707
  void hostCall("openPanel", { kind: "editor", contentId: alias.replace(/^diagram:/, ""), title: "Excalidraw" });
681
708
  } }) : mount.kind === "workspace" ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(DiagramWorkspace, { ...p }) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(DiagramEditor, { ...p }));
682
709
  }
710
+ function observeToolSize() {
711
+ document.body.dataset.mountKind = "tool";
712
+ const element = document.getElementById("root");
713
+ const report = () => {
714
+ cancelAnimationFrame(resizeFrame);
715
+ resizeFrame = requestAnimationFrame(() => {
716
+ if (disposed) return;
717
+ const height = Math.min(4096, Math.ceil(element.getBoundingClientRect().height));
718
+ if (height === lastHeight) return;
719
+ lastHeight = height;
720
+ void hostCall("resize", { height }).catch(() => {
721
+ });
722
+ });
723
+ };
724
+ resizeObserver = new ResizeObserver(report);
725
+ resizeObserver.observe(element);
726
+ report();
727
+ }
683
728
  function busy() {
684
729
  return composing || pointers.size > 0 || canvasBusy() || modelStatus().busy;
685
730
  }
@@ -699,6 +744,8 @@ async function handle(method, input) {
699
744
  if (mount) throw new Error("Already initialized");
700
745
  if (!input || !["controller", "workspace", "editor", "tool"].includes(input.kind) || typeof input.sessionId !== "string" || !["zh", "en"].includes(input.locale) || !["light", "dark"].includes(input.theme)) throw new Error("Invalid mount");
701
746
  mount = input;
747
+ if (mount.kind === "tool" && mount.hostUi?.toolCard) await hostCall("toolPresentation", { title: "create view" });
748
+ if (mount.kind === "tool" && mount.hostUi?.autoResize) observeToolSize();
702
749
  render();
703
750
  await hostCall("changed", { ...modelStatus(), busy: busy() });
704
751
  return contribution;
@@ -767,6 +814,8 @@ var timer = setInterval(() => {
767
814
  function cleanup(closePort = true) {
768
815
  disposed = true;
769
816
  clearInterval(timer);
817
+ resizeObserver?.disconnect();
818
+ cancelAnimationFrame(resizeFrame);
770
819
  root.unmount();
771
820
  disposeModels();
772
821
  clearDownloads();
@@ -2541,8 +2541,8 @@
2541
2541
  },
2542
2542
  {
2543
2543
  "path": "./dist/frame.css",
2544
- "bytes": 171762,
2545
- "sha256": "58fe918e041e61a913ee08ecf86b523c5388ce2bcacb0e0f9136d5f6eb81808c",
2544
+ "bytes": 172768,
2545
+ "sha256": "46f2666e4ae9695bfcbc48f534838ffb0e972de76c67fa4bd8ae487143aca07c",
2546
2546
  "mime": "text/css"
2547
2547
  },
2548
2548
  {
@@ -2553,8 +2553,8 @@
2553
2553
  },
2554
2554
  {
2555
2555
  "path": "./dist/frame.js",
2556
- "bytes": 39976,
2557
- "sha256": "c172386fed4c67221c6f7ab4a185575d7dea76fcf4503cd12ba3d0053bd2ccff",
2556
+ "bytes": 41820,
2557
+ "sha256": "2c03db46afb6cf0d3fc00c55d76c326ddee3b07b889b71a188b84273b38d549b",
2558
2558
  "mime": "text/javascript"
2559
2559
  },
2560
2560
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chengzhiyi/pi-excalidraw",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Self-contained hosted v2 Excalidraw plugin for pi-webapp",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -45,7 +45,7 @@
45
45
  "esbuild": "0.28.2",
46
46
  "typescript": "7.0.2",
47
47
  "@playwright/test": "1.63.0",
48
- "@chengzhiyi/pi-web-protocol": "0.1.1"
48
+ "@chengzhiyi/pi-web-protocol": "0.1.2"
49
49
  },
50
50
  "repository": {
51
51
  "type": "git",
@@ -53,8 +53,8 @@
53
53
  "directory": "packages/excalidraw"
54
54
  },
55
55
  "piRelease": {
56
- "fingerprint": "ce315999acb1ffe6c653ea6003303188e94ba15cb2a58788a86c1a32029a130f",
57
- "source": "627e9d680d7978cf7398052b82e8afac5295e3b4"
56
+ "fingerprint": "86e31d409497c494f6a349dc725a9dcc1a4490cf5cb70e681a01e25699f76491",
57
+ "source": "d419333729106787d069ca9617a7637b66d4280d"
58
58
  },
59
59
  "scripts": {
60
60
  "build": "node scripts/build.mjs",
@@ -62,6 +62,7 @@
62
62
  "test": "node --experimental-transform-types --test test/*.test.ts test/*.test.mjs",
63
63
  "verify": "node scripts/verify.mjs",
64
64
  "test:browser": "node test/frame.browser.mjs",
65
+ "test:shadow": "node test/shadow-dom.browser.mjs",
65
66
  "test:csp": "node test/csp.browser.mjs",
66
67
  "test:independent": "node scripts/independent-build.mjs"
67
68
  }