@lark-apaas/coding-registry-buildless 0.1.25 → 0.1.27-alpha.20261105143040

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.
@@ -31,9 +31,9 @@
31
31
  // it). Inlined per script rather than shipped as its own registry file:
32
32
  // call sites use the global bare, so inlining is what guarantees
33
33
  // definition-before-use — a separate copy-in file would add a whole "SDK
34
- // copied but <script> tag missing" failure mode. Keep this block identical
35
- // across deck-stage.js / design-canvas.jsx / animations.jsx — same
36
- // convention as the mobile-UA regex. Two halves of
34
+ // copied but <script> tag missing" failure mode. Keep every inlined copy
35
+ // identical (find them by this block's heading) — same convention as the
36
+ // mobile-UA regex. Two halves of
37
37
  // the 妙享 (Magic Share) mirror protocol:
38
38
  // emit(msg, opts) — stamps msg.__mirror so the host relay treats the
39
39
  // message as a broadcastable UI-state snapshot;
@@ -101,9 +101,9 @@ function dcFitTransformToBounds(bounds, vp, minScale, maxScale) {
101
101
  // it). Inlined per script rather than shipped as its own registry file:
102
102
  // call sites use the global bare, so inlining is what guarantees
103
103
  // definition-before-use — a separate copy-in file would add a whole "SDK
104
- // copied but <script> tag missing" failure mode. Keep this block identical
105
- // across deck-stage.js / design-canvas.jsx / animations.jsx — same
106
- // convention as the mobile-UA regex. Two halves of
104
+ // copied but <script> tag missing" failure mode. Keep every inlined copy
105
+ // identical (find them by this block's heading) — same convention as the
106
+ // mobile-UA regex. Two halves of
107
107
  // the 妙享 (Magic Share) mirror protocol:
108
108
  // emit(msg, opts) — stamps msg.__mirror so the host relay treats the
109
109
  // message as a broadcastable UI-state snapshot;
@@ -315,6 +315,17 @@ if (typeof document !== 'undefined' && !document.getElementById('dc-styles')) {
315
315
  }
316
316
 
317
317
  const DCCtx = React.createContext(null);
318
+ const DC_EMPTY_SECTION = {};
319
+
320
+ function dcSectionsInDOMOrder(sections) {
321
+ return [...sections].sort(([a], [b]) => {
322
+ const position = a.compareDocumentPosition(b);
323
+ if (position & Node.DOCUMENT_POSITION_DISCONNECTED) return 0;
324
+ if (position & Node.DOCUMENT_POSITION_FOLLOWING) return -1;
325
+ if (position & Node.DOCUMENT_POSITION_PRECEDING) return 1;
326
+ return 0;
327
+ });
328
+ }
318
329
 
319
330
  // Recursively unwrap React.Fragment so <>…</> grouping doesn't hide
320
331
  // DCSection/DCArtboard children from the type-based walks below.
@@ -365,6 +376,23 @@ function miaodaWriteFile(path, content, writeContext) {
365
376
 
366
377
  function DesignCanvas({ children, minScale, maxScale, style }) {
367
378
  const [state, setState] = React.useState({ sections: {}, focus: null });
379
+ const [registeredSections, setRegisteredSections] = React.useState(() => new Map());
380
+ const registeredSectionsRef = React.useRef(registeredSections);
381
+ registeredSectionsRef.current = registeredSections;
382
+ const getSectionNavigation = React.useCallback(() =>
383
+ dcSectionsInDOMOrder(registeredSectionsRef.current).map(([, sec]) => ({
384
+ id: sec.id, slotIds: sec.slotIds,
385
+ })), []);
386
+ const registerSection = React.useCallback((node, snapshot) => {
387
+ setRegisteredSections((current) => new Map(current).set(node, snapshot));
388
+ return () => setRegisteredSections((current) => {
389
+ if (current.get(node) !== snapshot) return current;
390
+ const next = new Map(current);
391
+ next.delete(node);
392
+ return next;
393
+ });
394
+ }, []);
395
+
368
396
  // Hold rendering until the sidecar read settles so the saved order/titles
369
397
  // appear on first paint (no source-order flash). didRead gates writes until
370
398
  // the read settles so the empty initial state can't clobber a slow read;
@@ -480,51 +508,44 @@ function DesignCanvas({ children, minScale, maxScale, style }) {
480
508
  return () => offs.forEach((off) => off());
481
509
  }, []);
482
510
 
483
- // Build registries synchronously from children so FocusOverlay can read
484
- // them in the same render. Fragments are flattened; wrapping in other
485
- // elements still opts out of focus/reorder.
511
+ // Sections register the same visible artboards they render, even when a
512
+ // custom component separates them from DesignCanvas in the JSX tree.
486
513
  const registry = {}; // slotId -> { sectionId, artboard }
487
- const sectionMeta = {}; // sectionId -> { title, subtitle, slotIds[] }
488
- const sectionOrder = [];
489
- dcFlatten(children).forEach((sec) => {
490
- if (!sec || sec.type !== DCSection) return;
491
- const sid = sec.props.id ?? sec.props.title;
492
- if (!sid) return;
493
- sectionOrder.push(sid);
494
- const persisted = state.sections[sid] || {};
495
- const abs = [];
496
- dcFlatten(sec.props.children).forEach((ab) => {
497
- if (!ab || ab.type !== DCArtboard) return;
498
- const aid = ab.props.id ?? ab.props.label;
499
- if (aid) abs.push([aid, ab]);
500
- });
501
- // hidden is scoped to one source revision — when the agent regenerates
502
- // (artboard-ID set changes), prior deletes don't apply to new content.
503
- const srcKey = abs.map(([k]) => k).join('\x1f');
504
- const hidden = persisted.srcKey === srcKey ? (persisted.hidden || []) : [];
505
- const srcIds = [];
506
- abs.forEach(([aid, ab]) => {
507
- if (hidden.includes(aid)) return;
508
- registry[`${sid}/${aid}`] = { sectionId: sid, artboard: ab };
509
- srcIds.push(aid);
514
+ registeredSections.forEach((sec, node) => {
515
+ const sid = sec.id;
516
+ sec.slotIds.forEach((aid) => {
517
+ registry[`${sid}/${aid}`] = { sectionId: sid, artboard: sec.artboards[aid], node };
510
518
  });
511
- const kept = (persisted.order || []).filter((k) => srcIds.includes(k));
512
- sectionMeta[sid] = {
513
- title: persisted.title ?? sec.props.title,
514
- subtitle: sec.props.subtitle,
515
- slotIds: [...kept, ...srcIds.filter((k) => !kept.includes(k))],
516
- };
517
519
  });
518
520
 
521
+ const focusedEntry = registry[state.focus];
522
+ const focusedSection = focusedEntry && focusedEntry.node;
523
+ const displayedFocus = React.useRef(null);
524
+ React.useLayoutEffect(() => {
525
+ if (focusedEntry) displayedFocus.current = state.focus;
526
+ else if (state.focus && displayedFocus.current === state.focus) {
527
+ // Match the WC correction path: a removed target must not broadcast a
528
+ // focus:null that would close the presenter's overlay on another client.
529
+ skipNextFocusEmit.current = true;
530
+ focusRef.current = null;
531
+ setState((s) => s.focus === state.focus ? { ...s, focus: null } : s);
532
+ } else if (!state.focus) displayedFocus.current = null;
533
+ // A mirrored focus can precede section registration; only clear a target
534
+ // that was displayed here and has subsequently been removed.
535
+ }, [state.focus, focusedEntry]);
536
+
519
537
  const api = React.useMemo(() => ({
520
538
  state,
521
- section: (id) => state.sections[id] || {},
539
+ registerSection,
540
+ getSectionNavigation,
541
+ focusedSection,
542
+ section: (id) => state.sections[id] || DC_EMPTY_SECTION,
522
543
  patchSection: (id, p) => setState((s) => ({
523
544
  ...s,
524
545
  sections: { ...s.sections, [id]: { ...s.sections[id], ...(typeof p === 'function' ? p(s.sections[id] || {}) : p) } },
525
546
  })),
526
547
  setFocus: (slotId) => setState((s) => ({ ...s, focus: slotId })),
527
- }), [state]);
548
+ }), [state, registerSection, getSectionNavigation, focusedSection]);
528
549
 
529
550
  // Esc exits focus; any outside pointerdown commits an in-progress rename.
530
551
  React.useEffect(() => {
@@ -551,9 +572,6 @@ function DesignCanvas({ children, minScale, maxScale, style }) {
551
572
  <DCCtx.Provider value={api}>
552
573
  <DCViewport minScale={minScale} maxScale={maxScale} style={style}>{ready && worldChildren}</DCViewport>
553
574
  {ready && overlayChildren}
554
- {state.focus && registry[state.focus] && (
555
- <DCFocusOverlay entry={registry[state.focus]} sectionMeta={sectionMeta} sectionOrder={sectionOrder} />
556
- )}
557
575
  </DCCtx.Provider>
558
576
  );
559
577
  }
@@ -1249,13 +1267,19 @@ function DCViewport({ children, minScale = 0.1, maxScale = 8, style = {} }) {
1249
1267
  // ─────────────────────────────────────────────────────────────
1250
1268
  function DCSection({ id, title, subtitle, children, gap = 48 }) {
1251
1269
  const ctx = React.useContext(DCCtx);
1270
+ const sectionRef = React.useRef(null);
1252
1271
  const sid = id ?? title;
1253
- const all = React.Children.toArray(dcFlatten(children));
1254
- const artboards = all.filter((c) => c && c.type === DCArtboard);
1255
- const rest = all.filter((c) => !(c && c.type === DCArtboard));
1256
- const sec = (ctx && sid && ctx.section(sid)) || {};
1257
- // Must match DesignCanvas's srcKey computation exactly (it filters falsy
1258
- // IDs), or onDelete persists a srcKey that DesignCanvas never recognizes.
1272
+ const { artboards, rest, byId } = React.useMemo(() => {
1273
+ const all = React.Children.toArray(dcFlatten(children));
1274
+ const artboards = all.filter((c) => c && c.type === DCArtboard);
1275
+ return {
1276
+ artboards,
1277
+ rest: all.filter((c) => !(c && c.type === DCArtboard)),
1278
+ byId: Object.fromEntries(artboards.map((a) => [a.props.id ?? a.props.label, a])),
1279
+ };
1280
+ }, [children]);
1281
+ const sec = (ctx && sid && ctx.section(sid)) || DC_EMPTY_SECTION;
1282
+ // Scope saved deletes to the current source revision.
1259
1283
  const allIds = artboards.map((a) => a.props.id ?? a.props.label).filter(Boolean);
1260
1284
  const srcKey = allIds.join('\x1f');
1261
1285
  const hidden = sec.srcKey === srcKey ? (sec.hidden || []) : [];
@@ -1266,7 +1290,17 @@ function DCSection({ id, title, subtitle, children, gap = 48 }) {
1266
1290
  return [...kept, ...srcOrder.filter((k) => !kept.includes(k))];
1267
1291
  }, [sec.order, srcOrder.join('|')]);
1268
1292
 
1269
- const byId = Object.fromEntries(artboards.map((a) => [a.props.id ?? a.props.label, a]));
1293
+ const snapshot = React.useMemo(() => ({
1294
+ id: sid, title: sec.title ?? title, subtitle, slotIds: order, artboards: byId,
1295
+ }), [sid, sec.title, title, subtitle, order, byId]);
1296
+ const registerSection = ctx && ctx.registerSection;
1297
+ React.useLayoutEffect(() => {
1298
+ if (!sid || !registerSection) return;
1299
+ return registerSection(sectionRef.current, snapshot);
1300
+ }, [sid, registerSection, snapshot]);
1301
+
1302
+ const focusedId = order.find((k) => ctx && ctx.state.focus === `${sid}/${k}`);
1303
+ const focusedArtboard = focusedId === undefined ? null : byId[focusedId];
1270
1304
 
1271
1305
  // marginBottom counter-scales so the on-screen gap between sections stays
1272
1306
  // constant — otherwise at low zoom the (world-space) gap collapses while
@@ -1275,7 +1309,8 @@ function DCSection({ id, title, subtitle, children, gap = 48 }) {
1275
1309
  // the 24px artboard-header (abs-positioned above each card) plus ~8px, so
1276
1310
  // the title sits tight against its own row at every zoom.
1277
1311
  return (
1278
- <div data-dc-section={sid}
1312
+ <>
1313
+ <div ref={sectionRef} data-dc-section={sid}
1279
1314
  style={{ marginBottom: 'calc(80px * var(--dc-inv-zoom, 1))', position: 'relative' }}>
1280
1315
  <div style={{ padding: '0 60px' }}>
1281
1316
  <div className="dc-sectionhead" style={{ paddingBottom: 'calc(36px * var(--dc-inv-zoom, 1))' }}>
@@ -1305,6 +1340,13 @@ function DCSection({ id, title, subtitle, children, gap = 48 }) {
1305
1340
  </div>
1306
1341
  {rest}
1307
1342
  </div>
1343
+ {/* Keep the portal in the section's React ancestry so content retains
1344
+ its providers even though the overlay DOM lives under document.body. */}
1345
+ {focusedArtboard && ctx.focusedSection === sectionRef.current && (
1346
+ <DCFocusOverlay entry={{ sectionId: sid, artboard: focusedArtboard }}
1347
+ peers={order} getSectionNavigation={ctx.getSectionNavigation} />
1348
+ )}
1349
+ </>
1308
1350
  );
1309
1351
  }
1310
1352
 
@@ -1605,24 +1647,24 @@ function DCEditable({ value, onChange, style, tag = 'span', onClick }) {
1605
1647
  // Focus mode — overlay one artboard; ←/→ within section, ↑/↓ across
1606
1648
  // sections, Esc or backdrop click to exit.
1607
1649
  // ─────────────────────────────────────────────────────────────
1608
- function DCFocusOverlay({ entry, sectionMeta, sectionOrder }) {
1650
+ function DCFocusOverlay({ entry, peers, getSectionNavigation }) {
1609
1651
  const ctx = React.useContext(DCCtx);
1610
1652
  const { sectionId, artboard } = entry;
1611
- const meta = sectionMeta[sectionId];
1612
- const peers = meta.slotIds;
1613
1653
  const aid = artboard.props.id ?? artboard.props.label;
1614
1654
  const idx = peers.indexOf(aid);
1615
- const secIdx = sectionOrder.indexOf(sectionId);
1616
1655
 
1617
1656
  const go = (d) => { const n = peers[(idx + d + peers.length) % peers.length]; if (n) ctx.setFocus(`${sectionId}/${n}`); };
1618
1657
  const goSection = (d) => {
1658
+ // Other sections can update independently while this portal stays mounted.
1659
+ const sections = getSectionNavigation();
1660
+ const secIdx = sections.findIndex((sec) => sec.id === sectionId);
1619
1661
  // Sections whose artboards are all deleted have slotIds:[] — step past
1620
1662
  // them to the next non-empty section so ↑/↓ doesn't dead-end.
1621
- const n = sectionOrder.length;
1663
+ const n = sections.length;
1622
1664
  for (let i = 1; i < n; i++) {
1623
- const ns = sectionOrder[(((secIdx + d * i) % n) + n) % n];
1624
- const first = sectionMeta[ns] && sectionMeta[ns].slotIds[0];
1625
- if (first) { ctx.setFocus(`${ns}/${first}`); return; }
1665
+ const next = sections[(((secIdx + d * i) % n) + n) % n];
1666
+ const first = next.slotIds[0];
1667
+ if (first) { ctx.setFocus(`${next.id}/${first}`); return; }
1626
1668
  }
1627
1669
  };
1628
1670
 
@@ -1694,7 +1736,7 @@ function DCFocusOverlay({ entry, sectionMeta, sectionOrder }) {
1694
1736
  // Portal to body so position:fixed is the real viewport regardless of any
1695
1737
  // transform on DesignCanvas's ancestors (including the canvas zoom itself).
1696
1738
  return ReactDOM.createPortal(
1697
- <div onClick={() => ctx.setFocus(null)}
1739
+ <div onClick={(e) => { e.stopPropagation(); ctx.setFocus(null); }}
1698
1740
  onMouseMove={(e) => setChromeVisible(!(contentRef.current && contentRef.current.contains(e.target)))}
1699
1741
  onMouseLeave={() => setChromeVisible(false)}
1700
1742
  onWheel={(e) => { if (!(contentRef.current && contentRef.current.contains(e.target))) e.preventDefault(); }}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/coding-registry-buildless",
3
- "version": "0.1.25",
3
+ "version": "0.1.27-alpha.20261105143040",
4
4
  "description": "Miaoda registry (buildless runtime) — shadcn-style copy-in SDKs (globals + CDN-pinned, browser Babel) for buildless static HTML stacks",
5
5
  "type": "module",
6
6
  "files": [
@@ -80,6 +80,76 @@
80
80
  /* END USAGE */
81
81
 
82
82
  (() => {
83
+ // ── Mirror sync singleton ──────────────────────────────────────────────────
84
+ // Shared by every buildless script on the page (first one to load defines
85
+ // it). Inlined per script rather than shipped as its own registry file:
86
+ // call sites use the global bare, so inlining is what guarantees
87
+ // definition-before-use — a separate copy-in file would add a whole "SDK
88
+ // copied but <script> tag missing" failure mode. Keep every inlined copy
89
+ // identical (find them by this block's heading) — same convention as the
90
+ // mobile-UA regex. Two halves of
91
+ // the 妙享 (Magic Share) mirror protocol:
92
+ // emit(msg, opts) — stamps msg.__mirror so the host relay treats the
93
+ // message as a broadcastable UI-state snapshot;
94
+ // unstamped messages are never mirrored. opts.key is
95
+ // the sync channel (maps to the share SDK's
96
+ // actionType): late joiners receive only the LATEST
97
+ // snapshot per key, so states that overwrite each
98
+ // other must share one key. opts.throttle is the
99
+ // suggested host-side coalescing interval in ms —
100
+ // snapshots make dropped intermediate frames
101
+ // lossless, so hint it on any continuous gesture.
102
+ // onApply(type, fn) — replay entry for a message type this script itself
103
+ // emits. The host sends {type:'miaoda:mirror:apply',
104
+ // uuid, message, latencyMs} and needs
105
+ // {type:'miaoda:mirror:apply-result', uuid, result}
106
+ // back to drive the SDK's retry: result.type
107
+ // 'failed' means retry (use it for transient
108
+ // not-ready states), 'ignored' is terminal (not
109
+ // applicable / bad payload), 'applied' is success.
110
+ // Unregistered types stay silent on purpose —
111
+ // another script on the page may own them, and the
112
+ // host's timeout is the backstop.
113
+ if (!window.__miaodaMirror) {
114
+ const appliers = new Map(); // message.type -> Set<fn>
115
+ window.__miaodaMirror = {
116
+ emit(msg, opts) {
117
+ try { window.parent.postMessage(Object.assign({}, msg, { __mirror: Object.assign({ v: 1 }, opts) }), '*'); } catch (e) {}
118
+ },
119
+ onApply(type, fn) {
120
+ if (!appliers.has(type)) appliers.set(type, new Set());
121
+ appliers.get(type).add(fn);
122
+ return () => { const s = appliers.get(type); if (s) s.delete(fn); };
123
+ },
124
+ };
125
+ window.addEventListener('message', async (e) => {
126
+ // Replays are accepted from the host frame only — generated pages embed
127
+ // arbitrary third-party iframes, any of which could otherwise forge an
128
+ // apply and drive state (plus its persisted side effects).
129
+ if (e.source !== window.parent) return;
130
+ const d = e.data;
131
+ if (!d || d.type !== 'miaoda:mirror:apply' || !d.message || !d.message.type) return;
132
+ const fns = appliers.get(d.message.type);
133
+ if (!fns || !fns.size) return;
134
+ // Merge across handlers (multi-instance case): one success is enough.
135
+ // Instances share a channel, so a sibling that can't apply yet (a lazily
136
+ // slotted second deck returning 'failed') must not poison an applied
137
+ // result into an SDK retry storm of no-ops — 'failed' only stands when
138
+ // nobody applied. Every handler still runs (no short-circuit) so each
139
+ // sibling instance gets the state.
140
+ let result = { type: 'ignored', reason: 'no applier result' };
141
+ for (const fn of Array.from(fns)) {
142
+ let r;
143
+ try { r = await fn(d.message, { latencyMs: d.latencyMs, uuid: d.uuid }); }
144
+ catch (err) { r = { type: 'failed', reason: String((err && err.message) || err) }; }
145
+ if (!r) continue;
146
+ if (r.type === 'applied') result = r;
147
+ else if (result.type !== 'applied' && (r.type === 'failed' || result.type !== 'failed')) result = r;
148
+ }
149
+ try { window.parent.postMessage({ type: 'miaoda:mirror:apply-result', uuid: d.uuid, result }, '*'); } catch (e2) {}
150
+ });
151
+ }
152
+
83
153
  /** Shorthands for the common frames; anything else is computed by parseRatio — this is not an allowlist */
84
154
  const RATIOS = {
85
155
  '3:4': [1080, 1440],
@@ -414,13 +484,20 @@
414
484
  (paging / zoom / copy / download).
415
485
  overflow:auto is what lets you scroll a card that has been zoomed past the viewport,
416
486
  and the holder centres with margin:auto — flex centring clips the top once the content
417
- outgrows its container. */
487
+ outgrows its container.
488
+ The enlarged card must stay commentable while everything around it must not: the host
489
+ exempts a whole event path once any ancestor carries the chrome mark (csChrome), so
490
+ the mark cannot sit on .cs-focus. The dark ground is a separate scrim sibling carrying the mark,
491
+ and the holder is exactly the card's size — every empty pixel then belongs to the
492
+ scrim. pointer-events cannot carve that out instead: comment mode forces
493
+ pointer-events:auto on every element. */
418
494
  .cs-focus {
419
495
  position: fixed; inset: 0; z-index: 100;
420
- background: rgba(16,17,20,.92);
421
496
  display: grid; overflow: auto;
497
+ padding: 56px 24px 104px; box-sizing: border-box;
422
498
  }
423
- .cs-focus .cs-holder { margin: auto; padding: 56px 24px 104px; }
499
+ .cs-focus-scrim { position: fixed; inset: 0; background: rgba(16,17,20,.92); }
500
+ .cs-focus .cs-holder { position: relative; margin: auto; }
424
501
  .cs-focus .cs-holder > section { outline: none !important; box-shadow: none !important; }
425
502
  .cs-focus-close {
426
503
  position: fixed; top: 20px; right: 24px; z-index: 2;
@@ -1239,6 +1316,9 @@
1239
1316
  connectedCallback() {
1240
1317
  if (this.dataset.ready) return;
1241
1318
  this.dataset.ready = '1';
1319
+ // Before init, not after: a late joiner's replay can land while init still awaits the
1320
+ // sidecar, and only a registered applier can answer 'failed' to make the SDK retry
1321
+ this.wireMirror();
1242
1322
  // The script is usually loaded synchronously from <head>, at which point the child
1243
1323
  // <section>s are not parsed yet — wait for the document before initialising
1244
1324
  if (document.readyState === 'loading') {
@@ -1364,10 +1444,15 @@
1364
1444
  // themselves; the "fit" button and the 0 key do not count as framing, since following
1365
1445
  // the viewport is exactly what they ask for.
1366
1446
  addEventListener('resize', () => {
1447
+ if (this._remoteBounds) { this.fitBounds(this._remoteBounds); return; }
1367
1448
  if (this.userFramed) { this.placeHeads(); return; }
1368
1449
  this.fit();
1369
1450
  });
1370
1451
  }
1452
+ this._ready = true;
1453
+ // Baseline for late joiners (the viewport baseline already went out through fit)
1454
+ this.emitStructure();
1455
+ this.emitFocus();
1371
1456
  ready();
1372
1457
  void this.reportOverflow();
1373
1458
  }
@@ -1572,6 +1657,7 @@
1572
1657
  section.dataset.label = label.textContent.trim();
1573
1658
  this._labelOverrides[section.dataset.cardKey] = section.dataset.label;
1574
1659
  this.saveState();
1660
+ this.emitStructure();
1575
1661
  });
1576
1662
  label.addEventListener('keydown', (e) => {
1577
1663
  if (e.key === 'Enter') { e.preventDefault(); label.blur(); }
@@ -1679,20 +1765,27 @@
1679
1765
  deleteCard(n) {
1680
1766
  const section = this.cardAt(n);
1681
1767
  if (!section) return;
1682
- if (this.focusIndex === n) this.closeFocus();
1683
1768
  const key = section.dataset.cardKey;
1769
+ this.dropCard(section);
1770
+ if (key && !this._hiddenKeys.includes(key)) this._hiddenKeys.push(key);
1771
+ this.saveState();
1772
+ this.emitStructure();
1773
+ this.toast('已删除');
1774
+ }
1775
+
1776
+ /** Takes a card off the page — shared by a local delete and a replayed one, which must not persist */
1777
+ dropCard(section) {
1778
+ if (this.focusIndex === Number(section.dataset.cardIndex)) this.closeFocus();
1684
1779
  const head = this.headFor && this.headFor.get(section);
1685
1780
  if (head) head.remove();
1686
1781
  if (this.headFor) this.headFor.delete(section);
1687
1782
  section.remove();
1688
- if (key && !this._hiddenKeys.includes(key)) this._hiddenKeys.push(key);
1689
- this.saveState();
1690
1783
 
1691
1784
  const cards = this.cards();
1692
1785
  cards.forEach((s, i) => { s.dataset.cardIndex = String(i + 1); });
1693
- this.heads = cards.map((s) => this.headFor.get(s));
1786
+ if (this.headFor) this.heads = cards.map((s) => this.headFor.get(s));
1694
1787
  this.placeHeads();
1695
- this.toast('已删除');
1788
+ if (csIsMobile) this.goToPage(this.page, false);
1696
1789
  }
1697
1790
 
1698
1791
  /**
@@ -1811,10 +1904,18 @@
1811
1904
 
1812
1905
  // A click on a card opens focus mode (but not when the click lands on a link, button or
1813
1906
  // the label)
1907
+ // Releasing a text-selection drag still dispatches a click on the card. Opening focus
1908
+ // there would bury the selection under the overlay before the host's selection-comment
1909
+ // toolbar can use it, so a press that moved or left a selection is not a click.
1910
+ let downX = 0, downY = 0;
1911
+ this.addEventListener('pointerdown', (e) => { downX = e.clientX; downY = e.clientY; });
1814
1912
  this.addEventListener('click', (e) => {
1815
1913
  const section = e.target.closest('card-stage > section');
1816
1914
  if (!section) return;
1817
1915
  if (e.target.closest('a, button, input, textarea, [contenteditable]')) return;
1916
+ if (Math.hypot(e.clientX - downX, e.clientY - downY) > 4) return;
1917
+ const sel = getSelection();
1918
+ if (sel && !sel.isCollapsed && section.contains(sel.anchorNode)) return;
1818
1919
  this.openFocus(Number(section.dataset.cardIndex));
1819
1920
  });
1820
1921
 
@@ -1849,6 +1950,9 @@
1849
1950
  this.style.transform = `translate(${this.panX}px, ${this.panY}px) scale(${this.zoom})`;
1850
1951
  this.syncZoomBar();
1851
1952
  this.placeHeads();
1953
+ // Any local framing takes the view back from the presenter, resize included
1954
+ if (!this._applyingMirror) this._remoteBounds = null;
1955
+ this.emitViewport();
1852
1956
  }
1853
1957
 
1854
1958
  zoomAt(cx, cy, factor) {
@@ -1938,6 +2042,7 @@
1938
2042
  this._slideTimer = setTimeout(() => { this.style.transition = 'none'; }, 300);
1939
2043
  }
1940
2044
  this.syncDots();
2045
+ this.emitFocus();
1941
2046
  }
1942
2047
 
1943
2048
  wireSwipe() {
@@ -2250,6 +2355,9 @@
2250
2355
  this.focusZoom = this.focusFit;
2251
2356
  this._focusFrame = frame;
2252
2357
  this._focusClone = clone;
2358
+ // Mirrored by key, not index: indices shift under deletes, and the two ends may have
2359
+ // applied a different set of them when a snapshot crosses
2360
+ this._focusKey = src.dataset.cardKey;
2253
2361
 
2254
2362
  const close = document.createElement('button');
2255
2363
  close.className = 'cs-focus-close';
@@ -2299,9 +2407,20 @@
2299
2407
  };
2300
2408
  bar.append(prev, counter, next, sep(), zoomOut, this._focusPct, zoomIn, sep(), copy, download);
2301
2409
 
2302
- overlay.append(holder, close, bar);
2410
+ const scrim = document.createElement('div');
2411
+ scrim.className = 'cs-focus-scrim';
2412
+ // The scrim is fixed, so its containing block is the viewport and a wheel over it would
2413
+ // chain to the document rather than the overlay — scrolling a zoomed-in card from the
2414
+ // dark margin is forwarded by hand. Ctrl/⌘ is left to bubble to the zoom handler below.
2415
+ scrim.addEventListener('wheel', (e) => {
2416
+ if (e.ctrlKey || e.metaKey) return;
2417
+ e.preventDefault();
2418
+ overlay.scrollBy(e.deltaX, e.deltaY);
2419
+ }, { passive: false });
2420
+
2421
+ overlay.append(csChrome(scrim), holder, csChrome(close), csChrome(bar));
2303
2422
  overlay.addEventListener('click', (e) => {
2304
- if (e.target === overlay || e.target === holder) this.closeFocus();
2423
+ if (e.target === overlay || e.target === scrim) this.closeFocus();
2305
2424
  });
2306
2425
  // Focus mode's right-click shares the canvas menu
2307
2426
  clone.addEventListener('contextmenu', (e) => {
@@ -2315,7 +2434,9 @@
2315
2434
  this.setFocusZoom(this.focusZoom * Math.exp(-e.deltaY / 300));
2316
2435
  }, { passive: false });
2317
2436
 
2318
- document.body.appendChild(csChrome(overlay));
2437
+ overlay.addEventListener('scroll', () => this.emitFocus(), { passive: true });
2438
+
2439
+ document.body.appendChild(overlay);
2319
2440
  this.overlay = overlay;
2320
2441
  this.applyFocusZoom();
2321
2442
  }
@@ -2331,16 +2452,188 @@
2331
2452
  this._focusFrame.style.height = this.H * this.focusZoom + 'px';
2332
2453
  this._focusClone.style.transform = `scale(${this.focusZoom})`;
2333
2454
  if (this._focusPct) this._focusPct.textContent = Math.round(this.focusZoom * 100) + '%';
2455
+ this.emitFocus();
2334
2456
  }
2335
2457
 
2336
2458
  closeFocus() {
2337
2459
  this.closeContextMenu();
2460
+ const wasOpen = !!this.overlay;
2338
2461
  if (this.overlay) this.overlay.remove();
2339
2462
  this.overlay = null;
2340
2463
  this.focusIndex = null;
2464
+ this._focusKey = null;
2341
2465
  this._focusFrame = null;
2342
2466
  this._focusClone = null;
2343
2467
  this._focusPct = null;
2468
+ if (wasOpen) this.emitFocus();
2469
+ }
2470
+
2471
+ // —— Magic Share (妙享) ——
2472
+ // Three keys, because a late joiner receives only the latest snapshot per key: canvas
2473
+ // framing, the focused card, and deletes / renames never overwrite one another.
2474
+ // Every applier answers 'failed' until init has laid the cards out — that is transient and
2475
+ // only 'failed' makes the SDK retry.
2476
+ wireMirror() {
2477
+ // Capture iframes (?thumbnail= product screenshots, ?export=1) sit out in both directions
2478
+ // — same rule as design-canvas's dcCaptureMode
2479
+ if (/[?&](_snthumb|thumbnail)=|[?&]export=1(&|$)/.test(location.search || '')) return;
2480
+ this._mirrorOn = true;
2481
+ // The applier registry is page-global and outlives this element: once it is taken off the
2482
+ // page it must not answer for (or draw overlays on behalf of) the stage that replaced it
2483
+ const on = (type, fn) => window.__miaodaMirror.onApply(type, (m) =>
2484
+ this.isConnected ? fn(m) : { type: 'ignored', reason: 'stage detached' });
2485
+ on('miaoda:cards:viewport-changed', (m) => this.applyMirrorViewport(m));
2486
+ on('miaoda:cards:focus-changed', (m) => this.applyMirrorFocus(m));
2487
+ on('miaoda:cards:structure-changed', (m) => this.applyMirrorStructure(m));
2488
+ }
2489
+
2490
+ /** Runs a replay so that the emits it triggers are not echoed back as this end's own */
2491
+ asMirror(fn) {
2492
+ this._applyingMirror = true;
2493
+ try { fn(); } finally { this._applyingMirror = false; }
2494
+ }
2495
+
2496
+ // The transform is local screen geometry and cannot be replayed on another window size, so
2497
+ // the world rectangle in view is mirrored instead and each end fits it into its own window
2498
+ // — same scheme as design-canvas's viewport v2.
2499
+ emitViewport() {
2500
+ if (!this._mirrorOn || this._applyingMirror || csIsMobile || this._viewportT) return;
2501
+ // Trailing 120ms: applyTransform runs at pointer rate; reading at fire time loses nothing
2502
+ this._viewportT = setTimeout(() => {
2503
+ this._viewportT = 0;
2504
+ if (!this.isConnected) return;
2505
+ const z = this.zoom;
2506
+ window.__miaodaMirror.emit(
2507
+ { type: 'miaoda:cards:viewport-changed', bounds: { x: -this.panX / z, y: -this.panY / z, width: innerWidth / z, height: innerHeight / z } },
2508
+ { key: 'cards:viewport', throttle: 60 },
2509
+ );
2510
+ }, 120);
2511
+ }
2512
+
2513
+ applyMirrorViewport(m) {
2514
+ // Mobile pages card by card; it follows cards:focus instead
2515
+ if (csIsMobile) return { type: 'ignored', reason: 'mobile follows the focused card' };
2516
+ if (!this._ready) return { type: 'failed', reason: 'not laid out yet' };
2517
+ const b = m.bounds;
2518
+ if (!b || ![b.x, b.y, b.width, b.height].every(Number.isFinite) || !(b.width > 0) || !(b.height > 0)) {
2519
+ return { type: 'ignored', reason: 'bad bounds' };
2520
+ }
2521
+ // A timer armed by a local gesture would fire with the presenter's view and echo it back
2522
+ clearTimeout(this._viewportT);
2523
+ this._viewportT = 0;
2524
+ this.fitBounds(b);
2525
+ return { type: 'applied' };
2526
+ }
2527
+
2528
+ fitBounds(b) {
2529
+ const z = Math.min(MAX_PCT / 100, Math.max(MIN_PCT / 100, Math.min(innerWidth / b.width, innerHeight / b.height)));
2530
+ this.asMirror(() => {
2531
+ this.zoom = z;
2532
+ this.panX = innerWidth / 2 - (b.x + b.width / 2) * z;
2533
+ // Top-anchored: a contain fit's spare height would otherwise become a per-device offset
2534
+ this.panY = -b.y * z;
2535
+ this.applyTransform();
2536
+ });
2537
+ // Kept so a resize on this end refits the presenter's view rather than auto-fitting
2538
+ this._remoteBounds = b;
2539
+ }
2540
+
2541
+ focusSnapshot() {
2542
+ const r3 = (v) => Math.round(v * 1000) / 1000;
2543
+ if (csIsMobile) {
2544
+ const s = this.cardAt(this.page);
2545
+ return { key: s ? s.dataset.cardKey : null, zoom: null, sx: 0, sy: 0 };
2546
+ }
2547
+ const ov = this.overlay;
2548
+ if (!ov || !this._focusKey) return { key: null, zoom: null, sx: 0, sy: 0 };
2549
+ const frac = (pos, range) => (range > 0 ? r3(pos / range) : 0);
2550
+ return {
2551
+ key: this._focusKey,
2552
+ // Relative to the entry fit, not the absolute %: the fit follows each end's window, and
2553
+ // what has to match is how much of the card is in view
2554
+ zoom: r3(this.focusZoom / this.focusFit),
2555
+ sx: frac(ov.scrollLeft, ov.scrollWidth - ov.clientWidth),
2556
+ sy: frac(ov.scrollTop, ov.scrollHeight - ov.clientHeight),
2557
+ };
2558
+ }
2559
+
2560
+ emitFocus() {
2561
+ if (!this._mirrorOn || this._applyingMirror || this._focusT) return;
2562
+ this._focusT = setTimeout(() => {
2563
+ this._focusT = 0;
2564
+ if (!this.isConnected) return;
2565
+ const snap = this.focusSnapshot();
2566
+ // Scroll events from a replay arrive after the replay returns, so the flag cannot catch
2567
+ // them; an unchanged snapshot is that echo
2568
+ const sig = JSON.stringify(snap);
2569
+ if (sig === this._focusSig) return;
2570
+ this._focusSig = sig;
2571
+ window.__miaodaMirror.emit({ type: 'miaoda:cards:focus-changed', ...snap }, { key: 'cards:focus', throttle: 60 });
2572
+ }, 120);
2573
+ }
2574
+
2575
+ applyMirrorFocus(m) {
2576
+ if (!this._ready) return { type: 'failed', reason: 'not laid out yet' };
2577
+ if (m.key !== null && typeof m.key !== 'string') return { type: 'ignored', reason: 'bad key' };
2578
+ const section = m.key === null ? null : this.cards().find((s) => s.dataset.cardKey === m.key);
2579
+ if (m.key !== null && !section) return { type: 'ignored', reason: 'card not present' };
2580
+ clearTimeout(this._focusT);
2581
+ this._focusT = 0;
2582
+ this.asMirror(() => {
2583
+ const n = section ? Number(section.dataset.cardIndex) : null;
2584
+ if (csIsMobile) {
2585
+ if (n) this.goToPage(n);
2586
+ return;
2587
+ }
2588
+ if (!n) { this.closeFocus(); return; }
2589
+ if (this.focusIndex !== n) this.openFocus(n);
2590
+ if (Number.isFinite(m.zoom) && m.zoom > 0) this.setFocusZoom(this.focusFit * m.zoom);
2591
+ const ov = this.overlay;
2592
+ if (Number.isFinite(m.sx)) ov.scrollLeft = m.sx * (ov.scrollWidth - ov.clientWidth);
2593
+ if (Number.isFinite(m.sy)) ov.scrollTop = m.sy * (ov.scrollHeight - ov.clientHeight);
2594
+ });
2595
+ this._focusSig = JSON.stringify(this.focusSnapshot());
2596
+ return { type: 'applied' };
2597
+ }
2598
+
2599
+ emitStructure() {
2600
+ if (!this._mirrorOn || this._applyingMirror) return;
2601
+ window.__miaodaMirror.emit(
2602
+ { type: 'miaoda:cards:structure-changed', srcKey: this.srcKey, hidden: this._hiddenKeys.slice(), labels: { ...this._labelOverrides } },
2603
+ { key: 'cards:structure' },
2604
+ );
2605
+ }
2606
+
2607
+ applyMirrorStructure(m) {
2608
+ if (!this._ready) return { type: 'failed', reason: 'not laid out yet' };
2609
+ if (typeof m.srcKey !== 'string' || !Array.isArray(m.hidden) || !m.labels || typeof m.labels !== 'object') {
2610
+ return { type: 'ignored', reason: 'malformed structure' };
2611
+ }
2612
+ // Built from another version of the source (an agent edit one end has not reloaded): card
2613
+ // keys no longer line up, same rule the sidecar follows
2614
+ if (m.srcKey !== this.srcKey) return { type: 'ignored', reason: 'source mismatch' };
2615
+ // Replayed state is the presenter's, never persisted through this end's write bridge — but
2616
+ // it is folded into this end's state, or this end's next emit (say, a rename) would carry
2617
+ // the pre-replay hidden list and overwrite the latest snapshot late joiners receive.
2618
+ // Hidden is a union: a dropped section cannot come back.
2619
+ for (const key of m.hidden) {
2620
+ if (typeof key === 'string' && !this._hiddenKeys.includes(key)) this._hiddenKeys.push(key);
2621
+ }
2622
+ for (const [key, label] of Object.entries(m.labels)) {
2623
+ if (typeof label === 'string') this._labelOverrides[key] = label;
2624
+ }
2625
+ this.asMirror(() => {
2626
+ for (const section of this.cards()) {
2627
+ const key = section.dataset.cardKey;
2628
+ if (this._hiddenKeys.includes(key)) { this.dropCard(section); continue; }
2629
+ const label = m.labels[key];
2630
+ if (typeof label !== 'string' || label === section.dataset.label) continue;
2631
+ section.dataset.label = label;
2632
+ const head = this.headFor && this.headFor.get(section);
2633
+ if (head) head.querySelector('.cs-label').textContent = label;
2634
+ }
2635
+ });
2636
+ return { type: 'applied' };
2344
2637
  }
2345
2638
 
2346
2639
  }
@@ -139,9 +139,9 @@
139
139
  // it). Inlined per script rather than shipped as its own registry file:
140
140
  // call sites use the global bare, so inlining is what guarantees
141
141
  // definition-before-use — a separate copy-in file would add a whole "SDK
142
- // copied but <script> tag missing" failure mode. Keep this block identical
143
- // across deck-stage.js / design-canvas.jsx / animations.jsx — same
144
- // convention as the mobile-UA regex. Two halves of
142
+ // copied but <script> tag missing" failure mode. Keep every inlined copy
143
+ // identical (find them by this block's heading) — same convention as the
144
+ // mobile-UA regex. Two halves of
145
145
  // the 妙享 (Magic Share) mirror protocol:
146
146
  // emit(msg, opts) — stamps msg.__mirror so the host relay treats the
147
147
  // message as a broadcastable UI-state snapshot;
@@ -2196,7 +2196,8 @@
2196
2196
  this.registry = {};
2197
2197
  this.sectionMeta = {};
2198
2198
  this.sectionOrder = [];
2199
- for (const sec of this.querySelectorAll(':scope > dc-section')) {
2199
+ for (const sec of this.querySelectorAll('dc-section')) {
2200
+ if (sec.closest('design-canvas') !== this) continue;
2200
2201
  const sid = sec.sectionId;
2201
2202
  if (!sid) continue;
2202
2203
  this.sectionOrder.push(sid);
@@ -2223,8 +2224,7 @@
2223
2224
  // This is a LOCAL correction, not a user action, so it must not broadcast: in a
2224
2225
  // Magic Share session one follower whose content lacks the artboard would
2225
2226
  // otherwise push focus:null back and kick the presenter out of the overlay.
2226
- // The React version has no equivalent broadcast — it simply declines to render
2227
- // the overlay for an unknown id and leaves its own state alone.
2227
+ // Keep this correction local in both the React and custom-element versions.
2228
2228
  if (this.state.focus && !this.registry[this.state.focus]) {
2229
2229
  this._skipNextFocusEmit = true;
2230
2230
  this.setFocus(null);