@canvas-components/print-design 0.1.10 → 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/README.md +2 -0
- package/dist/index.cjs +693 -249
- package/dist/index.d.cts +74 -12
- package/dist/index.d.ts +74 -12
- package/dist/index.js +691 -245
- package/package.json +2 -2
- package/dist/font-assets/Arimo-Bold.ttf +0 -0
- package/dist/font-assets/Arimo-OFL.txt +0 -93
- package/dist/font-assets/Arimo-Regular.ttf +0 -0
- package/dist/font-assets/NotoSansSC-Bold.ttf +0 -0
- package/dist/font-assets/NotoSansSC-Regular.ttf +0 -0
- package/dist/font-assets/OFL.txt +0 -93
- package/dist/font-assets/Tinos-Bold.ttf +0 -0
- package/dist/font-assets/Tinos-OFL.txt +0 -93
- package/dist/font-assets/Tinos-Regular.ttf +0 -0
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(
|
|
224
|
+
function serializePrintDesignDocument(document, componentRegistry) {
|
|
226
225
|
return JSON.stringify(
|
|
227
|
-
componentRegistry ? parsePrintDesignDocument(
|
|
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
|
|
472
|
-
const canvas =
|
|
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(
|
|
868
|
-
const page =
|
|
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 ??
|
|
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:
|
|
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(
|
|
1458
|
-
return paginateDocument(
|
|
1410
|
+
function paginatePrintDesignDocument(document, data = {}, componentRegistry) {
|
|
1411
|
+
return paginateDocument(document, data, componentRegistry);
|
|
1459
1412
|
}
|
|
1460
|
-
function materializePrintDesignDocument(
|
|
1413
|
+
function materializePrintDesignDocument(document, data, componentRegistry) {
|
|
1461
1414
|
return componentRegistry?.list().reduce(
|
|
1462
1415
|
(current, renderer) => renderer.materializeDocument ? renderer.materializeDocument(current, data) : current,
|
|
1463
|
-
|
|
1464
|
-
) ??
|
|
1416
|
+
document
|
|
1417
|
+
) ?? document;
|
|
1465
1418
|
}
|
|
1466
|
-
function paginateDocument(
|
|
1419
|
+
function paginateDocument(document, data, componentRegistry) {
|
|
1467
1420
|
const resolvedDocument = materializePrintDesignDocument(
|
|
1468
|
-
|
|
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
|
-
|
|
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 { ...
|
|
1560
|
+
return { ...document, pages };
|
|
1608
1561
|
}
|
|
1609
|
-
function resolveContentHeightPage(
|
|
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
|
-
...
|
|
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(
|
|
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 =
|
|
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
|
-
}) :
|
|
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
|
-
|
|
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
|
-
{ ...
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
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(
|
|
1910
|
-
const task = generatePrintHtml(
|
|
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(
|
|
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 =
|
|
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
|
-
}) :
|
|
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
|
-
{ ...
|
|
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
|
-
|
|
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(
|
|
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(
|
|
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 =
|
|
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
|
|
1928
|
+
for (const page of document.pages) {
|
|
1970
1929
|
options.signal?.throwIfAborted();
|
|
1971
1930
|
totalPages += paginatePrintDesignDocument(
|
|
1972
|
-
{ ...
|
|
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
|
-
|
|
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 <
|
|
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
|
-
{ ...
|
|
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
|
-
|
|
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 ||
|
|
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) /
|
|
1991
|
+
options.onProgress?.((index + 1) / document.pages.length);
|
|
2033
1992
|
}
|
|
2034
1993
|
if (buffer.length)
|
|
2035
1994
|
yield {
|
|
@@ -2042,8 +2001,8 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
|
|
|
2042
2001
|
}
|
|
2043
2002
|
|
|
2044
2003
|
// src/helper/group-resize.ts
|
|
2045
|
-
function groupSelectionBounds(elements,
|
|
2046
|
-
const boxes = elements.filter((item) =>
|
|
2004
|
+
function groupSelectionBounds(elements, ids2, data = {}, componentRegistry) {
|
|
2005
|
+
const boxes = elements.filter((item) => ids2.includes(item.id) && item.visible !== false).map((element) => elementBounds(element, data, componentRegistry));
|
|
2047
2006
|
if (boxes.length < 2) return;
|
|
2048
2007
|
const x = Math.min(...boxes.map((box) => box.x)), y = Math.min(...boxes.map((box) => box.y));
|
|
2049
2008
|
return {
|
|
@@ -2172,9 +2131,9 @@ function groupMembers(elements, id) {
|
|
|
2172
2131
|
const groupId = element.groupPath?.[0]?.id;
|
|
2173
2132
|
return groupId ? elements.filter((item) => item.groupPath?.[0]?.id === groupId) : [element];
|
|
2174
2133
|
}
|
|
2175
|
-
function expandGroupSelection(elements,
|
|
2134
|
+
function expandGroupSelection(elements, ids2) {
|
|
2176
2135
|
const selected = new Set(
|
|
2177
|
-
|
|
2136
|
+
ids2.flatMap((id) => groupMembers(elements, id).map((item) => item.id))
|
|
2178
2137
|
);
|
|
2179
2138
|
return elements.filter((item) => selected.has(item.id)).map((item) => item.id);
|
|
2180
2139
|
}
|
|
@@ -2525,12 +2484,12 @@ var PrintDesignBase = class {
|
|
|
2525
2484
|
return sourceIndex?.(element, selector) ?? selector.index;
|
|
2526
2485
|
}
|
|
2527
2486
|
/** 由组件注册项命中内部单元。 */
|
|
2528
|
-
hitComponentCell(element, point,
|
|
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
|
|
2492
|
+
document,
|
|
2534
2493
|
data
|
|
2535
2494
|
});
|
|
2536
2495
|
}
|
|
@@ -2734,8 +2693,8 @@ var PrintDesignBase = class {
|
|
|
2734
2693
|
})
|
|
2735
2694
|
};
|
|
2736
2695
|
}
|
|
2737
|
-
getPageIn(
|
|
2738
|
-
return
|
|
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(
|
|
2756
|
-
this.history.push(
|
|
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
|
|
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
|
|
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((
|
|
3151
|
-
const page =
|
|
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((
|
|
3294
|
-
const target =
|
|
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,35 +4888,37 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
4881
4888
|
preview.data
|
|
4882
4889
|
)) {
|
|
4883
4890
|
this.selectElements([source.element.id]);
|
|
4884
|
-
|
|
4885
|
-
|
|
4886
|
-
|
|
4887
|
-
|
|
4888
|
-
|
|
4889
|
-
|
|
4890
|
-
|
|
4891
|
-
|
|
4892
|
-
|
|
4893
|
-
|
|
4894
|
-
|
|
4895
|
-
|
|
4896
|
-
|
|
4897
|
-
|
|
4898
|
-
|
|
4899
|
-
|
|
4900
|
-
|
|
4901
|
-
|
|
4902
|
-
|
|
4903
|
-
|
|
4904
|
-
|
|
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;
|
|
4908
4917
|
} else this.componentCellSelectionState = null;
|
|
4909
|
-
const
|
|
4918
|
+
const ids2 = groupMembers(source.page.elements, source.element.id).map(
|
|
4910
4919
|
(item) => item.id
|
|
4911
4920
|
);
|
|
4912
|
-
this.selectedElementIds =
|
|
4921
|
+
this.selectedElementIds = ids2;
|
|
4913
4922
|
this.emitSelection();
|
|
4914
4923
|
this.canvas.setPointerCapture?.(event.pointerId);
|
|
4915
4924
|
this.dragSession = {
|
|
@@ -4919,7 +4928,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
4919
4928
|
startClient: { x: event.clientX, y: event.clientY },
|
|
4920
4929
|
documentBefore: clonePrintDesignDocument(this.document),
|
|
4921
4930
|
elementPositions: new Map(
|
|
4922
|
-
source.page.elements.filter((item) =>
|
|
4931
|
+
source.page.elements.filter((item) => ids2.includes(item.id)).map((item) => [item.id, { x: item.x, y: item.y }])
|
|
4923
4932
|
),
|
|
4924
4933
|
changed: false
|
|
4925
4934
|
};
|
|
@@ -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
|
-
|
|
5279
|
-
|
|
5280
|
-
|
|
5281
|
-
|
|
5282
|
-
|
|
5283
|
-
|
|
5284
|
-
|
|
5285
|
-
|
|
5286
|
-
|
|
5287
|
-
|
|
5288
|
-
|
|
5289
|
-
|
|
5290
|
-
|
|
5291
|
-
|
|
5292
|
-
|
|
5293
|
-
|
|
5294
|
-
|
|
5295
|
-
|
|
5296
|
-
|
|
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) {
|
|
@@ -5548,7 +5559,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
5548
5559
|
const left = Math.min(point.x, session.start.x), top = Math.min(point.y, session.start.y);
|
|
5549
5560
|
const right = Math.max(point.x, session.start.x), bottom = Math.max(point.y, session.start.y);
|
|
5550
5561
|
const page2 = this.getActivePage();
|
|
5551
|
-
const
|
|
5562
|
+
const ids2 = new Set(session.marquee.initialIds);
|
|
5552
5563
|
for (const element of page2.elements) {
|
|
5553
5564
|
if (element.visible === false) continue;
|
|
5554
5565
|
const bounds = elementBounds(
|
|
@@ -5558,9 +5569,9 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
5558
5569
|
);
|
|
5559
5570
|
if (bounds.x <= right && bounds.y <= bottom && bounds.x + bounds.width >= left && bounds.y + bounds.height >= top)
|
|
5560
5571
|
for (const member of groupMembers(page2.elements, element.id))
|
|
5561
|
-
|
|
5572
|
+
ids2.add(member.id);
|
|
5562
5573
|
}
|
|
5563
|
-
this.selectedElementIds = [...
|
|
5574
|
+
this.selectedElementIds = [...ids2];
|
|
5564
5575
|
this.emitSelection();
|
|
5565
5576
|
this.render();
|
|
5566
5577
|
return;
|
|
@@ -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((
|
|
5877
|
-
const elements =
|
|
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
|
-
|
|
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((
|
|
6140
|
-
const target = this.getPageIn(
|
|
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((
|
|
6883
|
-
for (const element of this.getPageIn(
|
|
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((
|
|
6895
|
-
const page = this.getPageIn(
|
|
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;
|
|
@@ -6921,50 +7058,50 @@ var PrintDesignGrouping = class extends PrintDesignInteraction {
|
|
|
6921
7058
|
groupElements(elementIds = this.selectedElementIds) {
|
|
6922
7059
|
if (this.options.readonly) return this;
|
|
6923
7060
|
const elements = this.getActivePage().elements;
|
|
6924
|
-
const
|
|
6925
|
-
const members = elements.filter((item) =>
|
|
7061
|
+
const ids2 = expandGroupSelection(elements, elementIds);
|
|
7062
|
+
const members = elements.filter((item) => ids2.includes(item.id));
|
|
6926
7063
|
const units = new Set(
|
|
6927
7064
|
members.map((item) => item.groupPath?.[0]?.id ?? item.id)
|
|
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((
|
|
6932
|
-
const page = this.getPageIn(
|
|
6933
|
-
const selected = page.elements.filter((item) =>
|
|
7068
|
+
this.commitMutation((document) => {
|
|
7069
|
+
const page = this.getPageIn(document);
|
|
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))
|
|
6936
7073
|
);
|
|
6937
|
-
const before = page.elements.slice(0, highest + 1).filter((item) => !
|
|
7074
|
+
const before = page.elements.slice(0, highest + 1).filter((item) => !ids2.includes(item.id));
|
|
6938
7075
|
const after = page.elements.slice(highest + 1);
|
|
6939
7076
|
selected.forEach((item) => {
|
|
6940
7077
|
item.groupPath = [group, ...item.groupPath ?? []];
|
|
6941
7078
|
});
|
|
6942
7079
|
page.elements = [...before, ...selected, ...after];
|
|
6943
7080
|
});
|
|
6944
|
-
return this.selectElements(
|
|
7081
|
+
return this.selectElements(ids2);
|
|
6945
7082
|
}
|
|
6946
7083
|
/** 取消选中组合的最外层,保留其内部嵌套组合。 */
|
|
6947
7084
|
ungroupElements(elementIds = this.selectedElementIds) {
|
|
6948
7085
|
if (this.options.readonly) return this;
|
|
6949
|
-
const
|
|
7086
|
+
const ids2 = expandGroupSelection(this.getActivePage().elements, elementIds);
|
|
6950
7087
|
if (this.getActivePage().elements.some(
|
|
6951
|
-
(item) =>
|
|
7088
|
+
(item) => ids2.includes(item.id) && item.locked
|
|
6952
7089
|
))
|
|
6953
7090
|
return this;
|
|
6954
|
-
this.commitMutation((
|
|
6955
|
-
for (const item of this.getPageIn(
|
|
6956
|
-
if (!
|
|
7091
|
+
this.commitMutation((document) => {
|
|
7092
|
+
for (const item of this.getPageIn(document).elements) {
|
|
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;
|
|
6959
7096
|
}
|
|
6960
7097
|
});
|
|
6961
|
-
return this.selectElements(
|
|
7098
|
+
return this.selectElements(ids2);
|
|
6962
7099
|
}
|
|
6963
7100
|
};
|
|
6964
7101
|
|
|
6965
7102
|
// src/helper/layer-alignment.ts
|
|
6966
|
-
function getPrintDesignAlignmentUnits(elements,
|
|
6967
|
-
const selected = new Set(
|
|
7103
|
+
function getPrintDesignAlignmentUnits(elements, ids2) {
|
|
7104
|
+
const selected = new Set(ids2);
|
|
6968
7105
|
const groups = /* @__PURE__ */ new Map();
|
|
6969
7106
|
for (const element of elements)
|
|
6970
7107
|
for (const group of element.groupPath ?? []) {
|
|
@@ -6986,8 +7123,8 @@ function getPrintDesignAlignmentUnits(elements, ids) {
|
|
|
6986
7123
|
}
|
|
6987
7124
|
return units;
|
|
6988
7125
|
}
|
|
6989
|
-
function alignPrintDesignLayers(page,
|
|
6990
|
-
const units = getPrintDesignAlignmentUnits(page.elements,
|
|
7126
|
+
function alignPrintDesignLayers(page, ids2, align, data = {}, componentRegistry) {
|
|
7127
|
+
const units = getPrintDesignAlignmentUnits(page.elements, ids2);
|
|
6991
7128
|
if (!units.length || units.some((unit) => unit.some((item) => item.locked)))
|
|
6992
7129
|
return;
|
|
6993
7130
|
const boxes = units.map((unit) => {
|
|
@@ -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((
|
|
7128
|
-
const page = this.getPageIn(
|
|
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((
|
|
7248
|
-
this.getPageIn(
|
|
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
|
}
|
|
@@ -7257,33 +7394,33 @@ var PrintDesignActions = class extends PrintDesignGrouping {
|
|
|
7257
7394
|
/** 设置选区锁定或显隐状态。 */
|
|
7258
7395
|
setSelectionState(patch) {
|
|
7259
7396
|
if (this.options.readonly) return this;
|
|
7260
|
-
const
|
|
7397
|
+
const ids2 = new Set(
|
|
7261
7398
|
expandGroupSelection(
|
|
7262
7399
|
this.getActivePage().elements,
|
|
7263
7400
|
this.selectedElementIds
|
|
7264
7401
|
)
|
|
7265
7402
|
);
|
|
7266
|
-
this.commitMutation((
|
|
7267
|
-
for (const item of this.getPageIn(
|
|
7268
|
-
if (
|
|
7403
|
+
this.commitMutation((document) => {
|
|
7404
|
+
for (const item of this.getPageIn(document).elements)
|
|
7405
|
+
if (ids2.has(item.id)) Object.assign(item, patch);
|
|
7269
7406
|
});
|
|
7270
7407
|
return this;
|
|
7271
7408
|
}
|
|
7272
7409
|
/** 精确设置指定图层状态;组合节点由调用方传入全部成员,一次操作对应一次撤销。 */
|
|
7273
7410
|
setElementsState(elementIds, patch) {
|
|
7274
7411
|
if (this.options.readonly) return this;
|
|
7275
|
-
const
|
|
7276
|
-
this.commitMutation((
|
|
7277
|
-
for (const item of this.getPageIn(
|
|
7278
|
-
if (
|
|
7412
|
+
const ids2 = new Set(elementIds);
|
|
7413
|
+
this.commitMutation((document) => {
|
|
7414
|
+
for (const item of this.getPageIn(document).elements)
|
|
7415
|
+
if (ids2.has(item.id)) Object.assign(item, patch);
|
|
7279
7416
|
});
|
|
7280
7417
|
return this;
|
|
7281
7418
|
}
|
|
7282
7419
|
/** 全部解锁或显示当前页元素。 */
|
|
7283
7420
|
setAllElementState(patch) {
|
|
7284
7421
|
if (this.options.readonly) return this;
|
|
7285
|
-
this.commitMutation((
|
|
7286
|
-
for (const item of this.getPageIn(
|
|
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;
|
|
@@ -7292,9 +7429,9 @@ var PrintDesignActions = class extends PrintDesignGrouping {
|
|
|
7292
7429
|
moveSelectionLayer(direction) {
|
|
7293
7430
|
if (this.options.readonly || this.selectedElements().some((item) => item.locked))
|
|
7294
7431
|
return this;
|
|
7295
|
-
const
|
|
7296
|
-
this.commitMutation((
|
|
7297
|
-
const page = this.getPageIn(
|
|
7432
|
+
const ids2 = new Set(this.selectedElementIds);
|
|
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) {
|
|
@@ -7303,7 +7440,7 @@ var PrintDesignActions = class extends PrintDesignGrouping {
|
|
|
7303
7440
|
members.forEach((member) => seen.add(member.id));
|
|
7304
7441
|
units.push(members);
|
|
7305
7442
|
}
|
|
7306
|
-
const selected = (unit) => unit.some((item) =>
|
|
7443
|
+
const selected = (unit) => unit.some((item) => ids2.has(item.id));
|
|
7307
7444
|
if (direction === "front" || direction === "back") {
|
|
7308
7445
|
const picked = units.filter(selected), rest = units.filter((unit) => !selected(unit));
|
|
7309
7446
|
page.elements = (direction === "front" ? [...rest, ...picked] : [...picked, ...rest]).flat();
|
|
@@ -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((
|
|
7466
|
+
this.commitMutation((document) => {
|
|
7330
7467
|
alignPrintDesignLayers(
|
|
7331
|
-
this.getPageIn(
|
|
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(
|
|
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
|
-
|
|
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(
|
|
7526
|
+
setDocument(document) {
|
|
7390
7527
|
this.temporaryDocumentSnapshot = null;
|
|
7391
7528
|
this.document = parsePrintDesignDocument(
|
|
7392
|
-
|
|
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((
|
|
7477
|
-
|
|
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((
|
|
7485
|
-
const page =
|
|
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((
|
|
7495
|
-
|
|
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((
|
|
7526
|
-
this.getPageIn(
|
|
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((
|
|
7590
|
-
this.getPageIn(
|
|
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((
|
|
7608
|
-
const page = this.getPageIn(
|
|
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
|
|
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((
|
|
7637
|
-
const target =
|
|
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
|
});
|
|
@@ -7650,18 +7796,18 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7650
7796
|
/** 一次重置选中组件的通用和组件专属变形。 */
|
|
7651
7797
|
resetElementTransforms(elementIds = this.selectedElementIds) {
|
|
7652
7798
|
if (!elementIds.length) return this;
|
|
7653
|
-
const
|
|
7799
|
+
const ids2 = new Set(elementIds);
|
|
7654
7800
|
const targets = this.getActivePage().elements.filter(
|
|
7655
|
-
(element) =>
|
|
7801
|
+
(element) => ids2.has(element.id)
|
|
7656
7802
|
);
|
|
7657
7803
|
if (targets.some((element) => element.groupPath?.length)) return this;
|
|
7658
7804
|
if (!targets.some(
|
|
7659
|
-
(element) =>
|
|
7805
|
+
(element) => ids2.has(element.id) && !element.locked && this.hasElementTransform(element)
|
|
7660
7806
|
))
|
|
7661
7807
|
return this;
|
|
7662
|
-
this.commitMutation((
|
|
7663
|
-
for (const element of this.getPageIn(
|
|
7664
|
-
if (!
|
|
7808
|
+
this.commitMutation((document) => {
|
|
7809
|
+
for (const element of this.getPageIn(document).elements) {
|
|
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);
|
|
7667
7813
|
}
|
|
@@ -7671,25 +7817,25 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7671
7817
|
/** 删除当前选区或指定元素。 */
|
|
7672
7818
|
removeElements(elementIds = this.selectedElementIds) {
|
|
7673
7819
|
if (!elementIds.length) return this;
|
|
7674
|
-
const
|
|
7820
|
+
const ids2 = new Set(
|
|
7675
7821
|
expandGroupSelection(this.getActivePage().elements, elementIds)
|
|
7676
7822
|
);
|
|
7677
|
-
this.commitMutation((
|
|
7678
|
-
const page = this.getPageIn(
|
|
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
|
-
if (
|
|
7827
|
+
if (ids2.has(element.id)) continue;
|
|
7682
7828
|
componentRegistry.get(element.type)?.onElementsRemoved?.(element, {
|
|
7683
7829
|
page,
|
|
7684
|
-
removedIds:
|
|
7830
|
+
removedIds: ids2,
|
|
7685
7831
|
data: this.options.data ?? {},
|
|
7686
7832
|
componentRegistry
|
|
7687
7833
|
});
|
|
7688
7834
|
}
|
|
7689
|
-
page.elements = page.elements.filter((element) => !
|
|
7835
|
+
page.elements = page.elements.filter((element) => !ids2.has(element.id));
|
|
7690
7836
|
});
|
|
7691
7837
|
this.selectedElementIds = this.selectedElementIds.filter(
|
|
7692
|
-
(id) => !
|
|
7838
|
+
(id) => !ids2.has(id)
|
|
7693
7839
|
);
|
|
7694
7840
|
this.emitSelection();
|
|
7695
7841
|
this.render();
|
|
@@ -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((
|
|
7715
|
-
const elements = this.getPageIn(
|
|
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(
|
|
7874
|
+
this.getPageIn(document).elements = remaining;
|
|
7729
7875
|
});
|
|
7730
7876
|
return this;
|
|
7731
7877
|
}
|
|
@@ -7902,13 +8048,312 @@ function getPrintDesignPageSize(page) {
|
|
|
7902
8048
|
return { width: page.width, height: page.height };
|
|
7903
8049
|
}
|
|
7904
8050
|
|
|
8051
|
+
// src/agent/agent-api.ts
|
|
8052
|
+
var objectSchema = (properties, required = []) => ({
|
|
8053
|
+
type: "object",
|
|
8054
|
+
properties,
|
|
8055
|
+
required,
|
|
8056
|
+
additionalProperties: false
|
|
8057
|
+
});
|
|
8058
|
+
var idSchema = { type: "string", minLength: 1 };
|
|
8059
|
+
var idsSchema = { type: "array", items: idSchema, minItems: 1 };
|
|
8060
|
+
var printDesignAgentTools = [
|
|
8061
|
+
{
|
|
8062
|
+
name: "get_document",
|
|
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",
|
|
8064
|
+
parameters: objectSchema({})
|
|
8065
|
+
},
|
|
8066
|
+
{
|
|
8067
|
+
name: "get_selection",
|
|
8068
|
+
description: "\u8BFB\u53D6\u5F53\u524D\u6D3B\u52A8\u9875\u548C\u9009\u4E2D\u7684\u5143\u7D20 ID\u3002",
|
|
8069
|
+
parameters: objectSchema({})
|
|
8070
|
+
},
|
|
8071
|
+
{
|
|
8072
|
+
name: "list_components",
|
|
8073
|
+
description: "\u5217\u51FA\u5BBF\u4E3B\u5DF2\u6CE8\u518C\u7684\u7EC4\u4EF6\u7C7B\u578B\uFF1B\u65B0\u589E\u8868\u683C\u7B49\u63D2\u4EF6\u7EC4\u4EF6\u524D\u5148\u67E5\u770B\u6B64\u5217\u8868\u3002",
|
|
8074
|
+
parameters: objectSchema({})
|
|
8075
|
+
},
|
|
8076
|
+
{
|
|
8077
|
+
name: "get_component_template",
|
|
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",
|
|
8079
|
+
parameters: objectSchema({ type: idSchema, preset: { type: "string" } }, [
|
|
8080
|
+
"type"
|
|
8081
|
+
])
|
|
8082
|
+
},
|
|
8083
|
+
{
|
|
8084
|
+
name: "get_element",
|
|
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",
|
|
8086
|
+
parameters: objectSchema({ elementId: idSchema }, ["elementId"])
|
|
8087
|
+
},
|
|
8088
|
+
{
|
|
8089
|
+
name: "set_active_page",
|
|
8090
|
+
description: "\u5207\u6362\u5F53\u524D\u6D3B\u52A8\u9875\u3002",
|
|
8091
|
+
parameters: objectSchema({ pageId: idSchema }, ["pageId"])
|
|
8092
|
+
},
|
|
8093
|
+
{
|
|
8094
|
+
name: "select_elements",
|
|
8095
|
+
description: "\u5728\u5F53\u524D\u9875\u9009\u4E2D\u5143\u7D20\u3002",
|
|
8096
|
+
parameters: objectSchema({ elementIds: idsSchema }, ["elementIds"])
|
|
8097
|
+
},
|
|
8098
|
+
{
|
|
8099
|
+
name: "add_element",
|
|
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",
|
|
8101
|
+
parameters: objectSchema({ element: { type: "object" } }, ["element"])
|
|
8102
|
+
},
|
|
8103
|
+
{
|
|
8104
|
+
name: "update_element",
|
|
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",
|
|
8106
|
+
parameters: objectSchema(
|
|
8107
|
+
{ elementId: idSchema, patch: { type: "object" } },
|
|
8108
|
+
["elementId", "patch"]
|
|
8109
|
+
)
|
|
8110
|
+
},
|
|
8111
|
+
{
|
|
8112
|
+
name: "remove_elements",
|
|
8113
|
+
description: "\u5220\u9664\u5F53\u524D\u9875\u6307\u5B9A\u5143\u7D20\u3002",
|
|
8114
|
+
parameters: objectSchema({ elementIds: idsSchema }, ["elementIds"])
|
|
8115
|
+
},
|
|
8116
|
+
{
|
|
8117
|
+
name: "execute_component_command",
|
|
8118
|
+
description: "\u6267\u884C\u7EC4\u4EF6\u6CE8\u518C\u7684\u547D\u4EE4\uFF0C\u4F8B\u5982\u8868\u683C\u63D2\u5165\u884C\u5217\u6216\u5408\u5E76\u5355\u5143\u683C\uFF1B\u5148\u8BFB\u53D6\u7EC4\u4EF6\u6A21\u677F\u4E2D\u7684\u547D\u4EE4\u5217\u8868\u3002",
|
|
8119
|
+
parameters: objectSchema(
|
|
8120
|
+
{
|
|
8121
|
+
elementId: idSchema,
|
|
8122
|
+
command: idSchema,
|
|
8123
|
+
payload: { type: "object" }
|
|
8124
|
+
},
|
|
8125
|
+
["elementId", "command", "payload"]
|
|
8126
|
+
)
|
|
8127
|
+
},
|
|
8128
|
+
{
|
|
8129
|
+
name: "undo",
|
|
8130
|
+
description: "\u64A4\u9500\u4E0A\u4E00\u6B65\u7F16\u8F91\u3002",
|
|
8131
|
+
parameters: objectSchema({})
|
|
8132
|
+
},
|
|
8133
|
+
{
|
|
8134
|
+
name: "redo",
|
|
8135
|
+
description: "\u91CD\u505A\u4E0A\u4E00\u6B65\u7F16\u8F91\u3002",
|
|
8136
|
+
parameters: objectSchema({})
|
|
8137
|
+
}
|
|
8138
|
+
];
|
|
8139
|
+
function record(input) {
|
|
8140
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
8141
|
+
throw new TypeError("\u5DE5\u5177\u53C2\u6570\u5FC5\u987B\u662F\u5BF9\u8C61\u3002");
|
|
8142
|
+
return input;
|
|
8143
|
+
}
|
|
8144
|
+
function string(input, name) {
|
|
8145
|
+
if (typeof input !== "string" || !input.trim())
|
|
8146
|
+
throw new TypeError(`${name} \u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32\u3002`);
|
|
8147
|
+
return input;
|
|
8148
|
+
}
|
|
8149
|
+
function ids(input) {
|
|
8150
|
+
if (!Array.isArray(input) || !input.length)
|
|
8151
|
+
throw new TypeError("elementIds \u5FC5\u987B\u662F\u975E\u7A7A\u6570\u7EC4\u3002");
|
|
8152
|
+
return input.map((id) => string(id, "elementId"));
|
|
8153
|
+
}
|
|
8154
|
+
function safeJson(input, depth = 0) {
|
|
8155
|
+
if (depth > 20) throw new RangeError("\u5DE5\u5177\u53C2\u6570\u5D4C\u5957\u8FC7\u6DF1\u3002");
|
|
8156
|
+
if (input === null || typeof input === "string" || typeof input === "boolean")
|
|
8157
|
+
return;
|
|
8158
|
+
if (typeof input === "number" && Number.isFinite(input)) return;
|
|
8159
|
+
if (Array.isArray(input)) {
|
|
8160
|
+
if (input.length > 1e4) throw new RangeError("\u5DE5\u5177\u6570\u7EC4\u8FC7\u957F\u3002");
|
|
8161
|
+
input.forEach((item) => safeJson(item, depth + 1));
|
|
8162
|
+
return;
|
|
8163
|
+
}
|
|
8164
|
+
if (!input || typeof input !== "object")
|
|
8165
|
+
throw new TypeError("\u5DE5\u5177\u53C2\u6570\u5FC5\u987B\u662F JSON \u6570\u636E\u3002");
|
|
8166
|
+
const entries = Object.entries(input);
|
|
8167
|
+
if (entries.length > 1e3) throw new RangeError("\u5DE5\u5177\u5BF9\u8C61\u5B57\u6BB5\u8FC7\u591A\u3002");
|
|
8168
|
+
for (const [key, value] of entries) {
|
|
8169
|
+
if (["__proto__", "prototype", "constructor"].includes(key))
|
|
8170
|
+
throw new TypeError("\u5DE5\u5177\u53C2\u6570\u5305\u542B\u975E\u6CD5\u5B57\u6BB5\u3002");
|
|
8171
|
+
safeJson(value, depth + 1);
|
|
8172
|
+
}
|
|
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
|
+
}
|
|
8216
|
+
function createPrintDesignAgentApi(getInstance, options = {}) {
|
|
8217
|
+
return {
|
|
8218
|
+
tools: printDesignAgentTools,
|
|
8219
|
+
call(name, input) {
|
|
8220
|
+
const instance = getInstance();
|
|
8221
|
+
if (!instance) throw new Error("\u6253\u5370\u8BBE\u8BA1\u5668\u5C1A\u672A\u5C31\u7EEA\u3002");
|
|
8222
|
+
const args = record(input);
|
|
8223
|
+
safeJson(args);
|
|
8224
|
+
const unit = options.displayUnit?.() ?? "mm";
|
|
8225
|
+
if (name === "get_document")
|
|
8226
|
+
return documentInDisplayUnit(instance.getDocument(), unit);
|
|
8227
|
+
if (name === "get_selection") return instance.getSelection();
|
|
8228
|
+
if (name === "list_components")
|
|
8229
|
+
return options.componentCatalog?.list().map(({ type, label }) => ({
|
|
8230
|
+
type,
|
|
8231
|
+
label
|
|
8232
|
+
})) ?? [];
|
|
8233
|
+
if (name === "get_component_template") {
|
|
8234
|
+
const type = string(args.type, "type");
|
|
8235
|
+
const factory = options.componentCatalog?.list().find((item) => item.type === type);
|
|
8236
|
+
if (!factory || !options.componentCatalog)
|
|
8237
|
+
throw new Error(`\u672A\u6CE8\u518C\u7EC4\u4EF6\u521B\u5EFA\u5668\uFF1A${type}`);
|
|
8238
|
+
const preset = args.preset === void 0 ? void 0 : string(args.preset, "preset");
|
|
8239
|
+
const { id: _ignored, ...element } = options.componentCatalog.create(
|
|
8240
|
+
type,
|
|
8241
|
+
preset
|
|
8242
|
+
);
|
|
8243
|
+
return {
|
|
8244
|
+
type,
|
|
8245
|
+
label: factory.label,
|
|
8246
|
+
element: convertGeometry(element, unit, toDisplayUnit),
|
|
8247
|
+
displayUnit: unit,
|
|
8248
|
+
commands: factory.agentCommands ?? []
|
|
8249
|
+
};
|
|
8250
|
+
}
|
|
8251
|
+
if (name === "get_element") {
|
|
8252
|
+
const elementId = string(args.elementId, "elementId");
|
|
8253
|
+
const found = instance.getDocument().pages.find((item) => item.id === instance.getActivePageId())?.elements.find((item) => item.id === elementId);
|
|
8254
|
+
if (!found) throw new Error(`\u627E\u4E0D\u5230\u5143\u7D20\uFF1A${elementId}`);
|
|
8255
|
+
return convertGeometry(found, unit, toDisplayUnit);
|
|
8256
|
+
}
|
|
8257
|
+
if (options.readonly?.()) throw new Error("\u5F53\u524D\u8BBE\u8BA1\u5668\u4E3A\u53EA\u8BFB\u72B6\u6001\u3002");
|
|
8258
|
+
const document = instance.getDocument();
|
|
8259
|
+
const page = document.pages.find(
|
|
8260
|
+
(item) => item.id === instance.getActivePageId()
|
|
8261
|
+
);
|
|
8262
|
+
if (name === "set_active_page") {
|
|
8263
|
+
const pageId = string(args.pageId, "pageId");
|
|
8264
|
+
if (!document.pages.some((item) => item.id === pageId))
|
|
8265
|
+
throw new Error(`\u627E\u4E0D\u5230\u9875\u9762\uFF1A${pageId}`);
|
|
8266
|
+
instance.setActivePage(pageId);
|
|
8267
|
+
return instance.getSelection();
|
|
8268
|
+
}
|
|
8269
|
+
if (name === "select_elements") {
|
|
8270
|
+
const elementIds = ids(args.elementIds);
|
|
8271
|
+
if (elementIds.some((id) => !page.elements.some((item) => item.id === id)))
|
|
8272
|
+
throw new Error("\u5F53\u524D\u9875\u4E0D\u5B58\u5728\u6307\u5B9A\u5143\u7D20\u3002");
|
|
8273
|
+
instance.selectElements(elementIds);
|
|
8274
|
+
return instance.getSelection();
|
|
8275
|
+
}
|
|
8276
|
+
if (name === "add_element") {
|
|
8277
|
+
const element = record(args.element);
|
|
8278
|
+
const type = string(element.type, "type");
|
|
8279
|
+
const { id: _ignored, ...fields } = element;
|
|
8280
|
+
const defaults = options.componentCatalog?.list().some((item) => item.type === type) ? options.componentCatalog.create(type) : null;
|
|
8281
|
+
if (options.componentCatalog && !defaults)
|
|
8282
|
+
throw new Error(`\u672A\u6CE8\u518C\u7EC4\u4EF6\u521B\u5EFA\u5668\uFF1A${type}`);
|
|
8283
|
+
const { id: _defaultId, ...defaultFields } = defaults ?? {};
|
|
8284
|
+
const id = instance.addElement({
|
|
8285
|
+
...defaultFields,
|
|
8286
|
+
...convertGeometry(fields, unit, fromDisplayUnit)
|
|
8287
|
+
});
|
|
8288
|
+
return { id };
|
|
8289
|
+
}
|
|
8290
|
+
if (name === "update_element") {
|
|
8291
|
+
const elementId = string(args.elementId, "elementId");
|
|
8292
|
+
const target = page.elements.find((item) => item.id === elementId);
|
|
8293
|
+
if (!target) throw new Error(`\u627E\u4E0D\u5230\u5143\u7D20\uFF1A${elementId}`);
|
|
8294
|
+
if (target.locked) throw new Error(`\u5143\u7D20\u5DF2\u9501\u5B9A\uFF1A${elementId}`);
|
|
8295
|
+
const patch = record(args.patch);
|
|
8296
|
+
if ("id" in patch || "type" in patch)
|
|
8297
|
+
throw new TypeError("\u4E0D\u80FD\u4FEE\u6539\u5143\u7D20 id \u6216 type\u3002");
|
|
8298
|
+
instance.updateElement(
|
|
8299
|
+
elementId,
|
|
8300
|
+
convertGeometry(
|
|
8301
|
+
patch,
|
|
8302
|
+
unit,
|
|
8303
|
+
fromDisplayUnit
|
|
8304
|
+
)
|
|
8305
|
+
);
|
|
8306
|
+
return { elementId };
|
|
8307
|
+
}
|
|
8308
|
+
if (name === "remove_elements") {
|
|
8309
|
+
const elementIds = ids(args.elementIds);
|
|
8310
|
+
if (elementIds.some((id) => !page.elements.some((item) => item.id === id)))
|
|
8311
|
+
throw new Error("\u5F53\u524D\u9875\u4E0D\u5B58\u5728\u6307\u5B9A\u5143\u7D20\u3002");
|
|
8312
|
+
if (page.elements.some(
|
|
8313
|
+
(item) => elementIds.includes(item.id) && item.locked
|
|
8314
|
+
))
|
|
8315
|
+
throw new Error("\u4E0D\u80FD\u5220\u9664\u5DF2\u9501\u5B9A\u5143\u7D20\u3002");
|
|
8316
|
+
instance.removeElements(elementIds);
|
|
8317
|
+
return { removed: elementIds };
|
|
8318
|
+
}
|
|
8319
|
+
if (name === "execute_component_command") {
|
|
8320
|
+
const elementId = string(args.elementId, "elementId");
|
|
8321
|
+
const command = string(args.command, "command");
|
|
8322
|
+
const target = page.elements.find((item) => item.id === elementId);
|
|
8323
|
+
if (!target) throw new Error(`\u627E\u4E0D\u5230\u5143\u7D20\uFF1A${elementId}`);
|
|
8324
|
+
if (target.locked) throw new Error(`\u5143\u7D20\u5DF2\u9501\u5B9A\uFF1A${elementId}`);
|
|
8325
|
+
const definitions = options.componentCatalog?.list().find((item) => item.type === target.type)?.agentCommands;
|
|
8326
|
+
if (definitions && !definitions.some((item) => item.name === command))
|
|
8327
|
+
throw new Error(`\u7EC4\u4EF6\u4E0D\u652F\u6301\u547D\u4EE4\uFF1A${command}`);
|
|
8328
|
+
const payload = record(args.payload);
|
|
8329
|
+
instance.executeComponentCommand(elementId, command, payload);
|
|
8330
|
+
const updated = instance.getDocument().pages.find((item) => item.id === page.id)?.elements.find((item) => item.id === elementId);
|
|
8331
|
+
return {
|
|
8332
|
+
elementId,
|
|
8333
|
+
command,
|
|
8334
|
+
applied: JSON.stringify(updated) !== JSON.stringify(target)
|
|
8335
|
+
};
|
|
8336
|
+
}
|
|
8337
|
+
if (name === "undo") {
|
|
8338
|
+
instance.undo();
|
|
8339
|
+
return { canUndo: instance.canUndo(), canRedo: instance.canRedo() };
|
|
8340
|
+
}
|
|
8341
|
+
if (name === "redo") {
|
|
8342
|
+
instance.redo();
|
|
8343
|
+
return { canUndo: instance.canUndo(), canRedo: instance.canRedo() };
|
|
8344
|
+
}
|
|
8345
|
+
throw new Error(`\u672A\u77E5 Agent \u5DE5\u5177\uFF1A${name}`);
|
|
8346
|
+
}
|
|
8347
|
+
};
|
|
8348
|
+
}
|
|
8349
|
+
|
|
7905
8350
|
// src/model/collection-data.ts
|
|
7906
8351
|
function isPrintDesignRecordCollection(value) {
|
|
7907
8352
|
return !!value && typeof value === "object" && !Array.isArray(value) && Array.isArray(value.dataSource);
|
|
7908
8353
|
}
|
|
7909
8354
|
|
|
7910
8355
|
// package.json
|
|
7911
|
-
var version = "0.1.
|
|
8356
|
+
var version = "0.1.12";
|
|
7912
8357
|
|
|
7913
8358
|
exports.DEFAULT_PRINT_DESIGN_DEBUG_OPTIONS = DEFAULT_PRINT_DESIGN_DEBUG_OPTIONS;
|
|
7914
8359
|
exports.PRINT_DESIGN_CSS_DPI = PRINT_DESIGN_CSS_DPI;
|
|
@@ -7923,6 +8368,7 @@ exports.buildPrintDesignLayerTree = buildPrintDesignLayerTree;
|
|
|
7923
8368
|
exports.builtinPrintDesignComponentRegistry = builtinPrintDesignComponentRegistry;
|
|
7924
8369
|
exports.canvasPointToPageMillimeters = canvasPointToPageMillimeters;
|
|
7925
8370
|
exports.clonePrintDesignDocument = clonePrintDesignDocument;
|
|
8371
|
+
exports.createPrintDesignAgentApi = createPrintDesignAgentApi;
|
|
7926
8372
|
exports.createPrintDesignDocument = createPrintDesignDocument;
|
|
7927
8373
|
exports.createPrintDesignId = createPrintDesignId;
|
|
7928
8374
|
exports.createPrintDesignPage = createPrintDesignPage;
|
|
@@ -7941,11 +8387,8 @@ exports.formatDisplayUnit = formatDisplayUnit;
|
|
|
7941
8387
|
exports.fromDisplayUnit = fromDisplayUnit;
|
|
7942
8388
|
exports.getPrintDesignAlignmentUnits = getPrintDesignAlignmentUnits;
|
|
7943
8389
|
exports.getPrintDesignDataFields = getPrintDesignDataFields;
|
|
7944
|
-
exports.getPrintDesignDefaultFontUrl = getPrintDesignDefaultFontUrl;
|
|
7945
8390
|
exports.getPrintDesignElementDataConfig = getPrintDesignElementDataConfig;
|
|
7946
|
-
exports.getPrintDesignLatinFontUrl = getPrintDesignLatinFontUrl;
|
|
7947
8391
|
exports.getPrintDesignPageSize = getPrintDesignPageSize;
|
|
7948
|
-
exports.getPrintDesignSerifFontUrl = getPrintDesignSerifFontUrl;
|
|
7949
8392
|
exports.getSelectionRect = getSelectionRect;
|
|
7950
8393
|
exports.hitTestPrintDesignPage = hitTestPrintDesignPage;
|
|
7951
8394
|
exports.inverseTransformPoint = inverseTransformPoint;
|
|
@@ -7957,7 +8400,7 @@ exports.paginatePrintDesignDocument = paginatePrintDesignDocument;
|
|
|
7957
8400
|
exports.parsePrintDesignDocument = parsePrintDesignDocument;
|
|
7958
8401
|
exports.pixelsToMillimeters = pixelsToMillimeters;
|
|
7959
8402
|
exports.preparePrintDesignCanvasPage = preparePrintDesignCanvasPage;
|
|
7960
|
-
exports.
|
|
8403
|
+
exports.printDesignAgentTools = printDesignAgentTools;
|
|
7961
8404
|
exports.printPrintDesignHtml = printPrintDesignHtml;
|
|
7962
8405
|
exports.printPrintDesignHtmlInFrame = printPrintDesignHtmlInFrame;
|
|
7963
8406
|
exports.projectTransformPoint = projectTransformPoint;
|
|
@@ -7969,6 +8412,7 @@ exports.resolvePrintDesignDataPath = resolvePrintDesignDataPath;
|
|
|
7969
8412
|
exports.resolvePrintDesignDebugOptions = resolvePrintDesignDebugOptions;
|
|
7970
8413
|
exports.resolvePrintDesignElementData = resolvePrintDesignElementData;
|
|
7971
8414
|
exports.resolvePrintDesignElementVisible = resolvePrintDesignElementVisible;
|
|
8415
|
+
exports.resolvePrintDesignFontFamily = resolvePrintDesignFontFamily;
|
|
7972
8416
|
exports.resolvePrintDesignPageVisible = resolvePrintDesignPageVisible;
|
|
7973
8417
|
exports.rotatePoint = rotatePoint2;
|
|
7974
8418
|
exports.serializePrintDesignDocument = serializePrintDesignDocument;
|