wordinweb 0.1.15 → 0.1.17

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.js CHANGED
@@ -20716,7 +20716,7 @@ function resizeTableRow(doc, tblEl, rowIdx, newHeightPx) {
20716
20716
  doc.refresh();
20717
20717
  return true;
20718
20718
  }
20719
- function moveTableTo(doc, tblEl, xPx, yPx) {
20719
+ function moveTableTo(doc, tblEl, xPx, yPx, preservePageStart = false, pageDelta = 0) {
20720
20720
  if (localName(tblEl.name) !== "tbl") return false;
20721
20721
  const w = prefixOf5(tblEl);
20722
20722
  let tblPr = child(tblEl, "tblPr");
@@ -20725,11 +20725,55 @@ function moveTableTo(doc, tblEl, xPx, yPx) {
20725
20725
  tblEl.children.unshift(tblPr);
20726
20726
  }
20727
20727
  let position = child(tblPr, "tblpPr");
20728
+ const wasInline = !position;
20728
20729
  if (!position) {
20729
20730
  position = { name: `${w}tblpPr`, attrs: {}, children: [], text: "" };
20730
20731
  const styleIdx = tblPr.children.findIndex((c) => localName(c.name) === "tblStyle");
20731
20732
  tblPr.children.splice(styleIdx + 1, 0, position);
20732
20733
  }
20734
+ let pageBreakAdjustment = pageDelta + (wasInline && preservePageStart ? 1 : 0);
20735
+ const parent = doc.findParentOf(tblEl);
20736
+ if (parent && pageBreakAdjustment > 0) {
20737
+ const tableIndex = parent.children.indexOf(tblEl);
20738
+ let previous = tableIndex > 0 ? parent.children[tableIndex - 1] : void 0;
20739
+ if (!previous || localName(previous.name) !== "p") {
20740
+ previous = { name: `${w}p`, attrs: {}, children: [], text: "" };
20741
+ parent.children.splice(tableIndex, 0, previous);
20742
+ }
20743
+ while (pageBreakAdjustment-- > 0) {
20744
+ previous.children.push({
20745
+ name: `${w}r`,
20746
+ attrs: {},
20747
+ children: [{ name: `${w}br`, attrs: { [`${w}type`]: "page" }, children: [], text: "" }],
20748
+ text: ""
20749
+ });
20750
+ }
20751
+ } else if (parent && pageBreakAdjustment < 0) {
20752
+ while (pageBreakAdjustment++ < 0) {
20753
+ const tableIndex = parent.children.indexOf(tblEl);
20754
+ const previous = tableIndex > 0 ? parent.children[tableIndex - 1] : void 0;
20755
+ if (!previous || localName(previous.name) !== "p") break;
20756
+ let removed = false;
20757
+ for (let runIndex = previous.children.length - 1; runIndex >= 0 && !removed; runIndex--) {
20758
+ const run = previous.children[runIndex];
20759
+ if (localName(run.name) !== "r") continue;
20760
+ for (let childIndex = run.children.length - 1; childIndex >= 0; childIndex--) {
20761
+ const runChild = run.children[childIndex];
20762
+ if (localName(runChild.name) !== "br" || attr(runChild, "type") !== "page") continue;
20763
+ run.children.splice(childIndex, 1);
20764
+ if (!run.children.some((childEl) => localName(childEl.name) !== "rPr")) {
20765
+ previous.children.splice(runIndex, 1);
20766
+ }
20767
+ removed = true;
20768
+ break;
20769
+ }
20770
+ }
20771
+ if (!removed) break;
20772
+ if (!previous.children.some((childEl) => localName(childEl.name) !== "pPr")) {
20773
+ parent.children.splice(parent.children.indexOf(previous), 1);
20774
+ }
20775
+ }
20776
+ }
20733
20777
  const keyOf = (name) => Object.keys(position.attrs).find((key) => localName(key) === name);
20734
20778
  const set = (name, value) => {
20735
20779
  position.attrs[keyOf(name) ?? `${w}${name}`] = value;
@@ -23574,35 +23618,102 @@ var _DocxEditor = class _DocxEditor {
23574
23618
  const zoom = this.host.zoom ?? 1;
23575
23619
  const startX = e.clientX;
23576
23620
  const startY = e.clientY;
23621
+ const pages = Array.from(handle?.root.querySelectorAll(".dxw-page") ?? []);
23622
+ const sourcePage = surface.parentElement;
23623
+ const sourcePageIndex = sourcePage ? pages.indexOf(sourcePage) : -1;
23624
+ const sourceRect = surface.getBoundingClientRect();
23625
+ const pointerOffsetX = (startX - sourceRect.left) / zoom - grip.item.x;
23626
+ const pointerOffsetY = (startY - sourceRect.top) / zoom - grip.item.y1;
23577
23627
  let dx = 0;
23578
23628
  let dy = 0;
23579
- const onMove = (me) => {
23580
- dx = (me.clientX - startX) / zoom;
23581
- dy = (me.clientY - startY) / zoom;
23629
+ let targetPageIndex = sourcePageIndex;
23630
+ let targetX = grip.item.x;
23631
+ let targetY = grip.item.y1;
23632
+ let pointerX = startX;
23633
+ let pointerY = startY;
23634
+ let scrollParent = surface.parentElement;
23635
+ while (scrollParent) {
23636
+ const style = getComputedStyle(scrollParent);
23637
+ if (/(auto|scroll)/.test(style.overflowY) && scrollParent.scrollHeight > scrollParent.clientHeight) break;
23638
+ scrollParent = scrollParent.parentElement;
23639
+ }
23640
+ scrollParent ?? (scrollParent = document.scrollingElement);
23641
+ const updateMove = (clientX, clientY) => {
23642
+ dx = (clientX - startX) / zoom;
23643
+ dy = (clientY - startY) / zoom;
23582
23644
  if (isMove) {
23583
23645
  const width = (grip.item.x2 ?? grip.item.x) - grip.item.x;
23584
23646
  const height = grip.item.y2 - grip.item.y1;
23585
- const maxX = Math.max(0, parseFloat(surface.style.width) - width);
23586
- const maxY = Math.max(0, parseFloat(surface.style.height) - height);
23587
- dx = Math.max(-grip.item.x, Math.min(dx, maxX - grip.item.x));
23588
- dy = Math.max(-grip.item.y1, Math.min(dy, maxY - grip.item.y1));
23589
- guide.style.left = `${grip.item.x + dx}px`;
23590
- guide.style.top = `${grip.item.y1 + dy}px`;
23647
+ let targetSurface = surface;
23648
+ let bestDistance = Infinity;
23649
+ for (let pageIndex = 0; pageIndex < pages.length; pageIndex++) {
23650
+ const candidate = pages[pageIndex].firstElementChild;
23651
+ if (!candidate) continue;
23652
+ const rect = candidate.getBoundingClientRect();
23653
+ const distX = clientX < rect.left ? rect.left - clientX : clientX > rect.right ? clientX - rect.right : 0;
23654
+ const distY = clientY < rect.top ? rect.top - clientY : clientY > rect.bottom ? clientY - rect.bottom : 0;
23655
+ const distance = distX * distX + distY * distY;
23656
+ if (distance >= bestDistance) continue;
23657
+ bestDistance = distance;
23658
+ targetSurface = candidate;
23659
+ targetPageIndex = pageIndex;
23660
+ }
23661
+ const targetRect = targetSurface.getBoundingClientRect();
23662
+ const maxX = Math.max(0, targetRect.width / zoom - width);
23663
+ const maxY = Math.max(0, targetRect.height / zoom - height);
23664
+ targetX = Math.max(0, Math.min((clientX - targetRect.left) / zoom - pointerOffsetX, maxX));
23665
+ targetY = Math.max(0, Math.min((clientY - targetRect.top) / zoom - pointerOffsetY, maxY));
23666
+ dx = targetX - grip.item.x;
23667
+ dy = targetY - grip.item.y1;
23668
+ if (guide.parentElement !== targetSurface) targetSurface.appendChild(guide);
23669
+ guide.style.left = `${targetX}px`;
23670
+ guide.style.top = `${targetY}px`;
23591
23671
  } else if (isCol) {
23592
23672
  guide.style.left = `${grip.item.x + dx}px`;
23593
23673
  } else {
23594
23674
  guide.style.top = `${grip.item.y1 + dy}px`;
23595
23675
  }
23596
23676
  };
23677
+ const onMove = (me) => {
23678
+ pointerX = me.clientX;
23679
+ pointerY = me.clientY;
23680
+ updateMove(pointerX, pointerY);
23681
+ };
23682
+ let autoScrollFrame;
23683
+ const autoScroll = () => {
23684
+ if (isMove && scrollParent) {
23685
+ const isDocumentScroll = scrollParent === document.documentElement || scrollParent === document.body;
23686
+ const rect = isDocumentScroll ? { top: 0, bottom: window.innerHeight } : scrollParent.getBoundingClientRect();
23687
+ const margin = 48;
23688
+ const amount = pointerY < rect.top + margin ? -Math.min(28, Math.ceil(rect.top + margin - pointerY)) : pointerY > rect.bottom - margin ? Math.min(28, Math.ceil(pointerY - (rect.bottom - margin))) : 0;
23689
+ if (amount !== 0) {
23690
+ const before = scrollParent.scrollTop;
23691
+ scrollParent.scrollTop += amount;
23692
+ if (scrollParent.scrollTop !== before) updateMove(pointerX, pointerY);
23693
+ }
23694
+ }
23695
+ autoScrollFrame = requestAnimationFrame(autoScroll);
23696
+ };
23697
+ autoScrollFrame = requestAnimationFrame(autoScroll);
23597
23698
  const onUp = () => {
23598
23699
  document.removeEventListener("mousemove", onMove);
23599
23700
  document.removeEventListener("mouseup", onUp);
23701
+ if (autoScrollFrame !== void 0) cancelAnimationFrame(autoScrollFrame);
23600
23702
  guide.remove();
23601
23703
  this.suppressNextMouseUp = false;
23602
23704
  const delta = isCol ? dx : dy;
23603
- if ((isMove ? Math.hypot(dx, dy) : Math.abs(delta)) >= 1) {
23705
+ if (isMove ? targetPageIndex !== sourcePageIndex || Math.hypot(dx, dy) >= 1 : Math.abs(delta) >= 1) {
23604
23706
  this.host.history?.checkpoint();
23605
- const ok = isMove ? moveTableTo(this.host.doc, grip.item.tbl, grip.item.x + dx, grip.item.y1 + dy) : isCol ? resizeTableColumn(this.host.doc, grip.item.tbl, grip.item.boundary, dx, grip.item.renderedWidths) : resizeTableRow(this.host.doc, grip.item.tbl, grip.item.boundary, (grip.item.rowHeightPx ?? 0) + dy);
23707
+ const bodyTop = parseFloat(sourcePage?.dataset.bodyTop ?? "");
23708
+ const preservePageStart = isMove && sourcePage !== null && sourcePageIndex > 0 && Number.isFinite(bodyTop) && Math.abs(grip.item.y1 - bodyTop) <= 1;
23709
+ const ok = isMove ? moveTableTo(
23710
+ this.host.doc,
23711
+ grip.item.tbl,
23712
+ targetX,
23713
+ targetY,
23714
+ preservePageStart,
23715
+ targetPageIndex - sourcePageIndex
23716
+ ) : isCol ? resizeTableColumn(this.host.doc, grip.item.tbl, grip.item.boundary, dx, grip.item.renderedWidths) : resizeTableRow(this.host.doc, grip.item.tbl, grip.item.boundary, (grip.item.rowHeightPx ?? 0) + dy);
23606
23717
  if (ok) {
23607
23718
  this.host.rerender(void 0, isMove ? "global" : "local");
23608
23719
  this.positionCaret();