superdoc 2.0.0-next.50 → 2.0.0-next.52

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/AGENTS.md CHANGED
@@ -334,4 +334,4 @@ These point at the currently published documentation site. The v2 documentation
334
334
  - SDK: https://docs.superdoc.dev/document-engine/sdks
335
335
  - CLI: https://docs.superdoc.dev/document-engine/cli
336
336
  - LLM tools: https://docs.superdoc.dev/document-engine/ai-agents/llm-tools
337
- - Examples: https://github.com/superdoc-dev/superdoc/tree/main/examples
337
+ - Examples: https://github.com/superdoc/docx-editor/tree/main/examples
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![Documentation](https://img.shields.io/badge/docs-available-1355ff.svg)](https://docs.superdoc.dev/)
6
6
  [![License: AGPL v3](https://img.shields.io/badge/License-AGPL%20v3-1355ff.svg)](https://www.gnu.org/licenses/agpl-3.0)
7
7
  [![npm version](https://img.shields.io/npm/v/superdoc.svg?color=1355ff)](https://www.npmjs.com/package/superdoc)
8
- [![codecov](https://codecov.io/gh/superdoc-dev/superdoc/branch/main/graph/badge.svg)](https://codecov.io/gh/superdoc-dev/superdoc)
8
+ [![codecov](https://codecov.io/gh/superdoc/docx-editor/branch/main/graph/badge.svg)](https://codecov.io/gh/superdoc/docx-editor)
9
9
  [![Discord](https://img.shields.io/badge/discord-join-1355ff)](https://discord.com/invite/b9UuaZRyaB)
10
10
 
11
11
  SuperDoc renders and edits DOCX files in the browser. Built on OOXML — not bolted onto HTML. As you type, you write directly to the XML. Import a document, edit it, export it. Nothing lost.
@@ -90,7 +90,7 @@ claude mcp add superdoc -- npx @superdoc-dev/mcp # connect agent to DOCX files
90
90
 
91
91
  ## Contributing
92
92
 
93
- Check the [issue tracker](https://github.com/superdoc-dev/superdoc/issues) for open issues, or read the [Contributing Guide](../../CONTRIBUTING.md) to get started. Bug reports with reproduction .docx files are especially valuable.
93
+ Check the [issue tracker](https://github.com/superdoc/docx-editor/issues) for open issues, or read the [Contributing Guide](../../CONTRIBUTING.md) to get started. Bug reports with reproduction .docx files are especially valuable.
94
94
 
95
95
  ## Community
96
96
 
@@ -1664,6 +1664,48 @@ function readEntityTarget(row) {
1664
1664
  const target = row.target;
1665
1665
  return target && typeof target === "object" ? target : null;
1666
1666
  }
1667
+ function isHostGeometryTarget(target) {
1668
+ if (!target || typeof target !== "object") return false;
1669
+ const record = target;
1670
+ const isTextPoint = (point) => {
1671
+ if (!point || typeof point !== "object") return false;
1672
+ const candidate = point;
1673
+ return candidate.kind === "text" && typeof candidate.blockId === "string" && typeof candidate.offset === "number";
1674
+ };
1675
+ const isTextSegment = (segment) => {
1676
+ if (!segment || typeof segment !== "object") return false;
1677
+ const candidate = segment;
1678
+ const range$1 = candidate.range;
1679
+ return typeof candidate.blockId === "string" && typeof range$1?.start === "number" && typeof range$1.end === "number";
1680
+ };
1681
+ if (Array.isArray(record.segments)) {
1682
+ const first = record.segments[0];
1683
+ const last = record.segments[record.segments.length - 1];
1684
+ return isTextSegment(first) && isTextSegment(last);
1685
+ }
1686
+ if (record.kind === "selection") return isTextPoint(record.start) && isTextPoint(record.end);
1687
+ if (record.kind !== "text" || typeof record.blockId !== "string") return false;
1688
+ const range = record.range;
1689
+ return typeof range?.start === "number" && typeof range.end === "number";
1690
+ }
1691
+ function readTrackedChangeNavigationTarget(row) {
1692
+ if (!row || typeof row !== "object") return null;
1693
+ const target = readEntityTarget(row);
1694
+ if (isHostGeometryTarget(target)) return target;
1695
+ const record = row;
1696
+ const navigationTarget = record.navigationTarget && typeof record.navigationTarget === "object" ? record.navigationTarget : null;
1697
+ if (navigationTarget?.kind === "block" && typeof navigationTarget.blockId === "string" && navigationTarget.blockId.length > 0) return {
1698
+ kind: "text",
1699
+ blockId: navigationTarget.blockId,
1700
+ range: {
1701
+ start: 0,
1702
+ end: 0
1703
+ },
1704
+ ...navigationTarget.story && typeof navigationTarget.story === "object" ? { story: navigationTarget.story } : {},
1705
+ ...record.address && typeof record.address === "object" ? { address: record.address } : {}
1706
+ };
1707
+ return target;
1708
+ }
1667
1709
  function storyLocatorSignature(story) {
1668
1710
  if (!story || typeof story !== "object") return "story:body";
1669
1711
  const record = story;
@@ -2860,11 +2902,14 @@ function createSuperDocUI(options) {
2860
2902
  let explicitActiveChange = null;
2861
2903
  let explicitActiveChangeRevision = 0;
2862
2904
  let queuedTrackChangeNavigationInvalidation = 0;
2905
+ let trackChangeRevealInvalidation = 0;
2906
+ const pendingTrackChangeRevealFocuses = /* @__PURE__ */ new Set();
2863
2907
  const explicitActiveChangesEqual = (left, right) => {
2864
2908
  if (left === right) return true;
2865
2909
  if (!left || !right) return false;
2866
2910
  return left.id === right.id && left.paintedEntityId === right.paintedEntityId && storyLocatorSignature(left.story) === storyLocatorSignature(right.story);
2867
2911
  };
2912
+ const hasPendingTrackChangeRevealFocus = (focus) => Array.from(pendingTrackChangeRevealFocuses).some((pendingFocus) => explicitActiveChangesEqual(pendingFocus, focus));
2868
2913
  const mirrorTrackedChangeFocusToHost = (next) => {
2869
2914
  const host = getHost();
2870
2915
  if (typeof host?.getHandles !== "function") return;
@@ -2893,6 +2938,7 @@ function createSuperDocUI(options) {
2893
2938
  const setExplicitActiveChange = (next, options$1) => {
2894
2939
  explicitActiveChange = next;
2895
2940
  explicitActiveChangeRevision += 1;
2941
+ trackChangeRevealInvalidation += 1;
2896
2942
  if (options$1?.invalidateQueuedNavigation !== false) queuedTrackChangeNavigationInvalidation += 1;
2897
2943
  };
2898
2944
  const listeners = /* @__PURE__ */ new Set();
@@ -5630,6 +5676,7 @@ function createSuperDocUI(options) {
5630
5676
  ...typeof target.paintedEntityId === "string" && target.paintedEntityId.length > 0 ? { paintedEntityId: target.paintedEntityId } : {}
5631
5677
  };
5632
5678
  }
5679
+ if (rawTarget == null && next == null && explicitActiveChange != null && hasPendingTrackChangeRevealFocus(explicitActiveChange)) return false;
5633
5680
  if (explicitActiveChangesEqual(explicitActiveChange, next)) return false;
5634
5681
  setExplicitActiveChange(next);
5635
5682
  return true;
@@ -5900,8 +5947,13 @@ function createSuperDocUI(options) {
5900
5947
  const story = selectionStoryLocator(state.selection);
5901
5948
  const item = items.find((candidate) => entityRowMatchesRequest(candidate, activeId, story));
5902
5949
  if (!item) return "unavailable";
5903
- const type = trackChangesItemPayload(item).type;
5904
- const grouping = trackChangesItemPayload(item).grouping;
5950
+ const change = trackChangesItemPayload(item);
5951
+ const type = change.type;
5952
+ const grouping = change.grouping;
5953
+ if (type === "replacement" && grouping === "replacement-pair") {
5954
+ if (typeof change.insertedText === "string" && state.selection.quotedText === change.insertedText) return "id";
5955
+ return "range";
5956
+ }
5905
5957
  return (type === "insertion" || type === "deletion" || type === "insert" || type === "delete") && grouping === "standalone" ? "range" : "id";
5906
5958
  }
5907
5959
  function resolveTrackDecisionTarget(command, payload) {
@@ -5953,6 +6005,15 @@ function createSuperDocUI(options) {
5953
6005
  const target = trackDecisionRangeFromSelection(state.selection);
5954
6006
  return target ? executeTrackDecisionTarget(command.kind, target, null) : false;
5955
6007
  }
6008
+ if (route === "id") {
6009
+ const activeId = state.selection.activeChangeIds[0];
6010
+ const selectedStory = selectionStoryLocator(state.selection);
6011
+ const story = selectedStory && storyLocatorSignature(selectedStory) !== storyLocatorSignature(null) ? selectedStory : void 0;
6012
+ return executeTrackDecision(command.kind, story ? {
6013
+ id: activeId,
6014
+ story
6015
+ } : activeId);
6016
+ }
5956
6017
  }
5957
6018
  }
5958
6019
  const resolved = resolveTrackDecisionTarget(command, payload);
@@ -7477,11 +7538,12 @@ function createSuperDocUI(options) {
7477
7538
  reason: SUPERDOC_UI_REASONS.hostCapabilityUnavailable
7478
7539
  };
7479
7540
  try {
7480
- const result = await Promise.resolve(scroll.call(host, {
7541
+ const input = {
7481
7542
  target,
7482
7543
  block: options$1?.block ?? "center",
7483
7544
  behavior: options$1?.behavior ?? "smooth"
7484
- }));
7545
+ };
7546
+ const result = await Promise.resolve(options$1?.shouldContinue ? scroll.call(host, input, options$1.shouldContinue) : scroll.call(host, input));
7485
7547
  if (result && typeof result === "object" && result.success === false) return {
7486
7548
  success: false,
7487
7549
  ok: false,
@@ -7511,19 +7573,32 @@ function createSuperDocUI(options) {
7511
7573
  story: requestedStory
7512
7574
  };
7513
7575
  };
7514
- const fromList = readEntityTarget(loaded.find((row) => entityRowMatchesRequest(row, id, requestedStory)));
7515
- if (fromList) return withRequestedStory(fromList);
7576
+ const readTarget = namespace === "trackChanges" ? readTrackedChangeNavigationTarget : readEntityTarget;
7577
+ const loadedRow = loaded.find((row) => entityRowMatchesRequest(row, id, requestedStory));
7578
+ const loadedRecord = loadedRow && typeof loadedRow === "object" ? loadedRow : null;
7579
+ const moveSide = namespace === "trackChanges" && loadedRecord?.type === "move" && (loadedRecord.subtype === "move-to" || loadedRecord.subtype === "move-from") ? loadedRecord.subtype : null;
7580
+ const trackedChangeLookupId = moveSide && typeof loadedRecord?.trackedChangeCanonicalId === "string" ? loadedRecord.trackedChangeCanonicalId : id;
7581
+ const readResolvedTarget = (row) => {
7582
+ if (!moveSide) return readTarget(row);
7583
+ const moveTarget = readEntityTarget(row);
7584
+ if (moveTarget?.kind !== "move") return null;
7585
+ const sideTarget = moveSide === "move-to" ? moveTarget.destination : moveTarget.source;
7586
+ return isHostGeometryTarget(sideTarget) ? sideTarget : null;
7587
+ };
7588
+ const fromList = readTarget(loadedRow);
7589
+ const listTargetIsAuthoritative = namespace !== "trackChanges" || !moveSide && isHostGeometryTarget(readEntityTarget(loadedRow));
7590
+ if (fromList && listTargetIsAuthoritative) return withRequestedStory(fromList);
7516
7591
  const api = getDoc()?.[namespace];
7517
7592
  const input = namespace === "comments" ? { commentId: id } : {
7518
- id,
7593
+ id: trackedChangeLookupId,
7519
7594
  ...requestedStory ? { story: requestedStory } : {}
7520
7595
  };
7521
7596
  const get = api?.get;
7522
- if (typeof get !== "function") return null;
7597
+ if (typeof get !== "function") return withRequestedStory(fromList);
7523
7598
  try {
7524
- return withRequestedStory(readEntityTarget(await Promise.resolve(get.call(api, input))));
7599
+ return withRequestedStory(readResolvedTarget(await Promise.resolve(get.call(api, input))) ?? fromList);
7525
7600
  } catch {
7526
- return null;
7601
+ return withRequestedStory(fromList);
7527
7602
  }
7528
7603
  };
7529
7604
  function patchCommentStatus(commentId, status) {
@@ -7685,33 +7760,41 @@ function createSuperDocUI(options) {
7685
7760
  if (activeId == null) return { success: false };
7686
7761
  const optimisticActiveChange = explicitActiveChange;
7687
7762
  const optimisticRevision = explicitActiveChangeRevision;
7688
- const isCurrent = () => explicitActiveChange === optimisticActiveChange && explicitActiveChangeRevision === optimisticRevision;
7763
+ const isCurrent = () => queuedTrackChangeNavigationInvalidation === requestedAtInvalidation && explicitActiveChange === optimisticActiveChange && explicitActiveChangeRevision === optimisticRevision;
7689
7764
  const rollback = () => {
7690
7765
  if (!isCurrent()) return;
7691
7766
  setExplicitActiveChange(previousActiveChange, { invalidateQueuedNavigation: false });
7692
7767
  recompute();
7693
7768
  };
7694
- const target = await resolveEntityTarget("trackChanges", activeId, trackChangesSub.get().items, optimisticActiveChange?.story ? { story: optimisticActiveChange.story } : void 0);
7695
- if (!isCurrent()) return { success: false };
7696
- if (!target) {
7697
- rollback();
7698
- return { success: false };
7699
- }
7700
- trackedChangeNavigationInFlight += 1;
7769
+ if (optimisticActiveChange) pendingTrackChangeRevealFocuses.add(optimisticActiveChange);
7701
7770
  try {
7702
- const result = await scrollTargetIntoView(target, { behavior: "auto" });
7771
+ const target = await resolveEntityTarget("trackChanges", activeId, trackChangesSub.get().items, optimisticActiveChange?.story ? { story: optimisticActiveChange.story } : void 0);
7703
7772
  if (!isCurrent()) return { success: false };
7704
- if (!result.ok && result.reason !== SUPERDOC_UI_REASONS.targetNotVisible) {
7773
+ if (!target) {
7705
7774
  rollback();
7706
- mirrorTrackedChangeFocusToHost(previousActiveChange);
7707
- } else mirrorTrackedChangeFocusToHost(optimisticActiveChange);
7708
- return { success: result.ok };
7709
- } finally {
7710
- trackedChangeNavigationInFlight = Math.max(0, trackedChangeNavigationInFlight - 1);
7711
- if (trackedChangeNavigationInFlight === 0) {
7712
- if (!isCurrent()) mirrorTrackedChangeFocusToHost(explicitActiveChange);
7713
- else if (currentHostReviewSource) syncTrackedChangeFocusFromHostReviewTarget(readHostActiveReviewTarget(currentHostReviewSource));
7775
+ return { success: false };
7776
+ }
7777
+ trackedChangeNavigationInFlight += 1;
7778
+ try {
7779
+ const result = await scrollTargetIntoView(target, {
7780
+ behavior: "auto",
7781
+ shouldContinue: isCurrent
7782
+ });
7783
+ if (!isCurrent()) return { success: false };
7784
+ if (!result.ok && result.reason !== SUPERDOC_UI_REASONS.targetNotVisible) {
7785
+ rollback();
7786
+ mirrorTrackedChangeFocusToHost(previousActiveChange);
7787
+ } else mirrorTrackedChangeFocusToHost(optimisticActiveChange);
7788
+ return { success: result.ok };
7789
+ } finally {
7790
+ trackedChangeNavigationInFlight = Math.max(0, trackedChangeNavigationInFlight - 1);
7791
+ if (trackedChangeNavigationInFlight === 0) {
7792
+ if (!isCurrent()) mirrorTrackedChangeFocusToHost(explicitActiveChange);
7793
+ else if (currentHostReviewSource) syncTrackedChangeFocusFromHostReviewTarget(readHostActiveReviewTarget(currentHostReviewSource));
7794
+ }
7714
7795
  }
7796
+ } finally {
7797
+ if (optimisticActiveChange) pendingTrackChangeRevealFocuses.delete(optimisticActiveChange);
7715
7798
  }
7716
7799
  };
7717
7800
  const navigateAndScroll = (step) => {
@@ -7813,47 +7896,65 @@ function createSuperDocUI(options) {
7813
7896
  ok: false,
7814
7897
  reason: getEditor() ? SUPERDOC_UI_REASONS.documentApiUnavailable : SUPERDOC_UI_REASONS.notReady
7815
7898
  };
7899
+ queuedTrackChangeNavigationInvalidation += 1;
7900
+ trackChangeRevealInvalidation += 1;
7901
+ const requestedAtInvalidation = trackChangeRevealInvalidation;
7816
7902
  const loadedItems = trackChangesSub.get().items;
7817
- const story = readEntityRequestStory(loadedItems.find((item) => readEntityId(item) === changeId));
7818
- const target = await resolveEntityTarget("trackChanges", changeId, loadedItems, story ? { story } : void 0);
7819
- if (!target) return {
7820
- success: false,
7821
- ok: false,
7822
- reason: SUPERDOC_UI_REASONS.targetUnresolved
7823
- };
7903
+ const publicId = buildTrackedChangeIdContext(loadedItems).toPublicId(changeId) ?? changeId;
7904
+ const story = readEntityRequestStory(loadedItems.find((item) => readEntityId(item) === publicId));
7824
7905
  const previousActiveChange = explicitActiveChange;
7825
- const requestedActiveChange = story ? {
7826
- id: changeId,
7827
- story
7828
- } : { id: changeId };
7829
- setExplicitActiveChange(requestedActiveChange);
7830
- recompute();
7831
- const optimisticActiveChange = explicitActiveChange;
7832
- const optimisticRevision = explicitActiveChangeRevision;
7833
- const isCurrent = () => explicitActiveChange === optimisticActiveChange && explicitActiveChangeRevision === optimisticRevision;
7834
- const rollback = () => {
7835
- if (!isCurrent()) return;
7836
- setExplicitActiveChange(previousActiveChange, { invalidateQueuedNavigation: false });
7837
- recompute();
7906
+ const requestedActiveChange = {
7907
+ id: publicId,
7908
+ ...story ? { story } : {},
7909
+ ...publicId !== changeId ? { paintedEntityId: changeId } : {}
7838
7910
  };
7839
- trackedChangeNavigationInFlight += 1;
7911
+ pendingTrackChangeRevealFocuses.add(requestedActiveChange);
7840
7912
  try {
7841
- const result = await scrollTargetIntoView(target, { behavior: "auto" });
7842
- if (!isCurrent()) return {
7913
+ const target = await resolveEntityTarget("trackChanges", publicId, loadedItems, story ? { story } : void 0);
7914
+ if (trackChangeRevealInvalidation !== requestedAtInvalidation) return {
7843
7915
  success: false,
7844
7916
  ok: false
7845
7917
  };
7846
- if (!result.ok && result.reason !== SUPERDOC_UI_REASONS.targetNotVisible) {
7847
- rollback();
7848
- mirrorTrackedChangeFocusToHost(previousActiveChange);
7849
- } else mirrorTrackedChangeFocusToHost(requestedActiveChange);
7850
- return result;
7851
- } finally {
7852
- trackedChangeNavigationInFlight = Math.max(0, trackedChangeNavigationInFlight - 1);
7853
- if (trackedChangeNavigationInFlight === 0) {
7854
- if (!isCurrent()) mirrorTrackedChangeFocusToHost(explicitActiveChange);
7855
- else if (currentHostReviewSource) syncTrackedChangeFocusFromHostReviewTarget(readHostActiveReviewTarget(currentHostReviewSource));
7918
+ if (!target) return {
7919
+ success: false,
7920
+ ok: false,
7921
+ reason: SUPERDOC_UI_REASONS.targetUnresolved
7922
+ };
7923
+ setExplicitActiveChange(requestedActiveChange);
7924
+ const revealInvalidation = trackChangeRevealInvalidation;
7925
+ const optimisticActiveChange = explicitActiveChange;
7926
+ const optimisticRevision = explicitActiveChangeRevision;
7927
+ const isCurrent = () => trackChangeRevealInvalidation === revealInvalidation && explicitActiveChange === optimisticActiveChange && explicitActiveChangeRevision === optimisticRevision;
7928
+ const rollback = () => {
7929
+ if (!isCurrent()) return;
7930
+ setExplicitActiveChange(previousActiveChange, { invalidateQueuedNavigation: false });
7931
+ recompute();
7932
+ };
7933
+ recompute();
7934
+ trackedChangeNavigationInFlight += 1;
7935
+ try {
7936
+ const result = await scrollTargetIntoView(target, {
7937
+ behavior: "auto",
7938
+ shouldContinue: isCurrent
7939
+ });
7940
+ if (!isCurrent()) return {
7941
+ success: false,
7942
+ ok: false
7943
+ };
7944
+ if (!result.ok && result.reason !== SUPERDOC_UI_REASONS.targetNotVisible) {
7945
+ rollback();
7946
+ mirrorTrackedChangeFocusToHost(previousActiveChange);
7947
+ } else mirrorTrackedChangeFocusToHost(requestedActiveChange);
7948
+ return result;
7949
+ } finally {
7950
+ trackedChangeNavigationInFlight = Math.max(0, trackedChangeNavigationInFlight - 1);
7951
+ if (trackedChangeNavigationInFlight === 0) {
7952
+ if (!isCurrent()) mirrorTrackedChangeFocusToHost(explicitActiveChange);
7953
+ else if (currentHostReviewSource) syncTrackedChangeFocusFromHostReviewTarget(readHostActiveReviewTarget(currentHostReviewSource));
7954
+ }
7856
7955
  }
7956
+ } finally {
7957
+ pendingTrackChangeRevealFocuses.delete(requestedActiveChange);
7857
7958
  }
7858
7959
  }
7859
7960
  };