@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/README.md CHANGED
@@ -32,12 +32,15 @@ registry.register(QuizContentType);
32
32
  onUpload={async (file) => URL.createObjectURL(file)} // your upload story
33
33
  onSave={(doc) => save(doc)} // the host owns persistence
34
34
  onError={(e) => showError(e.message)}
35
+ onDirtyChange={(dirty) => setHasUnsavedChanges(dirty)} // optional
35
36
  />
36
37
  </div>
37
38
  ```
38
39
 
39
40
  `onSave` receives the serialized document (JSON) ready for the player (`@interlacejs/player`). The editor never persists on its own — bring your own storage.
40
41
 
42
+ `onDirtyChange` fires when the editor gains or loses unsaved changes, relative to the loaded `document` (or an empty one) and the last `onSave`. Use it to warn before navigating away. Duration corrections the editor makes from the video's metadata do not count as changes.
43
+
41
44
  ## Strings and theming
42
45
 
43
46
  Both `InterlaceEditor` and sub-components accept `strings`/`theme` overrides; the built-in default strings derive from `@interlacejs/core`'s `DEFAULT_STRINGS` so hosts can localize once.
package/dist/index.cjs CHANGED
@@ -2463,6 +2463,7 @@ function InterlaceEditor({
2463
2463
  document,
2464
2464
  onUpload,
2465
2465
  onSave,
2466
+ onDirtyChange,
2466
2467
  strings: stringsOverride,
2467
2468
  theme,
2468
2469
  onError
@@ -2482,6 +2483,7 @@ function InterlaceEditor({
2482
2483
  );
2483
2484
  const [selectedItemId, setSelectedItemId] = (0, import_react10.useState)(null);
2484
2485
  const [pendingHook, setPendingHook] = (0, import_react10.useState)(null);
2486
+ const [loadVersion, setLoadVersion] = (0, import_react10.useState)(0);
2485
2487
  const [replaceMode, setReplaceMode] = (0, import_react10.useState)(false);
2486
2488
  const [previewOpen, setPreviewOpen] = (0, import_react10.useState)(false);
2487
2489
  const [currentTime, setCurrentTime] = (0, import_react10.useState)(0);
@@ -2506,6 +2508,7 @@ function InterlaceEditor({
2506
2508
  if (loadedInitialDocRef.current === document) return;
2507
2509
  loadedInitialDocRef.current = document;
2508
2510
  setPendingHook(null);
2511
+ setLoadVersion((v) => v + 1);
2509
2512
  const { validationErrors, warnings } = loadDocument(
2510
2513
  document,
2511
2514
  registryRef.current
@@ -2603,6 +2606,28 @@ function InterlaceEditor({
2603
2606
  }))
2604
2607
  };
2605
2608
  }, [state.videoSrc, state.videoDuration, state.items]);
2609
+ const authoredSnapshot = (0, import_react10.useMemo)(
2610
+ () => JSON.stringify({
2611
+ src: previewDocument.video.src,
2612
+ items: previewDocument.items
2613
+ }),
2614
+ [previewDocument]
2615
+ );
2616
+ const baselineRef = (0, import_react10.useRef)(
2617
+ null
2618
+ );
2619
+ if (baselineRef.current?.version !== loadVersion) {
2620
+ baselineRef.current = { version: loadVersion, snapshot: authoredSnapshot };
2621
+ }
2622
+ const [saved, setSaved] = (0, import_react10.useState)(null);
2623
+ const baselineSnapshot = saved?.version === loadVersion ? saved.snapshot : baselineRef.current.snapshot;
2624
+ const isDirty = authoredSnapshot !== baselineSnapshot || pendingHook != null;
2625
+ const lastReportedDirtyRef = (0, import_react10.useRef)(null);
2626
+ (0, import_react10.useEffect)(() => {
2627
+ if (lastReportedDirtyRef.current === isDirty) return;
2628
+ lastReportedDirtyRef.current = isDirty;
2629
+ onDirtyChange?.(isDirty);
2630
+ }, [isDirty, onDirtyChange]);
2606
2631
  const handleSelectEntry = (0, import_react10.useCallback)(
2607
2632
  (id) => {
2608
2633
  setSelectedItemId(id);
@@ -2901,7 +2926,8 @@ function InterlaceEditor({
2901
2926
  );
2902
2927
  const handleSave = (0, import_react10.useCallback)(() => {
2903
2928
  onSave(serialize());
2904
- }, [onSave, serialize]);
2929
+ setSaved({ version: loadVersion, snapshot: authoredSnapshot });
2930
+ }, [onSave, serialize, authoredSnapshot, loadVersion]);
2905
2931
  const rootStyle = {
2906
2932
  backgroundColor: theme?.backgroundColor ?? COLORS.surface,
2907
2933
  color: theme?.textColor ?? COLORS.text,