@canvas-components/print-design 0.1.11 → 0.1.12

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.cjs CHANGED
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
4
3
  var __defProp = Object.defineProperty;
5
4
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
5
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
@@ -222,9 +221,9 @@ function parsePrintDesignDocument(input, componentRegistry) {
222
221
  }
223
222
  return clonePrintDesignDocument(value);
224
223
  }
225
- function serializePrintDesignDocument(document2, componentRegistry) {
224
+ function serializePrintDesignDocument(document, componentRegistry) {
226
225
  return JSON.stringify(
227
- componentRegistry ? parsePrintDesignDocument(document2, componentRegistry) : parsePrintDesignDocument(document2),
226
+ componentRegistry ? parsePrintDesignDocument(document, componentRegistry) : parsePrintDesignDocument(document),
228
227
  null,
229
228
  2
230
229
  );
@@ -468,8 +467,8 @@ function drawTransformedElement(ctx, element, scale, data, onImageLoad, page, fl
468
467
  ) + padding;
469
468
  const rasterWidth = rasterRight - rasterLeft;
470
469
  const rasterHeight = rasterBottom - rasterTop;
471
- const document2 = ctx.canvas?.ownerDocument;
472
- const canvas = document2?.createElement("canvas") ?? globalThis.document?.createElement("canvas") ?? (typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(1, 1) : null);
470
+ const document = ctx.canvas?.ownerDocument;
471
+ const canvas = document?.createElement("canvas") ?? globalThis.document?.createElement("canvas") ?? (typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(1, 1) : null);
473
472
  if (!canvas) return;
474
473
  const sourceScale = Math.min(scale, 4096 / rasterWidth, 4096 / rasterHeight);
475
474
  canvas.width = Math.max(1, Math.ceil(rasterWidth * sourceScale));
@@ -610,56 +609,10 @@ function printDesignComponentError(operation, documentId, page, element, cause)
610
609
  );
611
610
  }
612
611
 
613
- // src/rendering/default-font.ts
614
- var PRINT_DESIGN_DEFAULT_FONT_FAMILY = "PrintDesignArimo, PrintDesignNotoSansSC, sans-serif";
615
- var loadedDocuments = /* @__PURE__ */ new WeakMap();
616
- function getPrintDesignDefaultFontUrl(weight) {
617
- return weight === 700 ? new URL("./font-assets/NotoSansSC-Bold.ttf", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))).href : new URL("./font-assets/NotoSansSC-Regular.ttf", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))).href;
618
- }
619
- function getPrintDesignLatinFontUrl(weight) {
620
- return weight === 700 ? new URL("./font-assets/Arimo-Bold.ttf", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))).href : new URL("./font-assets/Arimo-Regular.ttf", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))).href;
621
- }
622
- function getPrintDesignSerifFontUrl(weight) {
623
- return weight === 700 ? new URL("./font-assets/Tinos-Bold.ttf", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))).href : new URL("./font-assets/Tinos-Regular.ttf", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))).href;
624
- }
625
- var bundledFaces = (weight) => [
626
- ["PrintDesignArimo", getPrintDesignLatinFontUrl(weight)],
627
- ["PrintDesignNotoSansSC", getPrintDesignDefaultFontUrl(weight)]
628
- ];
629
- function preparePrintDesignDefaultFonts(ownerDocument = document) {
630
- if (typeof FontFace === "undefined" || !ownerDocument.fonts)
631
- return Promise.resolve();
632
- const existing = loadedDocuments.get(ownerDocument);
633
- if (existing) return existing;
634
- const ready = Promise.all(
635
- [400, 700].flatMap(
636
- (weight) => bundledFaces(weight).map(async ([family, source]) => {
637
- const face = new FontFace(family, `url(${JSON.stringify(source)})`, {
638
- weight: String(weight)
639
- });
640
- await face.load();
641
- ownerDocument.fonts.add(face);
642
- })
643
- )
644
- ).then(() => void 0);
645
- loadedDocuments.set(ownerDocument, ready);
646
- void ready.catch(() => loadedDocuments.delete(ownerDocument));
647
- return ready;
648
- }
649
- function printDesignDefaultFontCss() {
650
- return [400, 700].flatMap(
651
- (weight) => bundledFaces(weight).map(
652
- ([family, source]) => `@font-face{font-family:${family};src:url(${JSON.stringify(source)});font-weight:${weight}}`
653
- )
654
- ).join("");
655
- }
656
-
657
612
  // src/rendering/renderer.ts
658
613
  var preparedCanvasPages = /* @__PURE__ */ new WeakSet();
659
614
  async function preparePrintDesignCanvasPage(page, data, componentRegistry, options = {}) {
660
615
  options.signal?.throwIfAborted();
661
- if (typeof document !== "undefined")
662
- await preparePrintDesignDefaultFonts(document);
663
616
  for (const element of page.elements) {
664
617
  options.signal?.throwIfAborted();
665
618
  if (!resolvePrintDesignElementVisible(element, data)) continue;
@@ -864,10 +817,10 @@ function drawPrintDesignPage(ctx, page, options = {}) {
864
817
  ctx.restore();
865
818
  }
866
819
  }
867
- function renderPrintDesignPageToCanvas(document2, pageId, options = {}) {
868
- const page = document2.pages.find((item) => item.id === pageId);
820
+ function renderPrintDesignPageToCanvas(document, pageId, options = {}) {
821
+ const page = document.pages.find((item) => item.id === pageId);
869
822
  if (!page) throw new RangeError(`\u627E\u4E0D\u5230\u9875\u9762\uFF1A${pageId}`);
870
- const dpi = options.dpi ?? document2.dpi ?? 300;
823
+ const dpi = options.dpi ?? document.dpi ?? 300;
871
824
  if (!Number.isFinite(dpi) || dpi <= 0) {
872
825
  throw new RangeError("\u5BFC\u51FA DPI \u5FC5\u987B\u662F\u5927\u4E8E 0 \u7684\u6709\u9650\u6570\u503C\u3002");
873
826
  }
@@ -890,7 +843,7 @@ function renderPrintDesignPageToCanvas(document2, pageId, options = {}) {
890
843
  transparent: options.transparent,
891
844
  scale,
892
845
  componentRegistry: options.componentRegistry,
893
- documentId: document2.id
846
+ documentId: document.id
894
847
  });
895
848
  } finally {
896
849
  context.restore();
@@ -1454,18 +1407,18 @@ function drawMeasurements(context, page, selectedIds, scale, unit = "mm", data =
1454
1407
 
1455
1408
  // src/model/pagination.ts
1456
1409
  var MAX_PAGES = 1e3;
1457
- function paginatePrintDesignDocument(document2, data = {}, componentRegistry) {
1458
- return paginateDocument(document2, data, componentRegistry);
1410
+ function paginatePrintDesignDocument(document, data = {}, componentRegistry) {
1411
+ return paginateDocument(document, data, componentRegistry);
1459
1412
  }
1460
- function materializePrintDesignDocument(document2, data, componentRegistry) {
1413
+ function materializePrintDesignDocument(document, data, componentRegistry) {
1461
1414
  return componentRegistry?.list().reduce(
1462
1415
  (current, renderer) => renderer.materializeDocument ? renderer.materializeDocument(current, data) : current,
1463
- document2
1464
- ) ?? document2;
1416
+ document
1417
+ ) ?? document;
1465
1418
  }
1466
- function paginateDocument(document2, data, componentRegistry) {
1419
+ function paginateDocument(document, data, componentRegistry) {
1467
1420
  const resolvedDocument = materializePrintDesignDocument(
1468
- document2,
1421
+ document,
1469
1422
  data,
1470
1423
  componentRegistry
1471
1424
  );
@@ -1473,7 +1426,7 @@ function paginateDocument(document2, data, componentRegistry) {
1473
1426
  for (const originalSource of resolvedDocument.pages) {
1474
1427
  if (!resolvePrintDesignPageVisible(originalSource, data)) continue;
1475
1428
  const source = originalSource.heightMode === "content" ? resolveContentHeightPage(
1476
- document2,
1429
+ document,
1477
1430
  originalSource,
1478
1431
  data,
1479
1432
  componentRegistry
@@ -1604,9 +1557,9 @@ function paginateDocument(document2, data, componentRegistry) {
1604
1557
  page.physicalPageNumber = index + 1;
1605
1558
  page.physicalTotalPages = pages.length;
1606
1559
  }
1607
- return { ...document2, pages };
1560
+ return { ...document, pages };
1608
1561
  }
1609
- function resolveContentHeightPage(document2, page, data, componentRegistry) {
1562
+ function resolveContentHeightPage(document, page, data, componentRegistry) {
1610
1563
  const top = page.margins?.top ?? 0;
1611
1564
  const bottom = page.margins?.bottom ?? 0;
1612
1565
  const minimumHeight = top + bottom + 1;
@@ -1629,7 +1582,7 @@ function resolveContentHeightPage(document2, page, data, componentRegistry) {
1629
1582
  const workingHeight = baseHeight + growth + 10;
1630
1583
  const measured = paginatePrintDesignDocument(
1631
1584
  {
1632
- ...document2,
1585
+ ...document,
1633
1586
  pages: [{ ...page, heightMode: "fixed", height: workingHeight }]
1634
1587
  },
1635
1588
  data,
@@ -1647,6 +1600,14 @@ function resolveContentHeightPage(document2, page, data, componentRegistry) {
1647
1600
  return { ...page, height };
1648
1601
  }
1649
1602
 
1603
+ // src/rendering/default-font.ts
1604
+ var PRINT_DESIGN_DEFAULT_FONT_FAMILY = "Arial, sans-serif";
1605
+ function resolvePrintDesignFontFamily(family) {
1606
+ const selected = family?.trim();
1607
+ if (!selected) return PRINT_DESIGN_DEFAULT_FONT_FAMILY;
1608
+ return /(?:^|,)\s*(?:sans-serif|serif|monospace|cursive|fantasy|system-ui)\s*$/i.test(selected) ? selected : `${selected}, sans-serif`;
1609
+ }
1610
+
1650
1611
  // src/component/builtin-html.ts
1651
1612
  var escapeHtml = (value) => String(value ?? "").replace(
1652
1613
  /[&<>"']/g,
@@ -1742,14 +1703,14 @@ async function prepareHtmlPage(page, documentId, data, registry, signal) {
1742
1703
  }
1743
1704
  return { ...page, elements };
1744
1705
  }
1745
- function* generatePrintHtml(document2, options = {}, physicalPages, pageOffset = 0, physicalTotalPages, preparedElements = false) {
1706
+ function* generatePrintHtml(document, options = {}, physicalPages, pageOffset = 0, physicalTotalPages, preparedElements = false) {
1746
1707
  const data = options.data ?? {};
1747
1708
  const componentRegistry = options.componentRegistry ?? builtinPrintDesignComponentRegistry;
1748
1709
  const selectedPages = options.pageIds ? options.pageIds.map((id) => {
1749
- const page = document2.pages.find((item) => item.id === id);
1710
+ const page = document.pages.find((item) => item.id === id);
1750
1711
  if (!page) throw new RangeError(`\u627E\u4E0D\u5230\u9875\u9762\uFF1A${id}`);
1751
1712
  return page;
1752
- }) : document2.pages;
1713
+ }) : document.pages;
1753
1714
  if (!selectedPages.length) throw new RangeError("\u81F3\u5C11\u9700\u8981\u4E00\u4E2A\u6253\u5370\u9875\u9762\u3002");
1754
1715
  if (!preparedElements) {
1755
1716
  for (const page of selectedPages)
@@ -1757,7 +1718,7 @@ function* generatePrintHtml(document2, options = {}, physicalPages, pageOffset =
1757
1718
  if (resolvePrintDesignPageVisible(page, data) && resolvePrintDesignElementVisible(element, data) && componentRegistry.get(element.type)?.prepareHtml)
1758
1719
  throw printDesignComponentError(
1759
1720
  "\u6253\u5370 HTML \u751F\u6210",
1760
- document2.id,
1721
+ document.id,
1761
1722
  page,
1762
1723
  element,
1763
1724
  new Error(`\u7EC4\u4EF6 ${element.type} \u9700\u8981\u5F02\u6B65 HTML \u5BFC\u51FA\u4EE5\u51C6\u5907\u8D44\u6E90\u3002`)
@@ -1766,7 +1727,7 @@ function* generatePrintHtml(document2, options = {}, physicalPages, pageOffset =
1766
1727
  const pages = physicalPages ?? [];
1767
1728
  for (let index = 0; !physicalPages && index < selectedPages.length; index++) {
1768
1729
  const result = paginatePrintDesignDocument(
1769
- { ...document2, pages: [selectedPages[index]] },
1730
+ { ...document, pages: [selectedPages[index]] },
1770
1731
  data,
1771
1732
  componentRegistry
1772
1733
  ).pages;
@@ -1797,7 +1758,7 @@ function* generatePrintHtml(document2, options = {}, physicalPages, pageOffset =
1797
1758
  } catch (cause) {
1798
1759
  throw printDesignComponentError(
1799
1760
  "\u6253\u5370\u6837\u5F0F\u751F\u6210",
1800
- document2.id,
1761
+ document.id,
1801
1762
  page,
1802
1763
  element,
1803
1764
  cause
@@ -1824,7 +1785,7 @@ function* generatePrintHtml(document2, options = {}, physicalPages, pageOffset =
1824
1785
  } catch (cause) {
1825
1786
  throw printDesignComponentError(
1826
1787
  "\u6253\u5370 HTML \u751F\u6210",
1827
- document2.id,
1788
+ document.id,
1828
1789
  page,
1829
1790
  element,
1830
1791
  cause
@@ -1837,7 +1798,7 @@ function* generatePrintHtml(document2, options = {}, physicalPages, pageOffset =
1837
1798
  yield 0.3 + (index + 1) / pages.length * 0.7;
1838
1799
  }
1839
1800
  const markup = parts.join("");
1840
- return `<!doctype html><html><head><meta charset="utf-8"><title>${escapeHtml(document2.name)}</title><style>${printDesignDefaultFontCss()}${pageStyles}${componentStyles}*{box-sizing:border-box}html,body{margin:0;padding:0}body{-webkit-print-color-adjust:exact;print-color-adjust:exact}.pd-page{position:relative;overflow:hidden;break-after:page;page-break-after:always}.pd-page:last-child{break-after:auto;page-break-after:auto}.pd-element{position:absolute;overflow:hidden;transform-origin:center center}.pd-element>svg{display:block;width:100%;height:100%}@media print{.pd-page{margin:0}}</style></head><body>${markup}</body></html>`;
1801
+ return `<!doctype html><html><head><meta charset="utf-8"><title>${escapeHtml(document.name)}</title><style>${pageStyles}${componentStyles}*{box-sizing:border-box}html,body{margin:0;padding:0}body{-webkit-print-color-adjust:exact;print-color-adjust:exact}.pd-page{position:relative;overflow:hidden;break-after:page;page-break-after:always}.pd-page:last-child{break-after:auto;page-break-after:auto}.pd-element{position:absolute;overflow:hidden;transform-origin:center center}.pd-element>svg{display:block;width:100%;height:100%}@media print{.pd-page{margin:0}}</style></head><body>${markup}</body></html>`;
1841
1802
  }
1842
1803
  function printPrintDesignHtml(popup, html, close = () => popup.close(), beforePrint) {
1843
1804
  popup.document.open();
@@ -1906,28 +1867,26 @@ function printPrintDesignHtmlInFrame(frame, html) {
1906
1867
  }
1907
1868
  return true;
1908
1869
  }
1909
- function transformPrintDesignToHtml(document2, options = {}) {
1910
- const task = generatePrintHtml(document2, options);
1870
+ function transformPrintDesignToHtml(document, options = {}) {
1871
+ const task = generatePrintHtml(document, options);
1911
1872
  let next = task.next();
1912
1873
  while (!next.done) next = task.next();
1913
1874
  return next.value;
1914
1875
  }
1915
- async function transformPrintDesignToHtmlAsync(document2, options = {}) {
1916
- if (globalThis.document)
1917
- await preparePrintDesignDefaultFonts(globalThis.document);
1876
+ async function transformPrintDesignToHtmlAsync(document, options = {}) {
1918
1877
  const data = options.data ?? {};
1919
1878
  const registry = options.componentRegistry ?? builtinPrintDesignComponentRegistry;
1920
1879
  const selectedPages = options.pageIds ? options.pageIds.map((id) => {
1921
- const page = document2.pages.find((item) => item.id === id);
1880
+ const page = document.pages.find((item) => item.id === id);
1922
1881
  if (!page) throw new RangeError(`\u627E\u4E0D\u5230\u9875\u9762\uFF1A${id}`);
1923
1882
  return page;
1924
- }) : document2.pages;
1883
+ }) : document.pages;
1925
1884
  const pages = [];
1926
1885
  for (let index = 0; index < selectedPages.length; index++) {
1927
1886
  await new Promise((resolve) => setTimeout(resolve, 0));
1928
1887
  options.signal?.throwIfAborted();
1929
1888
  const physical = paginatePrintDesignDocument(
1930
- { ...document2, pages: [selectedPages[index]] },
1889
+ { ...document, pages: [selectedPages[index]] },
1931
1890
  data,
1932
1891
  registry
1933
1892
  ).pages;
@@ -1935,7 +1894,7 @@ async function transformPrintDesignToHtmlAsync(document2, options = {}) {
1935
1894
  pages.push(
1936
1895
  await prepareHtmlPage(
1937
1896
  page,
1938
- document2.id,
1897
+ document.id,
1939
1898
  data,
1940
1899
  registry,
1941
1900
  options.signal
@@ -1943,7 +1902,7 @@ async function transformPrintDesignToHtmlAsync(document2, options = {}) {
1943
1902
  );
1944
1903
  options.onProgress?.((index + 1) / selectedPages.length * 0.3);
1945
1904
  }
1946
- const task = generatePrintHtml(document2, options, pages, 0, void 0, true);
1905
+ const task = generatePrintHtml(document, options, pages, 0, void 0, true);
1947
1906
  while (true) {
1948
1907
  await new Promise((resolve) => setTimeout(resolve, 0));
1949
1908
  options.signal?.throwIfAborted();
@@ -1955,10 +1914,10 @@ async function transformPrintDesignToHtmlAsync(document2, options = {}) {
1955
1914
  } while (performance.now() < deadline);
1956
1915
  }
1957
1916
  }
1958
- async function* transformPrintDesignToHtmlBatches(document2, options) {
1917
+ async function* transformPrintDesignToHtmlBatches(document, options) {
1959
1918
  const limit = Math.max(1, Math.floor(options.pagesPerBatch));
1960
1919
  if (!Number.isFinite(limit)) throw new RangeError("\u6BCF\u6B21\u6253\u5370\u9875\u6570\u5FC5\u987B\u662F\u6B63\u6574\u6570");
1961
- const needsTotal = document2.pages.some(
1920
+ const needsTotal = document.pages.some(
1962
1921
  (page) => page.elements.some(
1963
1922
  (element) => options.componentRegistry?.get(element.type)?.needsTotalPages?.(element) ?? false
1964
1923
  )
@@ -1966,10 +1925,10 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
1966
1925
  let totalPages;
1967
1926
  if (needsTotal) {
1968
1927
  totalPages = 0;
1969
- for (const page of document2.pages) {
1928
+ for (const page of document.pages) {
1970
1929
  options.signal?.throwIfAborted();
1971
1930
  totalPages += paginatePrintDesignDocument(
1972
- { ...document2, pages: [page] },
1931
+ { ...document, pages: [page] },
1973
1932
  options.data ?? {},
1974
1933
  options.componentRegistry
1975
1934
  ).pages.length;
@@ -1979,7 +1938,7 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
1979
1938
  let offset = 0;
1980
1939
  const render = async () => {
1981
1940
  const task = generatePrintHtml(
1982
- document2,
1941
+ document,
1983
1942
  options,
1984
1943
  buffer,
1985
1944
  offset,
@@ -1996,11 +1955,11 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
1996
1955
  } while (performance.now() < deadline);
1997
1956
  }
1998
1957
  };
1999
- for (let index = 0; index < document2.pages.length; index++) {
1958
+ for (let index = 0; index < document.pages.length; index++) {
2000
1959
  await new Promise((resolve) => setTimeout(resolve, 0));
2001
1960
  options.signal?.throwIfAborted();
2002
1961
  const pages = paginatePrintDesignDocument(
2003
- { ...document2, pages: [document2.pages[index]] },
1962
+ { ...document, pages: [document.pages[index]] },
2004
1963
  options.data ?? {},
2005
1964
  options.componentRegistry
2006
1965
  ).pages;
@@ -2008,7 +1967,7 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
2008
1967
  buffer.push(
2009
1968
  await prepareHtmlPage(
2010
1969
  pages[pageIndex],
2011
- document2.id,
1970
+ document.id,
2012
1971
  options.data ?? {},
2013
1972
  options.componentRegistry ?? builtinPrintDesignComponentRegistry,
2014
1973
  options.signal
@@ -2020,7 +1979,7 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
2020
1979
  html,
2021
1980
  firstPage: offset + 1,
2022
1981
  lastPage: offset + buffer.length,
2023
- hasMore: pageIndex + 1 < pages.length || document2.pages.slice(index + 1).some(
1982
+ hasMore: pageIndex + 1 < pages.length || document.pages.slice(index + 1).some(
2024
1983
  (page) => resolvePrintDesignPageVisible(page, options.data ?? {})
2025
1984
  )
2026
1985
  };
@@ -2029,7 +1988,7 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
2029
1988
  options.signal?.throwIfAborted();
2030
1989
  }
2031
1990
  }
2032
- options.onProgress?.((index + 1) / document2.pages.length);
1991
+ options.onProgress?.((index + 1) / document.pages.length);
2033
1992
  }
2034
1993
  if (buffer.length)
2035
1994
  yield {
@@ -2525,12 +2484,12 @@ var PrintDesignBase = class {
2525
2484
  return sourceIndex?.(element, selector) ?? selector.index;
2526
2485
  }
2527
2486
  /** 由组件注册项命中内部单元。 */
2528
- hitComponentCell(element, point, document2, data = {}) {
2487
+ hitComponentCell(element, point, document, data = {}) {
2529
2488
  const registry = this.options.componentRegistry ?? builtinPrintDesignComponentRegistry;
2530
2489
  const behavior = registry.get(element.type)?.cellHit ?? builtinPrintDesignComponentRegistry.get(element.type)?.cellHit;
2531
2490
  return behavior?.hit(element, {
2532
2491
  point,
2533
- document: document2,
2492
+ document,
2534
2493
  data
2535
2494
  });
2536
2495
  }
@@ -2734,8 +2693,8 @@ var PrintDesignBase = class {
2734
2693
  })
2735
2694
  };
2736
2695
  }
2737
- getPageIn(document2) {
2738
- return document2.pages.find((page) => page.id === this.activePageId) ?? document2.pages[0];
2696
+ getPageIn(document) {
2697
+ return document.pages.find((page) => page.id === this.activePageId) ?? document.pages[0];
2739
2698
  }
2740
2699
  commitMutation(mutator) {
2741
2700
  const before = clonePrintDesignDocument(this.document);
@@ -2752,8 +2711,8 @@ var PrintDesignBase = class {
2752
2711
  this.emitChange();
2753
2712
  this.render();
2754
2713
  }
2755
- pushHistory(document2) {
2756
- this.history.push(document2);
2714
+ pushHistory(document) {
2715
+ this.history.push(document);
2757
2716
  if (this.history.length > HISTORY_LIMIT) this.history.shift();
2758
2717
  }
2759
2718
  normalizeActivePageAfterHistory() {
@@ -2874,20 +2833,21 @@ function measureSpacedText(ctx, text) {
2874
2833
  cache.set(text, width);
2875
2834
  return width;
2876
2835
  }
2877
- function layoutAlignedText(text, width, measure) {
2836
+ function layoutAlignedText(text, width, measure, indent) {
2878
2837
  return text.split("\n").flatMap((paragraph) => {
2879
2838
  const lines = [];
2880
2839
  let line = "";
2840
+ const lineIndent = (index) => indent?.mode === "firstLine" ? index === 0 ? indent.value : 0 : indent?.mode === "hanging" && index > 0 ? indent.value : 0;
2881
2841
  const tokens = paragraph.match(
2882
2842
  /[¥¥$€£]?\p{N}+(?:[.,:/-]\p{N}+)*(?:[%%])?|[\p{Script=Latin}\p{N}]+|\s+|[^\p{Script=Latin}\p{N}\s]+/gu
2883
2843
  ) ?? [];
2884
2844
  const pieces = tokens.flatMap(
2885
2845
  (token) => /^(?:[¥¥$€£]?\p{N}+(?:[.,:/-]\p{N}+)*(?:[%%])?|[\p{Script=Latin}\p{N}\s]+)$/u.test(
2886
2846
  token
2887
- ) && measure(token) <= width ? [token] : graphemes(token)
2847
+ ) && measure(token) <= Math.max(1, width - Math.max(0, indent?.value ?? 0)) ? [token] : graphemes(token)
2888
2848
  );
2889
2849
  for (const piece of pieces) {
2890
- if (line && measure(line + piece) > width) {
2850
+ if (line && measure(line + piece) > Math.max(1, width - lineIndent(lines.length))) {
2891
2851
  lines.push(line.trimEnd());
2892
2852
  line = "";
2893
2853
  }
@@ -2970,6 +2930,10 @@ var PrintDesignTextEditor = class {
2970
2930
  const expanded = element.textAlign === "justify" || element.textAlign === "distributed";
2971
2931
  this.input.setAttribute("wrap", "soft");
2972
2932
  const fontSize = (element.resolveFontSize?.(this.input.value) ?? element.fontSize ?? 14) * 25.4 * origin.scale / 72;
2933
+ const indent = element.indentMode && element.indentMode !== "none" ? element.indentUnit === "char" || !element.indentUnit ? Math.max(0, element.indentValue ?? 0) * fontSize : fromDisplayUnit(
2934
+ Math.max(0, element.indentValue ?? 0),
2935
+ element.indentUnit
2936
+ ) * origin.scale : 0;
2973
2937
  const height = element.height * origin.scale;
2974
2938
  const padding = this.settings.padding;
2975
2939
  const topPadding = (padding?.top ?? 0) * origin.scale;
@@ -2986,11 +2950,12 @@ var PrintDesignTextEditor = class {
2986
2950
  if (!verticalText) {
2987
2951
  const context = this.host.ownerDocument.createElement("canvas").getContext("2d");
2988
2952
  if (context) {
2989
- context.font = `${element.fontWeight ?? "400"} ${fontSize}px ${element.fontFamily ?? "Arial, sans-serif"}`;
2953
+ context.font = `${element.fontStyle ?? "normal"} ${element.fontWeight ?? "400"} ${fontSize}px ${resolvePrintDesignFontFamily(element.fontFamily)}`;
2990
2954
  lineCount = layoutAlignedText(
2991
2955
  this.input.value,
2992
2956
  contentWidth,
2993
- (text) => measureSpacedText(context, text)
2957
+ (text) => measureSpacedText(context, text),
2958
+ element.indentMode && element.indentMode !== "none" ? { mode: element.indentMode, value: indent } : void 0
2994
2959
  ).length;
2995
2960
  }
2996
2961
  }
@@ -3002,16 +2967,18 @@ var PrintDesignTextEditor = class {
3002
2967
  width: `${element.width * origin.scale}px`,
3003
2968
  height: `${height}px`,
3004
2969
  transform: `rotate(${element.rotation ?? 0}deg)`,
3005
- padding: `${topPadding + Math.max(paddingTop, 0)}px ${rightPadding}px ${bottomPadding}px ${leftPadding}px`,
2970
+ padding: `${topPadding + Math.max(paddingTop, 0)}px ${rightPadding}px ${bottomPadding}px ${leftPadding + (element.indentMode === "hanging" ? indent : 0)}px`,
2971
+ textIndent: element.indentMode === "firstLine" ? `${indent}px` : element.indentMode === "hanging" ? `${-indent}px` : "0",
3006
2972
  whiteSpace: "pre-wrap",
3007
2973
  writingMode: verticalText ? "vertical-rl" : "horizontal-tb",
3008
2974
  textOrientation: verticalText ? "upright" : "mixed",
3009
2975
  letterSpacing: "0",
3010
2976
  textSpacingTrim: "space-all",
3011
2977
  textAutospace: "no-autospace",
3012
- fontFamily: element.fontFamily ?? "Arial, sans-serif",
2978
+ fontFamily: resolvePrintDesignFontFamily(element.fontFamily),
3013
2979
  fontSize: `${fontSize}px`,
3014
2980
  fontWeight: element.fontWeight ?? "400",
2981
+ fontStyle: element.fontStyle ?? "normal",
3015
2982
  lineHeight: String(element.lineHeight ?? 1.35),
3016
2983
  textAlign: expanded ? "justify" : element.textAlign ?? "left",
3017
2984
  textAlignLast: element.textAlign === "distributed" ? "justify" : "auto",
@@ -3147,8 +3114,8 @@ var PrintDesignComponentCellEditing = class extends PrintDesignBase {
3147
3114
  (value, restoreFocus) => {
3148
3115
  this.componentCellEditing = null;
3149
3116
  if (value !== void 0 && value !== text && !this.options.readonly) {
3150
- this.commitMutation((document2) => {
3151
- const page = document2.pages.find((page2) => page2.id === pageId);
3117
+ this.commitMutation((document) => {
3118
+ const page = document.pages.find((page2) => page2.id === pageId);
3152
3119
  const index = page?.elements.findIndex(
3153
3120
  (item) => item.id === element.id
3154
3121
  );
@@ -3265,6 +3232,14 @@ var PrintDesignTextEditing = class extends PrintDesignComponentCellEditing {
3265
3232
  this.selectElements([element.id]);
3266
3233
  return;
3267
3234
  }
3235
+ if (element && this.options.onElementDoubleClick) {
3236
+ this.discardTextEditing();
3237
+ this.selectElements([element.id]);
3238
+ if (this.options.onElementDoubleClick(element, sourcePage?.id ?? page.id)) {
3239
+ event.preventDefault();
3240
+ return;
3241
+ }
3242
+ }
3268
3243
  if (element && this.canEditComponentCell(element)) {
3269
3244
  event.preventDefault();
3270
3245
  this.textEditor?.finish(true);
@@ -3290,8 +3265,8 @@ var PrintDesignTextEditing = class extends PrintDesignComponentCellEditing {
3290
3265
  (value, restoreFocus) => {
3291
3266
  this.textEditor = null;
3292
3267
  if (value !== void 0 && !this.options.readonly) {
3293
- this.commitMutation((document2) => {
3294
- const target = document2.pages.find((p) => p.id === (sourcePage?.id ?? page.id))?.elements.find((e) => e.id === element.id);
3268
+ this.commitMutation((document) => {
3269
+ const target = document.pages.find((p) => p.id === (sourcePage?.id ?? page.id))?.elements.find((e) => e.id === element.id);
3295
3270
  const targetRules = target ? this.options.componentRegistry?.get(target.type)?.inlineTextEditor : void 0;
3296
3271
  if (target && target.type === element.type && targetRules?.canEdit(target) && targetRules.read(target) === session.initialText)
3297
3272
  Object.assign(target, targetRules.write(target, value));
@@ -3736,6 +3711,7 @@ var PrintDesignRenderer = class extends PrintDesignTextEditing {
3736
3711
  for (const element of page.elements) {
3737
3712
  if (this.componentCropState?.elementId === element.id) continue;
3738
3713
  if (element.id === this.textEditor?.elementId) continue;
3714
+ if (element.id === this.options.inlineEditingElementId) continue;
3739
3715
  if (element.id === this.drawingPathId && this.pathDrawingPoints.length < 2)
3740
3716
  continue;
3741
3717
  let drawFailed = false;
@@ -4439,6 +4415,36 @@ function flowingComponentIds(page, selectedIds, data = {}, componentRegistry = b
4439
4415
  var PrintDesignInteraction = class extends PrintDesignRenderer {
4440
4416
  constructor() {
4441
4417
  super(...arguments);
4418
+ __publicField(this, "keyboardMoveSession", null);
4419
+ /** 按真实帧间隔连续移动,系统重复按键只更新当前修饰键状态。 */
4420
+ __publicField(this, "advanceKeyboardMove", (time) => {
4421
+ const session = this.keyboardMoveSession;
4422
+ if (!session) return;
4423
+ session.frame = null;
4424
+ const elapsed = time - session.lastFrameAt;
4425
+ session.lastFrameAt = time;
4426
+ const heldMs = Date.now() - session.startedAt;
4427
+ if (!session.released && heldMs >= 260) {
4428
+ const delta = this.options.keyboardMove?.(session.event, heldMs);
4429
+ if (delta && Number.isFinite(delta.dx) && Number.isFinite(delta.dy)) {
4430
+ const factor = Math.min(Math.max(elapsed, 0) / 33, 2);
4431
+ session.targetDx += delta.dx * factor;
4432
+ session.targetDy += delta.dy * factor;
4433
+ }
4434
+ }
4435
+ const ease = 1 - Math.exp(-Math.max(elapsed, 0) / 32);
4436
+ this.applyKeyboardMove(
4437
+ (session.targetDx - session.dx) * ease,
4438
+ (session.targetDy - session.dy) * ease
4439
+ );
4440
+ if (session.released && Math.abs(session.targetDx - session.dx) < 5e-3 && Math.abs(session.targetDy - session.dy) < 5e-3) {
4441
+ this.finishKeyboardMove();
4442
+ return;
4443
+ }
4444
+ const view = this.container.ownerDocument.defaultView;
4445
+ if (view?.requestAnimationFrame)
4446
+ session.frame = view.requestAnimationFrame(this.advanceKeyboardMove);
4447
+ });
4442
4448
  __publicField(this, "panMode", false);
4443
4449
  __publicField(this, "componentCropDrag", null);
4444
4450
  __publicField(this, "strokeRenderFrame", null);
@@ -4471,6 +4477,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
4471
4477
  else this.wheelPanFrame = view.requestAnimationFrame(this.advanceWheelPan);
4472
4478
  });
4473
4479
  __publicField(this, "handlePointerDown", (event) => {
4480
+ this.finishKeyboardMove();
4474
4481
  if (event.button !== 0) return;
4475
4482
  if (this.dragSession) return;
4476
4483
  this.stopWheelPan();
@@ -4881,27 +4888,29 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
4881
4888
  preview.data
4882
4889
  )) {
4883
4890
  this.selectElements([source.element.id]);
4884
- const selection = this.componentCellSelectionState;
4885
- this.canvas.setPointerCapture?.(event.pointerId);
4886
- this.dragSession = {
4887
- mode: "component-cell-select",
4888
- componentCellSelection: {
4889
- element: previewComponent,
4890
- sourceId: source.element.id,
4891
- data: preview.data,
4892
- anchor: this.componentCellSelection(previewComponent).dragAnchor(
4893
- selection
4894
- )
4895
- },
4896
- pointerId: event.pointerId,
4897
- start: point,
4898
- startClient: { x: event.clientX, y: event.clientY },
4899
- documentBefore: clonePrintDesignDocument(this.document),
4900
- elementPositions: /* @__PURE__ */ new Map(),
4901
- changed: false
4902
- };
4903
- this.canvas.style.cursor = "crosshair";
4904
- return;
4891
+ if (event.shiftKey) {
4892
+ const selection = this.componentCellSelectionState;
4893
+ this.canvas.setPointerCapture?.(event.pointerId);
4894
+ this.dragSession = {
4895
+ mode: "component-cell-select",
4896
+ componentCellSelection: {
4897
+ element: previewComponent,
4898
+ sourceId: source.element.id,
4899
+ data: preview.data,
4900
+ anchor: this.componentCellSelection(previewComponent).dragAnchor(
4901
+ selection
4902
+ )
4903
+ },
4904
+ pointerId: event.pointerId,
4905
+ start: point,
4906
+ startClient: { x: event.clientX, y: event.clientY },
4907
+ documentBefore: clonePrintDesignDocument(this.document),
4908
+ elementPositions: /* @__PURE__ */ new Map(),
4909
+ changed: false
4910
+ };
4911
+ this.canvas.style.cursor = "crosshair";
4912
+ return;
4913
+ }
4905
4914
  }
4906
4915
  if (!this.componentCellSelection(previewComponent))
4907
4916
  this.componentCellSelectionState = null;
@@ -5275,25 +5284,27 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
5275
5284
  if (hit && !!this.componentCellSelection(hit) && !this.options.readonly && !hit.locked && !event.altKey && !event.metaKey && !event.ctrlKey) {
5276
5285
  if (this.selectComponentCellAtPoint(hit, point, event.shiftKey)) {
5277
5286
  this.selectElements([hit.id]);
5278
- const selection = this.componentCellSelectionState;
5279
- this.canvas.setPointerCapture?.(event.pointerId);
5280
- this.dragSession = {
5281
- mode: "component-cell-select",
5282
- componentCellSelection: {
5283
- element: hit,
5284
- sourceId: hit.id,
5285
- data: this.options.data ?? {},
5286
- anchor: this.componentCellSelection(hit).dragAnchor(selection)
5287
- },
5288
- pointerId: event.pointerId,
5289
- start: point,
5290
- startClient: { x: event.clientX, y: event.clientY },
5291
- documentBefore: clonePrintDesignDocument(this.document),
5292
- elementPositions: /* @__PURE__ */ new Map(),
5293
- changed: false
5294
- };
5295
- this.canvas.style.cursor = "crosshair";
5296
- return;
5287
+ if (event.shiftKey) {
5288
+ const selection = this.componentCellSelectionState;
5289
+ this.canvas.setPointerCapture?.(event.pointerId);
5290
+ this.dragSession = {
5291
+ mode: "component-cell-select",
5292
+ componentCellSelection: {
5293
+ element: hit,
5294
+ sourceId: hit.id,
5295
+ data: this.options.data ?? {},
5296
+ anchor: this.componentCellSelection(hit).dragAnchor(selection)
5297
+ },
5298
+ pointerId: event.pointerId,
5299
+ start: point,
5300
+ startClient: { x: event.clientX, y: event.clientY },
5301
+ documentBefore: clonePrintDesignDocument(this.document),
5302
+ elementPositions: /* @__PURE__ */ new Map(),
5303
+ changed: false
5304
+ };
5305
+ this.canvas.style.cursor = "crosshair";
5306
+ return;
5307
+ }
5297
5308
  }
5298
5309
  }
5299
5310
  if (!hit) {
@@ -5873,8 +5884,8 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
5873
5884
  if (replacement) {
5874
5885
  if (replacement.id !== element.id || replacement.type !== element.type)
5875
5886
  throw new Error(`\u7EC4\u4EF6\u70B9\u51FB\u7ED3\u679C\u7C7B\u578B\u4E0D\u5339\u914D\uFF1A${element.type}`);
5876
- this.commitMutation((document2) => {
5877
- const elements = document2.pages.find(
5887
+ this.commitMutation((document) => {
5888
+ const elements = document.pages.find(
5878
5889
  (page) => page.id === this.activePageId
5879
5890
  )?.elements;
5880
5891
  const index = elements?.findIndex((item) => item.id === element.id) ?? -1;
@@ -5972,7 +5983,54 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
5972
5983
  return;
5973
5984
  }
5974
5985
  const modifier = event.metaKey || event.ctrlKey;
5975
- if (modifier && event.key.toLowerCase() === "g" && !this.options.readonly) {
5986
+ const arrow = ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(
5987
+ event.key
5988
+ );
5989
+ if (this.keyboardMoveSession && (modifier || event.altKey || ["Escape", "Delete", "Backspace"].includes(event.key)))
5990
+ this.finishKeyboardMove();
5991
+ if (arrow && !modifier && !event.altKey && this.selectedElementIds.length && !this.options.readonly && this.options.keyboardMove) {
5992
+ event.preventDefault();
5993
+ if (this.keyboardMoveSession?.event.key === event.key && !this.keyboardMoveSession.released) {
5994
+ this.keyboardMoveSession.event = event;
5995
+ return;
5996
+ }
5997
+ this.finishKeyboardMove();
5998
+ const elements = this.getActivePage().elements;
5999
+ const ids2 = new Set(
6000
+ expandGroupSelection(elements, this.selectedElementIds)
6001
+ );
6002
+ const selected = elements.filter((element) => ids2.has(element.id));
6003
+ if (!selected.length || selected.some((element) => element.locked))
6004
+ return;
6005
+ const documentBefore = clonePrintDesignDocument(this.document);
6006
+ const beforePage = this.getPageIn(documentBefore);
6007
+ this.keyboardMoveSession = {
6008
+ event,
6009
+ startedAt: Date.now(),
6010
+ lastFrameAt: this.container.ownerDocument.defaultView?.performance.now() ?? 0,
6011
+ frame: null,
6012
+ documentBefore,
6013
+ sources: new Map(
6014
+ beforePage.elements.filter((element) => ids2.has(element.id)).map((element) => [element.id, element])
6015
+ ),
6016
+ dx: 0,
6017
+ dy: 0,
6018
+ targetDx: 0,
6019
+ targetDy: 0,
6020
+ released: false,
6021
+ changed: false
6022
+ };
6023
+ const delta = this.options.keyboardMove(event, 0);
6024
+ if (delta && Number.isFinite(delta.dx) && Number.isFinite(delta.dy)) {
6025
+ this.keyboardMoveSession.targetDx = delta.dx;
6026
+ this.keyboardMoveSession.targetDy = delta.dy;
6027
+ }
6028
+ const view = this.container.ownerDocument.defaultView;
6029
+ if (view?.requestAnimationFrame)
6030
+ this.keyboardMoveSession.frame = view.requestAnimationFrame(
6031
+ this.advanceKeyboardMove
6032
+ );
6033
+ } else if (modifier && event.key.toLowerCase() === "g" && !this.options.readonly) {
5976
6034
  event.preventDefault();
5977
6035
  if (event.shiftKey) this.ungroupElements();
5978
6036
  else this.groupElements();
@@ -5996,11 +6054,17 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
5996
6054
  }
5997
6055
  });
5998
6056
  __publicField(this, "handleKeyUp", (event) => {
6057
+ if (this.keyboardMoveSession?.event.key === event.key)
6058
+ this.releaseKeyboardMove();
5999
6059
  if (event.code !== "Space" && event.key !== " ") return;
6000
6060
  event.preventDefault();
6001
6061
  this.spacePressed = false;
6002
6062
  this.canvas.style.cursor = this.dragSession ? "grabbing" : this.panMode ? "grab" : "default";
6003
6063
  });
6064
+ __publicField(this, "handleBlur", () => {
6065
+ this.spacePressed = false;
6066
+ this.finishKeyboardMove();
6067
+ });
6004
6068
  __publicField(this, "handleWheel", (event) => {
6005
6069
  if (this.dragSession) return;
6006
6070
  if (event.ctrlKey || event.metaKey) {
@@ -6039,6 +6103,78 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
6039
6103
  this.wheelPanFrame = view.requestAnimationFrame(this.advanceWheelPan);
6040
6104
  });
6041
6105
  }
6106
+ /** 长按期间只重绘画布,避免每帧提交文档并通知 React。 */
6107
+ applyKeyboardMove(dx, dy) {
6108
+ const session = this.keyboardMoveSession;
6109
+ if (!session || !dx && !dy) return;
6110
+ session.dx += dx;
6111
+ session.dy += dy;
6112
+ for (const element of this.getActivePage().elements) {
6113
+ const source = session.sources.get(element.id);
6114
+ if (!source) continue;
6115
+ element.x = Math.round((source.x + session.dx) * 100) / 100;
6116
+ element.y = Math.round((source.y + session.dy) * 100) / 100;
6117
+ this.options.componentRegistry?.get(element.type)?.translatePageCoordinates?.(
6118
+ element,
6119
+ source,
6120
+ session.dx,
6121
+ session.dy
6122
+ );
6123
+ }
6124
+ session.changed = true;
6125
+ this.render();
6126
+ }
6127
+ /** 松开按键时将整段连续移动合并为一条撤销记录。 */
6128
+ finishKeyboardMove() {
6129
+ const session = this.keyboardMoveSession;
6130
+ if (!session) return;
6131
+ this.applyKeyboardMove(
6132
+ session.targetDx - session.dx,
6133
+ session.targetDy - session.dy
6134
+ );
6135
+ this.keyboardMoveSession = null;
6136
+ if (session.frame !== null)
6137
+ this.container.ownerDocument.defaultView?.cancelAnimationFrame?.(
6138
+ session.frame
6139
+ );
6140
+ if (!session.changed) return;
6141
+ this.pushHistory(session.documentBefore);
6142
+ this.future = [];
6143
+ this.emitChange();
6144
+ }
6145
+ /** 松键后继续补完当前位移的短暂过渡。 */
6146
+ releaseKeyboardMove() {
6147
+ const session = this.keyboardMoveSession;
6148
+ if (!session) return;
6149
+ session.released = true;
6150
+ if (session.frame === null) this.finishKeyboardMove();
6151
+ }
6152
+ /** 按页面毫米坐标平移选中组件;一次操作只生成一条撤销记录。 */
6153
+ moveSelectedElements(dx, dy) {
6154
+ if (this.options.readonly || !dx && !dy) return this;
6155
+ const elements = this.getActivePage().elements;
6156
+ const ids2 = new Set(
6157
+ expandGroupSelection(elements, this.selectedElementIds)
6158
+ );
6159
+ const selected = elements.filter((element) => ids2.has(element.id));
6160
+ if (!selected.length || selected.some((element) => element.locked))
6161
+ return this;
6162
+ this.commitMutation((document) => {
6163
+ for (const element of this.getPageIn(document).elements) {
6164
+ if (!ids2.has(element.id)) continue;
6165
+ const source = selected.find((item) => item.id === element.id);
6166
+ element.x = Math.round((source.x + dx) * 100) / 100;
6167
+ element.y = Math.round((source.y + dy) * 100) / 100;
6168
+ this.options.componentRegistry?.get(element.type)?.translatePageCoordinates?.(
6169
+ element,
6170
+ source,
6171
+ dx,
6172
+ dy
6173
+ );
6174
+ }
6175
+ });
6176
+ return this;
6177
+ }
6042
6178
  /** 尺寸更新后的组件布局模式由注册项维护。 */
6043
6179
  notifyComponentResized(element, vertical) {
6044
6180
  const registry = this.options.componentRegistry ?? builtinPrintDesignComponentRegistry;
@@ -6136,8 +6272,8 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
6136
6272
  state.sourceHeight
6137
6273
  );
6138
6274
  const next = crop.width >= 0.999 && crop.height >= 0.999 ? void 0 : crop;
6139
- this.commitMutation((document2) => {
6140
- const target = this.getPageIn(document2).elements.find(
6275
+ this.commitMutation((document) => {
6276
+ const target = this.getPageIn(document).elements.find(
6141
6277
  (item) => item.id === state.elementId
6142
6278
  );
6143
6279
  if (target?.type === element.type) {
@@ -6398,6 +6534,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
6398
6534
  this.wheelPanLastTime = 0;
6399
6535
  }
6400
6536
  destroy() {
6537
+ this.finishKeyboardMove();
6401
6538
  if (this.transformRenderFrame !== null)
6402
6539
  this.container.ownerDocument.defaultView?.cancelAnimationFrame?.(
6403
6540
  this.transformRenderFrame
@@ -6552,7 +6689,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
6552
6689
  "body",
6553
6690
  source.element
6554
6691
  ));
6555
- this.canvas.style.cursor = rowBoundary ? "row-resize" : boundary ? "col-resize" : editable ? source.previewElement && this.componentCellSelection(source.previewElement) && !event.altKey ? "crosshair" : "move" : "grab";
6692
+ this.canvas.style.cursor = rowBoundary ? "row-resize" : boundary ? "col-resize" : editable ? source.previewElement && this.componentCellSelection(source.previewElement) && event.shiftKey && !event.altKey ? "crosshair" : "move" : "grab";
6556
6693
  return;
6557
6694
  }
6558
6695
  const rect = this.canvas.getBoundingClientRect();
@@ -6767,7 +6904,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
6767
6904
  this.canvas.style.cursor = "col-resize";
6768
6905
  return;
6769
6906
  }
6770
- this.canvas.style.cursor = hit && this.componentCellSelection(hit) && !hit.locked && !event.altKey ? "crosshair" : hit && !hit.locked ? "move" : "default";
6907
+ this.canvas.style.cursor = hit && this.componentCellSelection(hit) && !hit.locked && event.shiftKey && !event.altKey ? "crosshair" : hit && !hit.locked ? "move" : "default";
6771
6908
  }
6772
6909
  removeSelectedGuide() {
6773
6910
  const selected = this.selectedGuide;
@@ -6879,8 +7016,8 @@ var PrintDesignGrouping = class extends PrintDesignInteraction {
6879
7016
  renameGroup(groupId, name) {
6880
7017
  const normalized = name.trim();
6881
7018
  if (this.options.readonly || !normalized) return this;
6882
- this.commitMutation((document2) => {
6883
- for (const element of this.getPageIn(document2).elements) {
7019
+ this.commitMutation((document) => {
7020
+ for (const element of this.getPageIn(document).elements) {
6884
7021
  const group = element.groupPath?.find((item) => item.id === groupId);
6885
7022
  if (group) group.name = normalized;
6886
7023
  }
@@ -6891,8 +7028,8 @@ var PrintDesignGrouping = class extends PrintDesignInteraction {
6891
7028
  moveLayerRelative(elementId, targetId, after, depth = 0) {
6892
7029
  if (this.options.readonly || !Number.isInteger(depth) || depth < 0)
6893
7030
  return this;
6894
- this.commitMutation((document2) => {
6895
- const page = this.getPageIn(document2);
7031
+ this.commitMutation((document) => {
7032
+ const page = this.getPageIn(document);
6896
7033
  const source = page.elements.find((item) => item.id === elementId);
6897
7034
  const target = page.elements.find((item) => item.id === targetId);
6898
7035
  if (!source || !target) return;
@@ -6928,8 +7065,8 @@ var PrintDesignGrouping = class extends PrintDesignInteraction {
6928
7065
  );
6929
7066
  if (units.size < 2 || members.some((item) => item.locked)) return this;
6930
7067
  const group = { id: createPrintDesignId("group"), name: "\u7EC4\u5408" };
6931
- this.commitMutation((document2) => {
6932
- const page = this.getPageIn(document2);
7068
+ this.commitMutation((document) => {
7069
+ const page = this.getPageIn(document);
6933
7070
  const selected = page.elements.filter((item) => ids2.includes(item.id));
6934
7071
  const highest = Math.max(
6935
7072
  ...selected.map((item) => page.elements.indexOf(item))
@@ -6951,8 +7088,8 @@ var PrintDesignGrouping = class extends PrintDesignInteraction {
6951
7088
  (item) => ids2.includes(item.id) && item.locked
6952
7089
  ))
6953
7090
  return this;
6954
- this.commitMutation((document2) => {
6955
- for (const item of this.getPageIn(document2).elements) {
7091
+ this.commitMutation((document) => {
7092
+ for (const item of this.getPageIn(document).elements) {
6956
7093
  if (!ids2.includes(item.id) || !item.groupPath?.length) continue;
6957
7094
  item.groupPath = item.groupPath.slice(1);
6958
7095
  if (!item.groupPath.length) delete item.groupPath;
@@ -7124,8 +7261,8 @@ var PrintDesignActions = class extends PrintDesignGrouping {
7124
7261
  if (!element || element.locked) return this;
7125
7262
  const commands = this.componentCommands(element);
7126
7263
  if (!commands) return this;
7127
- this.commitMutation((document2) => {
7128
- const page = this.getPageIn(document2);
7264
+ this.commitMutation((document) => {
7265
+ const page = this.getPageIn(document);
7129
7266
  const index = page.elements.findIndex((item) => item.id === elementId);
7130
7267
  if (index < 0) return;
7131
7268
  const target = page.elements[index];
@@ -7244,8 +7381,8 @@ var PrintDesignActions = class extends PrintDesignGrouping {
7244
7381
  this.options.componentRegistry?.get(item.type)?.translatePageCoordinates?.(copy, item, 5, 5);
7245
7382
  return copy;
7246
7383
  });
7247
- this.commitMutation((document2) => {
7248
- this.getPageIn(document2).elements.push(...copies);
7384
+ this.commitMutation((document) => {
7385
+ this.getPageIn(document).elements.push(...copies);
7249
7386
  });
7250
7387
  return this.selectElements(copies.map((item) => item.id));
7251
7388
  }
@@ -7263,8 +7400,8 @@ var PrintDesignActions = class extends PrintDesignGrouping {
7263
7400
  this.selectedElementIds
7264
7401
  )
7265
7402
  );
7266
- this.commitMutation((document2) => {
7267
- for (const item of this.getPageIn(document2).elements)
7403
+ this.commitMutation((document) => {
7404
+ for (const item of this.getPageIn(document).elements)
7268
7405
  if (ids2.has(item.id)) Object.assign(item, patch);
7269
7406
  });
7270
7407
  return this;
@@ -7273,8 +7410,8 @@ var PrintDesignActions = class extends PrintDesignGrouping {
7273
7410
  setElementsState(elementIds, patch) {
7274
7411
  if (this.options.readonly) return this;
7275
7412
  const ids2 = new Set(elementIds);
7276
- this.commitMutation((document2) => {
7277
- for (const item of this.getPageIn(document2).elements)
7413
+ this.commitMutation((document) => {
7414
+ for (const item of this.getPageIn(document).elements)
7278
7415
  if (ids2.has(item.id)) Object.assign(item, patch);
7279
7416
  });
7280
7417
  return this;
@@ -7282,8 +7419,8 @@ var PrintDesignActions = class extends PrintDesignGrouping {
7282
7419
  /** 全部解锁或显示当前页元素。 */
7283
7420
  setAllElementState(patch) {
7284
7421
  if (this.options.readonly) return this;
7285
- this.commitMutation((document2) => {
7286
- for (const item of this.getPageIn(document2).elements)
7422
+ this.commitMutation((document) => {
7423
+ for (const item of this.getPageIn(document).elements)
7287
7424
  Object.assign(item, patch);
7288
7425
  });
7289
7426
  return this;
@@ -7293,8 +7430,8 @@ var PrintDesignActions = class extends PrintDesignGrouping {
7293
7430
  if (this.options.readonly || this.selectedElements().some((item) => item.locked))
7294
7431
  return this;
7295
7432
  const ids2 = new Set(this.selectedElementIds);
7296
- this.commitMutation((document2) => {
7297
- const page = this.getPageIn(document2);
7433
+ this.commitMutation((document) => {
7434
+ const page = this.getPageIn(document);
7298
7435
  const units = [];
7299
7436
  const seen = /* @__PURE__ */ new Set();
7300
7437
  for (const item of page.elements) {
@@ -7326,9 +7463,9 @@ var PrintDesignActions = class extends PrintDesignGrouping {
7326
7463
  /** 单个图层/组合对齐纸张,多选对齐选区;每次操作提交一条历史。 */
7327
7464
  alignSelection(align) {
7328
7465
  if (this.options.readonly) return this;
7329
- this.commitMutation((document2) => {
7466
+ this.commitMutation((document) => {
7330
7467
  alignPrintDesignLayers(
7331
- this.getPageIn(document2),
7468
+ this.getPageIn(document),
7332
7469
  this.selectedElementIds,
7333
7470
  align,
7334
7471
  this.options.data,
@@ -7346,7 +7483,7 @@ var PrintDesign = class extends PrintDesignActions {
7346
7483
  __publicField(this, "temporaryDocumentSnapshot", null);
7347
7484
  }
7348
7485
  /** 临时显示演示文档;原文档、选区与撤销记录保留在实例中。 */
7349
- showTemporaryDocument(document2, pageId, elementId) {
7486
+ showTemporaryDocument(document, pageId, elementId) {
7350
7487
  this.temporaryDocumentSnapshot ?? (this.temporaryDocumentSnapshot = {
7351
7488
  document: this.getDocument(),
7352
7489
  activePageId: this.activePageId,
@@ -7354,7 +7491,7 @@ var PrintDesign = class extends PrintDesignActions {
7354
7491
  guide: this.selectedGuide ? { ...this.selectedGuide } : null
7355
7492
  });
7356
7493
  this.document = parsePrintDesignDocument(
7357
- document2,
7494
+ document,
7358
7495
  this.options.componentRegistry ?? builtinPrintDesignComponentRegistry
7359
7496
  );
7360
7497
  this.activePageId = pageId && this.document.pages.some((page) => page.id === pageId) ? pageId : this.document.pages[0].id;
@@ -7386,10 +7523,10 @@ var PrintDesign = class extends PrintDesignActions {
7386
7523
  return clonePrintDesignDocument(this.document);
7387
7524
  }
7388
7525
  /** 替换当前文档并清空撤销历史。 */
7389
- setDocument(document2) {
7526
+ setDocument(document) {
7390
7527
  this.temporaryDocumentSnapshot = null;
7391
7528
  this.document = parsePrintDesignDocument(
7392
- document2,
7529
+ document,
7393
7530
  this.options.componentRegistry ?? builtinPrintDesignComponentRegistry
7394
7531
  );
7395
7532
  this.activePageId = this.document.pages[0].id;
@@ -7473,16 +7610,16 @@ var PrintDesign = class extends PrintDesignActions {
7473
7610
  }
7474
7611
  /** 新增空白页面并切换到该页。 */
7475
7612
  addPage(page = createPrintDesignPage(`\u9875\u9762 ${this.document.pages.length + 1}`)) {
7476
- this.commitMutation((document2) => {
7477
- document2.pages.push(clonePrintDesignDocument(page));
7613
+ this.commitMutation((document) => {
7614
+ document.pages.push(clonePrintDesignDocument(page));
7478
7615
  });
7479
7616
  this.setActivePage(page.id);
7480
7617
  return page.id;
7481
7618
  }
7482
7619
  /** 更新页面纸张、页边距或背景属性。 */
7483
7620
  updatePage(pageId, patch) {
7484
- this.commitMutation((document2) => {
7485
- const page = document2.pages.find((item) => item.id === pageId);
7621
+ this.commitMutation((document) => {
7622
+ const page = document.pages.find((item) => item.id === pageId);
7486
7623
  if (page)
7487
7624
  Object.assign(page, patch, { id: page.id, elements: page.elements });
7488
7625
  });
@@ -7491,8 +7628,8 @@ var PrintDesign = class extends PrintDesignActions {
7491
7628
  /** 删除页面;文档至少保留一页。 */
7492
7629
  removePage(pageId) {
7493
7630
  if (this.document.pages.length <= 1) return this;
7494
- this.commitMutation((document2) => {
7495
- document2.pages = document2.pages.filter((page) => page.id !== pageId);
7631
+ this.commitMutation((document) => {
7632
+ document.pages = document.pages.filter((page) => page.id !== pageId);
7496
7633
  });
7497
7634
  if (this.activePageId === pageId)
7498
7635
  this.setActivePage(this.document.pages[0].id);
@@ -7522,8 +7659,8 @@ var PrintDesign = class extends PrintDesignActions {
7522
7659
  return this;
7523
7660
  const guides = this.getActivePage().guides?.[axis];
7524
7661
  if (!guides || index < 0 || index >= guides.length) return this;
7525
- this.commitMutation((document2) => {
7526
- this.getPageIn(document2).guides[axis][index] = position;
7662
+ this.commitMutation((document) => {
7663
+ this.getPageIn(document).guides[axis][index] = position;
7527
7664
  });
7528
7665
  return this;
7529
7666
  }
@@ -7563,6 +7700,15 @@ var PrintDesign = class extends PrintDesignActions {
7563
7700
  y: (y - origin.y) / origin.scale
7564
7701
  };
7565
7702
  }
7703
+ /** 将当前页面的毫米坐标转换为浏览器视口坐标。 */
7704
+ pageToClientPoint(x, y) {
7705
+ const origin = this.pageOrigin();
7706
+ const canvas = this.canvas.getBoundingClientRect();
7707
+ return {
7708
+ x: canvas.left + origin.x + x * origin.scale,
7709
+ y: canvas.top + origin.y + y * origin.scale
7710
+ };
7711
+ }
7566
7712
  /** 获取当前页元素在浏览器视口中的包围盒,包含旋转后的占用范围。 */
7567
7713
  getElementClientRect(elementId) {
7568
7714
  const element = this.getActivePage().elements.find(
@@ -7586,8 +7732,8 @@ var PrintDesign = class extends PrintDesignActions {
7586
7732
  /** 新增元素并选中。 */
7587
7733
  addElement(element) {
7588
7734
  const next = { ...element, id: element.id ?? createPrintDesignId() };
7589
- this.commitMutation((document2) => {
7590
- this.getPageIn(document2).elements.push(
7735
+ this.commitMutation((document) => {
7736
+ this.getPageIn(document).elements.push(
7591
7737
  clonePrintDesignDocument(next)
7592
7738
  );
7593
7739
  });
@@ -7604,8 +7750,8 @@ var PrintDesign = class extends PrintDesignActions {
7604
7750
  );
7605
7751
  const beforeCropBehavior = beforeElement ? this.options.componentRegistry?.get(beforeElement.type)?.crop : void 0;
7606
7752
  const previousSource = beforeElement && beforeCropBehavior?.source(beforeElement);
7607
- this.commitMutation((document2) => {
7608
- const page = this.getPageIn(document2);
7753
+ this.commitMutation((document) => {
7754
+ const page = this.getPageIn(document);
7609
7755
  const element = page.elements.find((item) => item.id === elementId);
7610
7756
  if (element) {
7611
7757
  this.options.componentRegistry?.get(element.type)?.beforeUpdate?.(element, patch);
@@ -7615,7 +7761,7 @@ var PrintDesign = class extends PrintDesignActions {
7615
7761
  )?.onUpdate;
7616
7762
  if (onUpdate)
7617
7763
  onUpdate(element, patch, {
7618
- document: document2,
7764
+ document,
7619
7765
  page,
7620
7766
  data: this.options.data ?? {}
7621
7767
  });
@@ -7633,8 +7779,8 @@ var PrintDesign = class extends PrintDesignActions {
7633
7779
  if (!page || !current || current.type !== component.type || cropBehavior.source(current) !== src || current.dataConfig?.mode === "binding" || current.width !== width || current.height !== height || !source?.width || !source.height)
7634
7780
  return;
7635
7781
  const bounds = cropBehavior.fitToPage(source, page, current);
7636
- this.commitMutation((document2) => {
7637
- const target = document2.pages.find((item) => item.id === pageId)?.elements.find((item) => item.id === elementId);
7782
+ this.commitMutation((document) => {
7783
+ const target = document.pages.find((item) => item.id === pageId)?.elements.find((item) => item.id === elementId);
7638
7784
  if (target?.type === component.type && cropBehavior.source(target) === src)
7639
7785
  Object.assign(target, bounds);
7640
7786
  });
@@ -7659,8 +7805,8 @@ var PrintDesign = class extends PrintDesignActions {
7659
7805
  (element) => ids2.has(element.id) && !element.locked && this.hasElementTransform(element)
7660
7806
  ))
7661
7807
  return this;
7662
- this.commitMutation((document2) => {
7663
- for (const element of this.getPageIn(document2).elements) {
7808
+ this.commitMutation((document) => {
7809
+ for (const element of this.getPageIn(document).elements) {
7664
7810
  if (!ids2.has(element.id) || element.locked) continue;
7665
7811
  element.transform = void 0;
7666
7812
  this.options.componentRegistry?.get(element.type)?.transformReset?.reset(element);
@@ -7674,8 +7820,8 @@ var PrintDesign = class extends PrintDesignActions {
7674
7820
  const ids2 = new Set(
7675
7821
  expandGroupSelection(this.getActivePage().elements, elementIds)
7676
7822
  );
7677
- this.commitMutation((document2) => {
7678
- const page = this.getPageIn(document2);
7823
+ this.commitMutation((document) => {
7824
+ const page = this.getPageIn(document);
7679
7825
  const componentRegistry = this.options.componentRegistry ?? builtinPrintDesignComponentRegistry;
7680
7826
  for (const element of page.elements) {
7681
7827
  if (ids2.has(element.id)) continue;
@@ -7711,8 +7857,8 @@ var PrintDesign = class extends PrintDesignActions {
7711
7857
  /** 设置元素层级,索引从 0(底层)开始;越界值限制到当前页面范围。 */
7712
7858
  setElementLayer(elementId, layerIndex) {
7713
7859
  if (this.options.readonly || !Number.isInteger(layerIndex)) return this;
7714
- this.commitMutation((document2) => {
7715
- const elements = this.getPageIn(document2).elements;
7860
+ this.commitMutation((document) => {
7861
+ const elements = this.getPageIn(document).elements;
7716
7862
  const index = elements.findIndex((element) => element.id === elementId);
7717
7863
  if (index < 0) return;
7718
7864
  const target = Math.max(0, Math.min(elements.length - 1, layerIndex));
@@ -7725,7 +7871,7 @@ var PrintDesign = class extends PrintDesignActions {
7725
7871
  const insertion = elements.slice(0, boundary).filter((item) => !memberIds.has(item.id)).length;
7726
7872
  const remaining = elements.filter((item) => !memberIds.has(item.id));
7727
7873
  remaining.splice(insertion, 0, ...members);
7728
- this.getPageIn(document2).elements = remaining;
7874
+ this.getPageIn(document).elements = remaining;
7729
7875
  });
7730
7876
  return this;
7731
7877
  }
@@ -7914,7 +8060,7 @@ var idsSchema = { type: "array", items: idSchema, minItems: 1 };
7914
8060
  var printDesignAgentTools = [
7915
8061
  {
7916
8062
  name: "get_document",
7917
- description: "\u8BFB\u53D6\u5F53\u524D\u6253\u5370\u8BBE\u8BA1\u6587\u6863\u3002\u9875\u9762\u5C3A\u5BF8\u548C\u5143\u7D20\u5750\u6807\u5355\u4F4D\u4E3A\u6BEB\u7C73\u3002",
8063
+ description: "\u8BFB\u53D6\u5F53\u524D\u6253\u5370\u8BBE\u8BA1\u6587\u6863\u3002\u9875\u9762\u5C3A\u5BF8\u548C\u5143\u7D20\u5750\u6807\u4F7F\u7528\u8FD4\u56DE\u7684 displayUnit\u3002",
7918
8064
  parameters: objectSchema({})
7919
8065
  },
7920
8066
  {
@@ -7929,14 +8075,14 @@ var printDesignAgentTools = [
7929
8075
  },
7930
8076
  {
7931
8077
  name: "get_component_template",
7932
- description: "\u8BFB\u53D6\u5DF2\u6CE8\u518C\u7EC4\u4EF6\u7684\u9ED8\u8BA4\u5143\u7D20\u548C\u53EF\u7528\u547D\u4EE4\uFF1B\u521B\u5EFA\u6216\u4FEE\u6539\u590D\u6742\u7EC4\u4EF6\u524D\u5148\u67E5\u770B\u3002",
8078
+ description: "\u8BFB\u53D6\u5DF2\u6CE8\u518C\u7EC4\u4EF6\u7684\u9ED8\u8BA4\u5143\u7D20\u3001\u53EF\u7528\u547D\u4EE4\u53CA\u5176 payload \u53C2\u6570\u683C\u5F0F\uFF1B\u521B\u5EFA\u6216\u4FEE\u6539\u590D\u6742\u7EC4\u4EF6\u524D\u5148\u67E5\u770B\u3002",
7933
8079
  parameters: objectSchema({ type: idSchema, preset: { type: "string" } }, [
7934
8080
  "type"
7935
8081
  ])
7936
8082
  },
7937
8083
  {
7938
8084
  name: "get_element",
7939
- description: "\u8BFB\u53D6\u5F53\u524D\u9875\u6307\u5B9A\u5143\u7D20\u7684\u5B8C\u6574\u914D\u7F6E\u3002",
8085
+ description: "\u8BFB\u53D6\u5F53\u524D\u9875\u6307\u5B9A\u5143\u7D20\u7684\u914D\u7F6E\uFF1B\u4F4D\u7F6E\u548C\u5C3A\u5BF8\u4F7F\u7528\u5168\u5C40\u663E\u793A\u5355\u4F4D\u3002",
7940
8086
  parameters: objectSchema({ elementId: idSchema }, ["elementId"])
7941
8087
  },
7942
8088
  {
@@ -7951,12 +8097,12 @@ var printDesignAgentTools = [
7951
8097
  },
7952
8098
  {
7953
8099
  name: "add_element",
7954
- description: "\u5728\u5F53\u524D\u9875\u65B0\u589E\u5143\u7D20\u3002\u4F20\u5165 type \u548C\u9700\u8981\u8986\u76D6\u7684\u5B57\u6BB5\uFF1B\u5DF2\u6CE8\u518C\u7EC4\u4EF6\u4F1A\u81EA\u52A8\u8865\u9F50\u5DE5\u5382\u9ED8\u8BA4\u503C\uFF0C\u590D\u6742\u7EC4\u4EF6\u8BF7\u5148\u8BFB\u53D6\u6A21\u677F\u3002",
8100
+ description: "\u5728\u5F53\u524D\u9875\u65B0\u589E\u5143\u7D20\u3002\u4F4D\u7F6E\u548C\u5C3A\u5BF8\u4F7F\u7528\u5168\u5C40\u663E\u793A\u5355\u4F4D\uFF1B\u5DF2\u6CE8\u518C\u7EC4\u4EF6\u4F1A\u8865\u9F50\u9ED8\u8BA4\u503C\uFF0C\u590D\u6742\u7EC4\u4EF6\u8BF7\u5148\u8BFB\u53D6\u6A21\u677F\u3002",
7955
8101
  parameters: objectSchema({ element: { type: "object" } }, ["element"])
7956
8102
  },
7957
8103
  {
7958
8104
  name: "update_element",
7959
- description: "\u66F4\u65B0\u5F53\u524D\u9875\u6307\u5B9A\u5143\u7D20\u7684\u5C5E\u6027\uFF1B\u4E0D\u80FD\u4FEE\u6539 id \u6216 type\u3002",
8105
+ description: "\u66F4\u65B0\u5F53\u524D\u9875\u6307\u5B9A\u5143\u7D20\u7684\u5C5E\u6027\uFF1B\u4F4D\u7F6E\u548C\u5C3A\u5BF8\u4F7F\u7528\u5168\u5C40\u663E\u793A\u5355\u4F4D\uFF0C\u4E0D\u80FD\u4FEE\u6539 id \u6216 type\u3002",
7960
8106
  parameters: objectSchema(
7961
8107
  { elementId: idSchema, patch: { type: "object" } },
7962
8108
  ["elementId", "patch"]
@@ -8025,6 +8171,48 @@ function safeJson(input, depth = 0) {
8025
8171
  safeJson(value, depth + 1);
8026
8172
  }
8027
8173
  }
8174
+ var geometryKeys = ["x", "y", "width", "height"];
8175
+ function convertGeometry(value, unit, convert) {
8176
+ const result = {
8177
+ ...value
8178
+ };
8179
+ for (const key of geometryKeys)
8180
+ if (typeof result[key] === "number")
8181
+ result[key] = convert(result[key], unit);
8182
+ return result;
8183
+ }
8184
+ function documentInDisplayUnit(document, unit) {
8185
+ return {
8186
+ ...document,
8187
+ displayUnit: unit,
8188
+ pages: document.pages.map((page) => ({
8189
+ ...page,
8190
+ width: toDisplayUnit(page.width, unit),
8191
+ height: toDisplayUnit(page.height, unit),
8192
+ ...page.margins ? {
8193
+ margins: Object.fromEntries(
8194
+ Object.entries(page.margins).map(([key, value]) => [
8195
+ key,
8196
+ toDisplayUnit(value, unit)
8197
+ ])
8198
+ )
8199
+ } : {},
8200
+ ...page.guides ? {
8201
+ guides: {
8202
+ vertical: page.guides.vertical?.map(
8203
+ (value) => toDisplayUnit(value, unit)
8204
+ ),
8205
+ horizontal: page.guides.horizontal?.map(
8206
+ (value) => toDisplayUnit(value, unit)
8207
+ )
8208
+ }
8209
+ } : {},
8210
+ elements: page.elements.map(
8211
+ (element) => convertGeometry(element, unit, toDisplayUnit)
8212
+ )
8213
+ }))
8214
+ };
8215
+ }
8028
8216
  function createPrintDesignAgentApi(getInstance, options = {}) {
8029
8217
  return {
8030
8218
  tools: printDesignAgentTools,
@@ -8033,7 +8221,9 @@ function createPrintDesignAgentApi(getInstance, options = {}) {
8033
8221
  if (!instance) throw new Error("\u6253\u5370\u8BBE\u8BA1\u5668\u5C1A\u672A\u5C31\u7EEA\u3002");
8034
8222
  const args = record(input);
8035
8223
  safeJson(args);
8036
- if (name === "get_document") return instance.getDocument();
8224
+ const unit = options.displayUnit?.() ?? "mm";
8225
+ if (name === "get_document")
8226
+ return documentInDisplayUnit(instance.getDocument(), unit);
8037
8227
  if (name === "get_selection") return instance.getSelection();
8038
8228
  if (name === "list_components")
8039
8229
  return options.componentCatalog?.list().map(({ type, label }) => ({
@@ -8053,7 +8243,8 @@ function createPrintDesignAgentApi(getInstance, options = {}) {
8053
8243
  return {
8054
8244
  type,
8055
8245
  label: factory.label,
8056
- element,
8246
+ element: convertGeometry(element, unit, toDisplayUnit),
8247
+ displayUnit: unit,
8057
8248
  commands: factory.agentCommands ?? []
8058
8249
  };
8059
8250
  }
@@ -8061,16 +8252,16 @@ function createPrintDesignAgentApi(getInstance, options = {}) {
8061
8252
  const elementId = string(args.elementId, "elementId");
8062
8253
  const found = instance.getDocument().pages.find((item) => item.id === instance.getActivePageId())?.elements.find((item) => item.id === elementId);
8063
8254
  if (!found) throw new Error(`\u627E\u4E0D\u5230\u5143\u7D20\uFF1A${elementId}`);
8064
- return found;
8255
+ return convertGeometry(found, unit, toDisplayUnit);
8065
8256
  }
8066
8257
  if (options.readonly?.()) throw new Error("\u5F53\u524D\u8BBE\u8BA1\u5668\u4E3A\u53EA\u8BFB\u72B6\u6001\u3002");
8067
- const document2 = instance.getDocument();
8068
- const page = document2.pages.find(
8258
+ const document = instance.getDocument();
8259
+ const page = document.pages.find(
8069
8260
  (item) => item.id === instance.getActivePageId()
8070
8261
  );
8071
8262
  if (name === "set_active_page") {
8072
8263
  const pageId = string(args.pageId, "pageId");
8073
- if (!document2.pages.some((item) => item.id === pageId))
8264
+ if (!document.pages.some((item) => item.id === pageId))
8074
8265
  throw new Error(`\u627E\u4E0D\u5230\u9875\u9762\uFF1A${pageId}`);
8075
8266
  instance.setActivePage(pageId);
8076
8267
  return instance.getSelection();
@@ -8092,7 +8283,7 @@ function createPrintDesignAgentApi(getInstance, options = {}) {
8092
8283
  const { id: _defaultId, ...defaultFields } = defaults ?? {};
8093
8284
  const id = instance.addElement({
8094
8285
  ...defaultFields,
8095
- ...fields
8286
+ ...convertGeometry(fields, unit, fromDisplayUnit)
8096
8287
  });
8097
8288
  return { id };
8098
8289
  }
@@ -8104,7 +8295,14 @@ function createPrintDesignAgentApi(getInstance, options = {}) {
8104
8295
  const patch = record(args.patch);
8105
8296
  if ("id" in patch || "type" in patch)
8106
8297
  throw new TypeError("\u4E0D\u80FD\u4FEE\u6539\u5143\u7D20 id \u6216 type\u3002");
8107
- instance.updateElement(elementId, patch);
8298
+ instance.updateElement(
8299
+ elementId,
8300
+ convertGeometry(
8301
+ patch,
8302
+ unit,
8303
+ fromDisplayUnit
8304
+ )
8305
+ );
8108
8306
  return { elementId };
8109
8307
  }
8110
8308
  if (name === "remove_elements") {
@@ -8155,7 +8353,7 @@ function isPrintDesignRecordCollection(value) {
8155
8353
  }
8156
8354
 
8157
8355
  // package.json
8158
- var version = "0.1.11";
8356
+ var version = "0.1.12";
8159
8357
 
8160
8358
  exports.DEFAULT_PRINT_DESIGN_DEBUG_OPTIONS = DEFAULT_PRINT_DESIGN_DEBUG_OPTIONS;
8161
8359
  exports.PRINT_DESIGN_CSS_DPI = PRINT_DESIGN_CSS_DPI;
@@ -8189,11 +8387,8 @@ exports.formatDisplayUnit = formatDisplayUnit;
8189
8387
  exports.fromDisplayUnit = fromDisplayUnit;
8190
8388
  exports.getPrintDesignAlignmentUnits = getPrintDesignAlignmentUnits;
8191
8389
  exports.getPrintDesignDataFields = getPrintDesignDataFields;
8192
- exports.getPrintDesignDefaultFontUrl = getPrintDesignDefaultFontUrl;
8193
8390
  exports.getPrintDesignElementDataConfig = getPrintDesignElementDataConfig;
8194
- exports.getPrintDesignLatinFontUrl = getPrintDesignLatinFontUrl;
8195
8391
  exports.getPrintDesignPageSize = getPrintDesignPageSize;
8196
- exports.getPrintDesignSerifFontUrl = getPrintDesignSerifFontUrl;
8197
8392
  exports.getSelectionRect = getSelectionRect;
8198
8393
  exports.hitTestPrintDesignPage = hitTestPrintDesignPage;
8199
8394
  exports.inverseTransformPoint = inverseTransformPoint;
@@ -8205,7 +8400,6 @@ exports.paginatePrintDesignDocument = paginatePrintDesignDocument;
8205
8400
  exports.parsePrintDesignDocument = parsePrintDesignDocument;
8206
8401
  exports.pixelsToMillimeters = pixelsToMillimeters;
8207
8402
  exports.preparePrintDesignCanvasPage = preparePrintDesignCanvasPage;
8208
- exports.preparePrintDesignDefaultFonts = preparePrintDesignDefaultFonts;
8209
8403
  exports.printDesignAgentTools = printDesignAgentTools;
8210
8404
  exports.printPrintDesignHtml = printPrintDesignHtml;
8211
8405
  exports.printPrintDesignHtmlInFrame = printPrintDesignHtmlInFrame;
@@ -8218,6 +8412,7 @@ exports.resolvePrintDesignDataPath = resolvePrintDesignDataPath;
8218
8412
  exports.resolvePrintDesignDebugOptions = resolvePrintDesignDebugOptions;
8219
8413
  exports.resolvePrintDesignElementData = resolvePrintDesignElementData;
8220
8414
  exports.resolvePrintDesignElementVisible = resolvePrintDesignElementVisible;
8415
+ exports.resolvePrintDesignFontFamily = resolvePrintDesignFontFamily;
8221
8416
  exports.resolvePrintDesignPageVisible = resolvePrintDesignPageVisible;
8222
8417
  exports.rotatePoint = rotatePoint2;
8223
8418
  exports.serializePrintDesignDocument = serializePrintDesignDocument;