@extend-ai/react-docx 0.6.5 → 0.6.7

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CrowdView Inc, dba Extend
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs CHANGED
@@ -12297,7 +12297,15 @@ var defaultStarterModel = {
12297
12297
  headerSections: [],
12298
12298
  footerSections: [],
12299
12299
  paragraphStyles: [
12300
- { id: "Normal", name: "Body", isDefault: true },
12300
+ {
12301
+ id: "Normal",
12302
+ name: "Body",
12303
+ isDefault: true,
12304
+ // Word's blank-document default body typeface. Without this the body
12305
+ // default fell through to a heading font / Times New Roman and did not
12306
+ // match the "Calibri" shown in the toolbar.
12307
+ runStyle: { fontFamily: "Calibri", fontSizePt: 11 }
12308
+ },
12301
12309
  {
12302
12310
  id: "Heading1",
12303
12311
  name: "Heading 1",
@@ -12440,7 +12448,7 @@ function resolveDocumentInheritedFontFamily(model) {
12440
12448
  return cssFontFamily(defaultStyleFontFamily);
12441
12449
  }
12442
12450
  const paragraphStyleFontFamily = paragraphStyles.find(
12443
- (style) => Boolean(style.runStyle?.fontFamily?.trim())
12451
+ (style) => style.headingLevel === void 0 && Boolean(style.runStyle?.fontFamily?.trim())
12444
12452
  )?.runStyle?.fontFamily;
12445
12453
  if (paragraphStyleFontFamily) {
12446
12454
  return cssFontFamily(paragraphStyleFontFamily);
@@ -14675,7 +14683,17 @@ function nodeAlreadyEndsAtExplicitPageBoundary(node) {
14675
14683
  }
14676
14684
  return paragraphHasExplicitPageBreak2(node) || paragraphHasPageBreakBefore2(node) || sectionBreakAfterParagraphStartsNewPage(node);
14677
14685
  }
14686
+ var docxHardPageBreakStartNodeIndexesByModel = /* @__PURE__ */ new WeakMap();
14678
14687
  function collectDocxHardPageBreakStartNodeIndexes(model) {
14688
+ const cached = docxHardPageBreakStartNodeIndexesByModel.get(model);
14689
+ if (cached) {
14690
+ return cached;
14691
+ }
14692
+ const result = computeDocxHardPageBreakStartNodeIndexes(model);
14693
+ docxHardPageBreakStartNodeIndexesByModel.set(model, result);
14694
+ return result;
14695
+ }
14696
+ function computeDocxHardPageBreakStartNodeIndexes(model) {
14679
14697
  const breaks = collectTopLevelExplicitPageBreakStartNodeIndexes(model.nodes);
14680
14698
  const sections = resolveDocumentSectionsFromMetadata(model.metadata);
14681
14699
  for (let sectionIndex = 1; sectionIndex < sections.length; sectionIndex += 1) {
@@ -14699,7 +14717,17 @@ function collectDocxHardPageBreakStartNodeIndexes(model) {
14699
14717
  }
14700
14718
  return breaks;
14701
14719
  }
14720
+ var docxSectionStartPageBreakNodeIndexesByModel = /* @__PURE__ */ new WeakMap();
14702
14721
  function collectDocxSectionStartPageBreakNodeIndexes(model) {
14722
+ const cached = docxSectionStartPageBreakNodeIndexesByModel.get(model);
14723
+ if (cached) {
14724
+ return cached;
14725
+ }
14726
+ const result = computeDocxSectionStartPageBreakNodeIndexes(model);
14727
+ docxSectionStartPageBreakNodeIndexesByModel.set(model, result);
14728
+ return result;
14729
+ }
14730
+ function computeDocxSectionStartPageBreakNodeIndexes(model) {
14703
14731
  const breaks = /* @__PURE__ */ new Set();
14704
14732
  const sections = resolveDocumentSectionsFromMetadata(model.metadata);
14705
14733
  for (let sectionIndex = 1; sectionIndex < sections.length; sectionIndex += 1) {
@@ -30096,6 +30124,8 @@ function DocxEditorViewer({
30096
30124
  const fileDragDepthRef = React.useRef(0);
30097
30125
  const tableDraftLayoutRefreshRafRef = React.useRef(null);
30098
30126
  const activeRangeFlushFrameRef = React.useRef(null);
30127
+ const pendingEditableCaretRef = React.useRef(null);
30128
+ const pointerSelectionDragRef = React.useRef({ down: false, moved: false });
30099
30129
  const deferredCollapsedSelectionSyncTimeoutRef = React.useRef(
30100
30130
  null
30101
30131
  );
@@ -34144,14 +34174,62 @@ function DocxEditorViewer({
34144
34174
  });
34145
34175
  }, [resolveParagraphBoundaryFromSelectionPoint]);
34146
34176
  const setActiveRangeFromSelection = React.useCallback(() => {
34177
+ const session = editor.selectionSessionKind;
34178
+ const rootElement = viewerRootRef.current;
34179
+ const activeElement = document.activeElement;
34180
+ const editorHasFocus = Boolean(
34181
+ activeElement instanceof HTMLElement && activeElement.isContentEditable && rootElement && rootElement.contains(activeElement)
34182
+ );
34183
+ const liveSelection = window.getSelection();
34184
+ const anchorNode = liveSelection?.anchorNode ?? null;
34185
+ const collapsedElementAnchor = Boolean(
34186
+ liveSelection && liveSelection.isCollapsed && liveSelection.rangeCount > 0 && anchorNode && anchorNode.nodeType !== Node.TEXT_NODE
34187
+ );
34188
+ const selectionDropped = Boolean(
34189
+ editorHasFocus && (!liveSelection || liveSelection.rangeCount === 0)
34190
+ );
34191
+ if (collapsedElementAnchor || selectionDropped) {
34192
+ if (session === "keyboard" || session === "composition") {
34193
+ return;
34194
+ }
34195
+ const anchorElement = anchorNode instanceof Element ? anchorNode : anchorNode?.parentElement ?? null;
34196
+ const destroyedHost = anchorElement?.closest(
34197
+ "[data-docx-paragraph-host='true']"
34198
+ ) ?? (editorHasFocus ? activeElement.closest(
34199
+ "[data-docx-paragraph-host='true']"
34200
+ ) : null);
34201
+ const lastGoodRange = cloneTextRange(editor.activeTextRange);
34202
+ if (lastGoodRange && destroyedHost) {
34203
+ const normalizedLastGood = normalizeTextRange(lastGoodRange);
34204
+ const lastGoodHost = resolveParagraphHostElement(
34205
+ normalizedLastGood.start.location
34206
+ );
34207
+ if (lastGoodHost === destroyedHost) {
34208
+ setSelectionFromDocxBoundaries(
34209
+ normalizedLastGood.start,
34210
+ normalizedLastGood.end
34211
+ );
34212
+ }
34213
+ }
34214
+ return;
34215
+ }
34147
34216
  const range = resolveActiveRangeFromDomSelection();
34148
34217
  if (!range) {
34218
+ if (editorHasFocus && editor.activeTextRange) {
34219
+ return;
34220
+ }
34149
34221
  editor.setActiveTextRange(void 0);
34150
34222
  return;
34151
34223
  }
34152
34224
  clearTableCellSelection();
34153
34225
  editor.setActiveTextRange(range);
34154
- }, [clearTableCellSelection, editor, resolveActiveRangeFromDomSelection]);
34226
+ }, [
34227
+ clearTableCellSelection,
34228
+ editor,
34229
+ resolveActiveRangeFromDomSelection,
34230
+ resolveParagraphHostElement,
34231
+ setSelectionFromDocxBoundaries
34232
+ ]);
34155
34233
  const flushActiveRangeFromSelection = React.useCallback(() => {
34156
34234
  if (deferredCollapsedSelectionSyncTimeoutRef.current !== null) {
34157
34235
  window.clearTimeout(deferredCollapsedSelectionSyncTimeoutRef.current);
@@ -34165,6 +34243,73 @@ function DocxEditorViewer({
34165
34243
  setActiveRangeFromSelection();
34166
34244
  });
34167
34245
  }, [setActiveRangeFromSelection]);
34246
+ React.useLayoutEffect(() => {
34247
+ if (isReadOnly) {
34248
+ return;
34249
+ }
34250
+ const session = editor.selectionSessionKind;
34251
+ if (pointerSelectionDragRef.current.moved || session === "composition") {
34252
+ return;
34253
+ }
34254
+ const rootElement = viewerRootRef.current;
34255
+ if (!rootElement) {
34256
+ return;
34257
+ }
34258
+ const activeElement = document.activeElement;
34259
+ if (!(activeElement instanceof HTMLElement) || !activeElement.isContentEditable || !rootElement.contains(activeElement)) {
34260
+ return;
34261
+ }
34262
+ const focusedHost = activeElement.closest(
34263
+ "[data-docx-paragraph-host='true']"
34264
+ );
34265
+ if (!focusedHost) {
34266
+ return;
34267
+ }
34268
+ const selection = window.getSelection();
34269
+ if (!selection) {
34270
+ return;
34271
+ }
34272
+ const liveRange = selection.rangeCount > 0 ? selection.getRangeAt(0) : void 0;
34273
+ const anchorNode = selection.anchorNode;
34274
+ const selectionInsideHost = Boolean(
34275
+ liveRange && focusedHost.contains(liveRange.startContainer) && focusedHost.contains(liveRange.endContainer)
34276
+ );
34277
+ const anchorIsElement = Boolean(
34278
+ anchorNode && anchorNode.nodeType !== Node.TEXT_NODE
34279
+ );
34280
+ const selectionWasDestroyed = !liveRange || !selectionInsideHost || selection.isCollapsed && anchorIsElement;
34281
+ if (!selectionWasDestroyed) {
34282
+ return;
34283
+ }
34284
+ const focusedNodeIndexAttr = focusedHost.getAttribute(
34285
+ "data-docx-paragraph-node-index"
34286
+ );
34287
+ const focusedNodeIndex = focusedNodeIndexAttr != null ? Number.parseInt(focusedNodeIndexAttr, 10) : Number.NaN;
34288
+ const pendingCaret = pendingEditableCaretRef.current;
34289
+ if (pendingCaret && Number.isFinite(focusedNodeIndex) && pendingCaret.nodeIndex === focusedNodeIndex) {
34290
+ const textLength = editableTextFromElement(focusedHost).length;
34291
+ const safeStart = Math.max(0, Math.min(pendingCaret.start, textLength));
34292
+ const safeEnd = Math.max(
34293
+ safeStart,
34294
+ Math.min(pendingCaret.end, textLength)
34295
+ );
34296
+ setSelectionWithinElementByTextOffsets(focusedHost, safeStart, safeEnd);
34297
+ return;
34298
+ }
34299
+ if (session === "keyboard") {
34300
+ return;
34301
+ }
34302
+ const range = editor.activeTextRange;
34303
+ if (!range) {
34304
+ return;
34305
+ }
34306
+ const normalized = normalizeTextRange(range);
34307
+ const targetHost = resolveParagraphHostElement(normalized.start.location);
34308
+ if (!targetHost || targetHost !== focusedHost) {
34309
+ return;
34310
+ }
34311
+ setSelectionFromDocxBoundaries(normalized.start, normalized.end);
34312
+ });
34168
34313
  const selectionIsExpandedWithinElement = React.useCallback(
34169
34314
  (element) => {
34170
34315
  const selection = window.getSelection();
@@ -34618,6 +34763,28 @@ function DocxEditorViewer({
34618
34763
  const collapsedCaretInsideEditableHost = Boolean(
34619
34764
  selectionRange.collapsed && activeElement instanceof HTMLElement && activeElement.isContentEditable && rootElement.contains(activeElement) && activeElement.contains(selectionRange.startContainer)
34620
34765
  );
34766
+ const selectionWithinSingleEditableHost = Boolean(
34767
+ activeElement instanceof HTMLElement && activeElement.isContentEditable && rootElement.contains(activeElement) && activeElement.contains(selectionRange.startContainer) && activeElement.contains(selectionRange.endContainer) && selectionRange.startContainer.nodeType === Node.TEXT_NODE && selectionRange.endContainer.nodeType === Node.TEXT_NODE
34768
+ );
34769
+ if (selectionWithinSingleEditableHost && activeElement instanceof HTMLElement) {
34770
+ const host = activeElement.closest(
34771
+ "[data-docx-paragraph-host='true']"
34772
+ );
34773
+ const nodeIndexAttr = host?.getAttribute(
34774
+ "data-docx-paragraph-node-index"
34775
+ );
34776
+ const offsets = host ? selectionOffsetsWithinElement(host) : void 0;
34777
+ if (host && nodeIndexAttr != null && offsets) {
34778
+ const nodeIndex = Number.parseInt(nodeIndexAttr, 10);
34779
+ if (Number.isFinite(nodeIndex)) {
34780
+ pendingEditableCaretRef.current = {
34781
+ nodeIndex,
34782
+ start: offsets.start,
34783
+ end: offsets.end
34784
+ };
34785
+ }
34786
+ }
34787
+ }
34621
34788
  if (collapsedCaretInsideEditableHost) {
34622
34789
  scheduleDeferredCollapsedSelectionSync();
34623
34790
  return;
@@ -34629,6 +34796,27 @@ function DocxEditorViewer({
34629
34796
  document.removeEventListener("selectionchange", handleSelectionChange);
34630
34797
  };
34631
34798
  }, [flushActiveRangeFromSelection, scheduleDeferredCollapsedSelectionSync]);
34799
+ React.useEffect(() => {
34800
+ const onPointerDown = () => {
34801
+ pointerSelectionDragRef.current = { down: true, moved: false };
34802
+ };
34803
+ const onPointerMove = (event) => {
34804
+ if (pointerSelectionDragRef.current.down && event.buttons !== 0) {
34805
+ pointerSelectionDragRef.current.moved = true;
34806
+ }
34807
+ };
34808
+ const onPointerUp = () => {
34809
+ pointerSelectionDragRef.current = { down: false, moved: false };
34810
+ };
34811
+ window.addEventListener("pointerdown", onPointerDown, true);
34812
+ window.addEventListener("pointermove", onPointerMove, true);
34813
+ window.addEventListener("pointerup", onPointerUp, true);
34814
+ return () => {
34815
+ window.removeEventListener("pointerdown", onPointerDown, true);
34816
+ window.removeEventListener("pointermove", onPointerMove, true);
34817
+ window.removeEventListener("pointerup", onPointerUp, true);
34818
+ };
34819
+ }, []);
34632
34820
  const beginCrossNodeSelectionDrag = React.useCallback(
34633
34821
  (startBoundary, pointerId, startX, startY) => {
34634
34822
  tableSelectionDragRef.current = {
@@ -36864,6 +37052,8 @@ function DocxEditorViewer({
36864
37052
  if (Date.now() < paginationMeasurementSuspendUntilRef.current) {
36865
37053
  return;
36866
37054
  }
37055
+ const rootElement = viewerRootRef.current;
37056
+ const zoomScale = rootElement ? resolveEffectiveZoomScale(rootElement) : virtualizerMeasurementScale;
36867
37057
  const nextMeasuredHeights = {};
36868
37058
  editor.model.nodes.forEach((node, nodeIndex) => {
36869
37059
  if (node.type !== "table") {
@@ -36879,7 +37069,8 @@ function DocxEditorViewer({
36879
37069
  if (normalizedRowIndex >= node.rows.length || !visibleRowIndexes.has(normalizedRowIndex)) {
36880
37070
  return;
36881
37071
  }
36882
- const normalizedHeight = normalizeMeasuredTableRowHeightPx(rowHeightPx);
37072
+ const documentRowHeightPx = Number.isFinite(zoomScale) && zoomScale > 0 ? rowHeightPx / zoomScale : rowHeightPx;
37073
+ const normalizedHeight = normalizeMeasuredTableRowHeightPx(documentRowHeightPx);
36883
37074
  const previousHeight = measuredByRowIndex.get(normalizedRowIndex);
36884
37075
  measuredByRowIndex.set(
36885
37076
  normalizedRowIndex,
@@ -37008,6 +37199,7 @@ function DocxEditorViewer({
37008
37199
  tableColumnWidths,
37009
37200
  tableRowHeights,
37010
37201
  tableDraftLayoutEpoch,
37202
+ virtualizerMeasurementScale,
37011
37203
  visibleTableRowIndexesByNodeIndex
37012
37204
  ]);
37013
37205
  const normalizeResizableHeights = React.useCallback(
@@ -42056,6 +42248,18 @@ function DocxEditorViewer({
42056
42248
  ...leadingCoverLayoutSpacer ? {
42057
42249
  minHeight: `${estimateParagraphLineHeightPx(node, nodeDocGridLinePitchPx) + EMPTY_PARAGRAPH_EXTRA_HEIGHT_PX + LEADING_COVER_SPACER_EXTRA_HEIGHT_PX}px`
42058
42250
  } : void 0,
42251
+ // Carry the paragraph's run font on the editable host so text typed into
42252
+ // it (which is a bare, not-yet-committed text node, not a styled run
42253
+ // span) renders in the same font the committed run will use — i.e. the
42254
+ // font shown in the toolbar — instead of inheriting a generic default.
42255
+ // Rendered runs set their own font on their span, so this only affects
42256
+ // freshly typed/empty content. When the paragraph has no explicit run
42257
+ // font we leave it unset so the document default still applies.
42258
+ ...(() => {
42259
+ const runStyle = firstRunStyle(node);
42260
+ const hostFontFamily = cssFontFamily(runStyle?.fontFamily);
42261
+ return hostFontFamily ? { fontFamily: hostFontFamily } : {};
42262
+ })(),
42059
42263
  outline: "none"
42060
42264
  };
42061
42265
  let fallbackParagraphRuns;
@@ -42281,6 +42485,16 @@ function DocxEditorViewer({
42281
42485
  if (!editable) {
42282
42486
  return;
42283
42487
  }
42488
+ const mouseUpOffsets = selectionOffsetsWithinElement(
42489
+ event.currentTarget
42490
+ );
42491
+ if (mouseUpOffsets) {
42492
+ pendingEditableCaretRef.current = {
42493
+ nodeIndex,
42494
+ start: mouseUpOffsets.start,
42495
+ end: mouseUpOffsets.end
42496
+ };
42497
+ }
42284
42498
  if (selectionIsExpandedWithinElement(event.currentTarget)) {
42285
42499
  flushActiveRangeFromSelection();
42286
42500
  cancelPendingPointerSelectionReconcile();
@@ -42325,6 +42539,14 @@ function DocxEditorViewer({
42325
42539
  nodeIndex,
42326
42540
  event.currentTarget.innerHTML
42327
42541
  );
42542
+ const typedOffsets = selectionOffsetsWithinElement(
42543
+ event.currentTarget
42544
+ );
42545
+ pendingEditableCaretRef.current = typedOffsets ? {
42546
+ nodeIndex,
42547
+ start: typedOffsets.start,
42548
+ end: typedOffsets.end
42549
+ } : null;
42328
42550
  },
42329
42551
  onCompositionStart: () => {
42330
42552
  if (!editable) {