@open-slide/core 2.3.2 → 2.4.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.
Files changed (85) hide show
  1. package/dist/{build-8lRsgsO4.js → build-D7GwYl8d.js} +1 -1
  2. package/dist/cli/bin.js +4 -4
  3. package/dist/{config-DJ8ZOe7C.d.ts → config-CfhtJxQH.d.ts} +1 -1
  4. package/dist/{config-D2Mq1vSW.js → config-D4uPk_tM.js} +383 -138
  5. package/dist/{dev-BvefEp1Q.js → dev-DyCDMbHB.js} +10 -10
  6. package/dist/index.d.ts +2 -2
  7. package/dist/index.js +9 -1
  8. package/dist/locale/index.d.ts +1 -1
  9. package/dist/locale/index.js +1 -1
  10. package/dist/{preview-D8ognobz.js → preview-DjIFvt0K.js} +1 -1
  11. package/dist/{types-tdaXSyah.d.ts → types-ZfArJF0d.d.ts} +20 -13
  12. package/dist/{update-BivdNgL3.js → update-BKhRM7o4.js} +30 -3
  13. package/dist/vite/index.d.ts +1 -1
  14. package/dist/vite/index.js +1 -1
  15. package/dist/{zh-tw-DXcqQTrA.js → zh-tw-D7LfGVmA.js} +215 -65
  16. package/package.json +4 -5
  17. package/src/agent/provider.ts +8 -1
  18. package/src/agent/types.ts +17 -2
  19. package/src/app/app.tsx +2 -0
  20. package/src/app/components/agent/agent-markdown.tsx +3 -6
  21. package/src/app/components/agent/agent-panel.tsx +36 -258
  22. package/src/app/components/agent/agent-presence.tsx +1 -1
  23. package/src/app/components/agent/composer.tsx +52 -24
  24. package/src/app/components/agent/effort-slider.tsx +172 -0
  25. package/src/app/components/agent/model-menu.tsx +170 -0
  26. package/src/app/components/agent/permission-menu.tsx +88 -0
  27. package/src/app/components/agent/transcript.tsx +23 -31
  28. package/src/app/components/command/home-command-menu.tsx +4 -1
  29. package/src/app/components/command/slide-command-menu.tsx +4 -1
  30. package/src/app/components/export-progress-toast.tsx +3 -1
  31. package/src/app/components/inspector/asset-picker-dialog.tsx +1 -1
  32. package/src/app/components/inspector/comment-layer.tsx +3 -3
  33. package/src/app/components/inspector/inline-text-editor.tsx +1 -12
  34. package/src/app/components/inspector/inspect-overlay.tsx +9 -1
  35. package/src/app/components/inspector/inspector-panel.tsx +8 -35
  36. package/src/app/components/inspector/inspector-provider.tsx +177 -36
  37. package/src/app/components/inspector/page-panel.tsx +270 -0
  38. package/src/app/components/language-toggle.tsx +13 -11
  39. package/src/app/components/note-markdown.test.tsx +12 -6
  40. package/src/app/components/note-markdown.tsx +16 -21
  41. package/src/app/components/overview-grid.tsx +3 -3
  42. package/src/app/components/panel/editor-sidebar.tsx +43 -8
  43. package/src/app/components/panel/panel-shell.tsx +9 -5
  44. package/src/app/components/panel/save-card.tsx +1 -1
  45. package/src/app/components/sidebar/folder-item.tsx +4 -1
  46. package/src/app/components/sidebar/sidebar.tsx +11 -8
  47. package/src/app/components/style-panel/design-provider.tsx +48 -11
  48. package/src/app/components/style-panel/use-design.ts +6 -1
  49. package/src/app/components/theme-toggle.tsx +16 -22
  50. package/src/app/components/thumbnail-rail.tsx +68 -49
  51. package/src/app/components/ui/context-menu.tsx +9 -10
  52. package/src/app/components/ui/dialog.tsx +2 -4
  53. package/src/app/components/ui/dropdown-menu.tsx +9 -10
  54. package/src/app/components/ui/popover.tsx +1 -1
  55. package/src/app/components/ui/select.tsx +2 -2
  56. package/src/app/components/ui/sonner.tsx +2 -2
  57. package/src/app/index.html +7 -0
  58. package/src/app/lib/agent/use-agent.ts +131 -37
  59. package/src/app/lib/desktop-host.ts +31 -0
  60. package/src/app/lib/desktop-routes.test.ts +54 -0
  61. package/src/app/lib/desktop-routes.ts +22 -0
  62. package/src/app/lib/desktop.ts +106 -0
  63. package/src/app/lib/export-pdf.ts +15 -7
  64. package/src/app/lib/inspector/color.ts +51 -0
  65. package/src/app/lib/inspector/fiber.ts +6 -0
  66. package/src/app/lib/inspector/use-editor.ts +2 -1
  67. package/src/app/lib/inspector/use-notes.test.ts +209 -0
  68. package/src/app/lib/inspector/use-notes.ts +331 -57
  69. package/src/app/lib/inspector/use-visual-editor.ts +9 -1
  70. package/src/app/lib/locale-store.ts +14 -2
  71. package/src/app/lib/note-markdown.test.ts +69 -5
  72. package/src/app/lib/note-markdown.ts +97 -22
  73. package/src/app/lib/transition.test.ts +84 -1
  74. package/src/app/lib/transition.ts +43 -0
  75. package/src/app/lib/unsaved.test.ts +253 -0
  76. package/src/app/lib/unsaved.ts +155 -0
  77. package/src/app/routes/home.tsx +1 -1
  78. package/src/app/routes/slide.tsx +114 -117
  79. package/src/app/styles.css +421 -1
  80. package/src/locale/en.ts +40 -15
  81. package/src/locale/es.ts +40 -17
  82. package/src/locale/ja.ts +40 -15
  83. package/src/locale/types.ts +19 -14
  84. package/src/locale/zh-cn.ts +40 -14
  85. package/src/locale/zh-tw.ts +40 -14
@@ -1,5 +1,6 @@
1
1
  import { Crop, ImageIcon } from 'lucide-react';
2
2
  import { useEffect, useLayoutEffect, useRef, useState } from 'react';
3
+ import { haptic } from '@/lib/desktop';
3
4
  import { findSlideSource } from '@/lib/inspector/fiber';
4
5
  import { useFreezeSlideMotion } from '@/lib/inspector/freeze-motion';
5
6
  import {
@@ -49,11 +50,14 @@ type Gesture = {
49
50
  candidates: Rect[];
50
51
  moved: boolean;
51
52
  edits: VisualEdit[];
53
+ guideKeys: Set<string>;
52
54
  previousSelection: SelectedTarget[];
53
55
  clickSelection: SelectedTarget[];
54
56
  additive: boolean;
55
57
  };
56
58
 
59
+ const guideKey = (guide: Guide) => `${guide.axis}:${guide.position}`;
60
+
57
61
  type ScreenRect = { left: number; top: number; width: number; height: number };
58
62
  const HANDLES: { handle: ResizeHandle; x: number; y: number; cursor: string }[] = [
59
63
  { handle: 'nw', x: 0, y: 0, cursor: 'nwse-resize' },
@@ -262,6 +266,7 @@ export function InspectOverlay() {
262
266
  candidates,
263
267
  moved: false,
264
268
  edits: [],
269
+ guideKeys: new Set(),
265
270
  previousSelection: selection,
266
271
  clickSelection,
267
272
  additive: event.shiftKey,
@@ -363,6 +368,9 @@ export function InspectOverlay() {
363
368
  ];
364
369
  }
365
370
  for (const edit of gesture.edits) previewOps(edit.anchor, edit.ops);
371
+ const guideKeys = new Set(nextGuides.map(guideKey));
372
+ if ([...guideKeys].some((key) => !gesture.guideKeys.has(key))) haptic('alignment');
373
+ gesture.guideKeys = guideKeys;
366
374
  setGuides(nextGuides);
367
375
  measureRef.current?.();
368
376
  };
@@ -590,7 +598,7 @@ export function InspectOverlay() {
590
598
  {canvasFrame &&
591
599
  guides.map((guide) => (
592
600
  <div
593
- key={`${guide.axis}:${guide.position}`}
601
+ key={guideKey(guide)}
594
602
  data-alignment-guide={guide.axis}
595
603
  className="absolute bg-cyan-500"
596
604
  style={
@@ -8,7 +8,6 @@ import {
8
8
  Crop,
9
9
  ImageIcon,
10
10
  Italic,
11
- MousePointer2,
12
11
  Move,
13
12
  MoveHorizontal,
14
13
  Paintbrush,
@@ -40,6 +39,7 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
40
39
  import { Textarea } from '@/components/ui/textarea';
41
40
  import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group';
42
41
  import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';
42
+ import { opaqueHex, rgbToHex } from '@/lib/inspector/color';
43
43
  import { findSlideSource } from '@/lib/inspector/fiber';
44
44
  import { hasOnlyInlineTextChildren } from '@/lib/inspector/inline-text';
45
45
  import { styleContext } from '@/lib/inspector/text-selection';
@@ -51,6 +51,7 @@ import type { Locale } from '../../../locale/types';
51
51
  import { ArrangePanel } from './arrange-panel';
52
52
  import { AssetPickerDialog } from './asset-picker-dialog';
53
53
  import { type SelectedTarget, useInspector } from './inspector-provider';
54
+ import { type PageInfo, PagePanel } from './page-panel';
54
55
 
55
56
  type ElementSnapshot = {
56
57
  fontSize: number;
@@ -89,9 +90,11 @@ function resolveSelectedTarget(target: SelectedTarget, slideId: string): Selecte
89
90
  export function InspectorPanel({
90
91
  preferredTab,
91
92
  onTabChange,
93
+ page,
92
94
  }: {
93
95
  preferredTab: 'format' | 'arrange';
94
96
  onTabChange: (tab: 'format' | 'arrange') => void;
97
+ page?: PageInfo;
95
98
  }) {
96
99
  const {
97
100
  togglePanel,
@@ -426,17 +429,9 @@ export function InspectorPanel({
426
429
  <ArrangePanel />
427
430
  </TabsContent>
428
431
  </>
429
- ) : (
430
- <div className="flex flex-col items-center gap-3 px-7 py-16 text-center">
431
- <MousePointer2 aria-hidden className="size-8 text-muted-foreground/50" />
432
- <div className="flex flex-col gap-1.5">
433
- <h2 className="text-[13px] font-medium">{t.inspector.emptySelectionTitle}</h2>
434
- <p className="text-[12px] leading-relaxed text-muted-foreground">
435
- {t.inspector.emptySelectionHint}
436
- </p>
437
- </div>
438
- </div>
439
- )}
432
+ ) : page ? (
433
+ <PagePanel page={page} />
434
+ ) : null}
440
435
  </PanelShell>
441
436
  </Tabs>
442
437
  );
@@ -913,7 +908,7 @@ function readSnapshot(el: HTMLElement): ElementSnapshot {
913
908
 
914
909
  return {
915
910
  ...readTypography(el),
916
- backgroundColor: isTransparent(cs.backgroundColor) ? null : rgbToHex(cs.backgroundColor),
911
+ backgroundColor: opaqueHex(cs.backgroundColor),
917
912
  textAlign: normalizeTextAlign(cs.textAlign),
918
913
  lineHeight: parseLineHeight(cs.lineHeight, parseFloat(cs.fontSize) || 16),
919
914
  letterSpacing: parseLetterSpacing(cs.letterSpacing),
@@ -968,28 +963,6 @@ function renderedTextNodeValue(node: Text): string {
968
963
  return value.replace(/\s+/g, ' ');
969
964
  }
970
965
 
971
- function rgbToHex(value: string): string | null {
972
- const m = value.match(/^rgba?\(([^)]+)\)$/);
973
- if (!m) return null;
974
- const parts = m[1].split(',').map((s) => s.trim());
975
- if (parts.length < 3) return null;
976
- const r = clampByte(Number(parts[0]));
977
- const g = clampByte(Number(parts[1]));
978
- const b = clampByte(Number(parts[2]));
979
- return `#${[r, g, b].map((n) => n.toString(16).padStart(2, '0')).join('')}`;
980
- }
981
-
982
- function clampByte(n: number): number {
983
- return Math.max(0, Math.min(255, Math.round(Number.isFinite(n) ? n : 0)));
984
- }
985
-
986
- function isTransparent(value: string): boolean {
987
- if (!value) return true;
988
- if (value === 'transparent' || value === 'rgba(0, 0, 0, 0)') return true;
989
- const m = value.match(/^rgba\([^)]*,\s*0\)$/);
990
- return Boolean(m);
991
- }
992
-
993
966
  function normalizeTextAlign(v: string): ElementSnapshot['textAlign'] {
994
967
  if (v === 'center' || v === 'right' || v === 'justify') return v;
995
968
  return 'left';
@@ -25,8 +25,10 @@ import {
25
25
  import { type AddCommentInput, type SlideComment, useComments } from '@/lib/inspector/use-comments';
26
26
  import { type Edit, type EditOp, useEditor } from '@/lib/inspector/use-editor';
27
27
  import { useVisualEditor, type VisualEdit } from '@/lib/inspector/use-visual-editor';
28
+ import { independentTargets } from '@/lib/inspector/visual-dom';
28
29
  import { isShortcutControlTarget, isTypingTarget } from '@/lib/keys';
29
30
  import { textDiff } from '@/lib/text-diff';
31
+ import { createSaveQueue, createWriteLedger, trackWrite, useUnsavedSource } from '@/lib/unsaved';
30
32
  import { format, plural, useLocale } from '@/lib/use-locale';
31
33
  import { round2 } from '@/lib/utils';
32
34
  import { AssetPickerDialog } from './asset-picker-dialog';
@@ -40,6 +42,7 @@ export type SelectedTarget = {
40
42
  };
41
43
 
42
44
  const INSTANCE_ID_ATTR = 'data-slide-instance-id';
45
+ const REMOVED_ATTR = 'data-osd-removed';
43
46
 
44
47
  function elementPath(root: Element, el: Element): number[] | null {
45
48
  const path: number[] = [];
@@ -126,6 +129,10 @@ export type InlineEditTarget = SelectedTarget & {
126
129
  export type InlineTextSelection = { start: number; end: number };
127
130
  type InlineStyleHandler = (ops: EditOp[]) => boolean;
128
131
 
132
+ // Shared across providers so a save retried after its provider unmounted skips
133
+ // properties that a newer provider has saved since.
134
+ const editLedger = createWriteLedger();
135
+
129
136
  type AssetAttrOp = { assetPath: string; previewUrl: string };
130
137
  type Sequenced<T> = T & { seq: number };
131
138
  type StyleOp = { value: string | null; prevText?: string };
@@ -147,6 +154,7 @@ type Bucket = {
147
154
  origTexts: Map<string /* instanceId */, { value: string }>;
148
155
  origHtmls: Map<string /* instanceId */, TextDomSnapshot>;
149
156
  origAttrs: Map<string, string | null>;
157
+ removed: boolean;
150
158
  };
151
159
 
152
160
  function createBucket(line: number, column: number): Bucket {
@@ -161,9 +169,26 @@ function createBucket(line: number, column: number): Bucket {
161
169
  origTexts: new Map(),
162
170
  origHtmls: new Map(),
163
171
  origAttrs: new Map(),
172
+ removed: false,
164
173
  };
165
174
  }
166
175
 
176
+ function bucketIsEmpty(bucket: Bucket): boolean {
177
+ return (
178
+ bucket.styleOps.size === 0 &&
179
+ bucket.textEdits.size === 0 &&
180
+ bucket.attrOps.size === 0 &&
181
+ !bucket.removed
182
+ );
183
+ }
184
+
185
+ function hasBufferedOps(buckets: Map<string, Bucket>): boolean {
186
+ for (const b of buckets.values()) {
187
+ if (!bucketIsEmpty(b)) return true;
188
+ }
189
+ return false;
190
+ }
191
+
167
192
  export type DomTextPart = {
168
193
  node: Text | HTMLBRElement;
169
194
  current: string;
@@ -341,7 +366,21 @@ function captureTextDom(
341
366
  el: HTMLElement,
342
367
  whiteSpace: WhiteSpaceResolver = computedWhiteSpace,
343
368
  ): TextDomSnapshot {
344
- return { html: el.innerHTML, whiteSpaces: textSnapshotElements(el).map(whiteSpace) };
369
+ return {
370
+ html: htmlWithoutRemovalMarkers(el),
371
+ whiteSpaces: textSnapshotElements(el).map(whiteSpace),
372
+ };
373
+ }
374
+
375
+ // Removal markers are pending preview state; a snapshot that kept them would
376
+ // re-hide a child on cancel, undo, or replay after its removal was dropped.
377
+ function htmlWithoutRemovalMarkers(el: HTMLElement): string {
378
+ if (!el.querySelector(`[${REMOVED_ATTR}]`)) return el.innerHTML;
379
+ const clone = el.cloneNode(true) as HTMLElement;
380
+ for (const marked of clone.querySelectorAll(`[${REMOVED_ATTR}]`)) {
381
+ marked.removeAttribute(REMOVED_ATTR);
382
+ }
383
+ return clone.innerHTML;
345
384
  }
346
385
 
347
386
  function textEditHtml(snapshot: TextDomSnapshot, steps: TextEditStep[]): TextDomSnapshot {
@@ -503,7 +542,8 @@ export function InspectorProvider({
503
542
  new WeakMap<HTMLElement, { text: string; html: TextDomSnapshot }>(),
504
543
  );
505
544
  const instanceCounterRef = useRef(0);
506
- const pendingSeqRef = useRef(0);
545
+ const [commitQueue] = useState(createSaveQueue);
546
+ const commitFailureRef = useRef<string | null>(null);
507
547
  const [pendingCount, setPendingCount] = useState(0);
508
548
  const [committing, setCommitting] = useState(false);
509
549
  const [cropTarget, setCropTarget] = useState<{
@@ -535,9 +575,7 @@ export function InspectorProvider({
535
575
  const refreshCount = useCallback(() => {
536
576
  let n = 0;
537
577
  for (const b of pendingRef.current.values()) {
538
- if (b.styleOps.size > 0 || b.textEdits.size > 0 || b.attrOps.size > 0) {
539
- n++;
540
- }
578
+ if (!bucketIsEmpty(b)) n++;
541
579
  }
542
580
  setPendingCount(n);
543
581
  setOpsVersion((v) => v + 1);
@@ -623,12 +661,16 @@ export function InspectorProvider({
623
661
  pendingRef.current.set(key, bucket);
624
662
  }
625
663
  const style = (anchor?.style ?? {}) as unknown as Record<string, string>;
664
+ // Every key is read before any is written: clearing a shorthand such as
665
+ // `background` also clears the longhands a later op in the batch sets.
666
+ for (const op of ops) {
667
+ if (op.kind === 'set-style' && anchor && !bucket.origStyle.has(op.key)) {
668
+ bucket.origStyle.set(op.key, style[op.key] ?? '');
669
+ }
670
+ }
626
671
  for (const op of ops) {
627
- const seq = ++pendingSeqRef.current;
672
+ const seq = editLedger.next();
628
673
  if (op.kind === 'set-style') {
629
- if (anchor && !bucket.origStyle.has(op.key)) {
630
- bucket.origStyle.set(op.key, style[op.key] ?? '');
631
- }
632
674
  bucket.styleOps.set(op.key, { value: op.value, prevText: op.prevText, seq });
633
675
  if (anchor?.isConnected) style[op.key] = op.value ?? '';
634
676
  } else if (op.kind === 'set-text-range-style' || op.kind === 'set-text') {
@@ -818,7 +860,7 @@ export function InspectorProvider({
818
860
  } else if (snap.kind === 'attr') {
819
861
  if (snap.source === 'op') {
820
862
  const op = snap.value as Sequenced<AssetAttrOp>;
821
- bucket.attrOps.set(snap.attr, { ...op, seq: ++pendingSeqRef.current });
863
+ bucket.attrOps.set(snap.attr, { ...op, seq: editLedger.next() });
822
864
  if (sharedAnchor?.isConnected) sharedAnchor.setAttribute(snap.attr, op.previewUrl);
823
865
  } else {
824
866
  bucket.attrOps.delete(snap.attr);
@@ -830,9 +872,7 @@ export function InspectorProvider({
830
872
  }
831
873
  }
832
874
  }
833
- if (bucket.styleOps.size === 0 && bucket.textEdits.size === 0 && bucket.attrOps.size === 0) {
834
- pendingRef.current.delete(key);
835
- }
875
+ if (bucketIsEmpty(bucket)) pendingRef.current.delete(key);
836
876
  refreshCount();
837
877
  },
838
878
  [findAnchor, findTextAnchor, refreshCount],
@@ -944,6 +984,60 @@ export function InspectorProvider({
944
984
  ],
945
985
  );
946
986
 
987
+ // Every DOM instance of the loc hides: removal edits the shared JSX, so
988
+ // `.map()` items and reused components all disappear on save.
989
+ const setRemovedRaw = useCallback(
990
+ (line: number, column: number, removed: boolean) => {
991
+ const key = `${line}:${column}`;
992
+ let bucket = pendingRef.current.get(key);
993
+ if (!bucket) {
994
+ if (!removed) return;
995
+ bucket = createBucket(line, column);
996
+ pendingRef.current.set(key, bucket);
997
+ }
998
+ bucket.removed = removed;
999
+ const root = document.querySelector<HTMLElement>('[data-inspector-root]');
1000
+ for (const el of root?.querySelectorAll(`[data-slide-loc="${key}"]`) ?? []) {
1001
+ el.toggleAttribute(REMOVED_ATTR, removed);
1002
+ }
1003
+ if (bucketIsEmpty(bucket)) pendingRef.current.delete(key);
1004
+ refreshCount();
1005
+ },
1006
+ [refreshCount],
1007
+ );
1008
+
1009
+ const removeElements = useCallback(
1010
+ (targets: SelectedTarget[]) => {
1011
+ if (committing) return;
1012
+ const unique = new Map<string, SelectedTarget>();
1013
+ for (const target of independentTargets(targets)) {
1014
+ const hit = findSlideSource(target.anchor, slideId, { hostOnly: true }) ?? target;
1015
+ const key = `${hit.line}:${hit.column}`;
1016
+ if (!pendingRef.current.get(key)?.removed) unique.set(key, hit);
1017
+ }
1018
+ const removed = [...unique.values()];
1019
+ if (!removed.length) return;
1020
+ const reveal = removed.map(captureRevealTarget);
1021
+ const apply = (value: boolean) => {
1022
+ for (const target of removed) setRemovedRaw(target.line, target.column, value);
1023
+ };
1024
+ apply(true);
1025
+ setSelection([]);
1026
+ history.record({
1027
+ pageIndex,
1028
+ undo: () => {
1029
+ apply(false);
1030
+ revealTargets(reveal, pageIndex);
1031
+ },
1032
+ redo: () => {
1033
+ apply(true);
1034
+ setSelection([]);
1035
+ },
1036
+ });
1037
+ },
1038
+ [committing, slideId, setRemovedRaw, setSelection, history, pageIndex, revealTargets],
1039
+ );
1040
+
947
1041
  const visual = useVisualEditor({
948
1042
  active,
949
1043
  inlineEditing: !!inlineEdit,
@@ -952,25 +1046,41 @@ export function InspectorProvider({
952
1046
  selection,
953
1047
  setSelection,
954
1048
  bufferBatch,
1049
+ removeElements,
955
1050
  });
956
1051
 
957
- const commitEdits = useCallback(async () => {
1052
+ const writeEdits = useCallback(async () => {
958
1053
  const buckets = pendingRef.current;
1054
+ commitFailureRef.current = null;
959
1055
  if (buckets.size === 0) return;
960
1056
  type PendingItem = {
961
1057
  bucket: Bucket;
962
1058
  seq: number;
963
1059
  instanceId?: string;
1060
+ property?: string;
964
1061
  edit: Edit;
965
1062
  onSuccess: (bucket: Bucket) => void;
966
1063
  };
967
1064
  const pending: PendingItem[] = [];
968
1065
  for (const bucket of buckets.values()) {
969
1066
  const { line, column, styleOps, textEdits, attrOps } = bucket;
1067
+ if (bucket.removed) {
1068
+ pending.push({
1069
+ bucket,
1070
+ // Removals go last so edits inside the removed element don't fail
1071
+ // with "target was removed by an earlier edit".
1072
+ seq: Number.MAX_SAFE_INTEGER,
1073
+ edit: { line, column, ops: [{ kind: 'remove-element' }] },
1074
+ onSuccess: (b) => {
1075
+ b.removed = false;
1076
+ },
1077
+ });
1078
+ }
970
1079
  for (const [k, op] of styleOps) {
971
1080
  pending.push({
972
1081
  bucket,
973
1082
  seq: op.seq,
1083
+ property: `${slideId}\n${line}:${column}\nstyle:${k}`,
974
1084
  edit: {
975
1085
  line,
976
1086
  column,
@@ -986,6 +1096,7 @@ export function InspectorProvider({
986
1096
  pending.push({
987
1097
  bucket,
988
1098
  seq: op.seq,
1099
+ property: `${slideId}\n${line}:${column}\nattr:${attr}`,
989
1100
  edit: {
990
1101
  line,
991
1102
  column,
@@ -1031,14 +1142,27 @@ export function InspectorProvider({
1031
1142
  }
1032
1143
  }
1033
1144
  }
1034
- pending.sort((a, b) => a.seq - b.seq);
1145
+ const settle = (item: PendingItem) => {
1146
+ item.onSuccess(item.bucket);
1147
+ if (item.property) editLedger.record(item.property, item.seq);
1148
+ if (!bucketIsEmpty(item.bucket)) return;
1149
+ for (const [key, pendingBucket] of pendingRef.current) {
1150
+ if (pendingBucket === item.bucket) pendingRef.current.delete(key);
1151
+ }
1152
+ };
1153
+ const writes = pending.filter((item) => {
1154
+ if (!item.property || !editLedger.superseded(item.property, item.seq)) return true;
1155
+ settle(item);
1156
+ return false;
1157
+ });
1158
+ writes.sort((a, b) => a.seq - b.seq);
1035
1159
  const lastTextEdit = new Map<string, number>();
1036
- for (const [index, item] of pending.entries()) {
1160
+ for (const [index, item] of writes.entries()) {
1037
1161
  if (!item.instanceId) continue;
1038
1162
  item.edit.dependsOn = lastTextEdit.get(item.instanceId);
1039
1163
  lastTextEdit.set(item.instanceId, index);
1040
1164
  }
1041
- if (pending.length === 0) {
1165
+ if (writes.length === 0) {
1042
1166
  pendingRef.current = new Map();
1043
1167
  setPendingCount(0);
1044
1168
  history.clear();
@@ -1046,38 +1170,35 @@ export function InspectorProvider({
1046
1170
  }
1047
1171
  setCommitting(true);
1048
1172
  try {
1049
- const results = await applyEdits(pending.map((p) => p.edit));
1173
+ const results = await applyEdits(writes.map((p) => p.edit));
1050
1174
  const failures: string[] = [];
1051
1175
  for (let i = 0; i < results.length; i++) {
1052
- const item = pending[i];
1176
+ const item = writes[i];
1053
1177
  const r = results[i];
1054
- const bucket = item.bucket;
1055
1178
  if (r.ok) {
1056
- item.onSuccess(bucket);
1057
- if (
1058
- bucket.styleOps.size === 0 &&
1059
- bucket.textEdits.size === 0 &&
1060
- bucket.attrOps.size === 0
1061
- ) {
1062
- for (const [key, pendingBucket] of pendingRef.current) {
1063
- if (pendingBucket === bucket) pendingRef.current.delete(key);
1064
- }
1065
- }
1179
+ settle(item);
1066
1180
  } else {
1067
1181
  failures.push(`line ${item.edit.line}: ${r.error ?? 'edit failed'}`);
1068
1182
  }
1069
1183
  }
1070
1184
  refreshCount();
1071
- if (failures.length > 0) toast.error(`${t.inspector.saveFailed} ${failures.join('; ')}`);
1185
+ if (failures.length > 0) {
1186
+ commitFailureRef.current = `${t.inspector.saveFailed} ${failures.join('; ')}`;
1187
+ toast.error(commitFailureRef.current);
1188
+ }
1072
1189
  } catch (err) {
1073
1190
  const msg = err instanceof Error ? err.message : String(err);
1074
- toast.error(`${t.inspector.saveFailed} ${msg}`);
1191
+ commitFailureRef.current = `${t.inspector.saveFailed} ${msg}`;
1192
+ toast.error(commitFailureRef.current);
1075
1193
  throw err;
1076
1194
  } finally {
1077
1195
  setCommitting(false);
1078
1196
  history.clear();
1079
1197
  }
1080
- }, [applyEdits, history, refreshCount, t]);
1198
+ }, [applyEdits, history, refreshCount, slideId, t]);
1199
+
1200
+ // Two commits in flight would send the same buffered ops twice.
1201
+ const commitEdits = useCallback(() => commitQueue.run(writeEdits), [commitQueue, writeEdits]);
1081
1202
 
1082
1203
  const cancelEdits = useCallback(() => {
1083
1204
  if (pendingRef.current.size === 0) {
@@ -1107,27 +1228,46 @@ export function InspectorProvider({
1107
1228
  if (textEl?.isConnected) setEditableText(textEl, orig.value);
1108
1229
  }
1109
1230
  }
1231
+ for (const el of root?.querySelectorAll(`[${REMOVED_ATTR}]`) ?? []) {
1232
+ el.removeAttribute(REMOVED_ATTR);
1233
+ }
1110
1234
  pendingRef.current = new Map();
1111
1235
  setPendingCount(0);
1112
1236
  setOpsVersion((version) => version + 1);
1113
1237
  history.clear();
1114
1238
  }, [history]);
1115
1239
 
1240
+ // Rejects while any edit is still buffered, even when only some of them failed.
1241
+ const saveEdits = async () => {
1242
+ await commitEdits().catch(() => {});
1243
+ if (hasBufferedOps(pendingRef.current)) {
1244
+ throw new Error(commitFailureRef.current ?? t.inspector.saveFailed);
1245
+ }
1246
+ };
1247
+
1116
1248
  // Auto-flush on inspector close and on route unmount so toggling
1117
1249
  // off or navigating away doesn't drop buffered edits. Failures are
1118
1250
  // surfaced via toast inside `commitEdits`; the catch here only
1119
1251
  // swallows the rethrown rejection.
1120
1252
  const commitRef = useRef(commitEdits);
1121
1253
  commitRef.current = commitEdits;
1254
+ const saveRef = useRef(saveEdits);
1255
+ saveRef.current = saveEdits;
1122
1256
  useEffect(() => {
1123
1257
  if (!active) commitRef.current().catch(() => {});
1124
1258
  }, [active]);
1259
+ // Nothing else reports the edits once this unmounts.
1125
1260
  useEffect(() => {
1126
- return () => {
1127
- commitRef.current().catch(() => {});
1128
- };
1261
+ return () => trackWrite(pendingRef, () => saveRef.current());
1129
1262
  }, []);
1130
1263
 
1264
+ useUnsavedSource({
1265
+ kind: 'draft',
1266
+ dirty: () => commitQueue.busy() || hasBufferedOps(pendingRef.current),
1267
+ save: saveEdits,
1268
+ discard: cancelEdits,
1269
+ });
1270
+
1131
1271
  // Re-apply buffered ops onto any `[data-slide-loc]` element that gets
1132
1272
  // (re)mounted in the slide canvas. Without this, navigating to a
1133
1273
  // different page and back drops the optimistic styles, since the
@@ -1150,6 +1290,7 @@ export function InspectorProvider({
1150
1290
  for (const [attr, op] of bucket.attrOps) {
1151
1291
  if (el.getAttribute(attr) !== op.previewUrl) el.setAttribute(attr, op.previewUrl);
1152
1292
  }
1293
+ if (bucket.removed && !el.hasAttribute(REMOVED_ATTR)) el.setAttribute(REMOVED_ATTR, '');
1153
1294
  };
1154
1295
 
1155
1296
  let observer: MutationObserver | null = null;