@inkandswitch/patchwork-pkg-base 0.0.49 → 0.0.50

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.
@@ -3511,31 +3511,57 @@ function useAutomergeStore({
3511
3511
  const [storeWithStatus, setStoreWithStatus] = (0, import_react3.useState)({
3512
3512
  status: "loading"
3513
3513
  });
3514
+ const readOnlyRef = (0, import_react3.useRef)(readOnly);
3515
+ readOnlyRef.current = readOnly;
3514
3516
  (0, import_react3.useEffect)(() => {
3515
3517
  const unsubs = [];
3516
3518
  let preventPatchApplications = false;
3517
3519
  function syncStoreChangesToAutomergeDoc({
3518
3520
  changes
3519
3521
  }) {
3522
+ if (readOnlyRef.current) return;
3520
3523
  preventPatchApplications = true;
3521
3524
  handle.change((doc2) => {
3522
3525
  applyTLStoreChangesToAutomerge(doc2, changes);
3523
3526
  });
3524
3527
  preventPatchApplications = false;
3525
3528
  }
3526
- if (!readOnly) {
3527
- unsubs.push(
3528
- store.listen(syncStoreChangesToAutomergeDoc, {
3529
- source: "user",
3530
- scope: "document"
3531
- })
3532
- );
3533
- }
3529
+ unsubs.push(
3530
+ store.listen(syncStoreChangesToAutomergeDoc, {
3531
+ source: "user",
3532
+ scope: "document"
3533
+ })
3534
+ );
3534
3535
  const syncAutomergeDocChangesToStore = ({
3535
3536
  patches,
3537
+ scopeReplaced,
3536
3538
  doc: doc2
3537
3539
  }) => {
3538
3540
  if (preventPatchApplications) return;
3541
+ if (scopeReplaced) {
3542
+ const swapped = handle.doc();
3543
+ if (!swapped?.store) return;
3544
+ const migrated = store.schema.migrateStoreSnapshot({
3545
+ store: JSON.parse(JSON.stringify(swapped.store)),
3546
+ schema: JSON.parse(JSON.stringify(swapped.schema))
3547
+ });
3548
+ if (migrated.type === "error") {
3549
+ console.error(
3550
+ "[tldraw5] failed to migrate swapped-in snapshot:",
3551
+ migrated.reason
3552
+ );
3553
+ return;
3554
+ }
3555
+ const next = migrated.value;
3556
+ store.mergeRemoteChanges(() => {
3557
+ const toRemove = store.allRecords().filter(
3558
+ (record) => store.scopedTypes.document.has(record.typeName) && !(record.id in next)
3559
+ ).map((record) => record.id);
3560
+ if (toRemove.length) store.remove(toRemove);
3561
+ store.put(Object.values(next));
3562
+ });
3563
+ return;
3564
+ }
3539
3565
  applyAutomergePatchesToTLStore(patches, store, doc2 ?? handle.doc());
3540
3566
  };
3541
3567
  handle.on("change", syncAutomergeDocChangesToStore);
@@ -3558,9 +3584,32 @@ function useAutomergeStore({
3558
3584
  unsubs.forEach((fn) => fn());
3559
3585
  unsubs.length = 0;
3560
3586
  };
3561
- }, [handle, store, readOnly]);
3587
+ }, [handle, store]);
3562
3588
  return storeWithStatus;
3563
3589
  }
3590
+ function useIsHandleReadOnly(handle) {
3591
+ const [readOnly, setReadOnly] = (0, import_react3.useState)(() => handle.isReadOnly());
3592
+ (0, import_react3.useEffect)(() => {
3593
+ setReadOnly(handle.isReadOnly());
3594
+ const onChange = (payload) => {
3595
+ if (payload.scopeReplaced) setReadOnly(handle.isReadOnly());
3596
+ };
3597
+ handle.on("change", onChange);
3598
+ return () => void handle.off("change", onChange);
3599
+ }, [handle]);
3600
+ return readOnly;
3601
+ }
3602
+ function useClearHistoryOnScopeSwap(handle) {
3603
+ const editor = useEditor();
3604
+ (0, import_react3.useEffect)(() => {
3605
+ if (!editor) return;
3606
+ const onChange = (payload) => {
3607
+ if (payload.scopeReplaced) editor.clearHistory();
3608
+ };
3609
+ handle.on("change", onChange);
3610
+ return () => void handle.off("change", onChange);
3611
+ }, [editor, handle]);
3612
+ }
3564
3613
  function useAutomergePresence({
3565
3614
  handle,
3566
3615
  store,
@@ -5036,7 +5085,7 @@ function TldrawTool({
5036
5085
  element
5037
5086
  }) {
5038
5087
  const handle = useDocHandle(docUrl, { suspense: true });
5039
- const readOnly = handle.isReadOnly();
5088
+ const readOnly = useIsHandleReadOnly(handle);
5040
5089
  const [doc] = useDocument(docUrl, { suspense: true });
5041
5090
  if (doc && getDocType(doc) === TLDRAW4_TYPE) {
5042
5091
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(MigratePrompt, { handle, readOnly });
@@ -5138,7 +5187,7 @@ function TldrawConfiguredCanvas({
5138
5187
  config
5139
5188
  }) {
5140
5189
  const handle = useDocHandle(docUrl, { suspense: true });
5141
- const readOnly = handle.isReadOnly();
5190
+ const readOnly = useIsHandleReadOnly(handle);
5142
5191
  const contactInfo = useContactInfo();
5143
5192
  const colorScheme = useColorScheme(element);
5144
5193
  const store = useAutomergeStore({
@@ -5258,7 +5307,9 @@ function TldrawInner(props) {
5258
5307
  const key = (0, import_react15.useMemo)(() => `${props.docUrl}-camera`, [props.docUrl]);
5259
5308
  const editor = useEditor();
5260
5309
  const repo = useRepo();
5310
+ const handle = useDocHandle(props.docUrl, { suspense: true });
5261
5311
  usePatchworkDrop(props.element);
5312
+ useClearHistoryOnScopeSwap(handle);
5262
5313
  useDocumentScript(props.docUrl, editor, repo);
5263
5314
  const onChange = (0, import_react15.useCallback)(() => {
5264
5315
  if (!editor) return;