@interlacejs/editor 1.0.2 → 1.1.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.
package/dist/index.d.cts CHANGED
@@ -216,6 +216,14 @@ interface InterlaceEditorProps {
216
216
  * owns persistence — the editor never persists on its own.
217
217
  */
218
218
  onSave: (doc: SerializedInteractiveMediaDocument) => void;
219
+ /**
220
+ * Called when the editor gains or loses unsaved changes, relative to the
221
+ * loaded `document` (or an empty document) and the last `onSave`. Covers
222
+ * the video source, items, hooks, placements, content data, and an
223
+ * unmaterialized pending hook. The video duration is excluded: the
224
+ * editor corrects it from the video's metadata without user input.
225
+ */
226
+ onDirtyChange?: (dirty: boolean) => void;
219
227
  /** Optional string overrides. */
220
228
  strings?: Partial<EditorStrings>;
221
229
  /** Optional theme overrides. */
@@ -259,7 +267,7 @@ interface InterlaceEditorProps {
259
267
  * - `adapterType` is a host-level prop (default `"native"`); it is not
260
268
  * written into the serialized document.
261
269
  */
262
- declare function InterlaceEditor({ contentTypeRegistry, document, onUpload, onSave, strings: stringsOverride, theme, onError, }: InterlaceEditorProps): React.JSX.Element;
270
+ declare function InterlaceEditor({ contentTypeRegistry, document, onUpload, onSave, onDirtyChange, strings: stringsOverride, theme, onError, }: InterlaceEditorProps): React.JSX.Element;
263
271
 
264
272
  interface ContentTypeEditorProps<TData = unknown> {
265
273
  /** The content type to mount, or `null` while the hook has no content type yet. */
package/dist/index.d.ts CHANGED
@@ -216,6 +216,14 @@ interface InterlaceEditorProps {
216
216
  * owns persistence — the editor never persists on its own.
217
217
  */
218
218
  onSave: (doc: SerializedInteractiveMediaDocument) => void;
219
+ /**
220
+ * Called when the editor gains or loses unsaved changes, relative to the
221
+ * loaded `document` (or an empty document) and the last `onSave`. Covers
222
+ * the video source, items, hooks, placements, content data, and an
223
+ * unmaterialized pending hook. The video duration is excluded: the
224
+ * editor corrects it from the video's metadata without user input.
225
+ */
226
+ onDirtyChange?: (dirty: boolean) => void;
219
227
  /** Optional string overrides. */
220
228
  strings?: Partial<EditorStrings>;
221
229
  /** Optional theme overrides. */
@@ -259,7 +267,7 @@ interface InterlaceEditorProps {
259
267
  * - `adapterType` is a host-level prop (default `"native"`); it is not
260
268
  * written into the serialized document.
261
269
  */
262
- declare function InterlaceEditor({ contentTypeRegistry, document, onUpload, onSave, strings: stringsOverride, theme, onError, }: InterlaceEditorProps): React.JSX.Element;
270
+ declare function InterlaceEditor({ contentTypeRegistry, document, onUpload, onSave, onDirtyChange, strings: stringsOverride, theme, onError, }: InterlaceEditorProps): React.JSX.Element;
263
271
 
264
272
  interface ContentTypeEditorProps<TData = unknown> {
265
273
  /** The content type to mount, or `null` while the hook has no content type yet. */
package/dist/index.js CHANGED
@@ -2439,6 +2439,7 @@ function InterlaceEditor({
2439
2439
  document,
2440
2440
  onUpload,
2441
2441
  onSave,
2442
+ onDirtyChange,
2442
2443
  strings: stringsOverride,
2443
2444
  theme,
2444
2445
  onError
@@ -2458,6 +2459,7 @@ function InterlaceEditor({
2458
2459
  );
2459
2460
  const [selectedItemId, setSelectedItemId] = useState6(null);
2460
2461
  const [pendingHook, setPendingHook] = useState6(null);
2462
+ const [loadVersion, setLoadVersion] = useState6(0);
2461
2463
  const [replaceMode, setReplaceMode] = useState6(false);
2462
2464
  const [previewOpen, setPreviewOpen] = useState6(false);
2463
2465
  const [currentTime, setCurrentTime] = useState6(0);
@@ -2482,6 +2484,7 @@ function InterlaceEditor({
2482
2484
  if (loadedInitialDocRef.current === document) return;
2483
2485
  loadedInitialDocRef.current = document;
2484
2486
  setPendingHook(null);
2487
+ setLoadVersion((v) => v + 1);
2485
2488
  const { validationErrors, warnings } = loadDocument(
2486
2489
  document,
2487
2490
  registryRef.current
@@ -2579,6 +2582,28 @@ function InterlaceEditor({
2579
2582
  }))
2580
2583
  };
2581
2584
  }, [state.videoSrc, state.videoDuration, state.items]);
2585
+ const authoredSnapshot = useMemo2(
2586
+ () => JSON.stringify({
2587
+ src: previewDocument.video.src,
2588
+ items: previewDocument.items
2589
+ }),
2590
+ [previewDocument]
2591
+ );
2592
+ const baselineRef = useRef8(
2593
+ null
2594
+ );
2595
+ if (baselineRef.current?.version !== loadVersion) {
2596
+ baselineRef.current = { version: loadVersion, snapshot: authoredSnapshot };
2597
+ }
2598
+ const [saved, setSaved] = useState6(null);
2599
+ const baselineSnapshot = saved?.version === loadVersion ? saved.snapshot : baselineRef.current.snapshot;
2600
+ const isDirty = authoredSnapshot !== baselineSnapshot || pendingHook != null;
2601
+ const lastReportedDirtyRef = useRef8(null);
2602
+ useEffect6(() => {
2603
+ if (lastReportedDirtyRef.current === isDirty) return;
2604
+ lastReportedDirtyRef.current = isDirty;
2605
+ onDirtyChange?.(isDirty);
2606
+ }, [isDirty, onDirtyChange]);
2582
2607
  const handleSelectEntry = useCallback7(
2583
2608
  (id) => {
2584
2609
  setSelectedItemId(id);
@@ -2877,7 +2902,8 @@ function InterlaceEditor({
2877
2902
  );
2878
2903
  const handleSave = useCallback7(() => {
2879
2904
  onSave(serialize());
2880
- }, [onSave, serialize]);
2905
+ setSaved({ version: loadVersion, snapshot: authoredSnapshot });
2906
+ }, [onSave, serialize, authoredSnapshot, loadVersion]);
2881
2907
  const rootStyle = {
2882
2908
  backgroundColor: theme?.backgroundColor ?? COLORS.surface,
2883
2909
  color: theme?.textColor ?? COLORS.text,