@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.js
CHANGED
|
@@ -219,9 +219,9 @@ function parsePrintDesignDocument(input, componentRegistry) {
|
|
|
219
219
|
}
|
|
220
220
|
return clonePrintDesignDocument(value);
|
|
221
221
|
}
|
|
222
|
-
function serializePrintDesignDocument(
|
|
222
|
+
function serializePrintDesignDocument(document, componentRegistry) {
|
|
223
223
|
return JSON.stringify(
|
|
224
|
-
componentRegistry ? parsePrintDesignDocument(
|
|
224
|
+
componentRegistry ? parsePrintDesignDocument(document, componentRegistry) : parsePrintDesignDocument(document),
|
|
225
225
|
null,
|
|
226
226
|
2
|
|
227
227
|
);
|
|
@@ -465,8 +465,8 @@ function drawTransformedElement(ctx, element, scale, data, onImageLoad, page, fl
|
|
|
465
465
|
) + padding;
|
|
466
466
|
const rasterWidth = rasterRight - rasterLeft;
|
|
467
467
|
const rasterHeight = rasterBottom - rasterTop;
|
|
468
|
-
const
|
|
469
|
-
const canvas =
|
|
468
|
+
const document = ctx.canvas?.ownerDocument;
|
|
469
|
+
const canvas = document?.createElement("canvas") ?? globalThis.document?.createElement("canvas") ?? (typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(1, 1) : null);
|
|
470
470
|
if (!canvas) return;
|
|
471
471
|
const sourceScale = Math.min(scale, 4096 / rasterWidth, 4096 / rasterHeight);
|
|
472
472
|
canvas.width = Math.max(1, Math.ceil(rasterWidth * sourceScale));
|
|
@@ -607,56 +607,10 @@ function printDesignComponentError(operation, documentId, page, element, cause)
|
|
|
607
607
|
);
|
|
608
608
|
}
|
|
609
609
|
|
|
610
|
-
// src/rendering/default-font.ts
|
|
611
|
-
var PRINT_DESIGN_DEFAULT_FONT_FAMILY = "PrintDesignArimo, PrintDesignNotoSansSC, sans-serif";
|
|
612
|
-
var loadedDocuments = /* @__PURE__ */ new WeakMap();
|
|
613
|
-
function getPrintDesignDefaultFontUrl(weight) {
|
|
614
|
-
return weight === 700 ? new URL("./font-assets/NotoSansSC-Bold.ttf", import.meta.url).href : new URL("./font-assets/NotoSansSC-Regular.ttf", import.meta.url).href;
|
|
615
|
-
}
|
|
616
|
-
function getPrintDesignLatinFontUrl(weight) {
|
|
617
|
-
return weight === 700 ? new URL("./font-assets/Arimo-Bold.ttf", import.meta.url).href : new URL("./font-assets/Arimo-Regular.ttf", import.meta.url).href;
|
|
618
|
-
}
|
|
619
|
-
function getPrintDesignSerifFontUrl(weight) {
|
|
620
|
-
return weight === 700 ? new URL("./font-assets/Tinos-Bold.ttf", import.meta.url).href : new URL("./font-assets/Tinos-Regular.ttf", import.meta.url).href;
|
|
621
|
-
}
|
|
622
|
-
var bundledFaces = (weight) => [
|
|
623
|
-
["PrintDesignArimo", getPrintDesignLatinFontUrl(weight)],
|
|
624
|
-
["PrintDesignNotoSansSC", getPrintDesignDefaultFontUrl(weight)]
|
|
625
|
-
];
|
|
626
|
-
function preparePrintDesignDefaultFonts(ownerDocument = document) {
|
|
627
|
-
if (typeof FontFace === "undefined" || !ownerDocument.fonts)
|
|
628
|
-
return Promise.resolve();
|
|
629
|
-
const existing = loadedDocuments.get(ownerDocument);
|
|
630
|
-
if (existing) return existing;
|
|
631
|
-
const ready = Promise.all(
|
|
632
|
-
[400, 700].flatMap(
|
|
633
|
-
(weight) => bundledFaces(weight).map(async ([family, source]) => {
|
|
634
|
-
const face = new FontFace(family, `url(${JSON.stringify(source)})`, {
|
|
635
|
-
weight: String(weight)
|
|
636
|
-
});
|
|
637
|
-
await face.load();
|
|
638
|
-
ownerDocument.fonts.add(face);
|
|
639
|
-
})
|
|
640
|
-
)
|
|
641
|
-
).then(() => void 0);
|
|
642
|
-
loadedDocuments.set(ownerDocument, ready);
|
|
643
|
-
void ready.catch(() => loadedDocuments.delete(ownerDocument));
|
|
644
|
-
return ready;
|
|
645
|
-
}
|
|
646
|
-
function printDesignDefaultFontCss() {
|
|
647
|
-
return [400, 700].flatMap(
|
|
648
|
-
(weight) => bundledFaces(weight).map(
|
|
649
|
-
([family, source]) => `@font-face{font-family:${family};src:url(${JSON.stringify(source)});font-weight:${weight}}`
|
|
650
|
-
)
|
|
651
|
-
).join("");
|
|
652
|
-
}
|
|
653
|
-
|
|
654
610
|
// src/rendering/renderer.ts
|
|
655
611
|
var preparedCanvasPages = /* @__PURE__ */ new WeakSet();
|
|
656
612
|
async function preparePrintDesignCanvasPage(page, data, componentRegistry, options = {}) {
|
|
657
613
|
options.signal?.throwIfAborted();
|
|
658
|
-
if (typeof document !== "undefined")
|
|
659
|
-
await preparePrintDesignDefaultFonts(document);
|
|
660
614
|
for (const element of page.elements) {
|
|
661
615
|
options.signal?.throwIfAborted();
|
|
662
616
|
if (!resolvePrintDesignElementVisible(element, data)) continue;
|
|
@@ -861,10 +815,10 @@ function drawPrintDesignPage(ctx, page, options = {}) {
|
|
|
861
815
|
ctx.restore();
|
|
862
816
|
}
|
|
863
817
|
}
|
|
864
|
-
function renderPrintDesignPageToCanvas(
|
|
865
|
-
const page =
|
|
818
|
+
function renderPrintDesignPageToCanvas(document, pageId, options = {}) {
|
|
819
|
+
const page = document.pages.find((item) => item.id === pageId);
|
|
866
820
|
if (!page) throw new RangeError(`\u627E\u4E0D\u5230\u9875\u9762\uFF1A${pageId}`);
|
|
867
|
-
const dpi = options.dpi ??
|
|
821
|
+
const dpi = options.dpi ?? document.dpi ?? 300;
|
|
868
822
|
if (!Number.isFinite(dpi) || dpi <= 0) {
|
|
869
823
|
throw new RangeError("\u5BFC\u51FA DPI \u5FC5\u987B\u662F\u5927\u4E8E 0 \u7684\u6709\u9650\u6570\u503C\u3002");
|
|
870
824
|
}
|
|
@@ -887,7 +841,7 @@ function renderPrintDesignPageToCanvas(document2, pageId, options = {}) {
|
|
|
887
841
|
transparent: options.transparent,
|
|
888
842
|
scale,
|
|
889
843
|
componentRegistry: options.componentRegistry,
|
|
890
|
-
documentId:
|
|
844
|
+
documentId: document.id
|
|
891
845
|
});
|
|
892
846
|
} finally {
|
|
893
847
|
context.restore();
|
|
@@ -1451,18 +1405,18 @@ function drawMeasurements(context, page, selectedIds, scale, unit = "mm", data =
|
|
|
1451
1405
|
|
|
1452
1406
|
// src/model/pagination.ts
|
|
1453
1407
|
var MAX_PAGES = 1e3;
|
|
1454
|
-
function paginatePrintDesignDocument(
|
|
1455
|
-
return paginateDocument(
|
|
1408
|
+
function paginatePrintDesignDocument(document, data = {}, componentRegistry) {
|
|
1409
|
+
return paginateDocument(document, data, componentRegistry);
|
|
1456
1410
|
}
|
|
1457
|
-
function materializePrintDesignDocument(
|
|
1411
|
+
function materializePrintDesignDocument(document, data, componentRegistry) {
|
|
1458
1412
|
return componentRegistry?.list().reduce(
|
|
1459
1413
|
(current, renderer) => renderer.materializeDocument ? renderer.materializeDocument(current, data) : current,
|
|
1460
|
-
|
|
1461
|
-
) ??
|
|
1414
|
+
document
|
|
1415
|
+
) ?? document;
|
|
1462
1416
|
}
|
|
1463
|
-
function paginateDocument(
|
|
1417
|
+
function paginateDocument(document, data, componentRegistry) {
|
|
1464
1418
|
const resolvedDocument = materializePrintDesignDocument(
|
|
1465
|
-
|
|
1419
|
+
document,
|
|
1466
1420
|
data,
|
|
1467
1421
|
componentRegistry
|
|
1468
1422
|
);
|
|
@@ -1470,7 +1424,7 @@ function paginateDocument(document2, data, componentRegistry) {
|
|
|
1470
1424
|
for (const originalSource of resolvedDocument.pages) {
|
|
1471
1425
|
if (!resolvePrintDesignPageVisible(originalSource, data)) continue;
|
|
1472
1426
|
const source = originalSource.heightMode === "content" ? resolveContentHeightPage(
|
|
1473
|
-
|
|
1427
|
+
document,
|
|
1474
1428
|
originalSource,
|
|
1475
1429
|
data,
|
|
1476
1430
|
componentRegistry
|
|
@@ -1601,9 +1555,9 @@ function paginateDocument(document2, data, componentRegistry) {
|
|
|
1601
1555
|
page.physicalPageNumber = index + 1;
|
|
1602
1556
|
page.physicalTotalPages = pages.length;
|
|
1603
1557
|
}
|
|
1604
|
-
return { ...
|
|
1558
|
+
return { ...document, pages };
|
|
1605
1559
|
}
|
|
1606
|
-
function resolveContentHeightPage(
|
|
1560
|
+
function resolveContentHeightPage(document, page, data, componentRegistry) {
|
|
1607
1561
|
const top = page.margins?.top ?? 0;
|
|
1608
1562
|
const bottom = page.margins?.bottom ?? 0;
|
|
1609
1563
|
const minimumHeight = top + bottom + 1;
|
|
@@ -1626,7 +1580,7 @@ function resolveContentHeightPage(document2, page, data, componentRegistry) {
|
|
|
1626
1580
|
const workingHeight = baseHeight + growth + 10;
|
|
1627
1581
|
const measured = paginatePrintDesignDocument(
|
|
1628
1582
|
{
|
|
1629
|
-
...
|
|
1583
|
+
...document,
|
|
1630
1584
|
pages: [{ ...page, heightMode: "fixed", height: workingHeight }]
|
|
1631
1585
|
},
|
|
1632
1586
|
data,
|
|
@@ -1644,6 +1598,14 @@ function resolveContentHeightPage(document2, page, data, componentRegistry) {
|
|
|
1644
1598
|
return { ...page, height };
|
|
1645
1599
|
}
|
|
1646
1600
|
|
|
1601
|
+
// src/rendering/default-font.ts
|
|
1602
|
+
var PRINT_DESIGN_DEFAULT_FONT_FAMILY = "Arial, sans-serif";
|
|
1603
|
+
function resolvePrintDesignFontFamily(family) {
|
|
1604
|
+
const selected = family?.trim();
|
|
1605
|
+
if (!selected) return PRINT_DESIGN_DEFAULT_FONT_FAMILY;
|
|
1606
|
+
return /(?:^|,)\s*(?:sans-serif|serif|monospace|cursive|fantasy|system-ui)\s*$/i.test(selected) ? selected : `${selected}, sans-serif`;
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1647
1609
|
// src/component/builtin-html.ts
|
|
1648
1610
|
var escapeHtml = (value) => String(value ?? "").replace(
|
|
1649
1611
|
/[&<>"']/g,
|
|
@@ -1739,14 +1701,14 @@ async function prepareHtmlPage(page, documentId, data, registry, signal) {
|
|
|
1739
1701
|
}
|
|
1740
1702
|
return { ...page, elements };
|
|
1741
1703
|
}
|
|
1742
|
-
function* generatePrintHtml(
|
|
1704
|
+
function* generatePrintHtml(document, options = {}, physicalPages, pageOffset = 0, physicalTotalPages, preparedElements = false) {
|
|
1743
1705
|
const data = options.data ?? {};
|
|
1744
1706
|
const componentRegistry = options.componentRegistry ?? builtinPrintDesignComponentRegistry;
|
|
1745
1707
|
const selectedPages = options.pageIds ? options.pageIds.map((id) => {
|
|
1746
|
-
const page =
|
|
1708
|
+
const page = document.pages.find((item) => item.id === id);
|
|
1747
1709
|
if (!page) throw new RangeError(`\u627E\u4E0D\u5230\u9875\u9762\uFF1A${id}`);
|
|
1748
1710
|
return page;
|
|
1749
|
-
}) :
|
|
1711
|
+
}) : document.pages;
|
|
1750
1712
|
if (!selectedPages.length) throw new RangeError("\u81F3\u5C11\u9700\u8981\u4E00\u4E2A\u6253\u5370\u9875\u9762\u3002");
|
|
1751
1713
|
if (!preparedElements) {
|
|
1752
1714
|
for (const page of selectedPages)
|
|
@@ -1754,7 +1716,7 @@ function* generatePrintHtml(document2, options = {}, physicalPages, pageOffset =
|
|
|
1754
1716
|
if (resolvePrintDesignPageVisible(page, data) && resolvePrintDesignElementVisible(element, data) && componentRegistry.get(element.type)?.prepareHtml)
|
|
1755
1717
|
throw printDesignComponentError(
|
|
1756
1718
|
"\u6253\u5370 HTML \u751F\u6210",
|
|
1757
|
-
|
|
1719
|
+
document.id,
|
|
1758
1720
|
page,
|
|
1759
1721
|
element,
|
|
1760
1722
|
new Error(`\u7EC4\u4EF6 ${element.type} \u9700\u8981\u5F02\u6B65 HTML \u5BFC\u51FA\u4EE5\u51C6\u5907\u8D44\u6E90\u3002`)
|
|
@@ -1763,7 +1725,7 @@ function* generatePrintHtml(document2, options = {}, physicalPages, pageOffset =
|
|
|
1763
1725
|
const pages = physicalPages ?? [];
|
|
1764
1726
|
for (let index = 0; !physicalPages && index < selectedPages.length; index++) {
|
|
1765
1727
|
const result = paginatePrintDesignDocument(
|
|
1766
|
-
{ ...
|
|
1728
|
+
{ ...document, pages: [selectedPages[index]] },
|
|
1767
1729
|
data,
|
|
1768
1730
|
componentRegistry
|
|
1769
1731
|
).pages;
|
|
@@ -1794,7 +1756,7 @@ function* generatePrintHtml(document2, options = {}, physicalPages, pageOffset =
|
|
|
1794
1756
|
} catch (cause) {
|
|
1795
1757
|
throw printDesignComponentError(
|
|
1796
1758
|
"\u6253\u5370\u6837\u5F0F\u751F\u6210",
|
|
1797
|
-
|
|
1759
|
+
document.id,
|
|
1798
1760
|
page,
|
|
1799
1761
|
element,
|
|
1800
1762
|
cause
|
|
@@ -1821,7 +1783,7 @@ function* generatePrintHtml(document2, options = {}, physicalPages, pageOffset =
|
|
|
1821
1783
|
} catch (cause) {
|
|
1822
1784
|
throw printDesignComponentError(
|
|
1823
1785
|
"\u6253\u5370 HTML \u751F\u6210",
|
|
1824
|
-
|
|
1786
|
+
document.id,
|
|
1825
1787
|
page,
|
|
1826
1788
|
element,
|
|
1827
1789
|
cause
|
|
@@ -1834,7 +1796,7 @@ function* generatePrintHtml(document2, options = {}, physicalPages, pageOffset =
|
|
|
1834
1796
|
yield 0.3 + (index + 1) / pages.length * 0.7;
|
|
1835
1797
|
}
|
|
1836
1798
|
const markup = parts.join("");
|
|
1837
|
-
return `<!doctype html><html><head><meta charset="utf-8"><title>${escapeHtml(
|
|
1799
|
+
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>`;
|
|
1838
1800
|
}
|
|
1839
1801
|
function printPrintDesignHtml(popup, html, close = () => popup.close(), beforePrint) {
|
|
1840
1802
|
popup.document.open();
|
|
@@ -1903,28 +1865,26 @@ function printPrintDesignHtmlInFrame(frame, html) {
|
|
|
1903
1865
|
}
|
|
1904
1866
|
return true;
|
|
1905
1867
|
}
|
|
1906
|
-
function transformPrintDesignToHtml(
|
|
1907
|
-
const task = generatePrintHtml(
|
|
1868
|
+
function transformPrintDesignToHtml(document, options = {}) {
|
|
1869
|
+
const task = generatePrintHtml(document, options);
|
|
1908
1870
|
let next = task.next();
|
|
1909
1871
|
while (!next.done) next = task.next();
|
|
1910
1872
|
return next.value;
|
|
1911
1873
|
}
|
|
1912
|
-
async function transformPrintDesignToHtmlAsync(
|
|
1913
|
-
if (globalThis.document)
|
|
1914
|
-
await preparePrintDesignDefaultFonts(globalThis.document);
|
|
1874
|
+
async function transformPrintDesignToHtmlAsync(document, options = {}) {
|
|
1915
1875
|
const data = options.data ?? {};
|
|
1916
1876
|
const registry = options.componentRegistry ?? builtinPrintDesignComponentRegistry;
|
|
1917
1877
|
const selectedPages = options.pageIds ? options.pageIds.map((id) => {
|
|
1918
|
-
const page =
|
|
1878
|
+
const page = document.pages.find((item) => item.id === id);
|
|
1919
1879
|
if (!page) throw new RangeError(`\u627E\u4E0D\u5230\u9875\u9762\uFF1A${id}`);
|
|
1920
1880
|
return page;
|
|
1921
|
-
}) :
|
|
1881
|
+
}) : document.pages;
|
|
1922
1882
|
const pages = [];
|
|
1923
1883
|
for (let index = 0; index < selectedPages.length; index++) {
|
|
1924
1884
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1925
1885
|
options.signal?.throwIfAborted();
|
|
1926
1886
|
const physical = paginatePrintDesignDocument(
|
|
1927
|
-
{ ...
|
|
1887
|
+
{ ...document, pages: [selectedPages[index]] },
|
|
1928
1888
|
data,
|
|
1929
1889
|
registry
|
|
1930
1890
|
).pages;
|
|
@@ -1932,7 +1892,7 @@ async function transformPrintDesignToHtmlAsync(document2, options = {}) {
|
|
|
1932
1892
|
pages.push(
|
|
1933
1893
|
await prepareHtmlPage(
|
|
1934
1894
|
page,
|
|
1935
|
-
|
|
1895
|
+
document.id,
|
|
1936
1896
|
data,
|
|
1937
1897
|
registry,
|
|
1938
1898
|
options.signal
|
|
@@ -1940,7 +1900,7 @@ async function transformPrintDesignToHtmlAsync(document2, options = {}) {
|
|
|
1940
1900
|
);
|
|
1941
1901
|
options.onProgress?.((index + 1) / selectedPages.length * 0.3);
|
|
1942
1902
|
}
|
|
1943
|
-
const task = generatePrintHtml(
|
|
1903
|
+
const task = generatePrintHtml(document, options, pages, 0, void 0, true);
|
|
1944
1904
|
while (true) {
|
|
1945
1905
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1946
1906
|
options.signal?.throwIfAborted();
|
|
@@ -1952,10 +1912,10 @@ async function transformPrintDesignToHtmlAsync(document2, options = {}) {
|
|
|
1952
1912
|
} while (performance.now() < deadline);
|
|
1953
1913
|
}
|
|
1954
1914
|
}
|
|
1955
|
-
async function* transformPrintDesignToHtmlBatches(
|
|
1915
|
+
async function* transformPrintDesignToHtmlBatches(document, options) {
|
|
1956
1916
|
const limit = Math.max(1, Math.floor(options.pagesPerBatch));
|
|
1957
1917
|
if (!Number.isFinite(limit)) throw new RangeError("\u6BCF\u6B21\u6253\u5370\u9875\u6570\u5FC5\u987B\u662F\u6B63\u6574\u6570");
|
|
1958
|
-
const needsTotal =
|
|
1918
|
+
const needsTotal = document.pages.some(
|
|
1959
1919
|
(page) => page.elements.some(
|
|
1960
1920
|
(element) => options.componentRegistry?.get(element.type)?.needsTotalPages?.(element) ?? false
|
|
1961
1921
|
)
|
|
@@ -1963,10 +1923,10 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
|
|
|
1963
1923
|
let totalPages;
|
|
1964
1924
|
if (needsTotal) {
|
|
1965
1925
|
totalPages = 0;
|
|
1966
|
-
for (const page of
|
|
1926
|
+
for (const page of document.pages) {
|
|
1967
1927
|
options.signal?.throwIfAborted();
|
|
1968
1928
|
totalPages += paginatePrintDesignDocument(
|
|
1969
|
-
{ ...
|
|
1929
|
+
{ ...document, pages: [page] },
|
|
1970
1930
|
options.data ?? {},
|
|
1971
1931
|
options.componentRegistry
|
|
1972
1932
|
).pages.length;
|
|
@@ -1976,7 +1936,7 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
|
|
|
1976
1936
|
let offset = 0;
|
|
1977
1937
|
const render = async () => {
|
|
1978
1938
|
const task = generatePrintHtml(
|
|
1979
|
-
|
|
1939
|
+
document,
|
|
1980
1940
|
options,
|
|
1981
1941
|
buffer,
|
|
1982
1942
|
offset,
|
|
@@ -1993,11 +1953,11 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
|
|
|
1993
1953
|
} while (performance.now() < deadline);
|
|
1994
1954
|
}
|
|
1995
1955
|
};
|
|
1996
|
-
for (let index = 0; index <
|
|
1956
|
+
for (let index = 0; index < document.pages.length; index++) {
|
|
1997
1957
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1998
1958
|
options.signal?.throwIfAborted();
|
|
1999
1959
|
const pages = paginatePrintDesignDocument(
|
|
2000
|
-
{ ...
|
|
1960
|
+
{ ...document, pages: [document.pages[index]] },
|
|
2001
1961
|
options.data ?? {},
|
|
2002
1962
|
options.componentRegistry
|
|
2003
1963
|
).pages;
|
|
@@ -2005,7 +1965,7 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
|
|
|
2005
1965
|
buffer.push(
|
|
2006
1966
|
await prepareHtmlPage(
|
|
2007
1967
|
pages[pageIndex],
|
|
2008
|
-
|
|
1968
|
+
document.id,
|
|
2009
1969
|
options.data ?? {},
|
|
2010
1970
|
options.componentRegistry ?? builtinPrintDesignComponentRegistry,
|
|
2011
1971
|
options.signal
|
|
@@ -2017,7 +1977,7 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
|
|
|
2017
1977
|
html,
|
|
2018
1978
|
firstPage: offset + 1,
|
|
2019
1979
|
lastPage: offset + buffer.length,
|
|
2020
|
-
hasMore: pageIndex + 1 < pages.length ||
|
|
1980
|
+
hasMore: pageIndex + 1 < pages.length || document.pages.slice(index + 1).some(
|
|
2021
1981
|
(page) => resolvePrintDesignPageVisible(page, options.data ?? {})
|
|
2022
1982
|
)
|
|
2023
1983
|
};
|
|
@@ -2026,7 +1986,7 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
|
|
|
2026
1986
|
options.signal?.throwIfAborted();
|
|
2027
1987
|
}
|
|
2028
1988
|
}
|
|
2029
|
-
options.onProgress?.((index + 1) /
|
|
1989
|
+
options.onProgress?.((index + 1) / document.pages.length);
|
|
2030
1990
|
}
|
|
2031
1991
|
if (buffer.length)
|
|
2032
1992
|
yield {
|
|
@@ -2039,8 +1999,8 @@ async function* transformPrintDesignToHtmlBatches(document2, options) {
|
|
|
2039
1999
|
}
|
|
2040
2000
|
|
|
2041
2001
|
// src/helper/group-resize.ts
|
|
2042
|
-
function groupSelectionBounds(elements,
|
|
2043
|
-
const boxes = elements.filter((item) =>
|
|
2002
|
+
function groupSelectionBounds(elements, ids2, data = {}, componentRegistry) {
|
|
2003
|
+
const boxes = elements.filter((item) => ids2.includes(item.id) && item.visible !== false).map((element) => elementBounds(element, data, componentRegistry));
|
|
2044
2004
|
if (boxes.length < 2) return;
|
|
2045
2005
|
const x = Math.min(...boxes.map((box) => box.x)), y = Math.min(...boxes.map((box) => box.y));
|
|
2046
2006
|
return {
|
|
@@ -2169,9 +2129,9 @@ function groupMembers(elements, id) {
|
|
|
2169
2129
|
const groupId = element.groupPath?.[0]?.id;
|
|
2170
2130
|
return groupId ? elements.filter((item) => item.groupPath?.[0]?.id === groupId) : [element];
|
|
2171
2131
|
}
|
|
2172
|
-
function expandGroupSelection(elements,
|
|
2132
|
+
function expandGroupSelection(elements, ids2) {
|
|
2173
2133
|
const selected = new Set(
|
|
2174
|
-
|
|
2134
|
+
ids2.flatMap((id) => groupMembers(elements, id).map((item) => item.id))
|
|
2175
2135
|
);
|
|
2176
2136
|
return elements.filter((item) => selected.has(item.id)).map((item) => item.id);
|
|
2177
2137
|
}
|
|
@@ -2522,12 +2482,12 @@ var PrintDesignBase = class {
|
|
|
2522
2482
|
return sourceIndex?.(element, selector) ?? selector.index;
|
|
2523
2483
|
}
|
|
2524
2484
|
/** 由组件注册项命中内部单元。 */
|
|
2525
|
-
hitComponentCell(element, point,
|
|
2485
|
+
hitComponentCell(element, point, document, data = {}) {
|
|
2526
2486
|
const registry = this.options.componentRegistry ?? builtinPrintDesignComponentRegistry;
|
|
2527
2487
|
const behavior = registry.get(element.type)?.cellHit ?? builtinPrintDesignComponentRegistry.get(element.type)?.cellHit;
|
|
2528
2488
|
return behavior?.hit(element, {
|
|
2529
2489
|
point,
|
|
2530
|
-
document
|
|
2490
|
+
document,
|
|
2531
2491
|
data
|
|
2532
2492
|
});
|
|
2533
2493
|
}
|
|
@@ -2731,8 +2691,8 @@ var PrintDesignBase = class {
|
|
|
2731
2691
|
})
|
|
2732
2692
|
};
|
|
2733
2693
|
}
|
|
2734
|
-
getPageIn(
|
|
2735
|
-
return
|
|
2694
|
+
getPageIn(document) {
|
|
2695
|
+
return document.pages.find((page) => page.id === this.activePageId) ?? document.pages[0];
|
|
2736
2696
|
}
|
|
2737
2697
|
commitMutation(mutator) {
|
|
2738
2698
|
const before = clonePrintDesignDocument(this.document);
|
|
@@ -2749,8 +2709,8 @@ var PrintDesignBase = class {
|
|
|
2749
2709
|
this.emitChange();
|
|
2750
2710
|
this.render();
|
|
2751
2711
|
}
|
|
2752
|
-
pushHistory(
|
|
2753
|
-
this.history.push(
|
|
2712
|
+
pushHistory(document) {
|
|
2713
|
+
this.history.push(document);
|
|
2754
2714
|
if (this.history.length > HISTORY_LIMIT) this.history.shift();
|
|
2755
2715
|
}
|
|
2756
2716
|
normalizeActivePageAfterHistory() {
|
|
@@ -2871,20 +2831,21 @@ function measureSpacedText(ctx, text) {
|
|
|
2871
2831
|
cache.set(text, width);
|
|
2872
2832
|
return width;
|
|
2873
2833
|
}
|
|
2874
|
-
function layoutAlignedText(text, width, measure) {
|
|
2834
|
+
function layoutAlignedText(text, width, measure, indent) {
|
|
2875
2835
|
return text.split("\n").flatMap((paragraph) => {
|
|
2876
2836
|
const lines = [];
|
|
2877
2837
|
let line = "";
|
|
2838
|
+
const lineIndent = (index) => indent?.mode === "firstLine" ? index === 0 ? indent.value : 0 : indent?.mode === "hanging" && index > 0 ? indent.value : 0;
|
|
2878
2839
|
const tokens = paragraph.match(
|
|
2879
2840
|
/[¥¥$€£]?\p{N}+(?:[.,:/-]\p{N}+)*(?:[%%])?|[\p{Script=Latin}\p{N}]+|\s+|[^\p{Script=Latin}\p{N}\s]+/gu
|
|
2880
2841
|
) ?? [];
|
|
2881
2842
|
const pieces = tokens.flatMap(
|
|
2882
2843
|
(token) => /^(?:[¥¥$€£]?\p{N}+(?:[.,:/-]\p{N}+)*(?:[%%])?|[\p{Script=Latin}\p{N}\s]+)$/u.test(
|
|
2883
2844
|
token
|
|
2884
|
-
) && measure(token) <= width ? [token] : graphemes(token)
|
|
2845
|
+
) && measure(token) <= Math.max(1, width - Math.max(0, indent?.value ?? 0)) ? [token] : graphemes(token)
|
|
2885
2846
|
);
|
|
2886
2847
|
for (const piece of pieces) {
|
|
2887
|
-
if (line && measure(line + piece) > width) {
|
|
2848
|
+
if (line && measure(line + piece) > Math.max(1, width - lineIndent(lines.length))) {
|
|
2888
2849
|
lines.push(line.trimEnd());
|
|
2889
2850
|
line = "";
|
|
2890
2851
|
}
|
|
@@ -2967,6 +2928,10 @@ var PrintDesignTextEditor = class {
|
|
|
2967
2928
|
const expanded = element.textAlign === "justify" || element.textAlign === "distributed";
|
|
2968
2929
|
this.input.setAttribute("wrap", "soft");
|
|
2969
2930
|
const fontSize = (element.resolveFontSize?.(this.input.value) ?? element.fontSize ?? 14) * 25.4 * origin.scale / 72;
|
|
2931
|
+
const indent = element.indentMode && element.indentMode !== "none" ? element.indentUnit === "char" || !element.indentUnit ? Math.max(0, element.indentValue ?? 0) * fontSize : fromDisplayUnit(
|
|
2932
|
+
Math.max(0, element.indentValue ?? 0),
|
|
2933
|
+
element.indentUnit
|
|
2934
|
+
) * origin.scale : 0;
|
|
2970
2935
|
const height = element.height * origin.scale;
|
|
2971
2936
|
const padding = this.settings.padding;
|
|
2972
2937
|
const topPadding = (padding?.top ?? 0) * origin.scale;
|
|
@@ -2983,11 +2948,12 @@ var PrintDesignTextEditor = class {
|
|
|
2983
2948
|
if (!verticalText) {
|
|
2984
2949
|
const context = this.host.ownerDocument.createElement("canvas").getContext("2d");
|
|
2985
2950
|
if (context) {
|
|
2986
|
-
context.font = `${element.fontWeight ?? "400"} ${fontSize}px ${element.fontFamily
|
|
2951
|
+
context.font = `${element.fontStyle ?? "normal"} ${element.fontWeight ?? "400"} ${fontSize}px ${resolvePrintDesignFontFamily(element.fontFamily)}`;
|
|
2987
2952
|
lineCount = layoutAlignedText(
|
|
2988
2953
|
this.input.value,
|
|
2989
2954
|
contentWidth,
|
|
2990
|
-
(text) => measureSpacedText(context, text)
|
|
2955
|
+
(text) => measureSpacedText(context, text),
|
|
2956
|
+
element.indentMode && element.indentMode !== "none" ? { mode: element.indentMode, value: indent } : void 0
|
|
2991
2957
|
).length;
|
|
2992
2958
|
}
|
|
2993
2959
|
}
|
|
@@ -2999,16 +2965,18 @@ var PrintDesignTextEditor = class {
|
|
|
2999
2965
|
width: `${element.width * origin.scale}px`,
|
|
3000
2966
|
height: `${height}px`,
|
|
3001
2967
|
transform: `rotate(${element.rotation ?? 0}deg)`,
|
|
3002
|
-
padding: `${topPadding + Math.max(paddingTop, 0)}px ${rightPadding}px ${bottomPadding}px ${leftPadding}px`,
|
|
2968
|
+
padding: `${topPadding + Math.max(paddingTop, 0)}px ${rightPadding}px ${bottomPadding}px ${leftPadding + (element.indentMode === "hanging" ? indent : 0)}px`,
|
|
2969
|
+
textIndent: element.indentMode === "firstLine" ? `${indent}px` : element.indentMode === "hanging" ? `${-indent}px` : "0",
|
|
3003
2970
|
whiteSpace: "pre-wrap",
|
|
3004
2971
|
writingMode: verticalText ? "vertical-rl" : "horizontal-tb",
|
|
3005
2972
|
textOrientation: verticalText ? "upright" : "mixed",
|
|
3006
2973
|
letterSpacing: "0",
|
|
3007
2974
|
textSpacingTrim: "space-all",
|
|
3008
2975
|
textAutospace: "no-autospace",
|
|
3009
|
-
fontFamily: element.fontFamily
|
|
2976
|
+
fontFamily: resolvePrintDesignFontFamily(element.fontFamily),
|
|
3010
2977
|
fontSize: `${fontSize}px`,
|
|
3011
2978
|
fontWeight: element.fontWeight ?? "400",
|
|
2979
|
+
fontStyle: element.fontStyle ?? "normal",
|
|
3012
2980
|
lineHeight: String(element.lineHeight ?? 1.35),
|
|
3013
2981
|
textAlign: expanded ? "justify" : element.textAlign ?? "left",
|
|
3014
2982
|
textAlignLast: element.textAlign === "distributed" ? "justify" : "auto",
|
|
@@ -3144,8 +3112,8 @@ var PrintDesignComponentCellEditing = class extends PrintDesignBase {
|
|
|
3144
3112
|
(value, restoreFocus) => {
|
|
3145
3113
|
this.componentCellEditing = null;
|
|
3146
3114
|
if (value !== void 0 && value !== text && !this.options.readonly) {
|
|
3147
|
-
this.commitMutation((
|
|
3148
|
-
const page =
|
|
3115
|
+
this.commitMutation((document) => {
|
|
3116
|
+
const page = document.pages.find((page2) => page2.id === pageId);
|
|
3149
3117
|
const index = page?.elements.findIndex(
|
|
3150
3118
|
(item) => item.id === element.id
|
|
3151
3119
|
);
|
|
@@ -3262,6 +3230,14 @@ var PrintDesignTextEditing = class extends PrintDesignComponentCellEditing {
|
|
|
3262
3230
|
this.selectElements([element.id]);
|
|
3263
3231
|
return;
|
|
3264
3232
|
}
|
|
3233
|
+
if (element && this.options.onElementDoubleClick) {
|
|
3234
|
+
this.discardTextEditing();
|
|
3235
|
+
this.selectElements([element.id]);
|
|
3236
|
+
if (this.options.onElementDoubleClick(element, sourcePage?.id ?? page.id)) {
|
|
3237
|
+
event.preventDefault();
|
|
3238
|
+
return;
|
|
3239
|
+
}
|
|
3240
|
+
}
|
|
3265
3241
|
if (element && this.canEditComponentCell(element)) {
|
|
3266
3242
|
event.preventDefault();
|
|
3267
3243
|
this.textEditor?.finish(true);
|
|
@@ -3287,8 +3263,8 @@ var PrintDesignTextEditing = class extends PrintDesignComponentCellEditing {
|
|
|
3287
3263
|
(value, restoreFocus) => {
|
|
3288
3264
|
this.textEditor = null;
|
|
3289
3265
|
if (value !== void 0 && !this.options.readonly) {
|
|
3290
|
-
this.commitMutation((
|
|
3291
|
-
const target =
|
|
3266
|
+
this.commitMutation((document) => {
|
|
3267
|
+
const target = document.pages.find((p) => p.id === (sourcePage?.id ?? page.id))?.elements.find((e) => e.id === element.id);
|
|
3292
3268
|
const targetRules = target ? this.options.componentRegistry?.get(target.type)?.inlineTextEditor : void 0;
|
|
3293
3269
|
if (target && target.type === element.type && targetRules?.canEdit(target) && targetRules.read(target) === session.initialText)
|
|
3294
3270
|
Object.assign(target, targetRules.write(target, value));
|
|
@@ -3733,6 +3709,7 @@ var PrintDesignRenderer = class extends PrintDesignTextEditing {
|
|
|
3733
3709
|
for (const element of page.elements) {
|
|
3734
3710
|
if (this.componentCropState?.elementId === element.id) continue;
|
|
3735
3711
|
if (element.id === this.textEditor?.elementId) continue;
|
|
3712
|
+
if (element.id === this.options.inlineEditingElementId) continue;
|
|
3736
3713
|
if (element.id === this.drawingPathId && this.pathDrawingPoints.length < 2)
|
|
3737
3714
|
continue;
|
|
3738
3715
|
let drawFailed = false;
|
|
@@ -4436,6 +4413,36 @@ function flowingComponentIds(page, selectedIds, data = {}, componentRegistry = b
|
|
|
4436
4413
|
var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
4437
4414
|
constructor() {
|
|
4438
4415
|
super(...arguments);
|
|
4416
|
+
__publicField(this, "keyboardMoveSession", null);
|
|
4417
|
+
/** 按真实帧间隔连续移动,系统重复按键只更新当前修饰键状态。 */
|
|
4418
|
+
__publicField(this, "advanceKeyboardMove", (time) => {
|
|
4419
|
+
const session = this.keyboardMoveSession;
|
|
4420
|
+
if (!session) return;
|
|
4421
|
+
session.frame = null;
|
|
4422
|
+
const elapsed = time - session.lastFrameAt;
|
|
4423
|
+
session.lastFrameAt = time;
|
|
4424
|
+
const heldMs = Date.now() - session.startedAt;
|
|
4425
|
+
if (!session.released && heldMs >= 260) {
|
|
4426
|
+
const delta = this.options.keyboardMove?.(session.event, heldMs);
|
|
4427
|
+
if (delta && Number.isFinite(delta.dx) && Number.isFinite(delta.dy)) {
|
|
4428
|
+
const factor = Math.min(Math.max(elapsed, 0) / 33, 2);
|
|
4429
|
+
session.targetDx += delta.dx * factor;
|
|
4430
|
+
session.targetDy += delta.dy * factor;
|
|
4431
|
+
}
|
|
4432
|
+
}
|
|
4433
|
+
const ease = 1 - Math.exp(-Math.max(elapsed, 0) / 32);
|
|
4434
|
+
this.applyKeyboardMove(
|
|
4435
|
+
(session.targetDx - session.dx) * ease,
|
|
4436
|
+
(session.targetDy - session.dy) * ease
|
|
4437
|
+
);
|
|
4438
|
+
if (session.released && Math.abs(session.targetDx - session.dx) < 5e-3 && Math.abs(session.targetDy - session.dy) < 5e-3) {
|
|
4439
|
+
this.finishKeyboardMove();
|
|
4440
|
+
return;
|
|
4441
|
+
}
|
|
4442
|
+
const view = this.container.ownerDocument.defaultView;
|
|
4443
|
+
if (view?.requestAnimationFrame)
|
|
4444
|
+
session.frame = view.requestAnimationFrame(this.advanceKeyboardMove);
|
|
4445
|
+
});
|
|
4439
4446
|
__publicField(this, "panMode", false);
|
|
4440
4447
|
__publicField(this, "componentCropDrag", null);
|
|
4441
4448
|
__publicField(this, "strokeRenderFrame", null);
|
|
@@ -4468,6 +4475,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
4468
4475
|
else this.wheelPanFrame = view.requestAnimationFrame(this.advanceWheelPan);
|
|
4469
4476
|
});
|
|
4470
4477
|
__publicField(this, "handlePointerDown", (event) => {
|
|
4478
|
+
this.finishKeyboardMove();
|
|
4471
4479
|
if (event.button !== 0) return;
|
|
4472
4480
|
if (this.dragSession) return;
|
|
4473
4481
|
this.stopWheelPan();
|
|
@@ -4878,35 +4886,37 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
4878
4886
|
preview.data
|
|
4879
4887
|
)) {
|
|
4880
4888
|
this.selectElements([source.element.id]);
|
|
4881
|
-
|
|
4882
|
-
|
|
4883
|
-
|
|
4884
|
-
|
|
4885
|
-
|
|
4886
|
-
|
|
4887
|
-
|
|
4888
|
-
|
|
4889
|
-
|
|
4890
|
-
|
|
4891
|
-
|
|
4892
|
-
|
|
4893
|
-
|
|
4894
|
-
|
|
4895
|
-
|
|
4896
|
-
|
|
4897
|
-
|
|
4898
|
-
|
|
4899
|
-
|
|
4900
|
-
|
|
4901
|
-
|
|
4889
|
+
if (event.shiftKey) {
|
|
4890
|
+
const selection = this.componentCellSelectionState;
|
|
4891
|
+
this.canvas.setPointerCapture?.(event.pointerId);
|
|
4892
|
+
this.dragSession = {
|
|
4893
|
+
mode: "component-cell-select",
|
|
4894
|
+
componentCellSelection: {
|
|
4895
|
+
element: previewComponent,
|
|
4896
|
+
sourceId: source.element.id,
|
|
4897
|
+
data: preview.data,
|
|
4898
|
+
anchor: this.componentCellSelection(previewComponent).dragAnchor(
|
|
4899
|
+
selection
|
|
4900
|
+
)
|
|
4901
|
+
},
|
|
4902
|
+
pointerId: event.pointerId,
|
|
4903
|
+
start: point,
|
|
4904
|
+
startClient: { x: event.clientX, y: event.clientY },
|
|
4905
|
+
documentBefore: clonePrintDesignDocument(this.document),
|
|
4906
|
+
elementPositions: /* @__PURE__ */ new Map(),
|
|
4907
|
+
changed: false
|
|
4908
|
+
};
|
|
4909
|
+
this.canvas.style.cursor = "crosshair";
|
|
4910
|
+
return;
|
|
4911
|
+
}
|
|
4902
4912
|
}
|
|
4903
4913
|
if (!this.componentCellSelection(previewComponent))
|
|
4904
4914
|
this.componentCellSelectionState = null;
|
|
4905
4915
|
} else this.componentCellSelectionState = null;
|
|
4906
|
-
const
|
|
4916
|
+
const ids2 = groupMembers(source.page.elements, source.element.id).map(
|
|
4907
4917
|
(item) => item.id
|
|
4908
4918
|
);
|
|
4909
|
-
this.selectedElementIds =
|
|
4919
|
+
this.selectedElementIds = ids2;
|
|
4910
4920
|
this.emitSelection();
|
|
4911
4921
|
this.canvas.setPointerCapture?.(event.pointerId);
|
|
4912
4922
|
this.dragSession = {
|
|
@@ -4916,7 +4926,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
4916
4926
|
startClient: { x: event.clientX, y: event.clientY },
|
|
4917
4927
|
documentBefore: clonePrintDesignDocument(this.document),
|
|
4918
4928
|
elementPositions: new Map(
|
|
4919
|
-
source.page.elements.filter((item) =>
|
|
4929
|
+
source.page.elements.filter((item) => ids2.includes(item.id)).map((item) => [item.id, { x: item.x, y: item.y }])
|
|
4920
4930
|
),
|
|
4921
4931
|
changed: false
|
|
4922
4932
|
};
|
|
@@ -5272,25 +5282,27 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
5272
5282
|
if (hit && !!this.componentCellSelection(hit) && !this.options.readonly && !hit.locked && !event.altKey && !event.metaKey && !event.ctrlKey) {
|
|
5273
5283
|
if (this.selectComponentCellAtPoint(hit, point, event.shiftKey)) {
|
|
5274
5284
|
this.selectElements([hit.id]);
|
|
5275
|
-
|
|
5276
|
-
|
|
5277
|
-
|
|
5278
|
-
|
|
5279
|
-
|
|
5280
|
-
|
|
5281
|
-
|
|
5282
|
-
|
|
5283
|
-
|
|
5284
|
-
|
|
5285
|
-
|
|
5286
|
-
|
|
5287
|
-
|
|
5288
|
-
|
|
5289
|
-
|
|
5290
|
-
|
|
5291
|
-
|
|
5292
|
-
|
|
5293
|
-
|
|
5285
|
+
if (event.shiftKey) {
|
|
5286
|
+
const selection = this.componentCellSelectionState;
|
|
5287
|
+
this.canvas.setPointerCapture?.(event.pointerId);
|
|
5288
|
+
this.dragSession = {
|
|
5289
|
+
mode: "component-cell-select",
|
|
5290
|
+
componentCellSelection: {
|
|
5291
|
+
element: hit,
|
|
5292
|
+
sourceId: hit.id,
|
|
5293
|
+
data: this.options.data ?? {},
|
|
5294
|
+
anchor: this.componentCellSelection(hit).dragAnchor(selection)
|
|
5295
|
+
},
|
|
5296
|
+
pointerId: event.pointerId,
|
|
5297
|
+
start: point,
|
|
5298
|
+
startClient: { x: event.clientX, y: event.clientY },
|
|
5299
|
+
documentBefore: clonePrintDesignDocument(this.document),
|
|
5300
|
+
elementPositions: /* @__PURE__ */ new Map(),
|
|
5301
|
+
changed: false
|
|
5302
|
+
};
|
|
5303
|
+
this.canvas.style.cursor = "crosshair";
|
|
5304
|
+
return;
|
|
5305
|
+
}
|
|
5294
5306
|
}
|
|
5295
5307
|
}
|
|
5296
5308
|
if (!hit) {
|
|
@@ -5545,7 +5557,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
5545
5557
|
const left = Math.min(point.x, session.start.x), top = Math.min(point.y, session.start.y);
|
|
5546
5558
|
const right = Math.max(point.x, session.start.x), bottom = Math.max(point.y, session.start.y);
|
|
5547
5559
|
const page2 = this.getActivePage();
|
|
5548
|
-
const
|
|
5560
|
+
const ids2 = new Set(session.marquee.initialIds);
|
|
5549
5561
|
for (const element of page2.elements) {
|
|
5550
5562
|
if (element.visible === false) continue;
|
|
5551
5563
|
const bounds = elementBounds(
|
|
@@ -5555,9 +5567,9 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
5555
5567
|
);
|
|
5556
5568
|
if (bounds.x <= right && bounds.y <= bottom && bounds.x + bounds.width >= left && bounds.y + bounds.height >= top)
|
|
5557
5569
|
for (const member of groupMembers(page2.elements, element.id))
|
|
5558
|
-
|
|
5570
|
+
ids2.add(member.id);
|
|
5559
5571
|
}
|
|
5560
|
-
this.selectedElementIds = [...
|
|
5572
|
+
this.selectedElementIds = [...ids2];
|
|
5561
5573
|
this.emitSelection();
|
|
5562
5574
|
this.render();
|
|
5563
5575
|
return;
|
|
@@ -5870,8 +5882,8 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
5870
5882
|
if (replacement) {
|
|
5871
5883
|
if (replacement.id !== element.id || replacement.type !== element.type)
|
|
5872
5884
|
throw new Error(`\u7EC4\u4EF6\u70B9\u51FB\u7ED3\u679C\u7C7B\u578B\u4E0D\u5339\u914D\uFF1A${element.type}`);
|
|
5873
|
-
this.commitMutation((
|
|
5874
|
-
const elements =
|
|
5885
|
+
this.commitMutation((document) => {
|
|
5886
|
+
const elements = document.pages.find(
|
|
5875
5887
|
(page) => page.id === this.activePageId
|
|
5876
5888
|
)?.elements;
|
|
5877
5889
|
const index = elements?.findIndex((item) => item.id === element.id) ?? -1;
|
|
@@ -5969,7 +5981,54 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
5969
5981
|
return;
|
|
5970
5982
|
}
|
|
5971
5983
|
const modifier = event.metaKey || event.ctrlKey;
|
|
5972
|
-
|
|
5984
|
+
const arrow = ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(
|
|
5985
|
+
event.key
|
|
5986
|
+
);
|
|
5987
|
+
if (this.keyboardMoveSession && (modifier || event.altKey || ["Escape", "Delete", "Backspace"].includes(event.key)))
|
|
5988
|
+
this.finishKeyboardMove();
|
|
5989
|
+
if (arrow && !modifier && !event.altKey && this.selectedElementIds.length && !this.options.readonly && this.options.keyboardMove) {
|
|
5990
|
+
event.preventDefault();
|
|
5991
|
+
if (this.keyboardMoveSession?.event.key === event.key && !this.keyboardMoveSession.released) {
|
|
5992
|
+
this.keyboardMoveSession.event = event;
|
|
5993
|
+
return;
|
|
5994
|
+
}
|
|
5995
|
+
this.finishKeyboardMove();
|
|
5996
|
+
const elements = this.getActivePage().elements;
|
|
5997
|
+
const ids2 = new Set(
|
|
5998
|
+
expandGroupSelection(elements, this.selectedElementIds)
|
|
5999
|
+
);
|
|
6000
|
+
const selected = elements.filter((element) => ids2.has(element.id));
|
|
6001
|
+
if (!selected.length || selected.some((element) => element.locked))
|
|
6002
|
+
return;
|
|
6003
|
+
const documentBefore = clonePrintDesignDocument(this.document);
|
|
6004
|
+
const beforePage = this.getPageIn(documentBefore);
|
|
6005
|
+
this.keyboardMoveSession = {
|
|
6006
|
+
event,
|
|
6007
|
+
startedAt: Date.now(),
|
|
6008
|
+
lastFrameAt: this.container.ownerDocument.defaultView?.performance.now() ?? 0,
|
|
6009
|
+
frame: null,
|
|
6010
|
+
documentBefore,
|
|
6011
|
+
sources: new Map(
|
|
6012
|
+
beforePage.elements.filter((element) => ids2.has(element.id)).map((element) => [element.id, element])
|
|
6013
|
+
),
|
|
6014
|
+
dx: 0,
|
|
6015
|
+
dy: 0,
|
|
6016
|
+
targetDx: 0,
|
|
6017
|
+
targetDy: 0,
|
|
6018
|
+
released: false,
|
|
6019
|
+
changed: false
|
|
6020
|
+
};
|
|
6021
|
+
const delta = this.options.keyboardMove(event, 0);
|
|
6022
|
+
if (delta && Number.isFinite(delta.dx) && Number.isFinite(delta.dy)) {
|
|
6023
|
+
this.keyboardMoveSession.targetDx = delta.dx;
|
|
6024
|
+
this.keyboardMoveSession.targetDy = delta.dy;
|
|
6025
|
+
}
|
|
6026
|
+
const view = this.container.ownerDocument.defaultView;
|
|
6027
|
+
if (view?.requestAnimationFrame)
|
|
6028
|
+
this.keyboardMoveSession.frame = view.requestAnimationFrame(
|
|
6029
|
+
this.advanceKeyboardMove
|
|
6030
|
+
);
|
|
6031
|
+
} else if (modifier && event.key.toLowerCase() === "g" && !this.options.readonly) {
|
|
5973
6032
|
event.preventDefault();
|
|
5974
6033
|
if (event.shiftKey) this.ungroupElements();
|
|
5975
6034
|
else this.groupElements();
|
|
@@ -5993,11 +6052,17 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
5993
6052
|
}
|
|
5994
6053
|
});
|
|
5995
6054
|
__publicField(this, "handleKeyUp", (event) => {
|
|
6055
|
+
if (this.keyboardMoveSession?.event.key === event.key)
|
|
6056
|
+
this.releaseKeyboardMove();
|
|
5996
6057
|
if (event.code !== "Space" && event.key !== " ") return;
|
|
5997
6058
|
event.preventDefault();
|
|
5998
6059
|
this.spacePressed = false;
|
|
5999
6060
|
this.canvas.style.cursor = this.dragSession ? "grabbing" : this.panMode ? "grab" : "default";
|
|
6000
6061
|
});
|
|
6062
|
+
__publicField(this, "handleBlur", () => {
|
|
6063
|
+
this.spacePressed = false;
|
|
6064
|
+
this.finishKeyboardMove();
|
|
6065
|
+
});
|
|
6001
6066
|
__publicField(this, "handleWheel", (event) => {
|
|
6002
6067
|
if (this.dragSession) return;
|
|
6003
6068
|
if (event.ctrlKey || event.metaKey) {
|
|
@@ -6036,6 +6101,78 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
6036
6101
|
this.wheelPanFrame = view.requestAnimationFrame(this.advanceWheelPan);
|
|
6037
6102
|
});
|
|
6038
6103
|
}
|
|
6104
|
+
/** 长按期间只重绘画布,避免每帧提交文档并通知 React。 */
|
|
6105
|
+
applyKeyboardMove(dx, dy) {
|
|
6106
|
+
const session = this.keyboardMoveSession;
|
|
6107
|
+
if (!session || !dx && !dy) return;
|
|
6108
|
+
session.dx += dx;
|
|
6109
|
+
session.dy += dy;
|
|
6110
|
+
for (const element of this.getActivePage().elements) {
|
|
6111
|
+
const source = session.sources.get(element.id);
|
|
6112
|
+
if (!source) continue;
|
|
6113
|
+
element.x = Math.round((source.x + session.dx) * 100) / 100;
|
|
6114
|
+
element.y = Math.round((source.y + session.dy) * 100) / 100;
|
|
6115
|
+
this.options.componentRegistry?.get(element.type)?.translatePageCoordinates?.(
|
|
6116
|
+
element,
|
|
6117
|
+
source,
|
|
6118
|
+
session.dx,
|
|
6119
|
+
session.dy
|
|
6120
|
+
);
|
|
6121
|
+
}
|
|
6122
|
+
session.changed = true;
|
|
6123
|
+
this.render();
|
|
6124
|
+
}
|
|
6125
|
+
/** 松开按键时将整段连续移动合并为一条撤销记录。 */
|
|
6126
|
+
finishKeyboardMove() {
|
|
6127
|
+
const session = this.keyboardMoveSession;
|
|
6128
|
+
if (!session) return;
|
|
6129
|
+
this.applyKeyboardMove(
|
|
6130
|
+
session.targetDx - session.dx,
|
|
6131
|
+
session.targetDy - session.dy
|
|
6132
|
+
);
|
|
6133
|
+
this.keyboardMoveSession = null;
|
|
6134
|
+
if (session.frame !== null)
|
|
6135
|
+
this.container.ownerDocument.defaultView?.cancelAnimationFrame?.(
|
|
6136
|
+
session.frame
|
|
6137
|
+
);
|
|
6138
|
+
if (!session.changed) return;
|
|
6139
|
+
this.pushHistory(session.documentBefore);
|
|
6140
|
+
this.future = [];
|
|
6141
|
+
this.emitChange();
|
|
6142
|
+
}
|
|
6143
|
+
/** 松键后继续补完当前位移的短暂过渡。 */
|
|
6144
|
+
releaseKeyboardMove() {
|
|
6145
|
+
const session = this.keyboardMoveSession;
|
|
6146
|
+
if (!session) return;
|
|
6147
|
+
session.released = true;
|
|
6148
|
+
if (session.frame === null) this.finishKeyboardMove();
|
|
6149
|
+
}
|
|
6150
|
+
/** 按页面毫米坐标平移选中组件;一次操作只生成一条撤销记录。 */
|
|
6151
|
+
moveSelectedElements(dx, dy) {
|
|
6152
|
+
if (this.options.readonly || !dx && !dy) return this;
|
|
6153
|
+
const elements = this.getActivePage().elements;
|
|
6154
|
+
const ids2 = new Set(
|
|
6155
|
+
expandGroupSelection(elements, this.selectedElementIds)
|
|
6156
|
+
);
|
|
6157
|
+
const selected = elements.filter((element) => ids2.has(element.id));
|
|
6158
|
+
if (!selected.length || selected.some((element) => element.locked))
|
|
6159
|
+
return this;
|
|
6160
|
+
this.commitMutation((document) => {
|
|
6161
|
+
for (const element of this.getPageIn(document).elements) {
|
|
6162
|
+
if (!ids2.has(element.id)) continue;
|
|
6163
|
+
const source = selected.find((item) => item.id === element.id);
|
|
6164
|
+
element.x = Math.round((source.x + dx) * 100) / 100;
|
|
6165
|
+
element.y = Math.round((source.y + dy) * 100) / 100;
|
|
6166
|
+
this.options.componentRegistry?.get(element.type)?.translatePageCoordinates?.(
|
|
6167
|
+
element,
|
|
6168
|
+
source,
|
|
6169
|
+
dx,
|
|
6170
|
+
dy
|
|
6171
|
+
);
|
|
6172
|
+
}
|
|
6173
|
+
});
|
|
6174
|
+
return this;
|
|
6175
|
+
}
|
|
6039
6176
|
/** 尺寸更新后的组件布局模式由注册项维护。 */
|
|
6040
6177
|
notifyComponentResized(element, vertical) {
|
|
6041
6178
|
const registry = this.options.componentRegistry ?? builtinPrintDesignComponentRegistry;
|
|
@@ -6133,8 +6270,8 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
6133
6270
|
state.sourceHeight
|
|
6134
6271
|
);
|
|
6135
6272
|
const next = crop.width >= 0.999 && crop.height >= 0.999 ? void 0 : crop;
|
|
6136
|
-
this.commitMutation((
|
|
6137
|
-
const target = this.getPageIn(
|
|
6273
|
+
this.commitMutation((document) => {
|
|
6274
|
+
const target = this.getPageIn(document).elements.find(
|
|
6138
6275
|
(item) => item.id === state.elementId
|
|
6139
6276
|
);
|
|
6140
6277
|
if (target?.type === element.type) {
|
|
@@ -6395,6 +6532,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
6395
6532
|
this.wheelPanLastTime = 0;
|
|
6396
6533
|
}
|
|
6397
6534
|
destroy() {
|
|
6535
|
+
this.finishKeyboardMove();
|
|
6398
6536
|
if (this.transformRenderFrame !== null)
|
|
6399
6537
|
this.container.ownerDocument.defaultView?.cancelAnimationFrame?.(
|
|
6400
6538
|
this.transformRenderFrame
|
|
@@ -6549,7 +6687,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
6549
6687
|
"body",
|
|
6550
6688
|
source.element
|
|
6551
6689
|
));
|
|
6552
|
-
this.canvas.style.cursor = rowBoundary ? "row-resize" : boundary ? "col-resize" : editable ? source.previewElement && this.componentCellSelection(source.previewElement) && !event.altKey ? "crosshair" : "move" : "grab";
|
|
6690
|
+
this.canvas.style.cursor = rowBoundary ? "row-resize" : boundary ? "col-resize" : editable ? source.previewElement && this.componentCellSelection(source.previewElement) && event.shiftKey && !event.altKey ? "crosshair" : "move" : "grab";
|
|
6553
6691
|
return;
|
|
6554
6692
|
}
|
|
6555
6693
|
const rect = this.canvas.getBoundingClientRect();
|
|
@@ -6764,7 +6902,7 @@ var PrintDesignInteraction = class extends PrintDesignRenderer {
|
|
|
6764
6902
|
this.canvas.style.cursor = "col-resize";
|
|
6765
6903
|
return;
|
|
6766
6904
|
}
|
|
6767
|
-
this.canvas.style.cursor = hit && this.componentCellSelection(hit) && !hit.locked && !event.altKey ? "crosshair" : hit && !hit.locked ? "move" : "default";
|
|
6905
|
+
this.canvas.style.cursor = hit && this.componentCellSelection(hit) && !hit.locked && event.shiftKey && !event.altKey ? "crosshair" : hit && !hit.locked ? "move" : "default";
|
|
6768
6906
|
}
|
|
6769
6907
|
removeSelectedGuide() {
|
|
6770
6908
|
const selected = this.selectedGuide;
|
|
@@ -6876,8 +7014,8 @@ var PrintDesignGrouping = class extends PrintDesignInteraction {
|
|
|
6876
7014
|
renameGroup(groupId, name) {
|
|
6877
7015
|
const normalized = name.trim();
|
|
6878
7016
|
if (this.options.readonly || !normalized) return this;
|
|
6879
|
-
this.commitMutation((
|
|
6880
|
-
for (const element of this.getPageIn(
|
|
7017
|
+
this.commitMutation((document) => {
|
|
7018
|
+
for (const element of this.getPageIn(document).elements) {
|
|
6881
7019
|
const group = element.groupPath?.find((item) => item.id === groupId);
|
|
6882
7020
|
if (group) group.name = normalized;
|
|
6883
7021
|
}
|
|
@@ -6888,8 +7026,8 @@ var PrintDesignGrouping = class extends PrintDesignInteraction {
|
|
|
6888
7026
|
moveLayerRelative(elementId, targetId, after, depth = 0) {
|
|
6889
7027
|
if (this.options.readonly || !Number.isInteger(depth) || depth < 0)
|
|
6890
7028
|
return this;
|
|
6891
|
-
this.commitMutation((
|
|
6892
|
-
const page = this.getPageIn(
|
|
7029
|
+
this.commitMutation((document) => {
|
|
7030
|
+
const page = this.getPageIn(document);
|
|
6893
7031
|
const source = page.elements.find((item) => item.id === elementId);
|
|
6894
7032
|
const target = page.elements.find((item) => item.id === targetId);
|
|
6895
7033
|
if (!source || !target) return;
|
|
@@ -6918,50 +7056,50 @@ var PrintDesignGrouping = class extends PrintDesignInteraction {
|
|
|
6918
7056
|
groupElements(elementIds = this.selectedElementIds) {
|
|
6919
7057
|
if (this.options.readonly) return this;
|
|
6920
7058
|
const elements = this.getActivePage().elements;
|
|
6921
|
-
const
|
|
6922
|
-
const members = elements.filter((item) =>
|
|
7059
|
+
const ids2 = expandGroupSelection(elements, elementIds);
|
|
7060
|
+
const members = elements.filter((item) => ids2.includes(item.id));
|
|
6923
7061
|
const units = new Set(
|
|
6924
7062
|
members.map((item) => item.groupPath?.[0]?.id ?? item.id)
|
|
6925
7063
|
);
|
|
6926
7064
|
if (units.size < 2 || members.some((item) => item.locked)) return this;
|
|
6927
7065
|
const group = { id: createPrintDesignId("group"), name: "\u7EC4\u5408" };
|
|
6928
|
-
this.commitMutation((
|
|
6929
|
-
const page = this.getPageIn(
|
|
6930
|
-
const selected = page.elements.filter((item) =>
|
|
7066
|
+
this.commitMutation((document) => {
|
|
7067
|
+
const page = this.getPageIn(document);
|
|
7068
|
+
const selected = page.elements.filter((item) => ids2.includes(item.id));
|
|
6931
7069
|
const highest = Math.max(
|
|
6932
7070
|
...selected.map((item) => page.elements.indexOf(item))
|
|
6933
7071
|
);
|
|
6934
|
-
const before = page.elements.slice(0, highest + 1).filter((item) => !
|
|
7072
|
+
const before = page.elements.slice(0, highest + 1).filter((item) => !ids2.includes(item.id));
|
|
6935
7073
|
const after = page.elements.slice(highest + 1);
|
|
6936
7074
|
selected.forEach((item) => {
|
|
6937
7075
|
item.groupPath = [group, ...item.groupPath ?? []];
|
|
6938
7076
|
});
|
|
6939
7077
|
page.elements = [...before, ...selected, ...after];
|
|
6940
7078
|
});
|
|
6941
|
-
return this.selectElements(
|
|
7079
|
+
return this.selectElements(ids2);
|
|
6942
7080
|
}
|
|
6943
7081
|
/** 取消选中组合的最外层,保留其内部嵌套组合。 */
|
|
6944
7082
|
ungroupElements(elementIds = this.selectedElementIds) {
|
|
6945
7083
|
if (this.options.readonly) return this;
|
|
6946
|
-
const
|
|
7084
|
+
const ids2 = expandGroupSelection(this.getActivePage().elements, elementIds);
|
|
6947
7085
|
if (this.getActivePage().elements.some(
|
|
6948
|
-
(item) =>
|
|
7086
|
+
(item) => ids2.includes(item.id) && item.locked
|
|
6949
7087
|
))
|
|
6950
7088
|
return this;
|
|
6951
|
-
this.commitMutation((
|
|
6952
|
-
for (const item of this.getPageIn(
|
|
6953
|
-
if (!
|
|
7089
|
+
this.commitMutation((document) => {
|
|
7090
|
+
for (const item of this.getPageIn(document).elements) {
|
|
7091
|
+
if (!ids2.includes(item.id) || !item.groupPath?.length) continue;
|
|
6954
7092
|
item.groupPath = item.groupPath.slice(1);
|
|
6955
7093
|
if (!item.groupPath.length) delete item.groupPath;
|
|
6956
7094
|
}
|
|
6957
7095
|
});
|
|
6958
|
-
return this.selectElements(
|
|
7096
|
+
return this.selectElements(ids2);
|
|
6959
7097
|
}
|
|
6960
7098
|
};
|
|
6961
7099
|
|
|
6962
7100
|
// src/helper/layer-alignment.ts
|
|
6963
|
-
function getPrintDesignAlignmentUnits(elements,
|
|
6964
|
-
const selected = new Set(
|
|
7101
|
+
function getPrintDesignAlignmentUnits(elements, ids2) {
|
|
7102
|
+
const selected = new Set(ids2);
|
|
6965
7103
|
const groups = /* @__PURE__ */ new Map();
|
|
6966
7104
|
for (const element of elements)
|
|
6967
7105
|
for (const group of element.groupPath ?? []) {
|
|
@@ -6983,8 +7121,8 @@ function getPrintDesignAlignmentUnits(elements, ids) {
|
|
|
6983
7121
|
}
|
|
6984
7122
|
return units;
|
|
6985
7123
|
}
|
|
6986
|
-
function alignPrintDesignLayers(page,
|
|
6987
|
-
const units = getPrintDesignAlignmentUnits(page.elements,
|
|
7124
|
+
function alignPrintDesignLayers(page, ids2, align, data = {}, componentRegistry) {
|
|
7125
|
+
const units = getPrintDesignAlignmentUnits(page.elements, ids2);
|
|
6988
7126
|
if (!units.length || units.some((unit) => unit.some((item) => item.locked)))
|
|
6989
7127
|
return;
|
|
6990
7128
|
const boxes = units.map((unit) => {
|
|
@@ -7121,8 +7259,8 @@ var PrintDesignActions = class extends PrintDesignGrouping {
|
|
|
7121
7259
|
if (!element || element.locked) return this;
|
|
7122
7260
|
const commands = this.componentCommands(element);
|
|
7123
7261
|
if (!commands) return this;
|
|
7124
|
-
this.commitMutation((
|
|
7125
|
-
const page = this.getPageIn(
|
|
7262
|
+
this.commitMutation((document) => {
|
|
7263
|
+
const page = this.getPageIn(document);
|
|
7126
7264
|
const index = page.elements.findIndex((item) => item.id === elementId);
|
|
7127
7265
|
if (index < 0) return;
|
|
7128
7266
|
const target = page.elements[index];
|
|
@@ -7241,8 +7379,8 @@ var PrintDesignActions = class extends PrintDesignGrouping {
|
|
|
7241
7379
|
this.options.componentRegistry?.get(item.type)?.translatePageCoordinates?.(copy, item, 5, 5);
|
|
7242
7380
|
return copy;
|
|
7243
7381
|
});
|
|
7244
|
-
this.commitMutation((
|
|
7245
|
-
this.getPageIn(
|
|
7382
|
+
this.commitMutation((document) => {
|
|
7383
|
+
this.getPageIn(document).elements.push(...copies);
|
|
7246
7384
|
});
|
|
7247
7385
|
return this.selectElements(copies.map((item) => item.id));
|
|
7248
7386
|
}
|
|
@@ -7254,33 +7392,33 @@ var PrintDesignActions = class extends PrintDesignGrouping {
|
|
|
7254
7392
|
/** 设置选区锁定或显隐状态。 */
|
|
7255
7393
|
setSelectionState(patch) {
|
|
7256
7394
|
if (this.options.readonly) return this;
|
|
7257
|
-
const
|
|
7395
|
+
const ids2 = new Set(
|
|
7258
7396
|
expandGroupSelection(
|
|
7259
7397
|
this.getActivePage().elements,
|
|
7260
7398
|
this.selectedElementIds
|
|
7261
7399
|
)
|
|
7262
7400
|
);
|
|
7263
|
-
this.commitMutation((
|
|
7264
|
-
for (const item of this.getPageIn(
|
|
7265
|
-
if (
|
|
7401
|
+
this.commitMutation((document) => {
|
|
7402
|
+
for (const item of this.getPageIn(document).elements)
|
|
7403
|
+
if (ids2.has(item.id)) Object.assign(item, patch);
|
|
7266
7404
|
});
|
|
7267
7405
|
return this;
|
|
7268
7406
|
}
|
|
7269
7407
|
/** 精确设置指定图层状态;组合节点由调用方传入全部成员,一次操作对应一次撤销。 */
|
|
7270
7408
|
setElementsState(elementIds, patch) {
|
|
7271
7409
|
if (this.options.readonly) return this;
|
|
7272
|
-
const
|
|
7273
|
-
this.commitMutation((
|
|
7274
|
-
for (const item of this.getPageIn(
|
|
7275
|
-
if (
|
|
7410
|
+
const ids2 = new Set(elementIds);
|
|
7411
|
+
this.commitMutation((document) => {
|
|
7412
|
+
for (const item of this.getPageIn(document).elements)
|
|
7413
|
+
if (ids2.has(item.id)) Object.assign(item, patch);
|
|
7276
7414
|
});
|
|
7277
7415
|
return this;
|
|
7278
7416
|
}
|
|
7279
7417
|
/** 全部解锁或显示当前页元素。 */
|
|
7280
7418
|
setAllElementState(patch) {
|
|
7281
7419
|
if (this.options.readonly) return this;
|
|
7282
|
-
this.commitMutation((
|
|
7283
|
-
for (const item of this.getPageIn(
|
|
7420
|
+
this.commitMutation((document) => {
|
|
7421
|
+
for (const item of this.getPageIn(document).elements)
|
|
7284
7422
|
Object.assign(item, patch);
|
|
7285
7423
|
});
|
|
7286
7424
|
return this;
|
|
@@ -7289,9 +7427,9 @@ var PrintDesignActions = class extends PrintDesignGrouping {
|
|
|
7289
7427
|
moveSelectionLayer(direction) {
|
|
7290
7428
|
if (this.options.readonly || this.selectedElements().some((item) => item.locked))
|
|
7291
7429
|
return this;
|
|
7292
|
-
const
|
|
7293
|
-
this.commitMutation((
|
|
7294
|
-
const page = this.getPageIn(
|
|
7430
|
+
const ids2 = new Set(this.selectedElementIds);
|
|
7431
|
+
this.commitMutation((document) => {
|
|
7432
|
+
const page = this.getPageIn(document);
|
|
7295
7433
|
const units = [];
|
|
7296
7434
|
const seen = /* @__PURE__ */ new Set();
|
|
7297
7435
|
for (const item of page.elements) {
|
|
@@ -7300,7 +7438,7 @@ var PrintDesignActions = class extends PrintDesignGrouping {
|
|
|
7300
7438
|
members.forEach((member) => seen.add(member.id));
|
|
7301
7439
|
units.push(members);
|
|
7302
7440
|
}
|
|
7303
|
-
const selected = (unit) => unit.some((item) =>
|
|
7441
|
+
const selected = (unit) => unit.some((item) => ids2.has(item.id));
|
|
7304
7442
|
if (direction === "front" || direction === "back") {
|
|
7305
7443
|
const picked = units.filter(selected), rest = units.filter((unit) => !selected(unit));
|
|
7306
7444
|
page.elements = (direction === "front" ? [...rest, ...picked] : [...picked, ...rest]).flat();
|
|
@@ -7323,9 +7461,9 @@ var PrintDesignActions = class extends PrintDesignGrouping {
|
|
|
7323
7461
|
/** 单个图层/组合对齐纸张,多选对齐选区;每次操作提交一条历史。 */
|
|
7324
7462
|
alignSelection(align) {
|
|
7325
7463
|
if (this.options.readonly) return this;
|
|
7326
|
-
this.commitMutation((
|
|
7464
|
+
this.commitMutation((document) => {
|
|
7327
7465
|
alignPrintDesignLayers(
|
|
7328
|
-
this.getPageIn(
|
|
7466
|
+
this.getPageIn(document),
|
|
7329
7467
|
this.selectedElementIds,
|
|
7330
7468
|
align,
|
|
7331
7469
|
this.options.data,
|
|
@@ -7343,7 +7481,7 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7343
7481
|
__publicField(this, "temporaryDocumentSnapshot", null);
|
|
7344
7482
|
}
|
|
7345
7483
|
/** 临时显示演示文档;原文档、选区与撤销记录保留在实例中。 */
|
|
7346
|
-
showTemporaryDocument(
|
|
7484
|
+
showTemporaryDocument(document, pageId, elementId) {
|
|
7347
7485
|
this.temporaryDocumentSnapshot ?? (this.temporaryDocumentSnapshot = {
|
|
7348
7486
|
document: this.getDocument(),
|
|
7349
7487
|
activePageId: this.activePageId,
|
|
@@ -7351,7 +7489,7 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7351
7489
|
guide: this.selectedGuide ? { ...this.selectedGuide } : null
|
|
7352
7490
|
});
|
|
7353
7491
|
this.document = parsePrintDesignDocument(
|
|
7354
|
-
|
|
7492
|
+
document,
|
|
7355
7493
|
this.options.componentRegistry ?? builtinPrintDesignComponentRegistry
|
|
7356
7494
|
);
|
|
7357
7495
|
this.activePageId = pageId && this.document.pages.some((page) => page.id === pageId) ? pageId : this.document.pages[0].id;
|
|
@@ -7383,10 +7521,10 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7383
7521
|
return clonePrintDesignDocument(this.document);
|
|
7384
7522
|
}
|
|
7385
7523
|
/** 替换当前文档并清空撤销历史。 */
|
|
7386
|
-
setDocument(
|
|
7524
|
+
setDocument(document) {
|
|
7387
7525
|
this.temporaryDocumentSnapshot = null;
|
|
7388
7526
|
this.document = parsePrintDesignDocument(
|
|
7389
|
-
|
|
7527
|
+
document,
|
|
7390
7528
|
this.options.componentRegistry ?? builtinPrintDesignComponentRegistry
|
|
7391
7529
|
);
|
|
7392
7530
|
this.activePageId = this.document.pages[0].id;
|
|
@@ -7470,16 +7608,16 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7470
7608
|
}
|
|
7471
7609
|
/** 新增空白页面并切换到该页。 */
|
|
7472
7610
|
addPage(page = createPrintDesignPage(`\u9875\u9762 ${this.document.pages.length + 1}`)) {
|
|
7473
|
-
this.commitMutation((
|
|
7474
|
-
|
|
7611
|
+
this.commitMutation((document) => {
|
|
7612
|
+
document.pages.push(clonePrintDesignDocument(page));
|
|
7475
7613
|
});
|
|
7476
7614
|
this.setActivePage(page.id);
|
|
7477
7615
|
return page.id;
|
|
7478
7616
|
}
|
|
7479
7617
|
/** 更新页面纸张、页边距或背景属性。 */
|
|
7480
7618
|
updatePage(pageId, patch) {
|
|
7481
|
-
this.commitMutation((
|
|
7482
|
-
const page =
|
|
7619
|
+
this.commitMutation((document) => {
|
|
7620
|
+
const page = document.pages.find((item) => item.id === pageId);
|
|
7483
7621
|
if (page)
|
|
7484
7622
|
Object.assign(page, patch, { id: page.id, elements: page.elements });
|
|
7485
7623
|
});
|
|
@@ -7488,8 +7626,8 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7488
7626
|
/** 删除页面;文档至少保留一页。 */
|
|
7489
7627
|
removePage(pageId) {
|
|
7490
7628
|
if (this.document.pages.length <= 1) return this;
|
|
7491
|
-
this.commitMutation((
|
|
7492
|
-
|
|
7629
|
+
this.commitMutation((document) => {
|
|
7630
|
+
document.pages = document.pages.filter((page) => page.id !== pageId);
|
|
7493
7631
|
});
|
|
7494
7632
|
if (this.activePageId === pageId)
|
|
7495
7633
|
this.setActivePage(this.document.pages[0].id);
|
|
@@ -7519,8 +7657,8 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7519
7657
|
return this;
|
|
7520
7658
|
const guides = this.getActivePage().guides?.[axis];
|
|
7521
7659
|
if (!guides || index < 0 || index >= guides.length) return this;
|
|
7522
|
-
this.commitMutation((
|
|
7523
|
-
this.getPageIn(
|
|
7660
|
+
this.commitMutation((document) => {
|
|
7661
|
+
this.getPageIn(document).guides[axis][index] = position;
|
|
7524
7662
|
});
|
|
7525
7663
|
return this;
|
|
7526
7664
|
}
|
|
@@ -7560,6 +7698,15 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7560
7698
|
y: (y - origin.y) / origin.scale
|
|
7561
7699
|
};
|
|
7562
7700
|
}
|
|
7701
|
+
/** 将当前页面的毫米坐标转换为浏览器视口坐标。 */
|
|
7702
|
+
pageToClientPoint(x, y) {
|
|
7703
|
+
const origin = this.pageOrigin();
|
|
7704
|
+
const canvas = this.canvas.getBoundingClientRect();
|
|
7705
|
+
return {
|
|
7706
|
+
x: canvas.left + origin.x + x * origin.scale,
|
|
7707
|
+
y: canvas.top + origin.y + y * origin.scale
|
|
7708
|
+
};
|
|
7709
|
+
}
|
|
7563
7710
|
/** 获取当前页元素在浏览器视口中的包围盒,包含旋转后的占用范围。 */
|
|
7564
7711
|
getElementClientRect(elementId) {
|
|
7565
7712
|
const element = this.getActivePage().elements.find(
|
|
@@ -7583,8 +7730,8 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7583
7730
|
/** 新增元素并选中。 */
|
|
7584
7731
|
addElement(element) {
|
|
7585
7732
|
const next = { ...element, id: element.id ?? createPrintDesignId() };
|
|
7586
|
-
this.commitMutation((
|
|
7587
|
-
this.getPageIn(
|
|
7733
|
+
this.commitMutation((document) => {
|
|
7734
|
+
this.getPageIn(document).elements.push(
|
|
7588
7735
|
clonePrintDesignDocument(next)
|
|
7589
7736
|
);
|
|
7590
7737
|
});
|
|
@@ -7601,8 +7748,8 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7601
7748
|
);
|
|
7602
7749
|
const beforeCropBehavior = beforeElement ? this.options.componentRegistry?.get(beforeElement.type)?.crop : void 0;
|
|
7603
7750
|
const previousSource = beforeElement && beforeCropBehavior?.source(beforeElement);
|
|
7604
|
-
this.commitMutation((
|
|
7605
|
-
const page = this.getPageIn(
|
|
7751
|
+
this.commitMutation((document) => {
|
|
7752
|
+
const page = this.getPageIn(document);
|
|
7606
7753
|
const element = page.elements.find((item) => item.id === elementId);
|
|
7607
7754
|
if (element) {
|
|
7608
7755
|
this.options.componentRegistry?.get(element.type)?.beforeUpdate?.(element, patch);
|
|
@@ -7612,7 +7759,7 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7612
7759
|
)?.onUpdate;
|
|
7613
7760
|
if (onUpdate)
|
|
7614
7761
|
onUpdate(element, patch, {
|
|
7615
|
-
document
|
|
7762
|
+
document,
|
|
7616
7763
|
page,
|
|
7617
7764
|
data: this.options.data ?? {}
|
|
7618
7765
|
});
|
|
@@ -7630,8 +7777,8 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7630
7777
|
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)
|
|
7631
7778
|
return;
|
|
7632
7779
|
const bounds = cropBehavior.fitToPage(source, page, current);
|
|
7633
|
-
this.commitMutation((
|
|
7634
|
-
const target =
|
|
7780
|
+
this.commitMutation((document) => {
|
|
7781
|
+
const target = document.pages.find((item) => item.id === pageId)?.elements.find((item) => item.id === elementId);
|
|
7635
7782
|
if (target?.type === component.type && cropBehavior.source(target) === src)
|
|
7636
7783
|
Object.assign(target, bounds);
|
|
7637
7784
|
});
|
|
@@ -7647,18 +7794,18 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7647
7794
|
/** 一次重置选中组件的通用和组件专属变形。 */
|
|
7648
7795
|
resetElementTransforms(elementIds = this.selectedElementIds) {
|
|
7649
7796
|
if (!elementIds.length) return this;
|
|
7650
|
-
const
|
|
7797
|
+
const ids2 = new Set(elementIds);
|
|
7651
7798
|
const targets = this.getActivePage().elements.filter(
|
|
7652
|
-
(element) =>
|
|
7799
|
+
(element) => ids2.has(element.id)
|
|
7653
7800
|
);
|
|
7654
7801
|
if (targets.some((element) => element.groupPath?.length)) return this;
|
|
7655
7802
|
if (!targets.some(
|
|
7656
|
-
(element) =>
|
|
7803
|
+
(element) => ids2.has(element.id) && !element.locked && this.hasElementTransform(element)
|
|
7657
7804
|
))
|
|
7658
7805
|
return this;
|
|
7659
|
-
this.commitMutation((
|
|
7660
|
-
for (const element of this.getPageIn(
|
|
7661
|
-
if (!
|
|
7806
|
+
this.commitMutation((document) => {
|
|
7807
|
+
for (const element of this.getPageIn(document).elements) {
|
|
7808
|
+
if (!ids2.has(element.id) || element.locked) continue;
|
|
7662
7809
|
element.transform = void 0;
|
|
7663
7810
|
this.options.componentRegistry?.get(element.type)?.transformReset?.reset(element);
|
|
7664
7811
|
}
|
|
@@ -7668,25 +7815,25 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7668
7815
|
/** 删除当前选区或指定元素。 */
|
|
7669
7816
|
removeElements(elementIds = this.selectedElementIds) {
|
|
7670
7817
|
if (!elementIds.length) return this;
|
|
7671
|
-
const
|
|
7818
|
+
const ids2 = new Set(
|
|
7672
7819
|
expandGroupSelection(this.getActivePage().elements, elementIds)
|
|
7673
7820
|
);
|
|
7674
|
-
this.commitMutation((
|
|
7675
|
-
const page = this.getPageIn(
|
|
7821
|
+
this.commitMutation((document) => {
|
|
7822
|
+
const page = this.getPageIn(document);
|
|
7676
7823
|
const componentRegistry = this.options.componentRegistry ?? builtinPrintDesignComponentRegistry;
|
|
7677
7824
|
for (const element of page.elements) {
|
|
7678
|
-
if (
|
|
7825
|
+
if (ids2.has(element.id)) continue;
|
|
7679
7826
|
componentRegistry.get(element.type)?.onElementsRemoved?.(element, {
|
|
7680
7827
|
page,
|
|
7681
|
-
removedIds:
|
|
7828
|
+
removedIds: ids2,
|
|
7682
7829
|
data: this.options.data ?? {},
|
|
7683
7830
|
componentRegistry
|
|
7684
7831
|
});
|
|
7685
7832
|
}
|
|
7686
|
-
page.elements = page.elements.filter((element) => !
|
|
7833
|
+
page.elements = page.elements.filter((element) => !ids2.has(element.id));
|
|
7687
7834
|
});
|
|
7688
7835
|
this.selectedElementIds = this.selectedElementIds.filter(
|
|
7689
|
-
(id) => !
|
|
7836
|
+
(id) => !ids2.has(id)
|
|
7690
7837
|
);
|
|
7691
7838
|
this.emitSelection();
|
|
7692
7839
|
this.render();
|
|
@@ -7708,8 +7855,8 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7708
7855
|
/** 设置元素层级,索引从 0(底层)开始;越界值限制到当前页面范围。 */
|
|
7709
7856
|
setElementLayer(elementId, layerIndex) {
|
|
7710
7857
|
if (this.options.readonly || !Number.isInteger(layerIndex)) return this;
|
|
7711
|
-
this.commitMutation((
|
|
7712
|
-
const elements = this.getPageIn(
|
|
7858
|
+
this.commitMutation((document) => {
|
|
7859
|
+
const elements = this.getPageIn(document).elements;
|
|
7713
7860
|
const index = elements.findIndex((element) => element.id === elementId);
|
|
7714
7861
|
if (index < 0) return;
|
|
7715
7862
|
const target = Math.max(0, Math.min(elements.length - 1, layerIndex));
|
|
@@ -7722,7 +7869,7 @@ var PrintDesign = class extends PrintDesignActions {
|
|
|
7722
7869
|
const insertion = elements.slice(0, boundary).filter((item) => !memberIds.has(item.id)).length;
|
|
7723
7870
|
const remaining = elements.filter((item) => !memberIds.has(item.id));
|
|
7724
7871
|
remaining.splice(insertion, 0, ...members);
|
|
7725
|
-
this.getPageIn(
|
|
7872
|
+
this.getPageIn(document).elements = remaining;
|
|
7726
7873
|
});
|
|
7727
7874
|
return this;
|
|
7728
7875
|
}
|
|
@@ -7899,12 +8046,311 @@ function getPrintDesignPageSize(page) {
|
|
|
7899
8046
|
return { width: page.width, height: page.height };
|
|
7900
8047
|
}
|
|
7901
8048
|
|
|
8049
|
+
// src/agent/agent-api.ts
|
|
8050
|
+
var objectSchema = (properties, required = []) => ({
|
|
8051
|
+
type: "object",
|
|
8052
|
+
properties,
|
|
8053
|
+
required,
|
|
8054
|
+
additionalProperties: false
|
|
8055
|
+
});
|
|
8056
|
+
var idSchema = { type: "string", minLength: 1 };
|
|
8057
|
+
var idsSchema = { type: "array", items: idSchema, minItems: 1 };
|
|
8058
|
+
var printDesignAgentTools = [
|
|
8059
|
+
{
|
|
8060
|
+
name: "get_document",
|
|
8061
|
+
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",
|
|
8062
|
+
parameters: objectSchema({})
|
|
8063
|
+
},
|
|
8064
|
+
{
|
|
8065
|
+
name: "get_selection",
|
|
8066
|
+
description: "\u8BFB\u53D6\u5F53\u524D\u6D3B\u52A8\u9875\u548C\u9009\u4E2D\u7684\u5143\u7D20 ID\u3002",
|
|
8067
|
+
parameters: objectSchema({})
|
|
8068
|
+
},
|
|
8069
|
+
{
|
|
8070
|
+
name: "list_components",
|
|
8071
|
+
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",
|
|
8072
|
+
parameters: objectSchema({})
|
|
8073
|
+
},
|
|
8074
|
+
{
|
|
8075
|
+
name: "get_component_template",
|
|
8076
|
+
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",
|
|
8077
|
+
parameters: objectSchema({ type: idSchema, preset: { type: "string" } }, [
|
|
8078
|
+
"type"
|
|
8079
|
+
])
|
|
8080
|
+
},
|
|
8081
|
+
{
|
|
8082
|
+
name: "get_element",
|
|
8083
|
+
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",
|
|
8084
|
+
parameters: objectSchema({ elementId: idSchema }, ["elementId"])
|
|
8085
|
+
},
|
|
8086
|
+
{
|
|
8087
|
+
name: "set_active_page",
|
|
8088
|
+
description: "\u5207\u6362\u5F53\u524D\u6D3B\u52A8\u9875\u3002",
|
|
8089
|
+
parameters: objectSchema({ pageId: idSchema }, ["pageId"])
|
|
8090
|
+
},
|
|
8091
|
+
{
|
|
8092
|
+
name: "select_elements",
|
|
8093
|
+
description: "\u5728\u5F53\u524D\u9875\u9009\u4E2D\u5143\u7D20\u3002",
|
|
8094
|
+
parameters: objectSchema({ elementIds: idsSchema }, ["elementIds"])
|
|
8095
|
+
},
|
|
8096
|
+
{
|
|
8097
|
+
name: "add_element",
|
|
8098
|
+
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",
|
|
8099
|
+
parameters: objectSchema({ element: { type: "object" } }, ["element"])
|
|
8100
|
+
},
|
|
8101
|
+
{
|
|
8102
|
+
name: "update_element",
|
|
8103
|
+
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",
|
|
8104
|
+
parameters: objectSchema(
|
|
8105
|
+
{ elementId: idSchema, patch: { type: "object" } },
|
|
8106
|
+
["elementId", "patch"]
|
|
8107
|
+
)
|
|
8108
|
+
},
|
|
8109
|
+
{
|
|
8110
|
+
name: "remove_elements",
|
|
8111
|
+
description: "\u5220\u9664\u5F53\u524D\u9875\u6307\u5B9A\u5143\u7D20\u3002",
|
|
8112
|
+
parameters: objectSchema({ elementIds: idsSchema }, ["elementIds"])
|
|
8113
|
+
},
|
|
8114
|
+
{
|
|
8115
|
+
name: "execute_component_command",
|
|
8116
|
+
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",
|
|
8117
|
+
parameters: objectSchema(
|
|
8118
|
+
{
|
|
8119
|
+
elementId: idSchema,
|
|
8120
|
+
command: idSchema,
|
|
8121
|
+
payload: { type: "object" }
|
|
8122
|
+
},
|
|
8123
|
+
["elementId", "command", "payload"]
|
|
8124
|
+
)
|
|
8125
|
+
},
|
|
8126
|
+
{
|
|
8127
|
+
name: "undo",
|
|
8128
|
+
description: "\u64A4\u9500\u4E0A\u4E00\u6B65\u7F16\u8F91\u3002",
|
|
8129
|
+
parameters: objectSchema({})
|
|
8130
|
+
},
|
|
8131
|
+
{
|
|
8132
|
+
name: "redo",
|
|
8133
|
+
description: "\u91CD\u505A\u4E0A\u4E00\u6B65\u7F16\u8F91\u3002",
|
|
8134
|
+
parameters: objectSchema({})
|
|
8135
|
+
}
|
|
8136
|
+
];
|
|
8137
|
+
function record(input) {
|
|
8138
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
8139
|
+
throw new TypeError("\u5DE5\u5177\u53C2\u6570\u5FC5\u987B\u662F\u5BF9\u8C61\u3002");
|
|
8140
|
+
return input;
|
|
8141
|
+
}
|
|
8142
|
+
function string(input, name) {
|
|
8143
|
+
if (typeof input !== "string" || !input.trim())
|
|
8144
|
+
throw new TypeError(`${name} \u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32\u3002`);
|
|
8145
|
+
return input;
|
|
8146
|
+
}
|
|
8147
|
+
function ids(input) {
|
|
8148
|
+
if (!Array.isArray(input) || !input.length)
|
|
8149
|
+
throw new TypeError("elementIds \u5FC5\u987B\u662F\u975E\u7A7A\u6570\u7EC4\u3002");
|
|
8150
|
+
return input.map((id) => string(id, "elementId"));
|
|
8151
|
+
}
|
|
8152
|
+
function safeJson(input, depth = 0) {
|
|
8153
|
+
if (depth > 20) throw new RangeError("\u5DE5\u5177\u53C2\u6570\u5D4C\u5957\u8FC7\u6DF1\u3002");
|
|
8154
|
+
if (input === null || typeof input === "string" || typeof input === "boolean")
|
|
8155
|
+
return;
|
|
8156
|
+
if (typeof input === "number" && Number.isFinite(input)) return;
|
|
8157
|
+
if (Array.isArray(input)) {
|
|
8158
|
+
if (input.length > 1e4) throw new RangeError("\u5DE5\u5177\u6570\u7EC4\u8FC7\u957F\u3002");
|
|
8159
|
+
input.forEach((item) => safeJson(item, depth + 1));
|
|
8160
|
+
return;
|
|
8161
|
+
}
|
|
8162
|
+
if (!input || typeof input !== "object")
|
|
8163
|
+
throw new TypeError("\u5DE5\u5177\u53C2\u6570\u5FC5\u987B\u662F JSON \u6570\u636E\u3002");
|
|
8164
|
+
const entries = Object.entries(input);
|
|
8165
|
+
if (entries.length > 1e3) throw new RangeError("\u5DE5\u5177\u5BF9\u8C61\u5B57\u6BB5\u8FC7\u591A\u3002");
|
|
8166
|
+
for (const [key, value] of entries) {
|
|
8167
|
+
if (["__proto__", "prototype", "constructor"].includes(key))
|
|
8168
|
+
throw new TypeError("\u5DE5\u5177\u53C2\u6570\u5305\u542B\u975E\u6CD5\u5B57\u6BB5\u3002");
|
|
8169
|
+
safeJson(value, depth + 1);
|
|
8170
|
+
}
|
|
8171
|
+
}
|
|
8172
|
+
var geometryKeys = ["x", "y", "width", "height"];
|
|
8173
|
+
function convertGeometry(value, unit, convert) {
|
|
8174
|
+
const result = {
|
|
8175
|
+
...value
|
|
8176
|
+
};
|
|
8177
|
+
for (const key of geometryKeys)
|
|
8178
|
+
if (typeof result[key] === "number")
|
|
8179
|
+
result[key] = convert(result[key], unit);
|
|
8180
|
+
return result;
|
|
8181
|
+
}
|
|
8182
|
+
function documentInDisplayUnit(document, unit) {
|
|
8183
|
+
return {
|
|
8184
|
+
...document,
|
|
8185
|
+
displayUnit: unit,
|
|
8186
|
+
pages: document.pages.map((page) => ({
|
|
8187
|
+
...page,
|
|
8188
|
+
width: toDisplayUnit(page.width, unit),
|
|
8189
|
+
height: toDisplayUnit(page.height, unit),
|
|
8190
|
+
...page.margins ? {
|
|
8191
|
+
margins: Object.fromEntries(
|
|
8192
|
+
Object.entries(page.margins).map(([key, value]) => [
|
|
8193
|
+
key,
|
|
8194
|
+
toDisplayUnit(value, unit)
|
|
8195
|
+
])
|
|
8196
|
+
)
|
|
8197
|
+
} : {},
|
|
8198
|
+
...page.guides ? {
|
|
8199
|
+
guides: {
|
|
8200
|
+
vertical: page.guides.vertical?.map(
|
|
8201
|
+
(value) => toDisplayUnit(value, unit)
|
|
8202
|
+
),
|
|
8203
|
+
horizontal: page.guides.horizontal?.map(
|
|
8204
|
+
(value) => toDisplayUnit(value, unit)
|
|
8205
|
+
)
|
|
8206
|
+
}
|
|
8207
|
+
} : {},
|
|
8208
|
+
elements: page.elements.map(
|
|
8209
|
+
(element) => convertGeometry(element, unit, toDisplayUnit)
|
|
8210
|
+
)
|
|
8211
|
+
}))
|
|
8212
|
+
};
|
|
8213
|
+
}
|
|
8214
|
+
function createPrintDesignAgentApi(getInstance, options = {}) {
|
|
8215
|
+
return {
|
|
8216
|
+
tools: printDesignAgentTools,
|
|
8217
|
+
call(name, input) {
|
|
8218
|
+
const instance = getInstance();
|
|
8219
|
+
if (!instance) throw new Error("\u6253\u5370\u8BBE\u8BA1\u5668\u5C1A\u672A\u5C31\u7EEA\u3002");
|
|
8220
|
+
const args = record(input);
|
|
8221
|
+
safeJson(args);
|
|
8222
|
+
const unit = options.displayUnit?.() ?? "mm";
|
|
8223
|
+
if (name === "get_document")
|
|
8224
|
+
return documentInDisplayUnit(instance.getDocument(), unit);
|
|
8225
|
+
if (name === "get_selection") return instance.getSelection();
|
|
8226
|
+
if (name === "list_components")
|
|
8227
|
+
return options.componentCatalog?.list().map(({ type, label }) => ({
|
|
8228
|
+
type,
|
|
8229
|
+
label
|
|
8230
|
+
})) ?? [];
|
|
8231
|
+
if (name === "get_component_template") {
|
|
8232
|
+
const type = string(args.type, "type");
|
|
8233
|
+
const factory = options.componentCatalog?.list().find((item) => item.type === type);
|
|
8234
|
+
if (!factory || !options.componentCatalog)
|
|
8235
|
+
throw new Error(`\u672A\u6CE8\u518C\u7EC4\u4EF6\u521B\u5EFA\u5668\uFF1A${type}`);
|
|
8236
|
+
const preset = args.preset === void 0 ? void 0 : string(args.preset, "preset");
|
|
8237
|
+
const { id: _ignored, ...element } = options.componentCatalog.create(
|
|
8238
|
+
type,
|
|
8239
|
+
preset
|
|
8240
|
+
);
|
|
8241
|
+
return {
|
|
8242
|
+
type,
|
|
8243
|
+
label: factory.label,
|
|
8244
|
+
element: convertGeometry(element, unit, toDisplayUnit),
|
|
8245
|
+
displayUnit: unit,
|
|
8246
|
+
commands: factory.agentCommands ?? []
|
|
8247
|
+
};
|
|
8248
|
+
}
|
|
8249
|
+
if (name === "get_element") {
|
|
8250
|
+
const elementId = string(args.elementId, "elementId");
|
|
8251
|
+
const found = instance.getDocument().pages.find((item) => item.id === instance.getActivePageId())?.elements.find((item) => item.id === elementId);
|
|
8252
|
+
if (!found) throw new Error(`\u627E\u4E0D\u5230\u5143\u7D20\uFF1A${elementId}`);
|
|
8253
|
+
return convertGeometry(found, unit, toDisplayUnit);
|
|
8254
|
+
}
|
|
8255
|
+
if (options.readonly?.()) throw new Error("\u5F53\u524D\u8BBE\u8BA1\u5668\u4E3A\u53EA\u8BFB\u72B6\u6001\u3002");
|
|
8256
|
+
const document = instance.getDocument();
|
|
8257
|
+
const page = document.pages.find(
|
|
8258
|
+
(item) => item.id === instance.getActivePageId()
|
|
8259
|
+
);
|
|
8260
|
+
if (name === "set_active_page") {
|
|
8261
|
+
const pageId = string(args.pageId, "pageId");
|
|
8262
|
+
if (!document.pages.some((item) => item.id === pageId))
|
|
8263
|
+
throw new Error(`\u627E\u4E0D\u5230\u9875\u9762\uFF1A${pageId}`);
|
|
8264
|
+
instance.setActivePage(pageId);
|
|
8265
|
+
return instance.getSelection();
|
|
8266
|
+
}
|
|
8267
|
+
if (name === "select_elements") {
|
|
8268
|
+
const elementIds = ids(args.elementIds);
|
|
8269
|
+
if (elementIds.some((id) => !page.elements.some((item) => item.id === id)))
|
|
8270
|
+
throw new Error("\u5F53\u524D\u9875\u4E0D\u5B58\u5728\u6307\u5B9A\u5143\u7D20\u3002");
|
|
8271
|
+
instance.selectElements(elementIds);
|
|
8272
|
+
return instance.getSelection();
|
|
8273
|
+
}
|
|
8274
|
+
if (name === "add_element") {
|
|
8275
|
+
const element = record(args.element);
|
|
8276
|
+
const type = string(element.type, "type");
|
|
8277
|
+
const { id: _ignored, ...fields } = element;
|
|
8278
|
+
const defaults = options.componentCatalog?.list().some((item) => item.type === type) ? options.componentCatalog.create(type) : null;
|
|
8279
|
+
if (options.componentCatalog && !defaults)
|
|
8280
|
+
throw new Error(`\u672A\u6CE8\u518C\u7EC4\u4EF6\u521B\u5EFA\u5668\uFF1A${type}`);
|
|
8281
|
+
const { id: _defaultId, ...defaultFields } = defaults ?? {};
|
|
8282
|
+
const id = instance.addElement({
|
|
8283
|
+
...defaultFields,
|
|
8284
|
+
...convertGeometry(fields, unit, fromDisplayUnit)
|
|
8285
|
+
});
|
|
8286
|
+
return { id };
|
|
8287
|
+
}
|
|
8288
|
+
if (name === "update_element") {
|
|
8289
|
+
const elementId = string(args.elementId, "elementId");
|
|
8290
|
+
const target = page.elements.find((item) => item.id === elementId);
|
|
8291
|
+
if (!target) throw new Error(`\u627E\u4E0D\u5230\u5143\u7D20\uFF1A${elementId}`);
|
|
8292
|
+
if (target.locked) throw new Error(`\u5143\u7D20\u5DF2\u9501\u5B9A\uFF1A${elementId}`);
|
|
8293
|
+
const patch = record(args.patch);
|
|
8294
|
+
if ("id" in patch || "type" in patch)
|
|
8295
|
+
throw new TypeError("\u4E0D\u80FD\u4FEE\u6539\u5143\u7D20 id \u6216 type\u3002");
|
|
8296
|
+
instance.updateElement(
|
|
8297
|
+
elementId,
|
|
8298
|
+
convertGeometry(
|
|
8299
|
+
patch,
|
|
8300
|
+
unit,
|
|
8301
|
+
fromDisplayUnit
|
|
8302
|
+
)
|
|
8303
|
+
);
|
|
8304
|
+
return { elementId };
|
|
8305
|
+
}
|
|
8306
|
+
if (name === "remove_elements") {
|
|
8307
|
+
const elementIds = ids(args.elementIds);
|
|
8308
|
+
if (elementIds.some((id) => !page.elements.some((item) => item.id === id)))
|
|
8309
|
+
throw new Error("\u5F53\u524D\u9875\u4E0D\u5B58\u5728\u6307\u5B9A\u5143\u7D20\u3002");
|
|
8310
|
+
if (page.elements.some(
|
|
8311
|
+
(item) => elementIds.includes(item.id) && item.locked
|
|
8312
|
+
))
|
|
8313
|
+
throw new Error("\u4E0D\u80FD\u5220\u9664\u5DF2\u9501\u5B9A\u5143\u7D20\u3002");
|
|
8314
|
+
instance.removeElements(elementIds);
|
|
8315
|
+
return { removed: elementIds };
|
|
8316
|
+
}
|
|
8317
|
+
if (name === "execute_component_command") {
|
|
8318
|
+
const elementId = string(args.elementId, "elementId");
|
|
8319
|
+
const command = string(args.command, "command");
|
|
8320
|
+
const target = page.elements.find((item) => item.id === elementId);
|
|
8321
|
+
if (!target) throw new Error(`\u627E\u4E0D\u5230\u5143\u7D20\uFF1A${elementId}`);
|
|
8322
|
+
if (target.locked) throw new Error(`\u5143\u7D20\u5DF2\u9501\u5B9A\uFF1A${elementId}`);
|
|
8323
|
+
const definitions = options.componentCatalog?.list().find((item) => item.type === target.type)?.agentCommands;
|
|
8324
|
+
if (definitions && !definitions.some((item) => item.name === command))
|
|
8325
|
+
throw new Error(`\u7EC4\u4EF6\u4E0D\u652F\u6301\u547D\u4EE4\uFF1A${command}`);
|
|
8326
|
+
const payload = record(args.payload);
|
|
8327
|
+
instance.executeComponentCommand(elementId, command, payload);
|
|
8328
|
+
const updated = instance.getDocument().pages.find((item) => item.id === page.id)?.elements.find((item) => item.id === elementId);
|
|
8329
|
+
return {
|
|
8330
|
+
elementId,
|
|
8331
|
+
command,
|
|
8332
|
+
applied: JSON.stringify(updated) !== JSON.stringify(target)
|
|
8333
|
+
};
|
|
8334
|
+
}
|
|
8335
|
+
if (name === "undo") {
|
|
8336
|
+
instance.undo();
|
|
8337
|
+
return { canUndo: instance.canUndo(), canRedo: instance.canRedo() };
|
|
8338
|
+
}
|
|
8339
|
+
if (name === "redo") {
|
|
8340
|
+
instance.redo();
|
|
8341
|
+
return { canUndo: instance.canUndo(), canRedo: instance.canRedo() };
|
|
8342
|
+
}
|
|
8343
|
+
throw new Error(`\u672A\u77E5 Agent \u5DE5\u5177\uFF1A${name}`);
|
|
8344
|
+
}
|
|
8345
|
+
};
|
|
8346
|
+
}
|
|
8347
|
+
|
|
7902
8348
|
// src/model/collection-data.ts
|
|
7903
8349
|
function isPrintDesignRecordCollection(value) {
|
|
7904
8350
|
return !!value && typeof value === "object" && !Array.isArray(value) && Array.isArray(value.dataSource);
|
|
7905
8351
|
}
|
|
7906
8352
|
|
|
7907
8353
|
// package.json
|
|
7908
|
-
var version = "0.1.
|
|
8354
|
+
var version = "0.1.12";
|
|
7909
8355
|
|
|
7910
|
-
export { DEFAULT_PRINT_DESIGN_DEBUG_OPTIONS, PRINT_DESIGN_CSS_DPI, PRINT_DESIGN_DEFAULT_FONT_FAMILY, PRINT_DESIGN_MM_PER_INCH, PRINT_DESIGN_SCHEMA_VERSION, PRINT_DESIGN_UNIT_MM, version as PRINT_DESIGN_VERSION, PrintDesign, PrintDesignComponentRegistry, buildPrintDesignLayerTree, builtinPrintDesignComponentRegistry, canvasPointToPageMillimeters, clonePrintDesignDocument, createPrintDesignDocument, createPrintDesignId, createPrintDesignPage, createPrintDesignPrintFrame, cssColor, cssFont, cssNumber, definePrintDesignComponentRenderer, definePrintDesignPluginRenderer, drawPrintDesignElement, drawPrintDesignPage, elementTransformCss, elementTransformHomography, escapeHtml, formatDisplayUnit, fromDisplayUnit, getPrintDesignAlignmentUnits, getPrintDesignDataFields,
|
|
8356
|
+
export { DEFAULT_PRINT_DESIGN_DEBUG_OPTIONS, PRINT_DESIGN_CSS_DPI, PRINT_DESIGN_DEFAULT_FONT_FAMILY, PRINT_DESIGN_MM_PER_INCH, PRINT_DESIGN_SCHEMA_VERSION, PRINT_DESIGN_UNIT_MM, version as PRINT_DESIGN_VERSION, PrintDesign, PrintDesignComponentRegistry, buildPrintDesignLayerTree, builtinPrintDesignComponentRegistry, canvasPointToPageMillimeters, clonePrintDesignDocument, createPrintDesignAgentApi, createPrintDesignDocument, createPrintDesignId, createPrintDesignPage, createPrintDesignPrintFrame, cssColor, cssFont, cssNumber, definePrintDesignComponentRenderer, definePrintDesignPluginRenderer, drawPrintDesignElement, drawPrintDesignPage, elementTransformCss, elementTransformHomography, escapeHtml, formatDisplayUnit, fromDisplayUnit, getPrintDesignAlignmentUnits, getPrintDesignDataFields, getPrintDesignElementDataConfig, getPrintDesignPageSize, getSelectionRect, hitTestPrintDesignPage, inverseTransformPoint, isPrintDesignColorValue, isPrintDesignRecordCollection, materializePrintDesignDocument, millimetersToPixels, paginatePrintDesignDocument, parsePrintDesignDocument, pixelsToMillimeters, preparePrintDesignCanvasPage, printDesignAgentTools, printPrintDesignHtml, printPrintDesignHtmlInFrame, projectTransformPoint, renderPrintDesignHtmlFrame, renderPrintDesignPageToCanvas, resolveDesignText, resolvePrintDesignBinding, resolvePrintDesignDataPath, resolvePrintDesignDebugOptions, resolvePrintDesignElementData, resolvePrintDesignElementVisible, resolvePrintDesignFontFamily, resolvePrintDesignPageVisible, rotatePoint2 as rotatePoint, serializePrintDesignDocument, stringifyPrintDesignBinding, toDisplayUnit, transformPrintDesignToHtml, transformPrintDesignToHtmlAsync, transformPrintDesignToHtmlBatches, transformRect };
|