@chengzhiyi/pi-excalidraw 0.1.2 → 0.1.4

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;
@@ -158,6 +164,70 @@
158
164
  width: 100%;
159
165
  height: auto;
160
166
  }
167
+ .diagrams_placeholder {
168
+ box-sizing: border-box;
169
+ display: flex;
170
+ flex-direction: column;
171
+ align-items: center;
172
+ justify-content: center;
173
+ gap: 8px;
174
+ height: 100%;
175
+ min-height: 180px;
176
+ padding: 24px;
177
+ text-align: center;
178
+ color: var(--diagram-muted, var(--text-secondary));
179
+ font-size: 12px;
180
+ }
181
+ .diagrams_placeholder p {
182
+ margin: 0;
183
+ max-width: 420px;
184
+ overflow-wrap: anywhere;
185
+ }
186
+ .diagrams_placeholderTitle {
187
+ color: var(--diagram-fg, var(--text-primary));
188
+ font-size: 13px;
189
+ font-weight: 500;
190
+ }
191
+ .diagrams_placeholder button {
192
+ margin-top: 4px;
193
+ padding: 6px 14px;
194
+ border: 1px solid var(--diagram-border, var(--border-default));
195
+ border-radius: 7px;
196
+ color: var(--diagram-fg, var(--text-primary));
197
+ background: transparent;
198
+ font-size: 12px;
199
+ }
200
+ .diagrams_placeholder button:hover {
201
+ background: var(--dsw-alias-interactive-bg-hover);
202
+ }
203
+ .diagrams_placeholder button:focus-visible {
204
+ outline: 2px solid #818cf8;
205
+ outline-offset: 2px;
206
+ }
207
+ .diagrams_spinner {
208
+ width: 18px;
209
+ height: 18px;
210
+ border: 2px solid var(--diagram-border, var(--border-default));
211
+ border-top-color: var(--diagram-muted, var(--text-secondary));
212
+ border-radius: 50%;
213
+ opacity: 0;
214
+ animation: diagrams_fadeIn .2s ease .6s forwards, diagrams_spin .8s linear infinite;
215
+ }
216
+ @keyframes diagrams_fadeIn {
217
+ to {
218
+ opacity: .8;
219
+ }
220
+ }
221
+ @keyframes diagrams_spin {
222
+ to {
223
+ transform: rotate(360deg);
224
+ }
225
+ }
226
+ @media (prefers-reduced-motion: reduce) {
227
+ .diagrams_spinner {
228
+ animation: diagrams_fadeIn .2s ease .6s forwards;
229
+ }
230
+ }
161
231
  .diagrams_previewPlaceholder {
162
232
  display: grid;
163
233
  place-items: center;
@@ -6263,6 +6333,43 @@ body.excalidraw-cursor-resize * {
6263
6333
  left: -5px;
6264
6334
  }
6265
6335
 
6336
+ /* src/client/ui.module.css */
6337
+ .ui_dialog {
6338
+ box-sizing: border-box;
6339
+ width: min(760px, calc(100% - 32px));
6340
+ max-height: calc(100dvh - 32px);
6341
+ padding: 20px;
6342
+ border: 1px solid var(--dsw-alias-border-l2);
6343
+ border-radius: 12px;
6344
+ color: var(--dsw-alias-label-primary);
6345
+ background: var(--dsw-alias-bg-layer-1);
6346
+ }
6347
+ .ui_dialog::backdrop {
6348
+ background: #0006;
6349
+ }
6350
+ .ui_header {
6351
+ display: flex;
6352
+ align-items: center;
6353
+ justify-content: space-between;
6354
+ gap: 16px;
6355
+ margin-bottom: 16px;
6356
+ }
6357
+ .ui_header h2 {
6358
+ margin: 0;
6359
+ font-size: 16px;
6360
+ }
6361
+ .ui_header button {
6362
+ border: 1px solid var(--dsw-alias-border-l2);
6363
+ border-radius: 6px;
6364
+ padding: 6px 10px;
6365
+ color: inherit;
6366
+ background: transparent;
6367
+ }
6368
+ .ui_content {
6369
+ overflow: auto;
6370
+ max-height: calc(100dvh - 140px);
6371
+ }
6372
+
6266
6373
  /* src/client/frame.css */
6267
6374
  html,
6268
6375
  body,
@@ -6304,3 +6411,9 @@ input {
6304
6411
  button {
6305
6412
  cursor: pointer;
6306
6413
  }
6414
+ body[data-mount-kind=tool],
6415
+ body[data-mount-kind=tool] #root {
6416
+ height: auto;
6417
+ min-height: 0;
6418
+ display: flow-root;
6419
+ }
package/dist/frame.js CHANGED
@@ -283,6 +283,11 @@ var diagrams_default = {
283
283
  toolDetails: "diagrams_toolDetails",
284
284
  previewViewport: "diagrams_previewViewport",
285
285
  streamImage: "diagrams_streamImage",
286
+ placeholder: "diagrams_placeholder",
287
+ placeholderTitle: "diagrams_placeholderTitle",
288
+ spinner: "diagrams_spinner",
289
+ fadeIn: "diagrams_fadeIn",
290
+ spin: "diagrams_spin",
286
291
  previewPlaceholder: "diagrams_previewPlaceholder",
287
292
  editButton: "diagrams_editButton",
288
293
  review: "diagrams_review",
@@ -301,15 +306,31 @@ function clearDownloads() {
301
306
  }
302
307
  var download = downloadDrawing;
303
308
  var errorText = (error) => error instanceof Error ? error.message : String(error);
309
+ function Placeholder({ error, locale, retry }) {
310
+ const t = (zh, en) => locale === "zh" ? zh : en;
311
+ if (!error) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: diagrams_default.placeholder, role: "status", "aria-busy": "true", "aria-label": t("\u6B63\u5728\u52A0\u8F7D Excalidraw", "Loading Excalidraw"), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: diagrams_default.spinner }) });
312
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: diagrams_default.placeholder, role: "alert", children: [
313
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: diagrams_default.placeholderTitle, children: t("Excalidraw \u6682\u65F6\u65E0\u6CD5\u52A0\u8F7D", "Excalidraw could not load") }),
314
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: diagrams_default.placeholderDetail, children: error }),
315
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", onClick: retry, children: t("\u91CD\u8BD5", "Retry") })
316
+ ] });
317
+ }
304
318
  function DiagramWorkspace(p) {
305
319
  const pending = (0, import_react.useRef)(null);
306
320
  const [draft, setDraft] = (0, import_react.useState)(null), [error, setError] = (0, import_react.useState)("");
321
+ const [attempt, setAttempt] = (0, import_react.useState)(0);
307
322
  (0, import_react.useEffect)(() => {
308
323
  if (!p.visible) return;
309
324
  pending.current ??= diagramAction(p.session.sessionId, "new", {});
310
- void pending.current.then(setDraft).catch((e) => setError(errorText(e)));
311
- }, [p.visible, p.session.sessionId]);
312
- return draft ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DiagramEditor, { ...p, contentId: draft.id }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { role: "status", children: error || "Excalidraw\u2026" });
325
+ void pending.current.then(setDraft).catch((e) => {
326
+ pending.current = null;
327
+ setError(errorText(e));
328
+ });
329
+ }, [p.visible, p.session.sessionId, attempt]);
330
+ return draft ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DiagramEditor, { ...p, contentId: draft.id }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Placeholder, { error, locale: p.locale, retry: () => {
331
+ setError("");
332
+ setAttempt((value) => value + 1);
333
+ } });
313
334
  }
314
335
  function DiagramEditor(p) {
315
336
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DiagramEditorRoute, { ...p }, JSON.stringify([p.session.sessionId, p.contentId]));
@@ -355,6 +376,7 @@ function NativeDiagramEditor(p) {
355
376
  const state = (0, import_react.useSyncExternalStore)(model.subscribe, model.getSnapshot);
356
377
  const [canvas, setCanvas] = (0, import_react.useState)(null), [error, setError] = (0, import_react.useState)("");
357
378
  const [dialog, setDialog] = (0, import_react.useState)(null), [path, setPath] = (0, import_react.useState)("");
379
+ const [attempt, setAttempt] = (0, import_react.useState)(0);
358
380
  const t = (zh, en) => p.locale === "zh" ? zh : en;
359
381
  const run = async (operation) => {
360
382
  setError("");
@@ -387,7 +409,7 @@ function NativeDiagramEditor(p) {
387
409
  clearInterval(timer2);
388
410
  model.suspend();
389
411
  };
390
- }, [model, p.visible, p.contentId, p.session.sessionId]);
412
+ }, [model, p.visible, p.contentId, p.session.sessionId, attempt]);
391
413
  const candidateId = state.draft?.candidate?.id;
392
414
  (0, import_react.useEffect)(() => {
393
415
  if (canvas && candidateId) void model.initialize(canvas.materialize).catch((e) => setError(errorText(e)));
@@ -406,6 +428,7 @@ function NativeDiagramEditor(p) {
406
428
  setPath(state.draft?.path ?? `diagrams/${p.contentId}.excalidraw`);
407
429
  setDialog("save");
408
430
  };
431
+ const ready = p.visible && canvas && state.scene;
409
432
  const downloadSource = async () => {
410
433
  const scene = model.getSnapshot().scene;
411
434
  if (scene) await download(new Blob([JSON.stringify(scene)], { type: "application/json" }), `${state.draft?.title ?? "diagram"}.excalidraw`);
@@ -417,7 +440,10 @@ function NativeDiagramEditor(p) {
417
440
  openDialog("save");
418
441
  }
419
442
  }, children: [
420
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: diagrams_default.canvas, "data-diagram-canvas": true, children: p.visible && canvas && state.scene ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(canvas.default, { scene: state.scene, readOnly: state.busy, locale: p.locale, onChange: (value) => model.change(value), onError: setError }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { role: "status", children: error || "Excalidraw\u2026" }) }),
443
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: diagrams_default.canvas, "data-diagram-canvas": true, children: ready ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(canvas.default, { scene: state.scene, readOnly: state.busy, locale: p.locale, onChange: (value) => model.change(value), onError: setError }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Placeholder, { error: error || state.error, locale: p.locale, retry: () => {
444
+ setError("");
445
+ setAttempt((value) => value + 1);
446
+ } }) }),
421
447
  dialog && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: diagrams_default.modalBackdrop, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("form", { className: diagrams_default.dialog, role: "dialog", "aria-label": t("\u4FDD\u5B58\u56FE\u7A3F", "Save diagram"), onSubmit: (event) => {
422
448
  event.preventDefault();
423
449
  void run(async () => {
@@ -440,7 +466,7 @@ function NativeDiagramEditor(p) {
440
466
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { onClick: () => model.restore(), children: t("\u6062\u590D", "Restore") }),
441
467
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { onClick: () => model.dismissRecovery(), children: t("\u4FDD\u7559\u5F53\u524D\u56FE\u7A3F", "Keep current") })
442
468
  ] }),
443
- !dialog && (error || state.error) && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: diagrams_default.error, role: "alert", children: [
469
+ ready && !dialog && (error || state.error) && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: diagrams_default.error, role: "alert", children: [
444
470
  error || state.error,
445
471
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { onClick: () => void run(() => model.reload()), children: t("\u91CD\u65B0\u8F7D\u5165", "Reload") }),
446
472
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { onClick: () => void run(downloadSource), children: t("\u4E0B\u8F7D\u8349\u7A3F", "Download draft") })
@@ -449,41 +475,65 @@ function NativeDiagramEditor(p) {
449
475
  }
450
476
 
451
477
  // src/client/DiagramPreview.tsx
478
+ var import_react4 = __toESM(require_react(), 1);
479
+
480
+ // src/client/ui.tsx
452
481
  var import_react3 = __toESM(require_react(), 1);
453
482
 
483
+ // src/client/ui.module.css
484
+ var ui_default = {
485
+ dialog: "ui_dialog",
486
+ header: "ui_header",
487
+ content: "ui_content"
488
+ };
489
+
454
490
  // src/client/ui.tsx
455
491
  var import_jsx_runtime2 = __toESM(require_jsx_runtime(), 1);
456
492
  function ExcalidrawIcon({ size = 18 }) {
457
493
  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
494
  }
459
495
  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();
496
+ const ref = (0, import_react3.useRef)(null);
497
+ (0, import_react3.useEffect)(() => {
498
+ if (!p.open || !ref.current) return;
499
+ const dialog = ref.current, previous = document.activeElement;
500
+ dialog.showModal();
501
+ return () => {
502
+ dialog.close();
503
+ if (previous instanceof HTMLElement && previous.isConnected) previous.focus();
504
+ };
505
+ }, [p.open]);
506
+ 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) => {
507
+ e.preventDefault();
508
+ p.onClose();
462
509
  }, 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 })
510
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("header", { className: ui_default.header, children: [
511
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h2", { children: p.title }),
512
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { autoFocus: true, type: "button", onClick: p.onClose, children: p.closeLabel })
513
+ ] }),
514
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: `${ui_default.content} ${p.contentClassName ?? ""}`, children: p.children })
465
515
  ] }) : null;
466
516
  }
467
517
 
468
518
  // src/client/DiagramPreview.tsx
469
519
  var import_jsx_runtime3 = __toESM(require_jsx_runtime(), 1);
470
520
  function useThumbnailUrl() {
471
- const [url, setUrl] = (0, import_react3.useState)("");
472
- (0, import_react3.useEffect)(() => () => {
521
+ const [url, setUrl] = (0, import_react4.useState)("");
522
+ (0, import_react4.useEffect)(() => () => {
473
523
  if (url) URL.revokeObjectURL(url);
474
524
  }, [url]);
475
525
  return [url, setUrl];
476
526
  }
477
527
  function DiagramToolView(p) {
478
528
  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);
529
+ const [collapsed, setCollapsed] = (0, import_react4.useState)(false), [details, setDetails] = (0, import_react4.useState)(false);
530
+ const bodyId = (0, import_react4.useId)();
531
+ const parametersButton = (0, import_react4.useRef)(null), restoreParametersFocus = (0, import_react4.useRef)(false);
482
532
  const closeDetails = () => {
483
533
  restoreParametersFocus.current = true;
484
534
  setDetails(false);
485
535
  };
486
- (0, import_react3.useEffect)(() => {
536
+ (0, import_react4.useEffect)(() => {
487
537
  if (!details && restoreParametersFocus.current) {
488
538
  parametersButton.current?.focus();
489
539
  restoreParametersFocus.current = false;
@@ -491,7 +541,7 @@ function DiagramToolView(p) {
491
541
  }, [details]);
492
542
  const t = (zh, en) => p.locale === "zh" ? zh : en;
493
543
  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: [
544
+ 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
545
  /* @__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
546
  /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ExcalidrawIcon, { size: 18 }),
497
547
  /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { className: diagrams_default.toolTitle, children: "create view" }),
@@ -514,7 +564,7 @@ function DiagramToolView(p) {
514
564
  ] });
515
565
  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
566
  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: [
567
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { "data-host-tool-card": p.hostToolCard || void 0, children: [
518
568
  content,
519
569
  /* @__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
570
  /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("h3", { children: t("\u8F93\u5165\u53C2\u6570", "Input") }),
@@ -528,10 +578,10 @@ function DiagramToolView(p) {
528
578
  }
529
579
  function StreamingDrawing({ input, locale, running, header, collapsed, bodyId }) {
530
580
  const theme = useDiagramTheme();
531
- const latest = (0, import_react3.useRef)(input);
581
+ const latest = (0, import_react4.useRef)(input);
532
582
  latest.current = input;
533
583
  const [image, setImage] = useThumbnailUrl();
534
- (0, import_react3.useEffect)(() => {
584
+ (0, import_react4.useEffect)(() => {
535
585
  let active = true, busy2 = false, prior2 = "";
536
586
  const render2 = async () => {
537
587
  if (collapsed || busy2 || !active || latest.current === prior2) return;
@@ -569,19 +619,19 @@ function StreamingDrawing({ input, locale, running, header, collapsed, bodyId })
569
619
  }
570
620
  function DiagramPreview({ sessionId, id, title, locale, open, header, collapsed, bodyId }) {
571
621
  const theme = useDiagramTheme();
572
- const ref = (0, import_react3.useRef)(null);
573
- const [visible, setVisible] = (0, import_react3.useState)(false);
622
+ const ref = (0, import_react4.useRef)(null);
623
+ const [visible, setVisible] = (0, import_react4.useState)(false);
574
624
  const [image, setImage] = useThumbnailUrl();
575
- const [error, setError] = (0, import_react3.useState)(""), [renderedKey, setRenderedKey] = (0, import_react3.useState)("");
576
- (0, import_react3.useEffect)(() => {
625
+ const [error, setError] = (0, import_react4.useState)(""), [renderedKey, setRenderedKey] = (0, import_react4.useState)("");
626
+ (0, import_react4.useEffect)(() => {
577
627
  const observer = new IntersectionObserver(([entry]) => setVisible(!!entry?.isIntersecting), { rootMargin: "160px" });
578
628
  if (ref.current) observer.observe(ref.current);
579
629
  return () => observer.disconnect();
580
630
  }, []);
581
631
  const model = diagramModel(sessionId, id);
582
- const state = (0, import_react3.useSyncExternalStore)(model.subscribe, model.getSnapshot);
632
+ const state = (0, import_react4.useSyncExternalStore)(model.subscribe, model.getSnapshot);
583
633
  const t = (zh, en) => locale === "zh" ? zh : en;
584
- (0, import_react3.useEffect)(() => {
634
+ (0, import_react4.useEffect)(() => {
585
635
  if (!visible || state.unavailable) return;
586
636
  const abort = new AbortController();
587
637
  const refresh = async () => {
@@ -604,7 +654,7 @@ function DiagramPreview({ sessionId, id, title, locale, open, header, collapsed,
604
654
  const candidate = state.draft?.candidate;
605
655
  const initial = !!candidate && state.draft?.revision === 0 && !state.draft.scene.elements.length && !candidate.patch;
606
656
  const stale = !!candidate && (candidate.baseRevision !== state.draft?.revision || state.unsynced);
607
- (0, import_react3.useEffect)(() => {
657
+ (0, import_react4.useEffect)(() => {
608
658
  if (!visible || !state.scene) return;
609
659
  let active = true;
610
660
  const render2 = async () => {
@@ -663,6 +713,9 @@ var tool = {};
663
713
  var frozen = false;
664
714
  var disposed = false;
665
715
  var composing = false;
716
+ var resizeObserver;
717
+ var resizeFrame = 0;
718
+ var lastHeight = -1;
666
719
  var pointers = /* @__PURE__ */ new Set();
667
720
  var contribution = {
668
721
  entry: { label: "Excalidraw", description: { zh: "\u624B\u7ED8\u56FE\u7A3F", en: "Editable drawings" }, kind: "workspace" },
@@ -676,10 +729,28 @@ function render() {
676
729
  document.body.toggleAttribute("data-ds-dark-theme", mount.theme === "dark");
677
730
  document.getElementById("root").inert = frozen;
678
731
  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) => {
732
+ 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
733
  void hostCall("openPanel", { kind: "editor", contentId: alias.replace(/^diagram:/, ""), title: "Excalidraw" });
681
734
  } }) : mount.kind === "workspace" ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(DiagramWorkspace, { ...p }) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(DiagramEditor, { ...p }));
682
735
  }
736
+ function observeToolSize() {
737
+ document.body.dataset.mountKind = "tool";
738
+ const element = document.getElementById("root");
739
+ const report = () => {
740
+ cancelAnimationFrame(resizeFrame);
741
+ resizeFrame = requestAnimationFrame(() => {
742
+ if (disposed) return;
743
+ const height = Math.min(4096, Math.ceil(element.getBoundingClientRect().height));
744
+ if (height === lastHeight) return;
745
+ lastHeight = height;
746
+ void hostCall("resize", { height }).catch(() => {
747
+ });
748
+ });
749
+ };
750
+ resizeObserver = new ResizeObserver(report);
751
+ resizeObserver.observe(element);
752
+ report();
753
+ }
683
754
  function busy() {
684
755
  return composing || pointers.size > 0 || canvasBusy() || modelStatus().busy;
685
756
  }
@@ -699,6 +770,8 @@ async function handle(method, input) {
699
770
  if (mount) throw new Error("Already initialized");
700
771
  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
772
  mount = input;
773
+ if (mount.kind === "tool" && mount.hostUi?.toolCard) await hostCall("toolPresentation", { title: "create view" });
774
+ if (mount.kind === "tool" && mount.hostUi?.autoResize) observeToolSize();
702
775
  render();
703
776
  await hostCall("changed", { ...modelStatus(), busy: busy() });
704
777
  return contribution;
@@ -767,6 +840,8 @@ var timer = setInterval(() => {
767
840
  function cleanup(closePort = true) {
768
841
  disposed = true;
769
842
  clearInterval(timer);
843
+ resizeObserver?.disconnect();
844
+ cancelAnimationFrame(resizeFrame);
770
845
  root.unmount();
771
846
  disposeModels();
772
847
  clearDownloads();
@@ -2541,8 +2541,8 @@
2541
2541
  },
2542
2542
  {
2543
2543
  "path": "./dist/frame.css",
2544
- "bytes": 171762,
2545
- "sha256": "58fe918e041e61a913ee08ecf86b523c5388ce2bcacb0e0f9136d5f6eb81808c",
2544
+ "bytes": 174290,
2545
+ "sha256": "c6754a0961df1226ed93e3d1aada5a346cb2bb805e9f0d28554136393a406ec0",
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": 43377,
2557
+ "sha256": "294fb4d91497fdd0c6fe71014c91eccb686afb3357eb747dba548346a25f2b27",
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.4",
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": "d00a2f0881c824b15a9178b1ff602db94e7c4dac935c46becc0db448d634542e",
57
+ "source": "5376a9abcf8ff7c3493b186f2b93b22d444e269b"
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
  }