wordinweb 0.1.20 → 0.1.21

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.d.ts CHANGED
@@ -1530,6 +1530,8 @@ interface TextItem {
1530
1530
  * the bookmark's page number (Word recomputes PAGEREF on open; the docx
1531
1531
  * cached result is stale in real TOCs). */
1532
1532
  pageRef?: string;
1533
+ /** DOCX bookmarks that begin on this text item. */
1534
+ bookmarks?: string[];
1533
1535
  /** Source m:oMath element when this text is a piece of an equation. */
1534
1536
  mathSrc?: XmlElement;
1535
1537
  href?: string;
package/dist/index.js CHANGED
@@ -14427,6 +14427,7 @@ var Engine = class {
14427
14427
  }
14428
14428
  floatOffset = clearBannerForLine(line2, li, floatOffset);
14429
14429
  this.y += floatOffset;
14430
+ const lineItemStart = this.cur.items.length;
14430
14431
  this.emitLine(line2, this.cur, this.colX, this.y);
14431
14432
  if (props.tabs) {
14432
14433
  for (const t of props.tabs) {
@@ -14448,12 +14449,16 @@ var Engine = class {
14448
14449
  if (li === 0 && para.bookmarks) {
14449
14450
  const pg = this.cur.physIndex === -1 ? this.lastRealPage : this.cur;
14450
14451
  if (pg) {
14452
+ const target = this.cur.physIndex === -1 ? void 0 : this.cur.items.slice(lineItemStart).find((item) => item.kind === "text");
14453
+ const targets = [];
14451
14454
  for (const bm of para.bookmarks) {
14452
14455
  if (!this.bookmarkPages.has(bm)) {
14453
14456
  this.bookmarkPages.set(bm, formatNumber(pg.displayNumber, PAGE_FMT[pg.sp.pageNumberFormat ?? "decimal"] ?? "decimal"));
14454
14457
  this.bookmarkPageIndices.set(bm, pg.physIndex - 1);
14458
+ targets.push(bm);
14455
14459
  }
14456
14460
  }
14461
+ if (target && targets.length > 0) target.bookmarks = targets;
14457
14462
  }
14458
14463
  }
14459
14464
  if (li === 0 && this.styleRefTrack && this.styleRefTrack.size > 0) {
@@ -17797,6 +17802,12 @@ function renderPageShell(page, zoom, options) {
17797
17802
  el16.dataset.bodyTop = String(page.bodyTop);
17798
17803
  el16.dataset.bodyBottom = String(page.bodyBottom);
17799
17804
  el16.dataset.columnBands = JSON.stringify(page.columnBands);
17805
+ const bookmarks = {};
17806
+ for (const item of page.items) {
17807
+ if (item.kind !== "text" || !item.bookmarks) continue;
17808
+ for (const name of item.bookmarks) bookmarks[name] = (item.glyphTop ?? item.lineTop) * zoom;
17809
+ }
17810
+ if (Object.keys(bookmarks).length > 0) el16.dataset.bookmarks = JSON.stringify(bookmarks);
17800
17811
  return el16;
17801
17812
  }
17802
17813
  function renderEmptyPageRecord(page, zoom, options) {
@@ -19431,17 +19442,22 @@ function renderText(item, interactive) {
19431
19442
  }
19432
19443
  if (item.href) {
19433
19444
  const anchor = el16;
19445
+ const bookmark = item.href.startsWith("#") ? item.href.slice(1) : void 0;
19434
19446
  anchor.href = item.href;
19435
- anchor.target = "_blank";
19436
- anchor.rel = "noreferrer noopener";
19447
+ if (!bookmark) {
19448
+ anchor.target = "_blank";
19449
+ anchor.rel = "noreferrer noopener";
19450
+ }
19437
19451
  anchor.title = item.href;
19438
- if (interactive) {
19452
+ if (interactive || bookmark) {
19439
19453
  anchor.addEventListener("mouseup", (event) => {
19440
19454
  if (event.metaKey || event.ctrlKey) event.stopPropagation();
19441
19455
  });
19442
19456
  anchor.addEventListener("click", (event) => {
19443
19457
  event.preventDefault();
19444
- if (event.metaKey || event.ctrlKey) {
19458
+ if (bookmark && (!interactive || event.metaKey || event.ctrlKey)) {
19459
+ scrollToBookmark(anchor, bookmark);
19460
+ } else if (!bookmark && (event.metaKey || event.ctrlKey)) {
19445
19461
  window.open(anchor.href, "_blank", "noopener,noreferrer");
19446
19462
  }
19447
19463
  });
@@ -19456,6 +19472,20 @@ function renderText(item, interactive) {
19456
19472
  setItemLayer(el16, item);
19457
19473
  return el16;
19458
19474
  }
19475
+ function scrollToBookmark(anchor, bookmark) {
19476
+ const root = anchor.closest(".dxw-pages");
19477
+ const container = root?.parentElement;
19478
+ if (!root || !container) return;
19479
+ for (const page of Array.from(root.children)) {
19480
+ if (!(page instanceof HTMLElement) || !page.dataset.bookmarks) continue;
19481
+ const bookmarks = JSON.parse(page.dataset.bookmarks);
19482
+ const y = bookmarks[bookmark];
19483
+ if (y === void 0) continue;
19484
+ const pageTop = page.getBoundingClientRect().top - container.getBoundingClientRect().top + container.scrollTop;
19485
+ container.scrollTo({ top: Math.max(0, pageTop + y - 24) });
19486
+ return;
19487
+ }
19488
+ }
19459
19489
  function renderEdge(x1, y1, x2, y2, border, rotate) {
19460
19490
  const el16 = document.createElement("div");
19461
19491
  el16.dataset.dxwEdge = "1";