@windoc/core 0.3.24 → 0.3.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -78,7 +78,7 @@ __export(index_exports, {
78
78
  module.exports = __toCommonJS(index_exports);
79
79
 
80
80
  // src/version.ts
81
- var version = "0.3.24";
81
+ var version = "0.3.25";
82
82
 
83
83
  // src/dataset/enum/Common.ts
84
84
  var MaxHeightRatio = /* @__PURE__ */ ((MaxHeightRatio2) => {
@@ -256,6 +256,13 @@ function downloadFile(href, fileName) {
256
256
  a.download = fileName;
257
257
  a.click();
258
258
  }
259
+ function downloadBlob(blob, fileName) {
260
+ const href = URL.createObjectURL(blob);
261
+ downloadFile(href, fileName);
262
+ window.setTimeout(() => {
263
+ URL.revokeObjectURL(href);
264
+ }, 0);
265
+ }
259
266
  function threeClick(dom, fn) {
260
267
  nClickEvent(3, dom, fn);
261
268
  }
@@ -3881,23 +3888,23 @@ var ListStyle = /* @__PURE__ */ ((ListStyle3) => {
3881
3888
  ListStyle3["CHECKBOX"] = "checkbox" /* CHECKBOX */;
3882
3889
  return ListStyle3;
3883
3890
  })(ListStyle || {});
3884
- var OlPreset = /* @__PURE__ */ ((OlPreset2) => {
3885
- OlPreset2["DEFAULT"] = "olDefault";
3886
- OlPreset2["PARENTHESIS"] = "olParen";
3887
- OlPreset2["OUTLINE"] = "olOutline";
3888
- OlPreset2["UPPER_ALPHA"] = "olUpperA";
3889
- OlPreset2["ROMAN"] = "olRoman";
3890
- OlPreset2["ZERO_PAD"] = "olZeroPad";
3891
- return OlPreset2;
3891
+ var OlPreset = /* @__PURE__ */ ((OlPreset3) => {
3892
+ OlPreset3["DEFAULT"] = "olDefault";
3893
+ OlPreset3["PARENTHESIS"] = "olParen";
3894
+ OlPreset3["OUTLINE"] = "olOutline";
3895
+ OlPreset3["UPPER_ALPHA"] = "olUpperA";
3896
+ OlPreset3["ROMAN"] = "olRoman";
3897
+ OlPreset3["ZERO_PAD"] = "olZeroPad";
3898
+ return OlPreset3;
3892
3899
  })(OlPreset || {});
3893
- var UlPreset = /* @__PURE__ */ ((UlPreset2) => {
3894
- UlPreset2["DEFAULT"] = "ulDefault";
3895
- UlPreset2["DIAMOND"] = "ulDiamond";
3896
- UlPreset2["HOLLOW_SQ"] = "ulHollowSq";
3897
- UlPreset2["ARROW"] = "ulArrow";
3898
- UlPreset2["STAR"] = "ulStar";
3899
- UlPreset2["CHECK_ARROW"] = "ulCheckArr";
3900
- return UlPreset2;
3900
+ var UlPreset = /* @__PURE__ */ ((UlPreset3) => {
3901
+ UlPreset3["DEFAULT"] = "ulDefault";
3902
+ UlPreset3["DIAMOND"] = "ulDiamond";
3903
+ UlPreset3["HOLLOW_SQ"] = "ulHollowSq";
3904
+ UlPreset3["ARROW"] = "ulArrow";
3905
+ UlPreset3["STAR"] = "ulStar";
3906
+ UlPreset3["CHECK_ARROW"] = "ulCheckArr";
3907
+ return UlPreset3;
3901
3908
  })(UlPreset || {});
3902
3909
 
3903
3910
  // src/dataset/constant/List.ts
@@ -24337,6 +24344,8 @@ var Command = class {
24337
24344
  this.executeSearchNavigateNext = adapt.searchNavigateNext.bind(adapt);
24338
24345
  this.executeReplace = adapt.replace.bind(adapt);
24339
24346
  this.executePrint = adapt.print.bind(adapt);
24347
+ this.executeExportDocx = adapt.exportDocx.bind(adapt);
24348
+ this.executeExportPdf = adapt.exportPdf.bind(adapt);
24340
24349
  this.executeReplaceImageElement = adapt.replaceImageElement.bind(adapt);
24341
24350
  this.executeSaveAsImageElement = adapt.saveAsImageElement.bind(adapt);
24342
24351
  this.executeSetImageCrop = adapt.setImageCrop.bind(adapt);
@@ -24417,175 +24426,3284 @@ var Command = class {
24417
24426
  }
24418
24427
  };
24419
24428
 
24420
- // src/utils/print.ts
24421
- function convertPxToPaperSize(width, height) {
24422
- if (width === 1125 && height === 1593) {
24423
- return {
24424
- size: "a3",
24425
- width: "297mm",
24426
- height: "420mm"
24427
- };
24428
- }
24429
- if (width === 794 && height === 1123) {
24430
- return {
24431
- size: "a4",
24432
- width: "210mm",
24433
- height: "297mm"
24434
- };
24435
- }
24436
- if (width === 565 && height === 796) {
24437
- return {
24438
- size: "a5",
24439
- width: "148mm",
24440
- height: "210mm"
24441
- };
24442
- }
24429
+ // src/utils/docx.ts
24430
+ var import_docx = require("docx");
24431
+ var DEFAULT_DOCX_FILE_NAME = "document.docx";
24432
+ var DEFAULT_IMAGE_WIDTH = 320;
24433
+ var DEFAULT_IMAGE_HEIGHT = 180;
24434
+ var DEFAULT_LINE_HEIGHT = 1.15;
24435
+ var docxBulletCharMap = {
24436
+ ["disc" /* DISC */]: "\u2022",
24437
+ ["circle" /* CIRCLE */]: "\u25E6",
24438
+ ["square" /* SQUARE */]: "\u25A0",
24439
+ ["checkbox" /* CHECKBOX */]: "\u2611"
24440
+ };
24441
+ var titleHeadingMap = {
24442
+ ["first" /* FIRST */]: import_docx.HeadingLevel.HEADING_1,
24443
+ ["second" /* SECOND */]: import_docx.HeadingLevel.HEADING_2,
24444
+ ["third" /* THIRD */]: import_docx.HeadingLevel.HEADING_3,
24445
+ ["fourth" /* FOURTH */]: import_docx.HeadingLevel.HEADING_4,
24446
+ ["fifth" /* FIFTH */]: import_docx.HeadingLevel.HEADING_5,
24447
+ ["sixth" /* SIXTH */]: import_docx.HeadingLevel.HEADING_6
24448
+ };
24449
+ var orderedStyleFormatMap = {
24450
+ ["decimal" /* DECIMAL */]: import_docx.LevelFormat.DECIMAL,
24451
+ ["lowerAlpha" /* LOWER_ALPHA */]: import_docx.LevelFormat.LOWER_LETTER,
24452
+ ["upperAlpha" /* UPPER_ALPHA */]: import_docx.LevelFormat.UPPER_LETTER,
24453
+ ["lowerRoman" /* LOWER_ROMAN */]: import_docx.LevelFormat.LOWER_ROMAN,
24454
+ ["upperRoman" /* UPPER_ROMAN */]: import_docx.LevelFormat.UPPER_ROMAN,
24455
+ ["decimalParen" /* DECIMAL_PAREN */]: import_docx.LevelFormat.DECIMAL,
24456
+ decimalZero: import_docx.LevelFormat.DECIMAL_ZERO,
24457
+ lowerAlphaParen: import_docx.LevelFormat.LOWER_LETTER,
24458
+ lowerRomanParen: import_docx.LevelFormat.LOWER_ROMAN,
24459
+ outline: import_docx.LevelFormat.DECIMAL
24460
+ };
24461
+ async function exportEditorDataToDocx(payload, options = {}) {
24462
+ const fileName = normalizeDocxFileName(options.fileName);
24463
+ const file = await createDocxDocument(payload);
24464
+ const blob = await import_docx.Packer.toBlob(file);
24443
24465
  return {
24444
- size: "",
24445
- width: `${width}px`,
24446
- height: `${height}px`
24466
+ blob,
24467
+ fileName
24447
24468
  };
24448
24469
  }
24449
- function printImageBase64(base64List, options) {
24450
- const { width, height, direction = "vertical" /* VERTICAL */ } = options;
24451
- const iframe = document.createElement("iframe");
24452
- iframe.style.visibility = "hidden";
24453
- iframe.style.position = "absolute";
24454
- iframe.style.left = "0";
24455
- iframe.style.top = "0";
24456
- iframe.style.width = "0";
24457
- iframe.style.height = "0";
24458
- iframe.style.border = "none";
24459
- document.body.append(iframe);
24460
- const contentWindow = iframe.contentWindow;
24461
- const doc = contentWindow.document;
24462
- doc.open();
24463
- const container = document.createElement("div");
24464
- const paperSize = convertPxToPaperSize(width, height);
24465
- base64List.forEach((base64) => {
24466
- const image = document.createElement("img");
24467
- image.style.width = direction === "horizontal" /* HORIZONTAL */ ? paperSize.height : paperSize.width;
24468
- image.style.height = direction === "horizontal" /* HORIZONTAL */ ? paperSize.width : paperSize.height;
24469
- image.src = base64;
24470
- container.append(image);
24471
- });
24472
- const style = document.createElement("style");
24473
- const stylesheet = `
24474
- * {
24475
- margin: 0;
24476
- padding: 0;
24470
+ async function createDocxDocument(payload) {
24471
+ const { data: data2, options } = payload;
24472
+ const editorOptions = options;
24473
+ const context = createSerializeContext();
24474
+ const headerChildren = editorOptions.header.disabled ? [] : await serializeElementList(data2.header || [], editorOptions, context);
24475
+ const footerChildren = editorOptions.footer.disabled ? [] : await serializeElementList(data2.footer || [], editorOptions, context);
24476
+ const pageNumberParagraph = createPageNumberParagraph(
24477
+ editorOptions.pageNumber
24478
+ );
24479
+ if (pageNumberParagraph) {
24480
+ footerChildren.push(pageNumberParagraph);
24477
24481
  }
24478
- @page {
24479
- margin: 0;
24480
- size: ${paperSize.size} ${direction === "horizontal" /* HORIZONTAL */ ? `landscape` : `portrait`};
24481
- }`;
24482
- style.append(document.createTextNode(stylesheet));
24483
- setTimeout(() => {
24484
- doc.write(`${style.outerHTML}${container.innerHTML}`);
24485
- contentWindow.print();
24486
- doc.close();
24487
- window.addEventListener(
24488
- "mouseover",
24489
- () => {
24490
- iframe?.remove();
24491
- },
24482
+ const sectionChildren = await serializeElementList(
24483
+ data2.main,
24484
+ editorOptions,
24485
+ context
24486
+ );
24487
+ const { width, height, paperDirection } = editorOptions;
24488
+ const pageWidth = paperDirection === "horizontal" /* HORIZONTAL */ ? height : width;
24489
+ const pageHeight = paperDirection === "horizontal" /* HORIZONTAL */ ? width : height;
24490
+ return new import_docx.Document({
24491
+ features: {
24492
+ updateFields: true
24493
+ },
24494
+ numbering: {
24495
+ config: createNumberingConfig()
24496
+ },
24497
+ sections: [
24492
24498
  {
24493
- once: true
24499
+ properties: {
24500
+ page: {
24501
+ size: {
24502
+ width: pxToTwips(pageWidth),
24503
+ height: pxToTwips(pageHeight),
24504
+ orientation: paperDirection === "horizontal" /* HORIZONTAL */ ? import_docx.PageOrientation.LANDSCAPE : import_docx.PageOrientation.PORTRAIT
24505
+ },
24506
+ margin: {
24507
+ top: pxToTwips(editorOptions.margins[0]),
24508
+ right: pxToTwips(editorOptions.margins[1]),
24509
+ bottom: pxToTwips(editorOptions.margins[2]),
24510
+ left: pxToTwips(editorOptions.margins[3]),
24511
+ header: pxToTwips(editorOptions.header.top),
24512
+ footer: pxToTwips(editorOptions.footer.bottom)
24513
+ },
24514
+ pageNumbers: !editorOptions.pageNumber.disabled && editorOptions.pageNumber.fromPageNo === 0 ? {
24515
+ start: editorOptions.pageNumber.startPageNo
24516
+ } : void 0
24517
+ }
24518
+ },
24519
+ headers: headerChildren.length > 0 ? {
24520
+ default: new import_docx.Header({
24521
+ children: headerChildren
24522
+ })
24523
+ } : void 0,
24524
+ footers: footerChildren.length > 0 ? {
24525
+ default: new import_docx.Footer({
24526
+ children: footerChildren
24527
+ })
24528
+ } : void 0,
24529
+ children: sectionChildren.length ? sectionChildren : [new import_docx.Paragraph({})]
24494
24530
  }
24495
- );
24531
+ ]
24496
24532
  });
24497
24533
  }
24498
-
24499
- // src/core/command/CommandAdapt.ts
24500
- var CommandAdapt = class {
24501
- constructor(draw) {
24502
- this.draw = draw;
24503
- this.range = draw.getRange();
24504
- this.position = draw.getPosition();
24505
- this.historyManager = draw.getHistoryManager();
24506
- this.canvasEvent = draw.getCanvasEvent();
24507
- this.options = draw.getOptions();
24508
- this.control = draw.getControl();
24509
- this.workerManager = draw.getWorkerManager();
24510
- this.searchManager = draw.getSearch();
24511
- this.i18n = draw.getI18n();
24512
- this.zone = draw.getZone();
24513
- this.tableOperate = draw.getTableOperate();
24534
+ async function serializeElementList(elementList, options, context) {
24535
+ const blocks = [];
24536
+ let inlineBuffer = [];
24537
+ const flushInlineBuffer = async () => {
24538
+ if (!inlineBuffer.length) return;
24539
+ blocks.push(
24540
+ ...await serializeInlineElements(inlineBuffer, options, context)
24541
+ );
24542
+ inlineBuffer = [];
24543
+ };
24544
+ for (let i = 0; i < elementList.length; i++) {
24545
+ const element = elementList[i];
24546
+ if (element.type === "area" /* AREA */ && element.valueList?.length) {
24547
+ await flushInlineBuffer();
24548
+ blocks.push(
24549
+ ...await serializeElementList(element.valueList, options, context)
24550
+ );
24551
+ continue;
24552
+ }
24553
+ if (element.type === "title" /* TITLE */ && element.valueList?.length) {
24554
+ await flushInlineBuffer();
24555
+ blocks.push(
24556
+ ...await serializeInlineElements(element.valueList, options, context, {
24557
+ heading: titleHeadingMap[element.level || "first" /* FIRST */]
24558
+ })
24559
+ );
24560
+ continue;
24561
+ }
24562
+ if (element.type === "list" /* LIST */ && element.valueList?.length) {
24563
+ await flushInlineBuffer();
24564
+ blocks.push(...await serializeListElement(element, options, context));
24565
+ continue;
24566
+ }
24567
+ if (element.type === "table" /* TABLE */ && element.trList?.length) {
24568
+ await flushInlineBuffer();
24569
+ blocks.push(await serializeTableElement(element, options, context));
24570
+ continue;
24571
+ }
24572
+ if (element.type === "pageBreak" /* PAGE_BREAK */) {
24573
+ await flushInlineBuffer();
24574
+ blocks.push(
24575
+ new import_docx.Paragraph({
24576
+ children: [new import_docx.PageBreak()]
24577
+ })
24578
+ );
24579
+ continue;
24580
+ }
24581
+ if (element.type === "separator" /* SEPARATOR */) {
24582
+ await flushInlineBuffer();
24583
+ blocks.push(
24584
+ new import_docx.Paragraph({
24585
+ thematicBreak: true
24586
+ })
24587
+ );
24588
+ continue;
24589
+ }
24590
+ if (element.type === "image" /* IMAGE */ && element.imgDisplay && element.imgDisplay !== "inline") {
24591
+ await flushInlineBuffer();
24592
+ blocks.push(...await serializeInlineElements([element], options));
24593
+ continue;
24594
+ }
24595
+ if (element.type === "block" /* BLOCK */) {
24596
+ await flushInlineBuffer();
24597
+ const blockText = getBlockFallbackText(element);
24598
+ if (blockText) {
24599
+ blocks.push(
24600
+ ...await serializeInlineElements(
24601
+ [{ value: blockText }],
24602
+ options,
24603
+ context
24604
+ )
24605
+ );
24606
+ }
24607
+ continue;
24608
+ }
24609
+ inlineBuffer.push(element);
24514
24610
  }
24515
- getStyleSelectionElementList(options = {}) {
24516
- const selection = this.range.getSelectionElementList();
24517
- if (!selection?.length) return null;
24518
- const { textLikeOnly = false } = options;
24519
- const result = [];
24520
- const collect = (elementList) => {
24521
- for (let i = 0; i < elementList.length; i++) {
24522
- const element = elementList[i];
24523
- if (element.type === "table" /* TABLE */ && element.trList?.length) {
24524
- for (let t = 0; t < element.trList.length; t++) {
24525
- const tr = element.trList[t];
24526
- for (let d = 0; d < tr.tdList.length; d++) {
24527
- collect(tr.tdList[d].value);
24528
- }
24529
- }
24530
- continue;
24611
+ await flushInlineBuffer();
24612
+ return blocks;
24613
+ }
24614
+ async function serializeInlineElements(elementList, options, _context, overrides = {}) {
24615
+ const paragraphs = [];
24616
+ let current = null;
24617
+ const getParagraphContext = (element) => ({
24618
+ heading: overrides.heading,
24619
+ numbering: overrides.numbering,
24620
+ alignment: overrides.alignment !== void 0 ? overrides.alignment : mapRowFlexToAlignment(element?.rowFlex),
24621
+ rowMargin: overrides.rowMargin !== void 0 ? overrides.rowMargin : element?.rowMargin
24622
+ });
24623
+ const flushParagraph = (context) => {
24624
+ if (!current) {
24625
+ if (!context) return;
24626
+ paragraphs.push(createParagraph([], context, options.defaultRowMargin));
24627
+ return;
24628
+ }
24629
+ paragraphs.push(
24630
+ createParagraph(
24631
+ current.children,
24632
+ current.context,
24633
+ options.defaultRowMargin
24634
+ )
24635
+ );
24636
+ current = null;
24637
+ };
24638
+ const ensureParagraph = (element) => {
24639
+ if (!current) {
24640
+ current = {
24641
+ children: [],
24642
+ context: getParagraphContext(element)
24643
+ };
24644
+ }
24645
+ return current;
24646
+ };
24647
+ const appendText = (element, text, childFactory, preserveTrailingBreak = false) => {
24648
+ const normalized = normalizeText(text);
24649
+ const parts = normalized.split("\n");
24650
+ let lastSeparatorFlushedContent = false;
24651
+ for (let i = 0; i < parts.length; i++) {
24652
+ const segment = parts[i];
24653
+ if (segment) {
24654
+ ensureParagraph(element).children.push(childFactory(segment));
24655
+ }
24656
+ if (i < parts.length - 1) {
24657
+ if (!current) {
24658
+ paragraphs.push(
24659
+ createParagraph(
24660
+ [],
24661
+ getParagraphContext(element),
24662
+ options.defaultRowMargin
24663
+ )
24664
+ );
24665
+ lastSeparatorFlushedContent = false;
24666
+ } else {
24667
+ flushParagraph();
24668
+ lastSeparatorFlushedContent = true;
24531
24669
  }
24532
- result.push(element);
24533
24670
  }
24534
- };
24535
- collect(selection);
24536
- if (!textLikeOnly) {
24537
- return result;
24538
24671
  }
24539
- return result.filter(
24540
- (element) => isTextLikeElement(element) || element.type === "label" /* LABEL */
24672
+ if (preserveTrailingBreak && normalized.endsWith("\n") && lastSeparatorFlushedContent) {
24673
+ paragraphs.push(
24674
+ createParagraph(
24675
+ [],
24676
+ getParagraphContext(element),
24677
+ options.defaultRowMargin
24678
+ )
24679
+ );
24680
+ }
24681
+ };
24682
+ for (let i = 0; i < elementList.length; i++) {
24683
+ const element = elementList[i];
24684
+ if (element.type === "hyperlink" /* HYPERLINK */ && element.valueList?.length) {
24685
+ const hyperlink = await createHyperlinkChild(element, options);
24686
+ if (hyperlink) {
24687
+ ensureParagraph(element).children.push(hyperlink);
24688
+ }
24689
+ continue;
24690
+ }
24691
+ if (element.type === "image" /* IMAGE */) {
24692
+ const imageChild = await createImageChild(element);
24693
+ if (!imageChild) {
24694
+ appendText(
24695
+ element,
24696
+ "[Image unavailable]",
24697
+ (segment) => createTextRun(
24698
+ segment,
24699
+ element,
24700
+ options.defaultSize,
24701
+ options.defaultFont
24702
+ ),
24703
+ i === elementList.length - 1
24704
+ );
24705
+ continue;
24706
+ }
24707
+ ensureParagraph(element).children.push(imageChild);
24708
+ continue;
24709
+ }
24710
+ if (element.type === "columnBreak" /* COLUMN_BREAK */) {
24711
+ ensureParagraph(element).children.push(new import_docx.ColumnBreak());
24712
+ continue;
24713
+ }
24714
+ if (element.type === "tab" /* TAB */) {
24715
+ appendText(
24716
+ element,
24717
+ " ",
24718
+ (segment) => createTextRun(
24719
+ segment,
24720
+ element,
24721
+ options.defaultSize,
24722
+ options.defaultFont
24723
+ ),
24724
+ i === elementList.length - 1
24725
+ );
24726
+ continue;
24727
+ }
24728
+ const textValue = getInlineElementText(element);
24729
+ if (!textValue) {
24730
+ continue;
24731
+ }
24732
+ appendText(
24733
+ element,
24734
+ textValue,
24735
+ (segment) => createTextRun(
24736
+ segment,
24737
+ element,
24738
+ options.defaultSize,
24739
+ options.defaultFont
24740
+ ),
24741
+ i === elementList.length - 1
24541
24742
  );
24542
24743
  }
24543
- mode(payload) {
24544
- this.draw.setMode(payload);
24744
+ if (current) {
24745
+ flushParagraph();
24545
24746
  }
24546
- async cut() {
24547
- const isDisabled = this.draw.isReadonly() || this.draw.isDisabled();
24548
- if (isDisabled) return;
24549
- await this.canvasEvent.cut();
24747
+ return paragraphs.length ? paragraphs : [createParagraph([], overrides, options.defaultRowMargin)];
24748
+ }
24749
+ async function serializeListElement(element, options, context) {
24750
+ const zipList = zipElementList(element.valueList || []);
24751
+ const listItemMap = splitListElement(zipList);
24752
+ const paragraphs = [];
24753
+ const listInstance = createListInstance(context);
24754
+ for (const listItemElements of listItemMap.values()) {
24755
+ const numbering = getListNumbering(
24756
+ getListItemNumberingElement(listItemElements, element),
24757
+ listInstance
24758
+ );
24759
+ paragraphs.push(
24760
+ ...await serializeInlineElements(listItemElements, options, context, {
24761
+ numbering
24762
+ })
24763
+ );
24550
24764
  }
24551
- async copy(payload) {
24552
- await this.canvasEvent.copy(payload);
24765
+ return paragraphs;
24766
+ }
24767
+ async function serializeTableElement(element, options, context) {
24768
+ const rows = [];
24769
+ const trList = element.trList || [];
24770
+ const tableContext = createTableSerializeContext(element);
24771
+ for (let i = 0; i < trList.length; i++) {
24772
+ rows.push(
24773
+ await serializeTableRow(trList[i], options, context, tableContext)
24774
+ );
24553
24775
  }
24554
- paste(payload) {
24555
- const isDisabled = this.draw.isReadonly() || this.draw.isDisabled();
24556
- if (isDisabled) return;
24557
- pasteByApi(this.canvasEvent, payload);
24776
+ const tableWidth = getTableWidthFromColgroup(element.colgroup);
24777
+ return new import_docx.Table({
24778
+ rows,
24779
+ width: tableWidth ? {
24780
+ size: pxToTwips(tableWidth),
24781
+ type: import_docx.WidthType.DXA
24782
+ } : {
24783
+ size: 100,
24784
+ type: import_docx.WidthType.PERCENTAGE
24785
+ },
24786
+ columnWidths: element.colgroup?.map((col) => pxToTwips(col.width)),
24787
+ layout: import_docx.TableLayoutType.FIXED,
24788
+ borders: createTableBorders(element)
24789
+ });
24790
+ }
24791
+ async function serializeTableRow(tr, options, context, tableContext) {
24792
+ const children = [];
24793
+ for (let i = 0; i < tr.tdList.length; i++) {
24794
+ children.push(
24795
+ await serializeTableCell(tr.tdList[i], options, context, tableContext)
24796
+ );
24558
24797
  }
24559
- selectAll() {
24560
- this.canvasEvent.selectAll();
24798
+ return new import_docx.TableRow({
24799
+ children
24800
+ });
24801
+ }
24802
+ async function serializeTableCell(td, options, context, tableContext) {
24803
+ const children = await serializeElementList(td.value, options, context);
24804
+ const shadingFill = td.backgroundColor ? normalizeHexColor(td.backgroundColor) : void 0;
24805
+ const cellWidth = tableContext.cellLayoutMap.get(td)?.width || td.width;
24806
+ return new import_docx.TableCell({
24807
+ children: children.length ? children : [new import_docx.Paragraph({})],
24808
+ columnSpan: td.colspan > 1 ? td.colspan : void 0,
24809
+ rowSpan: td.rowspan > 1 ? td.rowspan : void 0,
24810
+ verticalAlign: mapCellVerticalAlign(td.verticalAlign),
24811
+ width: cellWidth ? {
24812
+ size: pxToTwips(cellWidth),
24813
+ type: import_docx.WidthType.DXA
24814
+ } : void 0,
24815
+ shading: shadingFill ? {
24816
+ fill: shadingFill,
24817
+ type: import_docx.ShadingType.CLEAR
24818
+ } : void 0,
24819
+ margins: mapPaddingToTableMargins(td.padding),
24820
+ borders: createTableCellBorders(
24821
+ td,
24822
+ tableContext.borderColor,
24823
+ tableContext.borderWidth
24824
+ )
24825
+ });
24826
+ }
24827
+ function createParagraph(children, context, defaultRowMargin) {
24828
+ return new import_docx.Paragraph({
24829
+ children,
24830
+ heading: context.heading,
24831
+ numbering: context.numbering,
24832
+ alignment: context.alignment,
24833
+ spacing: {
24834
+ line: Math.round(
24835
+ 240 * (context.rowMargin || defaultRowMargin || DEFAULT_LINE_HEIGHT)
24836
+ ),
24837
+ lineRule: import_docx.LineRuleType.AUTO
24838
+ }
24839
+ });
24840
+ }
24841
+ function createTextRun(text, element, defaultSize, defaultFont) {
24842
+ const color = element.color ? normalizeHexColor(element.color, "000000") : void 0;
24843
+ const highlightFill = element.highlight ? normalizeHexColor(element.highlight) : void 0;
24844
+ return new import_docx.TextRun({
24845
+ text,
24846
+ bold: !!element.bold,
24847
+ italics: !!element.italic,
24848
+ strike: !!element.strikeout,
24849
+ subScript: element.type === "subscript" /* SUBSCRIPT */,
24850
+ superScript: element.type === "superscript" /* SUPERSCRIPT */,
24851
+ color,
24852
+ shading: highlightFill ? {
24853
+ fill: highlightFill,
24854
+ type: import_docx.ShadingType.CLEAR
24855
+ } : void 0,
24856
+ underline: element.underline ? {
24857
+ type: mapUnderlineType(element.textDecoration?.style)
24858
+ } : void 0,
24859
+ font: element.font || defaultFont,
24860
+ size: Math.round((element.size || defaultSize) * 2)
24861
+ });
24862
+ }
24863
+ async function createHyperlinkChild(element, options) {
24864
+ if (!element.url) {
24865
+ const fallbackText = getTextFromElementList(element.valueList || []);
24866
+ return fallbackText ? createTextRun(
24867
+ fallbackText,
24868
+ element.valueList?.[0] || element,
24869
+ options.defaultSize,
24870
+ options.defaultFont
24871
+ ) : null;
24872
+ }
24873
+ const children = await createHyperlinkRuns(
24874
+ element.valueList || [],
24875
+ options.defaultSize,
24876
+ options.defaultFont
24877
+ );
24878
+ if (!children.length) {
24879
+ return null;
24561
24880
  }
24562
- backspace() {
24563
- const isDisabled = this.draw.isReadonly() || this.draw.isDisabled();
24564
- if (isDisabled) return;
24565
- const elementList = this.draw.getElementList();
24566
- const { startIndex, endIndex } = this.range.getRange();
24567
- const isCollapsed = startIndex === endIndex;
24568
- if (isCollapsed && elementList[startIndex].value === ZERO && startIndex === 0) {
24569
- return;
24881
+ return new import_docx.ExternalHyperlink({
24882
+ link: element.url,
24883
+ children
24884
+ });
24885
+ }
24886
+ async function createHyperlinkRuns(valueList, defaultSize, defaultFont) {
24887
+ const children = [];
24888
+ for (let i = 0; i < valueList.length; i++) {
24889
+ const element = valueList[i];
24890
+ if (element.type === "tab" /* TAB */) {
24891
+ children.push(createTextRun(" ", element, defaultSize, defaultFont));
24892
+ continue;
24570
24893
  }
24571
- if (!isCollapsed) {
24572
- this.draw.spliceElementList(
24573
- elementList,
24574
- startIndex + 1,
24575
- endIndex - startIndex
24576
- );
24577
- } else {
24578
- this.draw.spliceElementList(elementList, startIndex, 1);
24894
+ const text = normalizeText(getInlineElementText(element)).replace(
24895
+ /\n/g,
24896
+ " "
24897
+ );
24898
+ if (text) {
24899
+ children.push(createTextRun(text, element, defaultSize, defaultFont));
24579
24900
  }
24580
- const curIndex = isCollapsed ? startIndex - 1 : startIndex;
24581
- this.range.setRange(curIndex, curIndex);
24582
- this.draw.render({ curIndex });
24583
24901
  }
24584
- setRange(startIndex, endIndex, tableId, startTdIndex, endTdIndex, startTrIndex, endTrIndex) {
24585
- if (startIndex < 0 || endIndex < 0 || endIndex < startIndex) return;
24586
- this.range.setRange(
24587
- startIndex,
24588
- endIndex,
24902
+ return children;
24903
+ }
24904
+ async function createImageChild(element) {
24905
+ try {
24906
+ const image = await loadImagePayload(element.value);
24907
+ const width = Math.max(1, Math.round(element.width || DEFAULT_IMAGE_WIDTH));
24908
+ const height = Math.max(
24909
+ 1,
24910
+ Math.round(element.height || DEFAULT_IMAGE_HEIGHT)
24911
+ );
24912
+ if (image.type === "svg" && image.fallbackPng) {
24913
+ return new import_docx.ImageRun({
24914
+ type: "svg",
24915
+ data: image.data,
24916
+ fallback: {
24917
+ type: "png",
24918
+ data: image.fallbackPng
24919
+ },
24920
+ transformation: {
24921
+ width,
24922
+ height
24923
+ }
24924
+ });
24925
+ }
24926
+ return new import_docx.ImageRun({
24927
+ type: image.type,
24928
+ data: image.data,
24929
+ transformation: {
24930
+ width,
24931
+ height
24932
+ }
24933
+ });
24934
+ } catch {
24935
+ return null;
24936
+ }
24937
+ }
24938
+ async function loadImagePayload(src) {
24939
+ const source = src.trim();
24940
+ if (source.startsWith("data:")) {
24941
+ return getImagePayloadFromDataUrl(source);
24942
+ }
24943
+ const response = await fetch(source);
24944
+ if (!response.ok) {
24945
+ throw new Error(`Image request failed: ${response.status}`);
24946
+ }
24947
+ const contentType = response.headers.get("content-type") || "";
24948
+ const data2 = await response.arrayBuffer();
24949
+ return normalizeImagePayload(data2, contentType);
24950
+ }
24951
+ async function getImagePayloadFromDataUrl(src) {
24952
+ const [, meta = "", encoded = ""] = src.match(/^data:([^,]*),(.*)$/) || [];
24953
+ const mime = meta.split(";")[0] || "";
24954
+ const isBase64 = meta.includes(";base64");
24955
+ const text = isBase64 ? atob(encoded) : decodeURIComponent(encoded);
24956
+ const bytes = new Uint8Array(text.length);
24957
+ for (let i = 0; i < text.length; i++) {
24958
+ bytes[i] = text.charCodeAt(i);
24959
+ }
24960
+ return normalizeImagePayload(bytes.buffer, mime);
24961
+ }
24962
+ async function normalizeImagePayload(data2, mime) {
24963
+ const type = getRegularImageType(mime);
24964
+ if (type) {
24965
+ return {
24966
+ type,
24967
+ data: data2
24968
+ };
24969
+ }
24970
+ if (mime.includes("svg")) {
24971
+ return {
24972
+ type: "svg",
24973
+ data: data2,
24974
+ fallbackPng: await rasterizeSvgToPng(data2)
24975
+ };
24976
+ }
24977
+ const fallbackPng = await rasterizeImageToPng(data2, mime);
24978
+ return {
24979
+ type: "png",
24980
+ data: fallbackPng
24981
+ };
24982
+ }
24983
+ async function rasterizeSvgToPng(data2) {
24984
+ return rasterizeImageToPng(data2, "image/svg+xml");
24985
+ }
24986
+ async function rasterizeImageToPng(data2, mime) {
24987
+ const blob = new Blob([data2], {
24988
+ type: mime || "image/png"
24989
+ });
24990
+ const url = URL.createObjectURL(blob);
24991
+ try {
24992
+ const image = await loadHtmlImage(url);
24993
+ const canvas = document.createElement("canvas");
24994
+ canvas.width = image.naturalWidth || DEFAULT_IMAGE_WIDTH;
24995
+ canvas.height = image.naturalHeight || DEFAULT_IMAGE_HEIGHT;
24996
+ const context = canvas.getContext("2d");
24997
+ if (!context) {
24998
+ throw new Error("Canvas context is unavailable");
24999
+ }
25000
+ context.drawImage(image, 0, 0, canvas.width, canvas.height);
25001
+ const pngBlob = await new Promise((resolve, reject) => {
25002
+ canvas.toBlob((output) => {
25003
+ if (output) {
25004
+ resolve(output);
25005
+ } else {
25006
+ reject(new Error("Failed to create PNG blob"));
25007
+ }
25008
+ }, "image/png");
25009
+ });
25010
+ return await pngBlob.arrayBuffer();
25011
+ } finally {
25012
+ URL.revokeObjectURL(url);
25013
+ }
25014
+ }
25015
+ function loadHtmlImage(src) {
25016
+ return new Promise((resolve, reject) => {
25017
+ const image = new Image();
25018
+ image.onload = () => resolve(image);
25019
+ image.onerror = () => reject(new Error("Failed to load image"));
25020
+ image.src = src;
25021
+ });
25022
+ }
25023
+ function createPageNumberParagraph(pageNumber) {
25024
+ if (pageNumber.disabled || pageNumber.fromPageNo > 0) {
25025
+ return null;
25026
+ }
25027
+ const children = [];
25028
+ const format = pageNumber.format || FORMAT_PLACEHOLDER.PAGE_NO;
25029
+ const tokens = format.split(/(\{pageNo\}|\{pageCount\})/);
25030
+ for (let i = 0; i < tokens.length; i++) {
25031
+ const token = tokens[i];
25032
+ if (!token) continue;
25033
+ const runChildren = token === FORMAT_PLACEHOLDER.PAGE_NO ? [import_docx.PageNumber.CURRENT] : token === FORMAT_PLACEHOLDER.PAGE_COUNT ? [import_docx.PageNumber.TOTAL_PAGES] : void 0;
25034
+ children.push(
25035
+ new import_docx.TextRun({
25036
+ text: runChildren ? void 0 : token,
25037
+ children: runChildren,
25038
+ color: normalizeHexColor(pageNumber.color || "#000000", "000000"),
25039
+ font: pageNumber.font,
25040
+ size: Math.round((pageNumber.size || 12) * 2)
25041
+ })
25042
+ );
25043
+ }
25044
+ return new import_docx.Paragraph({
25045
+ children,
25046
+ alignment: mapRowFlexToAlignment(pageNumber.rowFlex)
25047
+ });
25048
+ }
25049
+ function createNumberingConfig() {
25050
+ const config = [
25051
+ createOrderedReference("windoc-ol-decimal", Array(9).fill("decimal" /* DECIMAL */)),
25052
+ createOrderedReference(
25053
+ "windoc-ol-lower-alpha",
25054
+ Array(9).fill("lowerAlpha" /* LOWER_ALPHA */)
25055
+ ),
25056
+ createOrderedReference(
25057
+ "windoc-ol-upper-alpha",
25058
+ Array(9).fill("upperAlpha" /* UPPER_ALPHA */)
25059
+ ),
25060
+ createOrderedReference(
25061
+ "windoc-ol-lower-roman",
25062
+ Array(9).fill("lowerRoman" /* LOWER_ROMAN */)
25063
+ ),
25064
+ createOrderedReference(
25065
+ "windoc-ol-upper-roman",
25066
+ Array(9).fill("upperRoman" /* UPPER_ROMAN */)
25067
+ ),
25068
+ createOrderedReference(
25069
+ "windoc-ol-decimal-paren",
25070
+ Array(9).fill("decimalParen" /* DECIMAL_PAREN */)
25071
+ ),
25072
+ createOrderedReference(
25073
+ `windoc-ol-${"olDefault" /* DEFAULT */}`,
25074
+ createCycleList(olPresetCycles["olDefault" /* DEFAULT */], 9)
25075
+ ),
25076
+ createOrderedReference(
25077
+ `windoc-ol-${"olParen" /* PARENTHESIS */}`,
25078
+ createCycleList(olPresetCycles["olParen" /* PARENTHESIS */], 9)
25079
+ ),
25080
+ createOrderedReference(
25081
+ `windoc-ol-${"olOutline" /* OUTLINE */}`,
25082
+ createCycleList(olPresetCycles["olOutline" /* OUTLINE */], 9)
25083
+ ),
25084
+ createOrderedReference(
25085
+ `windoc-ol-${"olUpperA" /* UPPER_ALPHA */}`,
25086
+ createCycleList(olPresetCycles["olUpperA" /* UPPER_ALPHA */], 9)
25087
+ ),
25088
+ createOrderedReference(
25089
+ `windoc-ol-${"olRoman" /* ROMAN */}`,
25090
+ createCycleList(olPresetCycles["olRoman" /* ROMAN */], 9)
25091
+ ),
25092
+ createOrderedReference(
25093
+ `windoc-ol-${"olZeroPad" /* ZERO_PAD */}`,
25094
+ createCycleList(olPresetCycles["olZeroPad" /* ZERO_PAD */], 9)
25095
+ ),
25096
+ createBulletReference(
25097
+ "windoc-ul-disc",
25098
+ Array(9).fill(docxBulletCharMap["disc" /* DISC */])
25099
+ ),
25100
+ createBulletReference(
25101
+ "windoc-ul-circle",
25102
+ Array(9).fill(docxBulletCharMap["circle" /* CIRCLE */])
25103
+ ),
25104
+ createBulletReference(
25105
+ "windoc-ul-square",
25106
+ Array(9).fill(docxBulletCharMap["square" /* SQUARE */])
25107
+ ),
25108
+ createBulletReference(
25109
+ "windoc-ul-checkbox",
25110
+ Array(9).fill(docxBulletCharMap["checkbox" /* CHECKBOX */])
25111
+ ),
25112
+ createBulletReference(
25113
+ `windoc-ul-${"ulDefault" /* DEFAULT */}`,
25114
+ createCycleList(ulPresetCycles["ulDefault" /* DEFAULT */], 9)
25115
+ ),
25116
+ createBulletReference(
25117
+ `windoc-ul-${"ulDiamond" /* DIAMOND */}`,
25118
+ createCycleList(ulPresetCycles["ulDiamond" /* DIAMOND */], 9)
25119
+ ),
25120
+ createBulletReference(
25121
+ `windoc-ul-${"ulHollowSq" /* HOLLOW_SQ */}`,
25122
+ createCycleList(ulPresetCycles["ulHollowSq" /* HOLLOW_SQ */], 9)
25123
+ ),
25124
+ createBulletReference(
25125
+ `windoc-ul-${"ulArrow" /* ARROW */}`,
25126
+ createCycleList(ulPresetCycles["ulArrow" /* ARROW */], 9)
25127
+ ),
25128
+ createBulletReference(
25129
+ `windoc-ul-${"ulStar" /* STAR */}`,
25130
+ createCycleList(ulPresetCycles["ulStar" /* STAR */], 9)
25131
+ ),
25132
+ createBulletReference(
25133
+ `windoc-ul-${"ulCheckArr" /* CHECK_ARROW */}`,
25134
+ createCycleList(ulPresetCycles["ulCheckArr" /* CHECK_ARROW */], 9)
25135
+ )
25136
+ ];
25137
+ return config;
25138
+ }
25139
+ function createOrderedReference(reference, styles) {
25140
+ return {
25141
+ reference,
25142
+ levels: styles.map((style, level) => ({
25143
+ level,
25144
+ format: orderedStyleFormatMap[style] || import_docx.LevelFormat.DECIMAL,
25145
+ text: getOrderedLevelText(style, level),
25146
+ alignment: import_docx.AlignmentType.START,
25147
+ style: {
25148
+ paragraph: {
25149
+ indent: {
25150
+ left: 720 * (level + 1),
25151
+ hanging: 360
25152
+ }
25153
+ }
25154
+ }
25155
+ }))
25156
+ };
25157
+ }
25158
+ function createBulletReference(reference, bullets) {
25159
+ return {
25160
+ reference,
25161
+ levels: bullets.map((bullet, level) => ({
25162
+ level,
25163
+ format: import_docx.LevelFormat.BULLET,
25164
+ text: bullet,
25165
+ alignment: import_docx.AlignmentType.START,
25166
+ style: {
25167
+ paragraph: {
25168
+ indent: {
25169
+ left: 720 * (level + 1),
25170
+ hanging: 360
25171
+ }
25172
+ }
25173
+ }
25174
+ }))
25175
+ };
25176
+ }
25177
+ function getOrderedLevelText(style, level) {
25178
+ const placeholder = `%${level + 1}`;
25179
+ if (style === "outline") {
25180
+ return Array.from({ length: level + 1 }, (_, index) => `%${index + 1}`).join(
25181
+ "."
25182
+ ) + ".";
25183
+ }
25184
+ if (style.endsWith("Paren") || style === "decimalParen" /* DECIMAL_PAREN */) {
25185
+ return `${placeholder})`;
25186
+ }
25187
+ return `${placeholder}.`;
25188
+ }
25189
+ function createCycleList(source = [], size) {
25190
+ const result = [];
25191
+ for (let i = 0; i < size; i++) {
25192
+ result.push(source[i % source.length]);
25193
+ }
25194
+ return result;
25195
+ }
25196
+ function getListNumbering(element, instance) {
25197
+ const level = Math.max(0, Math.min(element.listLevel || 0, 8));
25198
+ if (element.listType === "ul" /* UL */) {
25199
+ const reference2 = element.listPreset ? `windoc-ul-${element.listPreset}` : `windoc-ul-${element.listStyle || "disc" /* DISC */}`;
25200
+ return {
25201
+ reference: reference2,
25202
+ level,
25203
+ instance
25204
+ };
25205
+ }
25206
+ const reference = element.listPreset ? `windoc-ol-${element.listPreset}` : getOrderedReferenceFromStyle(element.listStyle);
25207
+ return {
25208
+ reference,
25209
+ level,
25210
+ instance
25211
+ };
25212
+ }
25213
+ function getOrderedReferenceFromStyle(style) {
25214
+ switch (style) {
25215
+ case "lowerAlpha" /* LOWER_ALPHA */:
25216
+ return "windoc-ol-lower-alpha";
25217
+ case "upperAlpha" /* UPPER_ALPHA */:
25218
+ return "windoc-ol-upper-alpha";
25219
+ case "lowerRoman" /* LOWER_ROMAN */:
25220
+ return "windoc-ol-lower-roman";
25221
+ case "upperRoman" /* UPPER_ROMAN */:
25222
+ return "windoc-ol-upper-roman";
25223
+ case "decimalParen" /* DECIMAL_PAREN */:
25224
+ return "windoc-ol-decimal-paren";
25225
+ default:
25226
+ return "windoc-ol-decimal";
25227
+ }
25228
+ }
25229
+ function createTableBorders(element) {
25230
+ const outerBorder = createBorder(
25231
+ element.borderColor,
25232
+ import_docx.BorderStyle.SINGLE,
25233
+ element.borderExternalWidth || element.borderWidth
25234
+ );
25235
+ const innerBorder = createBorder(
25236
+ element.borderColor,
25237
+ import_docx.BorderStyle.SINGLE,
25238
+ element.borderWidth
25239
+ );
25240
+ switch (element.borderType) {
25241
+ case "empty" /* EMPTY */:
25242
+ return {
25243
+ top: noneBorder(),
25244
+ bottom: noneBorder(),
25245
+ left: noneBorder(),
25246
+ right: noneBorder(),
25247
+ insideHorizontal: noneBorder(),
25248
+ insideVertical: noneBorder()
25249
+ };
25250
+ case "external" /* EXTERNAL */:
25251
+ return {
25252
+ top: outerBorder,
25253
+ bottom: outerBorder,
25254
+ left: outerBorder,
25255
+ right: outerBorder,
25256
+ insideHorizontal: noneBorder(),
25257
+ insideVertical: noneBorder()
25258
+ };
25259
+ case "internal" /* INTERNAL */:
25260
+ return {
25261
+ top: noneBorder(),
25262
+ bottom: noneBorder(),
25263
+ left: noneBorder(),
25264
+ right: noneBorder(),
25265
+ insideHorizontal: innerBorder,
25266
+ insideVertical: innerBorder
25267
+ };
25268
+ case "dash" /* DASH */:
25269
+ return {
25270
+ top: createBorder(
25271
+ element.borderColor,
25272
+ import_docx.BorderStyle.DASHED,
25273
+ element.borderExternalWidth || element.borderWidth
25274
+ ),
25275
+ bottom: createBorder(
25276
+ element.borderColor,
25277
+ import_docx.BorderStyle.DASHED,
25278
+ element.borderExternalWidth || element.borderWidth
25279
+ ),
25280
+ left: createBorder(
25281
+ element.borderColor,
25282
+ import_docx.BorderStyle.DASHED,
25283
+ element.borderExternalWidth || element.borderWidth
25284
+ ),
25285
+ right: createBorder(
25286
+ element.borderColor,
25287
+ import_docx.BorderStyle.DASHED,
25288
+ element.borderExternalWidth || element.borderWidth
25289
+ ),
25290
+ insideHorizontal: createBorder(
25291
+ element.borderColor,
25292
+ import_docx.BorderStyle.DASHED,
25293
+ element.borderWidth
25294
+ ),
25295
+ insideVertical: createBorder(
25296
+ element.borderColor,
25297
+ import_docx.BorderStyle.DASHED,
25298
+ element.borderWidth
25299
+ )
25300
+ };
25301
+ default:
25302
+ return {
25303
+ top: outerBorder,
25304
+ bottom: outerBorder,
25305
+ left: outerBorder,
25306
+ right: outerBorder,
25307
+ insideHorizontal: innerBorder,
25308
+ insideVertical: innerBorder
25309
+ };
25310
+ }
25311
+ }
25312
+ function createTableCellBorders(td, inheritedColor, inheritedWidth) {
25313
+ if (!td.borderTypes?.length && !td.borderColor) {
25314
+ return void 0;
25315
+ }
25316
+ const color = td.borderColor || inheritedColor;
25317
+ const createCellBorder = () => createBorder(color, import_docx.BorderStyle.SINGLE, inheritedWidth);
25318
+ if (td.borderColor && !td.borderTypes?.length) {
25319
+ return {
25320
+ top: createCellBorder(),
25321
+ right: createCellBorder(),
25322
+ bottom: createCellBorder(),
25323
+ left: createCellBorder()
25324
+ };
25325
+ }
25326
+ const top = td.borderTypes?.includes("top" /* TOP */) ? createCellBorder() : noneBorder();
25327
+ const right2 = td.borderTypes?.includes("right" /* RIGHT */) ? createCellBorder() : noneBorder();
25328
+ const bottom = td.borderTypes?.includes("bottom" /* BOTTOM */) ? createCellBorder() : noneBorder();
25329
+ const left2 = td.borderTypes?.includes("left" /* LEFT */) ? createCellBorder() : noneBorder();
25330
+ return {
25331
+ top,
25332
+ right: right2,
25333
+ bottom,
25334
+ left: left2
25335
+ };
25336
+ }
25337
+ function createBorder(color = "#000000", style = import_docx.BorderStyle.SINGLE, width = 1) {
25338
+ return {
25339
+ style,
25340
+ color: normalizeHexColor(color, "000000") || "000000",
25341
+ size: pxBorderToDocxSize(width)
25342
+ };
25343
+ }
25344
+ function noneBorder() {
25345
+ return {
25346
+ style: import_docx.BorderStyle.NONE,
25347
+ size: 0,
25348
+ color: "FFFFFF"
25349
+ };
25350
+ }
25351
+ function mapPaddingToTableMargins(padding) {
25352
+ if (!padding) return void 0;
25353
+ return {
25354
+ top: pxToTwips(padding[0]),
25355
+ right: pxToTwips(padding[1]),
25356
+ bottom: pxToTwips(padding[2]),
25357
+ left: pxToTwips(padding[3]),
25358
+ marginUnitType: import_docx.WidthType.DXA
25359
+ };
25360
+ }
25361
+ function mapCellVerticalAlign(verticalAlign) {
25362
+ switch (verticalAlign) {
25363
+ case VerticalAlign.CENTER:
25364
+ return import_docx.VerticalAlign.CENTER;
25365
+ case "bottom" /* BOTTOM */:
25366
+ return import_docx.VerticalAlign.BOTTOM;
25367
+ default:
25368
+ return import_docx.VerticalAlign.TOP;
25369
+ }
25370
+ }
25371
+ function mapRowFlexToAlignment(rowFlex) {
25372
+ switch (rowFlex) {
25373
+ case "center" /* CENTER */:
25374
+ return import_docx.AlignmentType.CENTER;
25375
+ case "right" /* RIGHT */:
25376
+ return import_docx.AlignmentType.RIGHT;
25377
+ case "alignment" /* ALIGNMENT */:
25378
+ case "justify" /* JUSTIFY */:
25379
+ return import_docx.AlignmentType.JUSTIFIED;
25380
+ default:
25381
+ return import_docx.AlignmentType.LEFT;
25382
+ }
25383
+ }
25384
+ function mapUnderlineType(style) {
25385
+ switch (style) {
25386
+ case "double" /* DOUBLE */:
25387
+ return import_docx.UnderlineType.DOUBLE;
25388
+ case "dashed" /* DASHED */:
25389
+ return import_docx.UnderlineType.DASH;
25390
+ case "dotted" /* DOTTED */:
25391
+ return import_docx.UnderlineType.DOTTED;
25392
+ case "wavy" /* WAVY */:
25393
+ return import_docx.UnderlineType.WAVE;
25394
+ default:
25395
+ return import_docx.UnderlineType.SINGLE;
25396
+ }
25397
+ }
25398
+ function getInlineElementText(element) {
25399
+ switch (element.type) {
25400
+ case "control" /* CONTROL */:
25401
+ return getControlText(element.control);
25402
+ case "date" /* DATE */:
25403
+ return element.valueList?.length ? getTextFromElementList(element.valueList) : element.value;
25404
+ case "checkbox" /* CHECKBOX */:
25405
+ return element.checkbox?.value ? "\u2611" : "\u2610";
25406
+ case "radio" /* RADIO */:
25407
+ return element.radio?.value ? "\u25C9" : "\u25CB";
25408
+ case "label" /* LABEL */:
25409
+ case "latex" /* LATEX */:
25410
+ case "subscript" /* SUBSCRIPT */:
25411
+ case "superscript" /* SUPERSCRIPT */:
25412
+ return element.value;
25413
+ default:
25414
+ return element.value;
25415
+ }
25416
+ }
25417
+ function getControlText(control) {
25418
+ if (!control) return "";
25419
+ if (control.type === "text" /* TEXT */ || control.type === "date" /* DATE */ || control.type === "number" /* NUMBER */) {
25420
+ const value = control.value?.length ? getTextFromElementList(control.value) : "";
25421
+ const content = value || control.placeholder || "";
25422
+ return `${control.preText || ""}${content}${control.postText || ""}`;
25423
+ }
25424
+ if (control.type === "select" /* SELECT */) {
25425
+ const labels = getSelectedControlLabels(control.code, control.valueSets);
25426
+ const content = labels.length ? labels.join(control.multiSelectDelimiter || ", ") : control.placeholder || "";
25427
+ return `${control.preText || ""}${content}${control.postText || ""}`;
25428
+ }
25429
+ if (control.type === "checkbox" /* CHECKBOX */ || control.type === "radio" /* RADIO */) {
25430
+ const isRadio = control.type === "radio" /* RADIO */;
25431
+ return (control.valueSets || []).map((valueSet) => {
25432
+ const checked = control.code?.split(",").includes(valueSet.code) || false;
25433
+ return `${checked ? isRadio ? "\u25C9" : "\u2611" : isRadio ? "\u25CB" : "\u2610"} ${valueSet.value}`;
25434
+ }).join(" ");
25435
+ }
25436
+ return "";
25437
+ }
25438
+ function getSelectedControlLabels(code, valueSets) {
25439
+ if (!code || !valueSets?.length) return [];
25440
+ return code.split(",").map(
25441
+ (selectCode) => valueSets.find((valueSet) => valueSet.code === selectCode)?.value
25442
+ ).filter((value) => !!value);
25443
+ }
25444
+ function getBlockFallbackText(element) {
25445
+ if (element.block?.iframeBlock?.src) {
25446
+ return element.block.iframeBlock.src;
25447
+ }
25448
+ if (element.block?.videoBlock?.src) {
25449
+ return element.block.videoBlock.src;
25450
+ }
25451
+ return "";
25452
+ }
25453
+ function normalizeText(text = "") {
25454
+ return text.replace(new RegExp(ZERO, "g"), "").replace(/\r\n/g, "\n");
25455
+ }
25456
+ function createSerializeContext() {
25457
+ return {
25458
+ nextListInstance: 1
25459
+ };
25460
+ }
25461
+ function getListItemNumberingElement(listItemElements, fallback) {
25462
+ return listItemElements.find(
25463
+ (listItem) => listItem.listId || listItem.listLevel !== void 0 || !!listItem.listPreset || !!listItem.listStyle || !!listItem.listType
25464
+ ) || fallback;
25465
+ }
25466
+ function createListInstance(context) {
25467
+ return context.nextListInstance++;
25468
+ }
25469
+ function createTableSerializeContext(element) {
25470
+ return {
25471
+ cellLayoutMap: computeTableCellLayout(
25472
+ element.trList || [],
25473
+ element.colgroup || []
25474
+ ),
25475
+ borderColor: element.borderColor,
25476
+ borderWidth: element.borderWidth,
25477
+ borderExternalWidth: element.borderExternalWidth
25478
+ };
25479
+ }
25480
+ function computeTableCellLayout(trList, colgroup) {
25481
+ const cellLayoutMap = /* @__PURE__ */ new WeakMap();
25482
+ if (!trList.length || !colgroup.length) {
25483
+ return cellLayoutMap;
25484
+ }
25485
+ const occupiedUntilRow = Array(colgroup.length).fill(0);
25486
+ for (let rowIndex = 0; rowIndex < trList.length; rowIndex++) {
25487
+ const tr = trList[rowIndex];
25488
+ let colIndex = 0;
25489
+ for (let tdIndex = 0; tdIndex < tr.tdList.length; tdIndex++) {
25490
+ while (colIndex < colgroup.length && occupiedUntilRow[colIndex] > rowIndex) {
25491
+ colIndex += 1;
25492
+ }
25493
+ const td = tr.tdList[tdIndex];
25494
+ let width = 0;
25495
+ for (let currentCol = colIndex; currentCol < colIndex + td.colspan && currentCol < colgroup.length; currentCol++) {
25496
+ width += colgroup[currentCol].width;
25497
+ occupiedUntilRow[currentCol] = Math.max(
25498
+ occupiedUntilRow[currentCol],
25499
+ rowIndex + td.rowspan
25500
+ );
25501
+ }
25502
+ cellLayoutMap.set(td, { width });
25503
+ colIndex += td.colspan;
25504
+ }
25505
+ }
25506
+ return cellLayoutMap;
25507
+ }
25508
+ function getTableWidthFromColgroup(colgroup) {
25509
+ if (!colgroup?.length) {
25510
+ return void 0;
25511
+ }
25512
+ return colgroup.reduce((sum, col) => sum + col.width, 0);
25513
+ }
25514
+ function normalizeHexColor(color, fallback) {
25515
+ const normalized = normalizeHexLiteral(color) || normalizeRgbColor(color) || normalizeBrowserColor(color);
25516
+ if (normalized) {
25517
+ return normalized;
25518
+ }
25519
+ return fallback ? normalizeHexLiteral(fallback) || void 0 : void 0;
25520
+ }
25521
+ function normalizeHexLiteral(color) {
25522
+ const value = color.trim().replace(/^#/, "");
25523
+ if (/^[\da-f]{3}$/i.test(value)) {
25524
+ return value.split("").map((part) => `${part}${part}`).join("").toUpperCase();
25525
+ }
25526
+ if (/^[\da-f]{4}$/i.test(value)) {
25527
+ return value.slice(0, 3).split("").map((part) => `${part}${part}`).join("").toUpperCase();
25528
+ }
25529
+ if (/^[\da-f]{6}$/i.test(value)) {
25530
+ return value.toUpperCase();
25531
+ }
25532
+ if (/^[\da-f]{8}$/i.test(value)) {
25533
+ return value.slice(0, 6).toUpperCase();
25534
+ }
25535
+ return void 0;
25536
+ }
25537
+ function normalizeRgbColor(color) {
25538
+ const match = color.trim().match(/^rgba?\((.+)\)$/i);
25539
+ if (!match) {
25540
+ return void 0;
25541
+ }
25542
+ const parts = match[1].replace(/\s*\/\s*/g, ",").split(/\s*,\s*|\s+/).filter(Boolean);
25543
+ if (parts.length < 3) {
25544
+ return void 0;
25545
+ }
25546
+ const red = parseRgbChannel(parts[0]);
25547
+ const green = parseRgbChannel(parts[1]);
25548
+ const blue = parseRgbChannel(parts[2]);
25549
+ if (red === null || green === null || blue === null) {
25550
+ return void 0;
25551
+ }
25552
+ const alpha = parts[3] !== void 0 ? parseAlphaChannel(parts[3]) : 1;
25553
+ if (alpha === null || alpha <= 0) {
25554
+ return void 0;
25555
+ }
25556
+ const flattened = alpha >= 1 ? [red, green, blue] : [
25557
+ flattenChannel(red, alpha),
25558
+ flattenChannel(green, alpha),
25559
+ flattenChannel(blue, alpha)
25560
+ ];
25561
+ return flattened.map(toHexChannel).join("");
25562
+ }
25563
+ function normalizeBrowserColor(color) {
25564
+ if (typeof document === "undefined") {
25565
+ return void 0;
25566
+ }
25567
+ const canvas = document.createElement("canvas");
25568
+ const context = canvas.getContext("2d");
25569
+ if (!context) {
25570
+ return void 0;
25571
+ }
25572
+ context.fillStyle = "#000000";
25573
+ context.fillStyle = color;
25574
+ const normalized = context.fillStyle;
25575
+ if (typeof normalized !== "string") {
25576
+ return void 0;
25577
+ }
25578
+ return normalizeHexLiteral(normalized) || normalizeRgbColor(normalized);
25579
+ }
25580
+ function parseRgbChannel(value) {
25581
+ if (value.endsWith("%")) {
25582
+ const percent = Number(value.slice(0, -1));
25583
+ if (!Number.isFinite(percent)) {
25584
+ return null;
25585
+ }
25586
+ return clampColorChannel(Math.round(percent / 100 * 255));
25587
+ }
25588
+ const channel = Number(value);
25589
+ if (!Number.isFinite(channel)) {
25590
+ return null;
25591
+ }
25592
+ return clampColorChannel(Math.round(channel));
25593
+ }
25594
+ function parseAlphaChannel(value) {
25595
+ if (value.endsWith("%")) {
25596
+ const percent = Number(value.slice(0, -1));
25597
+ if (!Number.isFinite(percent)) {
25598
+ return null;
25599
+ }
25600
+ return Math.min(Math.max(percent / 100, 0), 1);
25601
+ }
25602
+ const alpha = Number(value);
25603
+ if (!Number.isFinite(alpha)) {
25604
+ return null;
25605
+ }
25606
+ return Math.min(Math.max(alpha, 0), 1);
25607
+ }
25608
+ function clampColorChannel(value) {
25609
+ return Math.min(Math.max(value, 0), 255);
25610
+ }
25611
+ function flattenChannel(value, alpha) {
25612
+ return clampColorChannel(Math.round(255 * (1 - alpha) + value * alpha));
25613
+ }
25614
+ function toHexChannel(value) {
25615
+ return value.toString(16).padStart(2, "0").toUpperCase();
25616
+ }
25617
+ function pxBorderToDocxSize(width) {
25618
+ const safeWidth = Number.isFinite(width) && width > 0 ? width : 1;
25619
+ return Math.max(2, Math.round(safeWidth * 6));
25620
+ }
25621
+ function getRegularImageType(mime) {
25622
+ if (mime.includes("png")) return "png";
25623
+ if (mime.includes("jpeg") || mime.includes("jpg")) return "jpg";
25624
+ if (mime.includes("gif")) return "gif";
25625
+ if (mime.includes("bmp")) return "bmp";
25626
+ return null;
25627
+ }
25628
+ function normalizeDocxFileName(fileName) {
25629
+ const trimmed = fileName?.trim();
25630
+ if (!trimmed) {
25631
+ return DEFAULT_DOCX_FILE_NAME;
25632
+ }
25633
+ return trimmed.toLowerCase().endsWith(".docx") ? trimmed : `${trimmed}.docx`;
25634
+ }
25635
+ function pxToTwips(px) {
25636
+ return Math.round(px * 15);
25637
+ }
25638
+
25639
+ // src/utils/pdf.ts
25640
+ var import_pdf_lib = require("pdf-lib");
25641
+ var DEFAULT_PDF_FILE_NAME = "document.pdf";
25642
+ var DEFAULT_IMAGE_WIDTH2 = 320;
25643
+ var DEFAULT_IMAGE_HEIGHT2 = 180;
25644
+ var DEFAULT_LINE_HEIGHT2 = 1.15;
25645
+ var PAGE_REGION_GAP = 12;
25646
+ var SEPARATOR_HEIGHT = 12;
25647
+ var LIST_GAP = 8;
25648
+ var FOOTER_MIN_BAR_HEIGHT = 29;
25649
+ var PDF_TEXT_FALLBACKS = {
25650
+ "\u2297": "x",
25651
+ "\u2611": "[x]",
25652
+ "\u2612": "[x]",
25653
+ "\u2610": "[]",
25654
+ "\u2713": "v",
25655
+ "\u2714": "v",
25656
+ "\u2717": "x",
25657
+ "\u2715": "x",
25658
+ "\u25E6": "o",
25659
+ "\u25AA": "*",
25660
+ "\u25A0": "*"
25661
+ };
25662
+ var pdfTextCache = /* @__PURE__ */ new WeakMap();
25663
+ var bulletCharMap = {
25664
+ ["disc" /* DISC */]: "\u2022",
25665
+ ["circle" /* CIRCLE */]: "\u25E6",
25666
+ ["square" /* SQUARE */]: "\u25A0",
25667
+ ["checkbox" /* CHECKBOX */]: "\u2611"
25668
+ };
25669
+ async function exportEditorDataToPdf(payload, options = {}) {
25670
+ const editorOptions = payload.options;
25671
+ const pdfDoc = await import_pdf_lib.PDFDocument.create();
25672
+ const context = await createPdfContext(pdfDoc, editorOptions);
25673
+ const headerBlocks = editorOptions.header.disabled ? [] : await parseElementList(payload.data.header || [], context, editorOptions);
25674
+ const footerBlocks = editorOptions.footer.disabled ? [] : await parseElementList(payload.data.footer || [], context, editorOptions);
25675
+ const mainBlocks = await parseElementList(
25676
+ payload.data.main,
25677
+ context,
25678
+ editorOptions
25679
+ );
25680
+ const headerHeight = measureBlocksHeight(
25681
+ headerBlocks,
25682
+ context.contentWidth,
25683
+ context
25684
+ );
25685
+ const footerHeight = measureBlocksHeight(
25686
+ footerBlocks,
25687
+ context.contentWidth,
25688
+ context
25689
+ );
25690
+ const footerBarHeight = getFooterBarHeight(footerHeight, context);
25691
+ const pageNumberHeight = getPageNumberHeight(editorOptions.pageNumber);
25692
+ const contentTop = Math.max(
25693
+ context.margins[0],
25694
+ context.headerTop + headerHeight + (headerBlocks.length ? context.headerGap : 0)
25695
+ );
25696
+ const contentBottom = Math.max(
25697
+ context.margins[2],
25698
+ context.footerBottom + footerBarHeight,
25699
+ getReservedPageNumberBottom(editorOptions.pageNumber, pageNumberHeight)
25700
+ );
25701
+ const pages = renderMainBlocks(
25702
+ mainBlocks,
25703
+ context,
25704
+ contentTop,
25705
+ contentBottom
25706
+ );
25707
+ renderPageDecorations(
25708
+ pages,
25709
+ context,
25710
+ headerBlocks,
25711
+ footerBlocks,
25712
+ headerHeight,
25713
+ footerHeight,
25714
+ footerBarHeight,
25715
+ editorOptions.pageNumber
25716
+ );
25717
+ const bytes = await pdfDoc.save();
25718
+ return {
25719
+ blob: new Blob([bytes], { type: "application/pdf" }),
25720
+ fileName: normalizePdfFileName(options.fileName)
25721
+ };
25722
+ }
25723
+ async function createPdfContext(pdfDoc, options) {
25724
+ const [sans, sansBold, sansItalic, sansBoldItalic] = await Promise.all([
25725
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.Helvetica),
25726
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.HelveticaBold),
25727
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.HelveticaOblique),
25728
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.HelveticaBoldOblique)
25729
+ ]);
25730
+ const [serif, serifBold, serifItalic, serifBoldItalic] = await Promise.all([
25731
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.TimesRoman),
25732
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.TimesRomanBold),
25733
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.TimesRomanItalic),
25734
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.TimesRomanBoldItalic)
25735
+ ]);
25736
+ const [mono, monoBold, monoItalic, monoBoldItalic] = await Promise.all([
25737
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.Courier),
25738
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.CourierBold),
25739
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.CourierOblique),
25740
+ pdfDoc.embedFont(import_pdf_lib.StandardFonts.CourierBoldOblique)
25741
+ ]);
25742
+ const pageWidth = options.paperDirection === "horizontal" /* HORIZONTAL */ ? pxToPt(options.height) : pxToPt(options.width);
25743
+ const pageHeight = options.paperDirection === "horizontal" /* HORIZONTAL */ ? pxToPt(options.width) : pxToPt(options.height);
25744
+ const margins = options.margins.map(pxToPt);
25745
+ return {
25746
+ pdfDoc,
25747
+ options,
25748
+ fonts: {
25749
+ sans: {
25750
+ regular: sans,
25751
+ bold: sansBold,
25752
+ italic: sansItalic,
25753
+ boldItalic: sansBoldItalic
25754
+ },
25755
+ serif: {
25756
+ regular: serif,
25757
+ bold: serifBold,
25758
+ italic: serifItalic,
25759
+ boldItalic: serifBoldItalic
25760
+ },
25761
+ mono: {
25762
+ regular: mono,
25763
+ bold: monoBold,
25764
+ italic: monoItalic,
25765
+ boldItalic: monoBoldItalic
25766
+ }
25767
+ },
25768
+ pageWidth,
25769
+ pageHeight,
25770
+ margins,
25771
+ contentWidth: pageWidth - margins[1] - margins[3],
25772
+ headerTop: pxToPt(options.header.top),
25773
+ footerBottom: pxToPt(options.footer.bottom),
25774
+ headerGap: PAGE_REGION_GAP,
25775
+ footerGap: PAGE_REGION_GAP,
25776
+ separatorGap: SEPARATOR_HEIGHT,
25777
+ imageCache: /* @__PURE__ */ new Map()
25778
+ };
25779
+ }
25780
+ async function parseElementList(elementList, context, options) {
25781
+ const blocks = [];
25782
+ let inlineBuffer = [];
25783
+ const flushInlineBuffer = async () => {
25784
+ if (!inlineBuffer.length) return;
25785
+ blocks.push(...await parseInlineElements(inlineBuffer, context, options));
25786
+ inlineBuffer = [];
25787
+ };
25788
+ for (let i = 0; i < elementList.length; i++) {
25789
+ const element = elementList[i];
25790
+ if (element.type === "area" /* AREA */ && element.valueList?.length) {
25791
+ await flushInlineBuffer();
25792
+ blocks.push(
25793
+ ...await parseElementList(element.valueList, context, options)
25794
+ );
25795
+ continue;
25796
+ }
25797
+ if (element.type === "title" /* TITLE */ && element.valueList?.length) {
25798
+ await flushInlineBuffer();
25799
+ blocks.push(
25800
+ ...await parseInlineElements(element.valueList, context, options, {
25801
+ alignment: element.rowFlex,
25802
+ rowMargin: element.rowMargin,
25803
+ listMarker: void 0,
25804
+ titleLevel: element.level || "first" /* FIRST */
25805
+ })
25806
+ );
25807
+ continue;
25808
+ }
25809
+ if (element.type === "list" /* LIST */ && element.valueList?.length) {
25810
+ await flushInlineBuffer();
25811
+ blocks.push(...await parseListBlock(element, context, options));
25812
+ continue;
25813
+ }
25814
+ if (element.type === "table" /* TABLE */ && element.trList?.length) {
25815
+ await flushInlineBuffer();
25816
+ blocks.push(await parseTableBlock(element, context, options));
25817
+ continue;
25818
+ }
25819
+ if (element.type === "pageBreak" /* PAGE_BREAK */) {
25820
+ await flushInlineBuffer();
25821
+ blocks.push({ type: "pageBreak" });
25822
+ continue;
25823
+ }
25824
+ if (element.type === "separator" /* SEPARATOR */) {
25825
+ await flushInlineBuffer();
25826
+ blocks.push({ type: "separator" });
25827
+ continue;
25828
+ }
25829
+ if (element.type === "image" /* IMAGE */ && element.imgDisplay && element.imgDisplay !== "inline") {
25830
+ await flushInlineBuffer();
25831
+ const imageBlock = await createImageBlock(element, context);
25832
+ if (imageBlock) {
25833
+ blocks.push(imageBlock);
25834
+ }
25835
+ continue;
25836
+ }
25837
+ if (element.type === "block" /* BLOCK */) {
25838
+ await flushInlineBuffer();
25839
+ const blockText = getBlockFallbackText2(element);
25840
+ if (blockText) {
25841
+ blocks.push(
25842
+ ...await parseInlineElements(
25843
+ [{ value: blockText }],
25844
+ context,
25845
+ options
25846
+ )
25847
+ );
25848
+ }
25849
+ continue;
25850
+ }
25851
+ inlineBuffer.push(element);
25852
+ }
25853
+ await flushInlineBuffer();
25854
+ return blocks;
25855
+ }
25856
+ async function parseInlineElements(elementList, context, options, overrides = {}) {
25857
+ const paragraphs = [];
25858
+ let current = null;
25859
+ const getParagraphContext = (element) => ({
25860
+ alignment: overrides.alignment !== void 0 ? overrides.alignment : element?.rowFlex,
25861
+ rowMargin: overrides.rowMargin !== void 0 ? overrides.rowMargin : element?.rowMargin,
25862
+ listMarker: overrides.listMarker
25863
+ });
25864
+ const ensureParagraph = (element) => {
25865
+ if (!current) {
25866
+ current = {
25867
+ runs: [],
25868
+ context: getParagraphContext(element)
25869
+ };
25870
+ }
25871
+ return current;
25872
+ };
25873
+ const flushParagraph = (contextValue) => {
25874
+ if (!current) {
25875
+ if (!contextValue) return;
25876
+ paragraphs.push({
25877
+ type: "paragraph",
25878
+ runs: [],
25879
+ context: contextValue
25880
+ });
25881
+ return;
25882
+ }
25883
+ paragraphs.push({
25884
+ type: "paragraph",
25885
+ runs: current.runs,
25886
+ context: current.context
25887
+ });
25888
+ current = null;
25889
+ };
25890
+ const appendText = (element, text, factory, preserveTrailingBreak = false) => {
25891
+ const normalized = normalizeText2(text);
25892
+ const parts = normalized.split("\n");
25893
+ let lastSeparatorFlushedContent = false;
25894
+ for (let i = 0; i < parts.length; i++) {
25895
+ const segment = parts[i];
25896
+ if (segment) {
25897
+ ensureParagraph(element).runs.push(factory(segment));
25898
+ }
25899
+ if (i < parts.length - 1) {
25900
+ if (!current) {
25901
+ paragraphs.push({
25902
+ type: "paragraph",
25903
+ runs: [],
25904
+ context: getParagraphContext(element)
25905
+ });
25906
+ lastSeparatorFlushedContent = false;
25907
+ } else {
25908
+ flushParagraph();
25909
+ lastSeparatorFlushedContent = true;
25910
+ }
25911
+ }
25912
+ }
25913
+ if (preserveTrailingBreak && normalized.endsWith("\n") && lastSeparatorFlushedContent) {
25914
+ paragraphs.push({
25915
+ type: "paragraph",
25916
+ runs: [],
25917
+ context: getParagraphContext(element)
25918
+ });
25919
+ }
25920
+ };
25921
+ for (let i = 0; i < elementList.length; i++) {
25922
+ const element = elementList[i];
25923
+ if (element.type === "hyperlink" /* HYPERLINK */ && element.valueList?.length) {
25924
+ const children = element.valueList || [];
25925
+ for (let childIndex = 0; childIndex < children.length; childIndex++) {
25926
+ const child = children[childIndex];
25927
+ const text = normalizeText2(getInlineElementText2(child));
25928
+ if (!text) continue;
25929
+ appendText(
25930
+ element,
25931
+ text,
25932
+ (segment) => {
25933
+ const run = createTextRun2(
25934
+ child,
25935
+ options,
25936
+ overrides.titleLevel,
25937
+ segment
25938
+ );
25939
+ run.color = run.color || options.defaultHyperlinkColor;
25940
+ run.underline = true;
25941
+ return run;
25942
+ },
25943
+ i === elementList.length - 1 && childIndex === children.length - 1
25944
+ );
25945
+ }
25946
+ continue;
25947
+ }
25948
+ if (element.type === "image" /* IMAGE */) {
25949
+ const imageRun = await createInlineImageRun(element, context);
25950
+ if (imageRun) {
25951
+ ensureParagraph(element).runs.push(imageRun);
25952
+ }
25953
+ continue;
25954
+ }
25955
+ if (element.type === "columnBreak" /* COLUMN_BREAK */) {
25956
+ flushParagraph(getParagraphContext(element));
25957
+ continue;
25958
+ }
25959
+ if (element.type === "pageBreak" /* PAGE_BREAK */) {
25960
+ flushParagraph();
25961
+ paragraphs.push({ type: "pageBreak" });
25962
+ continue;
25963
+ }
25964
+ if (element.type === "tab" /* TAB */) {
25965
+ ensureParagraph(element).runs.push({
25966
+ type: "tab",
25967
+ width: pxToPt(options.defaultTabWidth)
25968
+ });
25969
+ continue;
25970
+ }
25971
+ if (element.type === "label" /* LABEL */) {
25972
+ const badgeRun = createBadgeRun(element, options, overrides.titleLevel);
25973
+ ensureParagraph(element).runs.push(badgeRun);
25974
+ continue;
25975
+ }
25976
+ const textValue = getInlineElementText2(element);
25977
+ if (!textValue) {
25978
+ continue;
25979
+ }
25980
+ appendText(
25981
+ element,
25982
+ textValue,
25983
+ (segment) => createTextRun2(element, options, overrides.titleLevel, segment),
25984
+ i === elementList.length - 1
25985
+ );
25986
+ }
25987
+ if (current) {
25988
+ flushParagraph();
25989
+ }
25990
+ return paragraphs.length ? paragraphs : [
25991
+ {
25992
+ type: "paragraph",
25993
+ runs: [],
25994
+ context: getParagraphContext()
25995
+ }
25996
+ ];
25997
+ }
25998
+ async function parseListBlock(element, context, options) {
25999
+ const zipList = zipElementList(element.valueList || []);
26000
+ const listItemMap = splitListElement(zipList);
26001
+ const blocks = [];
26002
+ const orderedCounts = Array(9).fill(0);
26003
+ for (const listItemElements of listItemMap.values()) {
26004
+ const itemElement = getListItemNumberingElement2(listItemElements, element);
26005
+ const marker = getListMarker(itemElement, orderedCounts);
26006
+ blocks.push(
26007
+ ...await parseInlineElements(listItemElements, context, options, {
26008
+ alignment: itemElement.rowFlex,
26009
+ rowMargin: itemElement.rowMargin,
26010
+ listMarker: marker
26011
+ })
26012
+ );
26013
+ }
26014
+ return blocks;
26015
+ }
26016
+ async function parseTableBlock(element, context, options) {
26017
+ const placements = computeTableCellPlacement(
26018
+ element.trList || [],
26019
+ element.colgroup || []
26020
+ );
26021
+ const rows = [];
26022
+ for (let rowIndex = 0; rowIndex < (element.trList || []).length; rowIndex++) {
26023
+ const tr = element.trList[rowIndex];
26024
+ const cells = [];
26025
+ for (let cellIndex = 0; cellIndex < tr.tdList.length; cellIndex++) {
26026
+ const td = tr.tdList[cellIndex];
26027
+ const placement = placements.get(td);
26028
+ cells.push({
26029
+ colspan: td.colspan,
26030
+ rowspan: td.rowspan,
26031
+ blocks: await parseElementList(td.value, context, options),
26032
+ colIndex: placement?.colIndex || 0,
26033
+ width: placement?.width || td.width || 0,
26034
+ padding: td.padding || options.table.tdPadding,
26035
+ verticalAlign: td.verticalAlign,
26036
+ backgroundColor: td.backgroundColor,
26037
+ borderColor: td.borderColor,
26038
+ borderTypes: td.borderTypes
26039
+ });
26040
+ }
26041
+ rows.push({
26042
+ height: Math.max(tr.height || 0, options.table.defaultTrMinHeight),
26043
+ cells
26044
+ });
26045
+ }
26046
+ return {
26047
+ type: "table",
26048
+ rows,
26049
+ colWidths: (element.colgroup || []).map((col) => col.width),
26050
+ borderType: element.borderType,
26051
+ borderColor: element.borderColor || options.table.defaultBorderColor,
26052
+ borderWidth: element.borderWidth || 1,
26053
+ borderExternalWidth: element.borderExternalWidth
26054
+ };
26055
+ }
26056
+ async function createImageBlock(element, context) {
26057
+ const image = await loadPdfImage(element.value, context);
26058
+ if (!image) {
26059
+ return null;
26060
+ }
26061
+ return {
26062
+ type: "image",
26063
+ image,
26064
+ width: pxToPt(
26065
+ Math.max(1, Math.round(element.width || DEFAULT_IMAGE_WIDTH2))
26066
+ ),
26067
+ height: pxToPt(
26068
+ Math.max(1, Math.round(element.height || DEFAULT_IMAGE_HEIGHT2))
26069
+ ),
26070
+ alignment: element.rowFlex
26071
+ };
26072
+ }
26073
+ async function createInlineImageRun(element, context) {
26074
+ const image = await loadPdfImage(element.value, context);
26075
+ if (!image) {
26076
+ return null;
26077
+ }
26078
+ return {
26079
+ type: "image",
26080
+ image,
26081
+ width: pxToPt(
26082
+ Math.max(1, Math.round(element.width || DEFAULT_IMAGE_WIDTH2))
26083
+ ),
26084
+ height: pxToPt(
26085
+ Math.max(1, Math.round(element.height || DEFAULT_IMAGE_HEIGHT2))
26086
+ )
26087
+ };
26088
+ }
26089
+ function createTextRun2(element, options, titleLevel, text) {
26090
+ const titleSize = getTitleSize(titleLevel, options);
26091
+ const size = element.size || titleSize || options.defaultSize;
26092
+ const isTitle = !!titleLevel;
26093
+ return {
26094
+ type: "text",
26095
+ text,
26096
+ size,
26097
+ font: element.font || options.defaultFont,
26098
+ bold: isTitle ? true : !!element.bold,
26099
+ italic: !!element.italic,
26100
+ underline: !!element.underline,
26101
+ strikeout: !!element.strikeout,
26102
+ color: element.color || (isTitle ? "#000000" : void 0),
26103
+ highlight: element.highlight,
26104
+ superscript: element.type === "superscript" /* SUPERSCRIPT */,
26105
+ subscript: element.type === "subscript" /* SUBSCRIPT */
26106
+ };
26107
+ }
26108
+ function createBadgeRun(element, options, titleLevel) {
26109
+ const titleSize = getTitleSize(titleLevel, options);
26110
+ return {
26111
+ type: "badge",
26112
+ text: element.value,
26113
+ size: element.size || titleSize || options.defaultSize,
26114
+ font: element.font || options.defaultFont,
26115
+ bold: titleLevel ? true : !!element.bold,
26116
+ italic: !!element.italic,
26117
+ underline: !!element.underline,
26118
+ strikeout: !!element.strikeout,
26119
+ color: element.label?.color || element.color,
26120
+ backgroundColor: element.label?.backgroundColor,
26121
+ borderColor: element.label?.borderColor,
26122
+ padding: element.label?.padding || [2, 4, 2, 4]
26123
+ };
26124
+ }
26125
+ function renderMainBlocks(blocks, context, contentTop, contentBottom) {
26126
+ const pages = [];
26127
+ const contentEnd = context.pageHeight - contentBottom;
26128
+ const createPage = () => {
26129
+ const page = context.pdfDoc.addPage([
26130
+ context.pageWidth,
26131
+ context.pageHeight
26132
+ ]);
26133
+ const state = {
26134
+ page,
26135
+ cursorY: contentTop,
26136
+ isEmpty: true
26137
+ };
26138
+ pages.push(state);
26139
+ return state;
26140
+ };
26141
+ let currentPage = createPage();
26142
+ const ensureSpace = (height) => {
26143
+ if (currentPage.cursorY + height <= contentEnd || currentPage.isEmpty) {
26144
+ return;
26145
+ }
26146
+ currentPage = createPage();
26147
+ };
26148
+ for (let i = 0; i < blocks.length; i++) {
26149
+ const block = blocks[i];
26150
+ if (block.type === "pageBreak") {
26151
+ currentPage = createPage();
26152
+ continue;
26153
+ }
26154
+ if (block.type === "paragraph") {
26155
+ currentPage = renderParagraphAcrossPages(
26156
+ block,
26157
+ currentPage,
26158
+ pages,
26159
+ context,
26160
+ contentTop,
26161
+ contentEnd
26162
+ );
26163
+ continue;
26164
+ }
26165
+ if (block.type === "separator") {
26166
+ ensureSpace(context.separatorGap);
26167
+ renderSeparator(
26168
+ currentPage.page,
26169
+ context,
26170
+ currentPage.cursorY,
26171
+ context.margins[3],
26172
+ context.contentWidth
26173
+ );
26174
+ currentPage.cursorY += context.separatorGap;
26175
+ currentPage.isEmpty = false;
26176
+ continue;
26177
+ }
26178
+ if (block.type === "image") {
26179
+ ensureSpace(block.height);
26180
+ renderImageBlock(currentPage.page, block, context, currentPage.cursorY);
26181
+ currentPage.cursorY += block.height;
26182
+ currentPage.isEmpty = false;
26183
+ continue;
26184
+ }
26185
+ const tableLayout = layoutTable(block, context.contentWidth, context);
26186
+ currentPage = renderTableAcrossPages(
26187
+ block,
26188
+ tableLayout,
26189
+ currentPage,
26190
+ pages,
26191
+ context,
26192
+ contentTop,
26193
+ contentEnd
26194
+ );
26195
+ }
26196
+ return pages;
26197
+ }
26198
+ function renderParagraphAcrossPages(block, currentPage, pages, context, contentTop, contentEnd) {
26199
+ const layout = layoutParagraph(block, context.contentWidth, context);
26200
+ for (let lineIndex = 0; lineIndex < layout.lines.length; lineIndex++) {
26201
+ const line = layout.lines[lineIndex];
26202
+ if (currentPage.cursorY + line.height > contentEnd && !currentPage.isEmpty) {
26203
+ const page = context.pdfDoc.addPage([
26204
+ context.pageWidth,
26205
+ context.pageHeight
26206
+ ]);
26207
+ currentPage = {
26208
+ page,
26209
+ cursorY: contentTop,
26210
+ isEmpty: true
26211
+ };
26212
+ pages.push(currentPage);
26213
+ }
26214
+ renderParagraphLine(
26215
+ currentPage.page,
26216
+ block,
26217
+ line,
26218
+ lineIndex === 0,
26219
+ layout,
26220
+ context,
26221
+ currentPage.cursorY,
26222
+ context.margins[3],
26223
+ context.contentWidth
26224
+ );
26225
+ currentPage.cursorY += line.height;
26226
+ currentPage.isEmpty = false;
26227
+ }
26228
+ return currentPage;
26229
+ }
26230
+ function renderTableAcrossPages(block, tableLayout, currentPage, pages, context, contentTop, contentEnd) {
26231
+ const scaledColWidths = scaleColumnWidths(
26232
+ block.colWidths,
26233
+ context.contentWidth
26234
+ );
26235
+ let tableTop = currentPage.cursorY;
26236
+ for (let rowIndex = 0; rowIndex < block.rows.length; rowIndex++) {
26237
+ const row = block.rows[rowIndex];
26238
+ const rowHeight = tableLayout.rowHeights[rowIndex];
26239
+ const spanHeight = getSpanningRowHeight(
26240
+ block.rows,
26241
+ tableLayout.rowHeights,
26242
+ rowIndex
26243
+ );
26244
+ if (currentPage.cursorY + spanHeight > contentEnd && !currentPage.isEmpty) {
26245
+ const page = context.pdfDoc.addPage([
26246
+ context.pageWidth,
26247
+ context.pageHeight
26248
+ ]);
26249
+ currentPage = {
26250
+ page,
26251
+ cursorY: contentTop,
26252
+ isEmpty: true
26253
+ };
26254
+ pages.push(currentPage);
26255
+ tableTop = currentPage.cursorY;
26256
+ }
26257
+ const rowTop = currentPage.cursorY;
26258
+ for (let cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
26259
+ const cell = row.cells[cellIndex];
26260
+ const cellX = context.margins[3] + getColumnOffset(scaledColWidths, cell.colIndex);
26261
+ const cellWidth = getColumnSpanWidth(
26262
+ scaledColWidths,
26263
+ cell.colIndex,
26264
+ cell.colspan
26265
+ );
26266
+ const cellHeight = getRowSpanHeight(
26267
+ tableLayout.rowHeights,
26268
+ rowIndex,
26269
+ cell.rowspan
26270
+ );
26271
+ renderTableCell(
26272
+ currentPage.page,
26273
+ cell,
26274
+ context,
26275
+ block,
26276
+ cellX,
26277
+ rowTop,
26278
+ cellWidth,
26279
+ cellHeight
26280
+ );
26281
+ }
26282
+ currentPage.cursorY += rowHeight;
26283
+ currentPage.isEmpty = false;
26284
+ }
26285
+ return currentPage;
26286
+ }
26287
+ function renderPageDecorations(pages, context, headerBlocks, footerBlocks, headerHeight, footerHeight, footerBarHeight, pageNumber) {
26288
+ const totalPages = pages.length;
26289
+ for (let index = 0; index < pages.length; index++) {
26290
+ const pageState = pages[index];
26291
+ if (headerBlocks.length) {
26292
+ renderBlocksInRegion(
26293
+ pageState.page,
26294
+ headerBlocks,
26295
+ context,
26296
+ context.margins[3],
26297
+ context.headerTop,
26298
+ context.contentWidth
26299
+ );
26300
+ }
26301
+ if (footerBarHeight > 0) {
26302
+ if (context.options.footer.backgroundColor) {
26303
+ pageState.page.drawRectangle({
26304
+ x: 0,
26305
+ y: context.footerBottom,
26306
+ width: context.pageWidth,
26307
+ height: footerBarHeight,
26308
+ color: toPdfColor(context.options.footer.backgroundColor, "#FFFFFF")
26309
+ });
26310
+ }
26311
+ if (footerBlocks.length) {
26312
+ const footerTop = context.pageHeight - context.footerBottom - footerBarHeight;
26313
+ const footerContentTop = footerTop + Math.max(0, (footerBarHeight - footerHeight) / 2);
26314
+ renderBlocksInRegion(
26315
+ pageState.page,
26316
+ footerBlocks,
26317
+ context,
26318
+ context.margins[3],
26319
+ footerContentTop,
26320
+ context.contentWidth
26321
+ );
26322
+ }
26323
+ }
26324
+ renderPageNumber(pageState.page, context, pageNumber, index, totalPages);
26325
+ }
26326
+ }
26327
+ function renderBlocksInRegion(page, blocks, context, x, top, width) {
26328
+ let cursorY = top;
26329
+ for (let i = 0; i < blocks.length; i++) {
26330
+ const block = blocks[i];
26331
+ if (block.type === "pageBreak") {
26332
+ continue;
26333
+ }
26334
+ if (block.type === "paragraph") {
26335
+ const layout = layoutParagraph(block, width, context);
26336
+ for (let lineIndex = 0; lineIndex < layout.lines.length; lineIndex++) {
26337
+ const line = layout.lines[lineIndex];
26338
+ renderParagraphLine(
26339
+ page,
26340
+ block,
26341
+ line,
26342
+ lineIndex === 0,
26343
+ layout,
26344
+ context,
26345
+ cursorY,
26346
+ x,
26347
+ width
26348
+ );
26349
+ cursorY += line.height;
26350
+ }
26351
+ continue;
26352
+ }
26353
+ if (block.type === "separator") {
26354
+ renderSeparator(page, context, cursorY, x, width);
26355
+ cursorY += context.separatorGap;
26356
+ continue;
26357
+ }
26358
+ if (block.type === "image") {
26359
+ renderImageBlock(page, block, context, cursorY, x, width);
26360
+ cursorY += block.height;
26361
+ continue;
26362
+ }
26363
+ const tableLayout = layoutTable(block, width, context);
26364
+ const scaledColWidths = scaleColumnWidths(block.colWidths, width);
26365
+ for (let rowIndex = 0; rowIndex < block.rows.length; rowIndex++) {
26366
+ const row = block.rows[rowIndex];
26367
+ const rowTop = cursorY;
26368
+ for (let cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
26369
+ const cell = row.cells[cellIndex];
26370
+ const cellX = x + getColumnOffset(scaledColWidths, cell.colIndex);
26371
+ const cellWidth = getColumnSpanWidth(
26372
+ scaledColWidths,
26373
+ cell.colIndex,
26374
+ cell.colspan
26375
+ );
26376
+ const cellHeight = getRowSpanHeight(
26377
+ tableLayout.rowHeights,
26378
+ rowIndex,
26379
+ cell.rowspan
26380
+ );
26381
+ renderTableCell(
26382
+ page,
26383
+ cell,
26384
+ context,
26385
+ block,
26386
+ cellX,
26387
+ rowTop,
26388
+ cellWidth,
26389
+ cellHeight
26390
+ );
26391
+ }
26392
+ cursorY += tableLayout.rowHeights[rowIndex];
26393
+ }
26394
+ }
26395
+ }
26396
+ function renderParagraphLine(page, block, line, isFirstLine, layout, context, top, baseX, regionWidth) {
26397
+ const alignment = normalizeAlignment(block.context.alignment);
26398
+ const lineWidth = line.width;
26399
+ const availableWidth = Math.max(
26400
+ 1,
26401
+ regionWidth - layout.listIndent - layout.markerWidth
26402
+ );
26403
+ const justifyExtra = alignment === "justify" /* JUSTIFY */ && !isLastParagraphLine(layout, line) ? getJustifyExtra(line, availableWidth) : 0;
26404
+ let offsetX = 0;
26405
+ if (alignment === "center" /* CENTER */) {
26406
+ offsetX = (availableWidth - lineWidth) / 2;
26407
+ } else if (alignment === "right" /* RIGHT */) {
26408
+ offsetX = availableWidth - lineWidth;
26409
+ }
26410
+ const lineStart = baseX + layout.listIndent + layout.markerWidth + Math.max(0, offsetX);
26411
+ if (isFirstLine && block.context.listMarker) {
26412
+ const markerRun = getMarkerRun(block, context);
26413
+ const markerText = block.context.listMarker.text;
26414
+ drawTextRun(
26415
+ page,
26416
+ markerRun,
26417
+ markerText,
26418
+ baseX + layout.listIndent,
26419
+ top,
26420
+ line.height,
26421
+ context
26422
+ );
26423
+ }
26424
+ let cursorX = lineStart;
26425
+ for (let index = 0; index < line.fragments.length; index++) {
26426
+ const fragment = line.fragments[index];
26427
+ if (fragment.kind === "space" && justifyExtra > 0) {
26428
+ cursorX += justifyExtra;
26429
+ }
26430
+ drawLineFragment(page, fragment, cursorX, top, line.height, context);
26431
+ cursorX += fragment.width;
26432
+ }
26433
+ }
26434
+ function renderSeparator(page, context, top, x, width) {
26435
+ const lineY = toPdfY(context.pageHeight, top + SEPARATOR_HEIGHT / 2);
26436
+ page.drawLine({
26437
+ start: { x, y: lineY },
26438
+ end: { x: x + width, y: lineY },
26439
+ thickness: 1,
26440
+ color: (0, import_pdf_lib.rgb)(0.6, 0.6, 0.6)
26441
+ });
26442
+ }
26443
+ function renderImageBlock(page, block, context, top, baseX = context.margins[3], width = context.contentWidth) {
26444
+ const alignment = normalizeAlignment(block.alignment);
26445
+ let x = baseX;
26446
+ if (alignment === "center" /* CENTER */) {
26447
+ x += Math.max(0, (width - block.width) / 2);
26448
+ } else if (alignment === "right" /* RIGHT */) {
26449
+ x += Math.max(0, width - block.width);
26450
+ }
26451
+ page.drawImage(block.image, {
26452
+ x,
26453
+ y: toPdfY(context.pageHeight, top, block.height),
26454
+ width: block.width,
26455
+ height: block.height
26456
+ });
26457
+ }
26458
+ function renderTableCell(page, cell, context, table, x, top, width, height) {
26459
+ if (cell.backgroundColor) {
26460
+ page.drawRectangle({
26461
+ x,
26462
+ y: toPdfY(context.pageHeight, top, height),
26463
+ width,
26464
+ height,
26465
+ color: toPdfColor(cell.backgroundColor)
26466
+ });
26467
+ }
26468
+ drawTableCellBorders(page, cell, context, table, x, top, width, height);
26469
+ const padding = getPadding(cell.padding || defaultTableOption.tdPadding).map(
26470
+ pxToPt
26471
+ );
26472
+ const contentWidth = Math.max(1, width - padding[1] - padding[3]);
26473
+ const contentHeight = measureBlocksHeight(cell.blocks, contentWidth, context);
26474
+ let contentTop = top + padding[0];
26475
+ if (cell.verticalAlign === VerticalAlign.CENTER) {
26476
+ contentTop = top + (height - contentHeight) / 2;
26477
+ } else if (cell.verticalAlign === "bottom" /* BOTTOM */) {
26478
+ contentTop = top + height - contentHeight - padding[2];
26479
+ }
26480
+ renderBlocksInRegion(
26481
+ page,
26482
+ cell.blocks,
26483
+ context,
26484
+ x + padding[3],
26485
+ contentTop,
26486
+ contentWidth
26487
+ );
26488
+ }
26489
+ function renderPageNumber(page, context, pageNumber, pageIndex, totalPages) {
26490
+ if (pageNumber.disabled || pageIndex < pageNumber.fromPageNo || Number.isInteger(pageNumber.maxPageNo) && pageIndex >= pageNumber.maxPageNo) {
26491
+ return;
26492
+ }
26493
+ let text = pageNumber.format || FORMAT_PLACEHOLDER.PAGE_NO;
26494
+ const currentValue = pageIndex + pageNumber.startPageNo - pageNumber.fromPageNo;
26495
+ const totalValue = totalPages - pageNumber.fromPageNo;
26496
+ text = text.replace(
26497
+ new RegExp(FORMAT_PLACEHOLDER.PAGE_NO, "g"),
26498
+ `${currentValue}`
26499
+ );
26500
+ text = text.replace(
26501
+ new RegExp(FORMAT_PLACEHOLDER.PAGE_COUNT, "g"),
26502
+ `${Math.max(0, totalValue)}`
26503
+ );
26504
+ const font = getFont(
26505
+ pageNumber.font || context.options.defaultFont,
26506
+ false,
26507
+ false,
26508
+ context
26509
+ );
26510
+ const size = pageNumber.size || 12;
26511
+ const bottom = pxToPt(pageNumber.bottom || 0);
26512
+ const safeText = sanitizeTextForPdf(text, font);
26513
+ const width = font.widthOfTextAtSize(safeText, size);
26514
+ const descenderHeight = Math.max(
26515
+ 0,
26516
+ font.heightAtSize(size) - font.heightAtSize(size, { descender: false })
26517
+ );
26518
+ let x = context.margins[3];
26519
+ const alignment = normalizeAlignment(pageNumber.rowFlex);
26520
+ if (alignment === "center" /* CENTER */) {
26521
+ x = (context.pageWidth - width) / 2;
26522
+ } else if (alignment === "right" /* RIGHT */) {
26523
+ x = context.pageWidth - context.margins[1] - width;
26524
+ }
26525
+ page.drawText(safeText, {
26526
+ x,
26527
+ // Canvas draw uses the alphabetic baseline at `pageHeight - bottom`.
26528
+ // pdf-lib positions text from the text box bottom, so compensate by the
26529
+ // descender height to keep the baseline aligned.
26530
+ y: bottom - descenderHeight,
26531
+ size,
26532
+ font,
26533
+ color: toPdfColor(pageNumber.color, "#000000")
26534
+ });
26535
+ }
26536
+ function layoutParagraph(block, width, context) {
26537
+ const markerWidth = getMarkerWidth(block, context);
26538
+ const listIndent = block.context.listMarker ? pxToPt(block.context.listMarker.level * INDENT_PER_LEVEL) : 0;
26539
+ const availableWidth = Math.max(1, width - listIndent - markerWidth);
26540
+ const tokens = tokenizeParagraphRuns(block.runs, context);
26541
+ const lines = [];
26542
+ let line = {
26543
+ fragments: [],
26544
+ width: 0,
26545
+ height: getEmptyLineHeight(block, context)
26546
+ };
26547
+ const commitLine = () => {
26548
+ trimTrailingSpaces(line);
26549
+ lines.push(line);
26550
+ line = {
26551
+ fragments: [],
26552
+ width: 0,
26553
+ height: getEmptyLineHeight(block, context)
26554
+ };
26555
+ };
26556
+ for (let i = 0; i < tokens.length; i++) {
26557
+ let token = tokens[i];
26558
+ if (token.kind === "space" && !line.fragments.length) {
26559
+ continue;
26560
+ }
26561
+ if (token.width > availableWidth && token.kind === "text" && token.run.type === "text") {
26562
+ const segments = splitTextToken(token, availableWidth, context);
26563
+ for (let segmentIndex = 0; segmentIndex < segments.length; segmentIndex++) {
26564
+ tokens.splice(i + segmentIndex, 0, segments[segmentIndex]);
26565
+ }
26566
+ continue;
26567
+ }
26568
+ if (line.width + token.width > availableWidth && line.fragments.length) {
26569
+ commitLine();
26570
+ i--;
26571
+ continue;
26572
+ }
26573
+ line.fragments.push(token);
26574
+ line.width += token.width;
26575
+ line.height = Math.max(line.height, token.height);
26576
+ }
26577
+ if (line.fragments.length || !lines.length) {
26578
+ commitLine();
26579
+ }
26580
+ return {
26581
+ lines,
26582
+ height: lines.reduce((sum, current) => sum + current.height, 0),
26583
+ markerWidth,
26584
+ listIndent
26585
+ };
26586
+ }
26587
+ function tokenizeParagraphRuns(runs, context) {
26588
+ const fragments = [];
26589
+ for (let index = 0; index < runs.length; index++) {
26590
+ const run = runs[index];
26591
+ if (run.type === "tab") {
26592
+ fragments.push({
26593
+ kind: "tab",
26594
+ width: run.width,
26595
+ height: 0,
26596
+ run
26597
+ });
26598
+ continue;
26599
+ }
26600
+ if (run.type === "image") {
26601
+ fragments.push({
26602
+ kind: "image",
26603
+ width: run.width,
26604
+ height: run.height,
26605
+ run
26606
+ });
26607
+ continue;
26608
+ }
26609
+ if (run.type === "badge") {
26610
+ const badgeWidth = measureBadgeWidth(run, context);
26611
+ const badgeHeight = measureBadgeHeight(run);
26612
+ fragments.push({
26613
+ kind: "badge",
26614
+ width: badgeWidth,
26615
+ height: badgeHeight,
26616
+ run
26617
+ });
26618
+ continue;
26619
+ }
26620
+ const parts = run.text.split(/(\s+)/);
26621
+ for (let partIndex = 0; partIndex < parts.length; partIndex++) {
26622
+ const part = parts[partIndex];
26623
+ if (!part) continue;
26624
+ const isWhitespace = /^\s+$/.test(part);
26625
+ fragments.push({
26626
+ kind: isWhitespace ? "space" : "text",
26627
+ width: measureTextWidth(part, run, context),
26628
+ height: getRunHeight(run),
26629
+ run: { ...run, text: part }
26630
+ });
26631
+ }
26632
+ }
26633
+ return fragments;
26634
+ }
26635
+ function splitTextToken(token, availableWidth, context) {
26636
+ if (token.run.type !== "text") {
26637
+ return [token];
26638
+ }
26639
+ const chars = splitText(token.run.text);
26640
+ const result = [];
26641
+ let current = "";
26642
+ for (let i = 0; i < chars.length; i++) {
26643
+ const next = current + chars[i];
26644
+ const width = measureTextWidth(next, token.run, context);
26645
+ if (width > availableWidth && current) {
26646
+ result.push({
26647
+ kind: "text",
26648
+ width: measureTextWidth(current, token.run, context),
26649
+ height: getRunHeight(token.run),
26650
+ run: { ...token.run, text: current }
26651
+ });
26652
+ current = chars[i];
26653
+ } else {
26654
+ current = next;
26655
+ }
26656
+ }
26657
+ if (current) {
26658
+ result.push({
26659
+ kind: /^\s+$/.test(current) ? "space" : "text",
26660
+ width: measureTextWidth(current, token.run, context),
26661
+ height: getRunHeight(token.run),
26662
+ run: { ...token.run, text: current }
26663
+ });
26664
+ }
26665
+ return result.length ? result : [token];
26666
+ }
26667
+ function trimTrailingSpaces(line) {
26668
+ while (line.fragments.length && line.fragments[line.fragments.length - 1].kind === "space") {
26669
+ const fragment = line.fragments.pop();
26670
+ line.width -= fragment.width;
26671
+ }
26672
+ }
26673
+ function layoutTable(table, width, context) {
26674
+ const rowHeights = table.rows.map(
26675
+ (row) => Math.max(
26676
+ pxToPt(row.height),
26677
+ pxToPt(context.options.table.defaultTrMinHeight)
26678
+ )
26679
+ );
26680
+ const scaledColWidths = scaleColumnWidths(table.colWidths, width);
26681
+ for (let rowIndex = 0; rowIndex < table.rows.length; rowIndex++) {
26682
+ const row = table.rows[rowIndex];
26683
+ for (let cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
26684
+ const cell = row.cells[cellIndex];
26685
+ const cellWidth = getColumnSpanWidth(
26686
+ scaledColWidths,
26687
+ cell.colIndex,
26688
+ cell.colspan
26689
+ );
26690
+ const padding = getPadding(
26691
+ cell.padding || defaultTableOption.tdPadding
26692
+ ).map(pxToPt);
26693
+ const contentWidth = Math.max(1, cellWidth - padding[1] - padding[3]);
26694
+ const contentHeight = measureBlocksHeight(cell.blocks, contentWidth, context) + padding[0] + padding[2];
26695
+ const spanHeight = getRowSpanHeight(rowHeights, rowIndex, cell.rowspan);
26696
+ if (contentHeight > spanHeight) {
26697
+ rowHeights[rowIndex + cell.rowspan - 1] += contentHeight - spanHeight;
26698
+ }
26699
+ }
26700
+ }
26701
+ return {
26702
+ rowHeights,
26703
+ totalHeight: rowHeights.reduce((sum, height) => sum + height, 0)
26704
+ };
26705
+ }
26706
+ function measureBlocksHeight(blocks, width, context) {
26707
+ let height = 0;
26708
+ for (let i = 0; i < blocks.length; i++) {
26709
+ const block = blocks[i];
26710
+ if (block.type === "pageBreak") {
26711
+ continue;
26712
+ }
26713
+ if (block.type === "paragraph") {
26714
+ height += layoutParagraph(block, width, context).height;
26715
+ continue;
26716
+ }
26717
+ if (block.type === "separator") {
26718
+ height += context.separatorGap;
26719
+ continue;
26720
+ }
26721
+ if (block.type === "image") {
26722
+ height += block.height;
26723
+ continue;
26724
+ }
26725
+ height += layoutTable(block, width, context).totalHeight;
26726
+ }
26727
+ return height;
26728
+ }
26729
+ function drawLineFragment(page, fragment, x, top, lineHeight, context) {
26730
+ if (fragment.run.type === "tab") {
26731
+ return;
26732
+ }
26733
+ if (fragment.run.type === "image") {
26734
+ const imageTop = top + (lineHeight - fragment.run.height) / 2;
26735
+ page.drawImage(fragment.run.image, {
26736
+ x,
26737
+ y: toPdfY(context.pageHeight, imageTop, fragment.run.height),
26738
+ width: fragment.run.width,
26739
+ height: fragment.run.height
26740
+ });
26741
+ return;
26742
+ }
26743
+ if (fragment.run.type === "badge") {
26744
+ const badgeHeight = measureBadgeHeight(fragment.run);
26745
+ const badgeTop = top + (lineHeight - badgeHeight) / 2;
26746
+ const padding = getPadding(fragment.run.padding || [2, 4, 2, 4]).map(
26747
+ pxToPt
26748
+ );
26749
+ page.drawRectangle({
26750
+ x,
26751
+ y: toPdfY(context.pageHeight, badgeTop, badgeHeight),
26752
+ width: fragment.width,
26753
+ height: badgeHeight,
26754
+ color: toPdfColor(fragment.run.backgroundColor, "#FFFFFF"),
26755
+ borderColor: fragment.run.borderColor ? toPdfColor(fragment.run.borderColor) : void 0,
26756
+ borderWidth: fragment.run.borderColor ? 0.75 : void 0
26757
+ });
26758
+ drawTextRun(
26759
+ page,
26760
+ fragment.run,
26761
+ fragment.run.text,
26762
+ x + padding[3],
26763
+ badgeTop + padding[0],
26764
+ badgeHeight - padding[0] - padding[2],
26765
+ context
26766
+ );
26767
+ return;
26768
+ }
26769
+ if (fragment.run.type !== "text") {
26770
+ return;
26771
+ }
26772
+ drawTextRun(
26773
+ page,
26774
+ fragment.run,
26775
+ fragment.run.text,
26776
+ x,
26777
+ top,
26778
+ lineHeight,
26779
+ context
26780
+ );
26781
+ }
26782
+ function drawTextRun(page, run, text, x, top, lineHeight, context) {
26783
+ const resolved = resolveTextMetrics(run, context);
26784
+ const safeText = sanitizeTextForPdf(text, resolved.font);
26785
+ let textTop = top + (lineHeight - resolved.size) / 2;
26786
+ if (run.type === "text" && run.superscript) {
26787
+ textTop -= resolved.size * 0.25;
26788
+ }
26789
+ if (run.type === "text" && run.subscript) {
26790
+ textTop += resolved.size * 0.2;
26791
+ }
26792
+ const textY = toPdfY(context.pageHeight, textTop, resolved.size);
26793
+ if ("highlight" in run && run.highlight) {
26794
+ page.drawRectangle({
26795
+ x,
26796
+ y: textY,
26797
+ width: resolved.font.widthOfTextAtSize(safeText, resolved.size),
26798
+ height: resolved.size * 1.05,
26799
+ color: toPdfColor(run.highlight)
26800
+ });
26801
+ }
26802
+ page.drawText(safeText, {
26803
+ x,
26804
+ y: textY,
26805
+ size: resolved.size,
26806
+ font: resolved.font,
26807
+ color: toPdfColor(run.color, "#000000")
26808
+ });
26809
+ const textWidth = resolved.font.widthOfTextAtSize(safeText, resolved.size);
26810
+ if (run.underline) {
26811
+ const underlineY = textY - 1;
26812
+ page.drawLine({
26813
+ start: { x, y: underlineY },
26814
+ end: { x: x + textWidth, y: underlineY },
26815
+ thickness: 0.75,
26816
+ color: toPdfColor(run.color, "#000000")
26817
+ });
26818
+ }
26819
+ if (run.strikeout) {
26820
+ const strikeY = textY + resolved.size * 0.35;
26821
+ page.drawLine({
26822
+ start: { x, y: strikeY },
26823
+ end: { x: x + textWidth, y: strikeY },
26824
+ thickness: 0.75,
26825
+ color: toPdfColor(run.color, "#000000")
26826
+ });
26827
+ }
26828
+ }
26829
+ function drawTableCellBorders(page, cell, context, table, x, top, width, height) {
26830
+ const tableColor = table.borderColor || context.options.table.defaultBorderColor;
26831
+ const innerWidth = table.borderWidth || 1;
26832
+ const outerWidth = table.borderExternalWidth || innerWidth;
26833
+ const color = cell.borderColor || tableColor;
26834
+ const isEmpty = table.borderType === "empty" /* EMPTY */;
26835
+ const isExternal = table.borderType === "external" /* EXTERNAL */;
26836
+ const isInternal = table.borderType === "internal" /* INTERNAL */;
26837
+ const isDash = table.borderType === "dash" /* DASH */;
26838
+ const dashArray = isDash ? [3, 3] : void 0;
26839
+ const lineColor = toPdfColor(color, "#000000");
26840
+ const bottomY = toPdfY(context.pageHeight, top, height);
26841
+ const topY = toPdfY(context.pageHeight, top);
26842
+ const rightX = x + width;
26843
+ const drawEdge = (startX, startY, endX, endY, thickness) => {
26844
+ page.drawLine({
26845
+ start: { x: startX, y: startY },
26846
+ end: { x: endX, y: endY },
26847
+ thickness,
26848
+ color: lineColor,
26849
+ dashArray
26850
+ });
26851
+ };
26852
+ if (cell.borderTypes?.length) {
26853
+ if (cell.borderTypes.includes("top" /* TOP */)) {
26854
+ drawEdge(x, topY, rightX, topY, innerWidth);
26855
+ }
26856
+ if (cell.borderTypes.includes("right" /* RIGHT */)) {
26857
+ drawEdge(rightX, topY, rightX, bottomY, innerWidth);
26858
+ }
26859
+ if (cell.borderTypes.includes("bottom" /* BOTTOM */)) {
26860
+ drawEdge(x, bottomY, rightX, bottomY, innerWidth);
26861
+ }
26862
+ if (cell.borderTypes.includes("left" /* LEFT */)) {
26863
+ drawEdge(x, topY, x, bottomY, innerWidth);
26864
+ }
26865
+ }
26866
+ if (cell.borderColor && !cell.borderTypes?.length) {
26867
+ drawEdge(x, topY, rightX, topY, innerWidth);
26868
+ drawEdge(rightX, topY, rightX, bottomY, innerWidth);
26869
+ drawEdge(x, bottomY, rightX, bottomY, innerWidth);
26870
+ drawEdge(x, topY, x, bottomY, innerWidth);
26871
+ return;
26872
+ }
26873
+ if (isEmpty) {
26874
+ return;
26875
+ }
26876
+ if (!isInternal) {
26877
+ drawEdge(x, topY, rightX, topY, outerWidth);
26878
+ drawEdge(x, topY, x, bottomY, outerWidth);
26879
+ drawEdge(rightX, topY, rightX, bottomY, outerWidth);
26880
+ drawEdge(x, bottomY, rightX, bottomY, outerWidth);
26881
+ if (isExternal) {
26882
+ return;
26883
+ }
26884
+ }
26885
+ drawEdge(rightX, topY, rightX, bottomY, innerWidth);
26886
+ drawEdge(x, bottomY, rightX, bottomY, innerWidth);
26887
+ }
26888
+ function getMarkerWidth(block, context) {
26889
+ if (!block.context.listMarker) {
26890
+ return 0;
26891
+ }
26892
+ const markerRun = getMarkerRun(block, context);
26893
+ return measureTextWidth(block.context.listMarker.text, markerRun, context) + pxToPt(LIST_GAP);
26894
+ }
26895
+ function getMarkerRun(block, context) {
26896
+ const firstTextRun = block.runs.find((run) => run.type === "text" || run.type === "badge") || null;
26897
+ if (!firstTextRun) {
26898
+ return {
26899
+ type: "text",
26900
+ text: "",
26901
+ size: context.options.defaultSize,
26902
+ font: context.options.defaultFont
26903
+ };
26904
+ }
26905
+ if (firstTextRun.type === "badge") {
26906
+ return {
26907
+ type: "text",
26908
+ text: firstTextRun.text,
26909
+ size: firstTextRun.size,
26910
+ font: firstTextRun.font,
26911
+ bold: firstTextRun.bold,
26912
+ italic: firstTextRun.italic,
26913
+ color: firstTextRun.color
26914
+ };
26915
+ }
26916
+ return firstTextRun;
26917
+ }
26918
+ function getEmptyLineHeight(block, context) {
26919
+ const markerRun = getMarkerRun(block, context);
26920
+ return getRunHeight(
26921
+ markerRun,
26922
+ block.context.rowMargin || context.options.defaultRowMargin
26923
+ );
26924
+ }
26925
+ function getRunHeight(run, rowMargin = DEFAULT_LINE_HEIGHT2) {
26926
+ return run.size * rowMargin;
26927
+ }
26928
+ function resolveTextMetrics(run, context) {
26929
+ let size = run.size || context.options.defaultSize;
26930
+ if ("superscript" in run && run.superscript) {
26931
+ size *= 0.65;
26932
+ }
26933
+ if ("subscript" in run && run.subscript) {
26934
+ size *= 0.65;
26935
+ }
26936
+ return {
26937
+ size,
26938
+ font: getFont(
26939
+ run.font || context.options.defaultFont,
26940
+ !!run.bold,
26941
+ !!run.italic,
26942
+ context
26943
+ )
26944
+ };
26945
+ }
26946
+ function measureTextWidth(text, run, context) {
26947
+ const resolved = resolveTextMetrics(run, context);
26948
+ const safeText = sanitizeTextForPdf(text, resolved.font);
26949
+ return resolved.font.widthOfTextAtSize(safeText, resolved.size);
26950
+ }
26951
+ function measureBadgeWidth(run, context) {
26952
+ const padding = getPadding(run.padding || [2, 4, 2, 4]).map(pxToPt);
26953
+ return measureTextWidth(run.text, run, context) + padding[1] + padding[3];
26954
+ }
26955
+ function measureBadgeHeight(run) {
26956
+ const padding = getPadding(run.padding || [2, 4, 2, 4]).map(pxToPt);
26957
+ return run.size + padding[0] + padding[2];
26958
+ }
26959
+ function getJustifyExtra(line, availableWidth) {
26960
+ const spaceCount = line.fragments.filter(
26961
+ (fragment) => fragment.kind === "space"
26962
+ ).length;
26963
+ if (!spaceCount || line.width >= availableWidth) {
26964
+ return 0;
26965
+ }
26966
+ return (availableWidth - line.width) / spaceCount;
26967
+ }
26968
+ function isLastParagraphLine(layout, line) {
26969
+ return layout.lines[layout.lines.length - 1] === line;
26970
+ }
26971
+ function getSpanningRowHeight(rows, rowHeights, rowIndex) {
26972
+ const row = rows[rowIndex];
26973
+ let maxHeight = rowHeights[rowIndex];
26974
+ for (let i = 0; i < row.cells.length; i++) {
26975
+ const cell = row.cells[i];
26976
+ maxHeight = Math.max(
26977
+ maxHeight,
26978
+ getRowSpanHeight(rowHeights, rowIndex, cell.rowspan)
26979
+ );
26980
+ }
26981
+ return maxHeight;
26982
+ }
26983
+ function getRowSpanHeight(rowHeights, rowIndex, rowSpan) {
26984
+ let height = 0;
26985
+ for (let i = rowIndex; i < rowIndex + rowSpan; i++) {
26986
+ height += rowHeights[i] || 0;
26987
+ }
26988
+ return height;
26989
+ }
26990
+ function scaleColumnWidths(widths, maxWidth) {
26991
+ const ptWidths = widths.map(pxToPt);
26992
+ const total = ptWidths.reduce((sum, width) => sum + width, 0);
26993
+ if (!total || total <= maxWidth) {
26994
+ return ptWidths;
26995
+ }
26996
+ const ratio = maxWidth / total;
26997
+ return ptWidths.map((width) => width * ratio);
26998
+ }
26999
+ function getColumnOffset(widths, colIndex) {
27000
+ let offset = 0;
27001
+ for (let i = 0; i < colIndex; i++) {
27002
+ offset += widths[i] || 0;
27003
+ }
27004
+ return offset;
27005
+ }
27006
+ function getColumnSpanWidth(widths, colIndex, span) {
27007
+ let width = 0;
27008
+ for (let i = colIndex; i < colIndex + span; i++) {
27009
+ width += widths[i] || 0;
27010
+ }
27011
+ return width;
27012
+ }
27013
+ function computeTableCellPlacement(trList, colgroup) {
27014
+ const placementMap = /* @__PURE__ */ new WeakMap();
27015
+ if (!trList.length || !colgroup.length) {
27016
+ return placementMap;
27017
+ }
27018
+ const occupiedUntilRow = Array(colgroup.length).fill(0);
27019
+ for (let rowIndex = 0; rowIndex < trList.length; rowIndex++) {
27020
+ const tr = trList[rowIndex];
27021
+ let colIndex = 0;
27022
+ for (let tdIndex = 0; tdIndex < tr.tdList.length; tdIndex++) {
27023
+ while (colIndex < colgroup.length && occupiedUntilRow[colIndex] > rowIndex) {
27024
+ colIndex += 1;
27025
+ }
27026
+ const td = tr.tdList[tdIndex];
27027
+ let width = 0;
27028
+ for (let currentCol = colIndex; currentCol < colIndex + td.colspan; currentCol++) {
27029
+ width += colgroup[currentCol]?.width || 0;
27030
+ occupiedUntilRow[currentCol] = Math.max(
27031
+ occupiedUntilRow[currentCol],
27032
+ rowIndex + td.rowspan
27033
+ );
27034
+ }
27035
+ placementMap.set(td, { colIndex, width });
27036
+ colIndex += td.colspan;
27037
+ }
27038
+ }
27039
+ return placementMap;
27040
+ }
27041
+ async function loadPdfImage(src, context) {
27042
+ if (context.imageCache.has(src)) {
27043
+ return context.imageCache.get(src);
27044
+ }
27045
+ try {
27046
+ const payload = await loadPdfImagePayload(src);
27047
+ const image = payload.type === "jpg" ? await context.pdfDoc.embedJpg(payload.data) : await context.pdfDoc.embedPng(payload.data);
27048
+ context.imageCache.set(src, image);
27049
+ return image;
27050
+ } catch {
27051
+ return null;
27052
+ }
27053
+ }
27054
+ async function loadPdfImagePayload(src) {
27055
+ const source = src.trim();
27056
+ if (source.startsWith("data:")) {
27057
+ return getPdfImagePayloadFromDataUrl(source);
27058
+ }
27059
+ const response = await fetch(source);
27060
+ if (!response.ok) {
27061
+ throw new Error(`Image request failed: ${response.status}`);
27062
+ }
27063
+ const contentType = response.headers.get("content-type") || "";
27064
+ const data2 = await response.arrayBuffer();
27065
+ return normalizePdfImagePayload(data2, contentType);
27066
+ }
27067
+ async function getPdfImagePayloadFromDataUrl(src) {
27068
+ const [, meta = "", encoded = ""] = src.match(/^data:([^,]*),(.*)$/) || [];
27069
+ const mime = meta.split(";")[0] || "";
27070
+ const isBase64 = meta.includes(";base64");
27071
+ const text = isBase64 ? atob(encoded) : decodeURIComponent(encoded);
27072
+ const bytes = new Uint8Array(text.length);
27073
+ for (let i = 0; i < text.length; i++) {
27074
+ bytes[i] = text.charCodeAt(i);
27075
+ }
27076
+ return normalizePdfImagePayload(bytes.buffer, mime);
27077
+ }
27078
+ async function normalizePdfImagePayload(data2, mime) {
27079
+ const lower = mime.toLowerCase();
27080
+ if (lower.includes("jpeg") || lower.includes("jpg")) {
27081
+ return {
27082
+ type: "jpg",
27083
+ data: data2
27084
+ };
27085
+ }
27086
+ if (lower.includes("png")) {
27087
+ return {
27088
+ type: "png",
27089
+ data: data2
27090
+ };
27091
+ }
27092
+ return {
27093
+ type: "png",
27094
+ data: await rasterizeImageToPng2(data2, mime)
27095
+ };
27096
+ }
27097
+ async function rasterizeImageToPng2(data2, mime) {
27098
+ const blob = new Blob([data2], {
27099
+ type: mime || "image/png"
27100
+ });
27101
+ const url = URL.createObjectURL(blob);
27102
+ try {
27103
+ const image = await loadHtmlImage2(url);
27104
+ const canvas = document.createElement("canvas");
27105
+ canvas.width = image.naturalWidth || DEFAULT_IMAGE_WIDTH2;
27106
+ canvas.height = image.naturalHeight || DEFAULT_IMAGE_HEIGHT2;
27107
+ const context = canvas.getContext("2d");
27108
+ if (!context) {
27109
+ throw new Error("Canvas context is unavailable");
27110
+ }
27111
+ context.drawImage(image, 0, 0, canvas.width, canvas.height);
27112
+ const pngBlob = await new Promise((resolve, reject) => {
27113
+ canvas.toBlob((output) => {
27114
+ if (output) {
27115
+ resolve(output);
27116
+ } else {
27117
+ reject(new Error("Failed to create PNG blob"));
27118
+ }
27119
+ }, "image/png");
27120
+ });
27121
+ return await pngBlob.arrayBuffer();
27122
+ } finally {
27123
+ URL.revokeObjectURL(url);
27124
+ }
27125
+ }
27126
+ function loadHtmlImage2(src) {
27127
+ return new Promise((resolve, reject) => {
27128
+ const image = new Image();
27129
+ image.onload = () => resolve(image);
27130
+ image.onerror = () => reject(new Error("Failed to load image"));
27131
+ image.src = src;
27132
+ });
27133
+ }
27134
+ function getInlineElementText2(element) {
27135
+ switch (element.type) {
27136
+ case "control" /* CONTROL */:
27137
+ return getControlText2(element.control);
27138
+ case "date" /* DATE */:
27139
+ return element.valueList?.length ? getTextFromElementList(element.valueList) : element.value;
27140
+ case "checkbox" /* CHECKBOX */:
27141
+ return element.checkbox?.value ? "\u2611" : "\u2610";
27142
+ case "radio" /* RADIO */:
27143
+ return element.radio?.value ? "\u25C9" : "\u25CB";
27144
+ case "label" /* LABEL */:
27145
+ case "latex" /* LATEX */:
27146
+ case "subscript" /* SUBSCRIPT */:
27147
+ case "superscript" /* SUPERSCRIPT */:
27148
+ return element.value;
27149
+ default:
27150
+ return element.value;
27151
+ }
27152
+ }
27153
+ function getControlText2(control) {
27154
+ if (!control) return "";
27155
+ if (control.type === "text" /* TEXT */ || control.type === "date" /* DATE */ || control.type === "number" /* NUMBER */) {
27156
+ const value = control.value?.length ? getTextFromElementList(control.value) : "";
27157
+ const content = value || control.placeholder || "";
27158
+ return `${control.preText || ""}${content}${control.postText || ""}`;
27159
+ }
27160
+ if (control.type === "select" /* SELECT */) {
27161
+ const labels = getSelectedControlLabels2(control.code, control.valueSets);
27162
+ const content = labels.length ? labels.join(control.multiSelectDelimiter || ", ") : control.placeholder || "";
27163
+ return `${control.preText || ""}${content}${control.postText || ""}`;
27164
+ }
27165
+ if (control.type === "checkbox" /* CHECKBOX */ || control.type === "radio" /* RADIO */) {
27166
+ const isRadio = control.type === "radio" /* RADIO */;
27167
+ return (control.valueSets || []).map((valueSet) => {
27168
+ const checked = control.code?.split(",").includes(valueSet.code) || false;
27169
+ return `${checked ? isRadio ? "\u25C9" : "\u2611" : isRadio ? "\u25CB" : "\u2610"} ${valueSet.value}`;
27170
+ }).join(" ");
27171
+ }
27172
+ return "";
27173
+ }
27174
+ function getSelectedControlLabels2(code, valueSets) {
27175
+ if (!code || !valueSets?.length) return [];
27176
+ return code.split(",").map(
27177
+ (selectCode) => valueSets.find((valueSet) => valueSet.code === selectCode)?.value
27178
+ ).filter((value) => !!value);
27179
+ }
27180
+ function getBlockFallbackText2(element) {
27181
+ if (element.block?.iframeBlock?.src) {
27182
+ return element.block.iframeBlock.src;
27183
+ }
27184
+ if (element.block?.videoBlock?.src) {
27185
+ return element.block.videoBlock.src;
27186
+ }
27187
+ return "";
27188
+ }
27189
+ function getListItemNumberingElement2(listItemElements, fallback) {
27190
+ return listItemElements.find(
27191
+ (listItem) => listItem.listId || listItem.listLevel !== void 0 || !!listItem.listPreset || !!listItem.listStyle || !!listItem.listType
27192
+ ) || fallback;
27193
+ }
27194
+ function getListMarker(element, orderedCounts) {
27195
+ const level = Math.max(0, Math.min(element.listLevel || 0, 8));
27196
+ if (element.listType === "ul" /* UL */) {
27197
+ return {
27198
+ text: getUnorderedMarkerText(element, level),
27199
+ level
27200
+ };
27201
+ }
27202
+ while (orderedCounts.length <= level) {
27203
+ orderedCounts.push(0);
27204
+ }
27205
+ orderedCounts[level] += 1;
27206
+ orderedCounts.length = level + 1;
27207
+ const style = element.listPreset ? getOrderedPresetStyle(element.listPreset, level) : getOrderedStyle(element.listStyle);
27208
+ const outlinePrefix = style === "outline" ? orderedCounts.slice(0, level + 1).join(".") : void 0;
27209
+ return {
27210
+ text: formatOlNumber2(orderedCounts[level] - 1, style, outlinePrefix),
27211
+ level
27212
+ };
27213
+ }
27214
+ function getOrderedPresetStyle(preset, level = 0) {
27215
+ const cycle = preset ? olPresetCycles[preset] : void 0;
27216
+ if (!cycle?.length) {
27217
+ return "decimal";
27218
+ }
27219
+ return cycle[level % cycle.length];
27220
+ }
27221
+ function getOrderedStyle(style) {
27222
+ switch (style) {
27223
+ case "lowerAlpha" /* LOWER_ALPHA */:
27224
+ return "lowerAlpha";
27225
+ case "upperAlpha" /* UPPER_ALPHA */:
27226
+ return "upperAlpha";
27227
+ case "lowerRoman" /* LOWER_ROMAN */:
27228
+ return "lowerRoman";
27229
+ case "upperRoman" /* UPPER_ROMAN */:
27230
+ return "upperRoman";
27231
+ case "decimalParen" /* DECIMAL_PAREN */:
27232
+ return "decimalParen";
27233
+ default:
27234
+ return "decimal";
27235
+ }
27236
+ }
27237
+ function getUnorderedMarkerText(element, level) {
27238
+ if (element.listPreset) {
27239
+ const cycle = ulPresetCycles[element.listPreset];
27240
+ if (cycle?.length) {
27241
+ return cycle[level % cycle.length];
27242
+ }
27243
+ }
27244
+ return bulletCharMap[element.listStyle || "disc" /* DISC */] || bulletCharMap["disc" /* DISC */];
27245
+ }
27246
+ function toAlpha2(n, upper) {
27247
+ let result = "";
27248
+ let num = n + 1;
27249
+ while (num > 0) {
27250
+ num--;
27251
+ result = String.fromCharCode((upper ? 65 : 97) + num % 26) + result;
27252
+ num = Math.floor(num / 26);
27253
+ }
27254
+ return result;
27255
+ }
27256
+ function toRoman2(n, upper) {
27257
+ const num = n + 1;
27258
+ const vals = [1e3, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
27259
+ const syms = [
27260
+ "M",
27261
+ "CM",
27262
+ "D",
27263
+ "CD",
27264
+ "C",
27265
+ "XC",
27266
+ "L",
27267
+ "XL",
27268
+ "X",
27269
+ "IX",
27270
+ "V",
27271
+ "IV",
27272
+ "I"
27273
+ ];
27274
+ let result = "";
27275
+ let remaining = num;
27276
+ for (let i = 0; i < vals.length; i++) {
27277
+ while (remaining >= vals[i]) {
27278
+ result += syms[i];
27279
+ remaining -= vals[i];
27280
+ }
27281
+ }
27282
+ return upper ? result : result.toLowerCase();
27283
+ }
27284
+ function formatOlNumber2(index, style, outlinePrefix) {
27285
+ switch (style) {
27286
+ case "decimal":
27287
+ return `${index + 1}.`;
27288
+ case "decimalParen":
27289
+ return `${index + 1})`;
27290
+ case "decimalZero":
27291
+ return `${String(index + 1).padStart(2, "0")}.`;
27292
+ case "lowerAlpha":
27293
+ return `${toAlpha2(index, false)}.`;
27294
+ case "lowerAlphaParen":
27295
+ return `${toAlpha2(index, false)})`;
27296
+ case "upperAlpha":
27297
+ return `${toAlpha2(index, true)}.`;
27298
+ case "lowerRoman":
27299
+ return `${toRoman2(index, false)}.`;
27300
+ case "lowerRomanParen":
27301
+ return `${toRoman2(index, false)})`;
27302
+ case "upperRoman":
27303
+ return `${toRoman2(index, true)}.`;
27304
+ case "outline":
27305
+ return outlinePrefix ? `${outlinePrefix}.` : `${index + 1}.`;
27306
+ default:
27307
+ return `${index + 1}.`;
27308
+ }
27309
+ }
27310
+ function normalizeAlignment(rowFlex) {
27311
+ if (rowFlex === "alignment" /* ALIGNMENT */) {
27312
+ return "justify" /* JUSTIFY */;
27313
+ }
27314
+ return rowFlex || "left" /* LEFT */;
27315
+ }
27316
+ function getFont(family, bold, italic, context) {
27317
+ const fontSet = context.fonts[resolveFontFamily(family)];
27318
+ if (bold && italic) {
27319
+ return fontSet.boldItalic;
27320
+ }
27321
+ if (bold) {
27322
+ return fontSet.bold;
27323
+ }
27324
+ if (italic) {
27325
+ return fontSet.italic;
27326
+ }
27327
+ return fontSet.regular;
27328
+ }
27329
+ function resolveFontFamily(font) {
27330
+ const normalized = (font || "").trim().toLowerCase();
27331
+ if (normalized.includes("sans-serif") || normalized.includes("arial") || normalized.includes("helvetica") || normalized.includes("yahei") || normalized.includes("microsoft") || normalized.includes("inter") || normalized.includes("roboto")) {
27332
+ return "sans";
27333
+ }
27334
+ if (normalized.includes("times") || normalized === "serif" || normalized.endsWith(", serif")) {
27335
+ return "serif";
27336
+ }
27337
+ if (normalized.includes("courier") || normalized.includes("mono") || normalized.includes("code")) {
27338
+ return "mono";
27339
+ }
27340
+ return "sans";
27341
+ }
27342
+ function sanitizeTextForPdf(text, font) {
27343
+ let fontCache = pdfTextCache.get(font);
27344
+ if (!fontCache) {
27345
+ fontCache = /* @__PURE__ */ new Map();
27346
+ pdfTextCache.set(font, fontCache);
27347
+ }
27348
+ if (fontCache.has(text)) {
27349
+ return fontCache.get(text);
27350
+ }
27351
+ let safeText = text;
27352
+ try {
27353
+ font.encodeText(text);
27354
+ } catch {
27355
+ safeText = Array.from(text).map((char) => sanitizePdfChar(char, font)).join("");
27356
+ }
27357
+ fontCache.set(text, safeText);
27358
+ return safeText;
27359
+ }
27360
+ function sanitizePdfChar(char, font) {
27361
+ try {
27362
+ font.encodeText(char);
27363
+ return char;
27364
+ } catch {
27365
+ const directFallback = PDF_TEXT_FALLBACKS[char];
27366
+ if (directFallback) {
27367
+ return directFallback;
27368
+ }
27369
+ if (/^\s$/u.test(char)) {
27370
+ return " ";
27371
+ }
27372
+ const normalized = char.normalize("NFKD").replace(/[\u0300-\u036f]/g, "").replace(/[^\x20-\x7E]/g, "");
27373
+ if (normalized) {
27374
+ try {
27375
+ font.encodeText(normalized);
27376
+ return normalized;
27377
+ } catch {
27378
+ return "?";
27379
+ }
27380
+ }
27381
+ return "?";
27382
+ }
27383
+ }
27384
+ function getPadding(padding) {
27385
+ return [padding[0] ?? 0, padding[1] ?? 0, padding[2] ?? 0, padding[3] ?? 0];
27386
+ }
27387
+ function getTitleSize(titleLevel, options) {
27388
+ if (!titleLevel) {
27389
+ return void 0;
27390
+ }
27391
+ return options.title[titleSizeMapping[titleLevel]];
27392
+ }
27393
+ function getPageNumberHeight(pageNumber) {
27394
+ if (pageNumber.disabled) {
27395
+ return 0;
27396
+ }
27397
+ return pageNumber.size || 12;
27398
+ }
27399
+ function getFooterBarHeight(footerHeight, context) {
27400
+ if (context.options.footer.disabled) {
27401
+ return 0;
27402
+ }
27403
+ return Math.max(footerHeight, pxToPt(FOOTER_MIN_BAR_HEIGHT));
27404
+ }
27405
+ function getReservedPageNumberBottom(pageNumber, pageNumberHeight) {
27406
+ if (pageNumber.disabled) {
27407
+ return 0;
27408
+ }
27409
+ return pxToPt(pageNumber.bottom + 4) + pageNumberHeight;
27410
+ }
27411
+ function normalizeText2(text = "") {
27412
+ return text.replace(new RegExp(ZERO, "g"), "").replace(/\r\n/g, "\n");
27413
+ }
27414
+ function normalizePdfFileName(fileName) {
27415
+ const trimmed = fileName?.trim();
27416
+ if (!trimmed) {
27417
+ return DEFAULT_PDF_FILE_NAME;
27418
+ }
27419
+ return trimmed.toLowerCase().endsWith(".pdf") ? trimmed : `${trimmed}.pdf`;
27420
+ }
27421
+ function pxToPt(px) {
27422
+ return px * (72 / 96);
27423
+ }
27424
+ function toPdfY(pageHeight, top, height = 0) {
27425
+ return pageHeight - top - height;
27426
+ }
27427
+ function toPdfColor(color, fallback = "#000000") {
27428
+ const hex = normalizeHexColor2(color || fallback, "000000") || "000000";
27429
+ const red = parseInt(hex.slice(0, 2), 16) / 255;
27430
+ const green = parseInt(hex.slice(2, 4), 16) / 255;
27431
+ const blue = parseInt(hex.slice(4, 6), 16) / 255;
27432
+ return (0, import_pdf_lib.rgb)(red, green, blue);
27433
+ }
27434
+ function normalizeHexColor2(color, fallback) {
27435
+ const normalized = normalizeHexLiteral2(color) || normalizeRgbColor2(color) || normalizeBrowserColor2(color);
27436
+ if (normalized) {
27437
+ return normalized;
27438
+ }
27439
+ return fallback ? normalizeHexLiteral2(fallback) || void 0 : void 0;
27440
+ }
27441
+ function normalizeHexLiteral2(color) {
27442
+ const value = color.trim().replace(/^#/, "");
27443
+ if (/^[\da-f]{3}$/i.test(value)) {
27444
+ return value.split("").map((part) => `${part}${part}`).join("").toUpperCase();
27445
+ }
27446
+ if (/^[\da-f]{4}$/i.test(value)) {
27447
+ return value.slice(0, 3).split("").map((part) => `${part}${part}`).join("").toUpperCase();
27448
+ }
27449
+ if (/^[\da-f]{6}$/i.test(value)) {
27450
+ return value.toUpperCase();
27451
+ }
27452
+ if (/^[\da-f]{8}$/i.test(value)) {
27453
+ return value.slice(0, 6).toUpperCase();
27454
+ }
27455
+ return void 0;
27456
+ }
27457
+ function normalizeRgbColor2(color) {
27458
+ const match = color.trim().match(/^rgba?\((.+)\)$/i);
27459
+ if (!match) {
27460
+ return void 0;
27461
+ }
27462
+ const parts = match[1].replace(/\s*\/\s*/g, ",").split(/\s*,\s*|\s+/).filter(Boolean);
27463
+ if (parts.length < 3) {
27464
+ return void 0;
27465
+ }
27466
+ const red = parseRgbChannel2(parts[0]);
27467
+ const green = parseRgbChannel2(parts[1]);
27468
+ const blue = parseRgbChannel2(parts[2]);
27469
+ if (red === null || green === null || blue === null) {
27470
+ return void 0;
27471
+ }
27472
+ const alpha = parts[3] !== void 0 ? parseAlphaChannel2(parts[3]) : 1;
27473
+ if (alpha === null || alpha <= 0) {
27474
+ return void 0;
27475
+ }
27476
+ const flattened = alpha >= 1 ? [red, green, blue] : [
27477
+ flattenChannel2(red, alpha),
27478
+ flattenChannel2(green, alpha),
27479
+ flattenChannel2(blue, alpha)
27480
+ ];
27481
+ return flattened.map(toHexChannel2).join("");
27482
+ }
27483
+ function normalizeBrowserColor2(color) {
27484
+ if (typeof document === "undefined") {
27485
+ return void 0;
27486
+ }
27487
+ const canvas = document.createElement("canvas");
27488
+ const context = canvas.getContext("2d");
27489
+ if (!context) {
27490
+ return void 0;
27491
+ }
27492
+ context.fillStyle = "#000000";
27493
+ context.fillStyle = color;
27494
+ const normalized = context.fillStyle;
27495
+ if (typeof normalized !== "string") {
27496
+ return void 0;
27497
+ }
27498
+ return normalizeHexLiteral2(normalized) || normalizeRgbColor2(normalized);
27499
+ }
27500
+ function parseRgbChannel2(value) {
27501
+ if (value.endsWith("%")) {
27502
+ const percent = Number(value.slice(0, -1));
27503
+ if (!Number.isFinite(percent)) {
27504
+ return null;
27505
+ }
27506
+ return clampColorChannel2(Math.round(percent / 100 * 255));
27507
+ }
27508
+ const channel = Number(value);
27509
+ if (!Number.isFinite(channel)) {
27510
+ return null;
27511
+ }
27512
+ return clampColorChannel2(Math.round(channel));
27513
+ }
27514
+ function parseAlphaChannel2(value) {
27515
+ if (value.endsWith("%")) {
27516
+ const percent = Number(value.slice(0, -1));
27517
+ if (!Number.isFinite(percent)) {
27518
+ return null;
27519
+ }
27520
+ return Math.min(Math.max(percent / 100, 0), 1);
27521
+ }
27522
+ const alpha = Number(value);
27523
+ if (!Number.isFinite(alpha)) {
27524
+ return null;
27525
+ }
27526
+ return Math.min(Math.max(alpha, 0), 1);
27527
+ }
27528
+ function clampColorChannel2(value) {
27529
+ return Math.min(Math.max(value, 0), 255);
27530
+ }
27531
+ function flattenChannel2(value, alpha) {
27532
+ return clampColorChannel2(Math.round(255 * (1 - alpha) + value * alpha));
27533
+ }
27534
+ function toHexChannel2(value) {
27535
+ return value.toString(16).padStart(2, "0").toUpperCase();
27536
+ }
27537
+
27538
+ // src/utils/print.ts
27539
+ function convertPxToPaperSize(width, height) {
27540
+ if (width === 1125 && height === 1593) {
27541
+ return {
27542
+ size: "a3",
27543
+ width: "297mm",
27544
+ height: "420mm"
27545
+ };
27546
+ }
27547
+ if (width === 794 && height === 1123) {
27548
+ return {
27549
+ size: "a4",
27550
+ width: "210mm",
27551
+ height: "297mm"
27552
+ };
27553
+ }
27554
+ if (width === 565 && height === 796) {
27555
+ return {
27556
+ size: "a5",
27557
+ width: "148mm",
27558
+ height: "210mm"
27559
+ };
27560
+ }
27561
+ return {
27562
+ size: "",
27563
+ width: `${width}px`,
27564
+ height: `${height}px`
27565
+ };
27566
+ }
27567
+ function printImageBase64(base64List, options) {
27568
+ const { width, height, direction = "vertical" /* VERTICAL */ } = options;
27569
+ const iframe = document.createElement("iframe");
27570
+ iframe.style.visibility = "hidden";
27571
+ iframe.style.position = "absolute";
27572
+ iframe.style.left = "0";
27573
+ iframe.style.top = "0";
27574
+ iframe.style.width = "0";
27575
+ iframe.style.height = "0";
27576
+ iframe.style.border = "none";
27577
+ document.body.append(iframe);
27578
+ const contentWindow = iframe.contentWindow;
27579
+ const doc = contentWindow.document;
27580
+ doc.open();
27581
+ const container = document.createElement("div");
27582
+ const paperSize = convertPxToPaperSize(width, height);
27583
+ base64List.forEach((base64) => {
27584
+ const image = document.createElement("img");
27585
+ image.style.width = direction === "horizontal" /* HORIZONTAL */ ? paperSize.height : paperSize.width;
27586
+ image.style.height = direction === "horizontal" /* HORIZONTAL */ ? paperSize.width : paperSize.height;
27587
+ image.src = base64;
27588
+ container.append(image);
27589
+ });
27590
+ const style = document.createElement("style");
27591
+ const stylesheet = `
27592
+ * {
27593
+ margin: 0;
27594
+ padding: 0;
27595
+ }
27596
+ @page {
27597
+ margin: 0;
27598
+ size: ${paperSize.size} ${direction === "horizontal" /* HORIZONTAL */ ? `landscape` : `portrait`};
27599
+ }`;
27600
+ style.append(document.createTextNode(stylesheet));
27601
+ setTimeout(() => {
27602
+ doc.write(`${style.outerHTML}${container.innerHTML}`);
27603
+ contentWindow.print();
27604
+ doc.close();
27605
+ window.addEventListener(
27606
+ "mouseover",
27607
+ () => {
27608
+ iframe?.remove();
27609
+ },
27610
+ {
27611
+ once: true
27612
+ }
27613
+ );
27614
+ });
27615
+ }
27616
+
27617
+ // src/core/command/CommandAdapt.ts
27618
+ var CommandAdapt = class {
27619
+ constructor(draw) {
27620
+ this.draw = draw;
27621
+ this.range = draw.getRange();
27622
+ this.position = draw.getPosition();
27623
+ this.historyManager = draw.getHistoryManager();
27624
+ this.canvasEvent = draw.getCanvasEvent();
27625
+ this.options = draw.getOptions();
27626
+ this.control = draw.getControl();
27627
+ this.workerManager = draw.getWorkerManager();
27628
+ this.searchManager = draw.getSearch();
27629
+ this.i18n = draw.getI18n();
27630
+ this.zone = draw.getZone();
27631
+ this.tableOperate = draw.getTableOperate();
27632
+ }
27633
+ getStyleSelectionElementList(options = {}) {
27634
+ const selection = this.range.getSelectionElementList();
27635
+ if (!selection?.length) return null;
27636
+ const { textLikeOnly = false } = options;
27637
+ const result = [];
27638
+ const collect = (elementList) => {
27639
+ for (let i = 0; i < elementList.length; i++) {
27640
+ const element = elementList[i];
27641
+ if (element.type === "table" /* TABLE */ && element.trList?.length) {
27642
+ for (let t = 0; t < element.trList.length; t++) {
27643
+ const tr = element.trList[t];
27644
+ for (let d = 0; d < tr.tdList.length; d++) {
27645
+ collect(tr.tdList[d].value);
27646
+ }
27647
+ }
27648
+ continue;
27649
+ }
27650
+ result.push(element);
27651
+ }
27652
+ };
27653
+ collect(selection);
27654
+ if (!textLikeOnly) {
27655
+ return result;
27656
+ }
27657
+ return result.filter(
27658
+ (element) => isTextLikeElement(element) || element.type === "label" /* LABEL */
27659
+ );
27660
+ }
27661
+ mode(payload) {
27662
+ this.draw.setMode(payload);
27663
+ }
27664
+ async cut() {
27665
+ const isDisabled = this.draw.isReadonly() || this.draw.isDisabled();
27666
+ if (isDisabled) return;
27667
+ await this.canvasEvent.cut();
27668
+ }
27669
+ async copy(payload) {
27670
+ await this.canvasEvent.copy(payload);
27671
+ }
27672
+ paste(payload) {
27673
+ const isDisabled = this.draw.isReadonly() || this.draw.isDisabled();
27674
+ if (isDisabled) return;
27675
+ pasteByApi(this.canvasEvent, payload);
27676
+ }
27677
+ selectAll() {
27678
+ this.canvasEvent.selectAll();
27679
+ }
27680
+ backspace() {
27681
+ const isDisabled = this.draw.isReadonly() || this.draw.isDisabled();
27682
+ if (isDisabled) return;
27683
+ const elementList = this.draw.getElementList();
27684
+ const { startIndex, endIndex } = this.range.getRange();
27685
+ const isCollapsed = startIndex === endIndex;
27686
+ if (isCollapsed && elementList[startIndex].value === ZERO && startIndex === 0) {
27687
+ return;
27688
+ }
27689
+ if (!isCollapsed) {
27690
+ this.draw.spliceElementList(
27691
+ elementList,
27692
+ startIndex + 1,
27693
+ endIndex - startIndex
27694
+ );
27695
+ } else {
27696
+ this.draw.spliceElementList(elementList, startIndex, 1);
27697
+ }
27698
+ const curIndex = isCollapsed ? startIndex - 1 : startIndex;
27699
+ this.range.setRange(curIndex, curIndex);
27700
+ this.draw.render({ curIndex });
27701
+ }
27702
+ setRange(startIndex, endIndex, tableId, startTdIndex, endTdIndex, startTrIndex, endTrIndex) {
27703
+ if (startIndex < 0 || endIndex < 0 || endIndex < startIndex) return;
27704
+ this.range.setRange(
27705
+ startIndex,
27706
+ endIndex,
24589
27707
  tableId,
24590
27708
  startTdIndex,
24591
27709
  endTdIndex,
@@ -25629,6 +28747,16 @@ var CommandAdapt = class {
25629
28747
  this.draw.setPageScale(scale);
25630
28748
  }
25631
28749
  }
28750
+ async exportDocx(payload = {}) {
28751
+ const result = this.draw.getValue();
28752
+ const { blob, fileName } = await exportEditorDataToDocx(result, payload);
28753
+ downloadBlob(blob, fileName);
28754
+ }
28755
+ async exportPdf(payload = {}) {
28756
+ const result = this.draw.getValue();
28757
+ const { blob, fileName } = await exportEditorDataToPdf(result, payload);
28758
+ downloadBlob(blob, fileName);
28759
+ }
25632
28760
  replaceImageElement(payload) {
25633
28761
  const { startIndex } = this.range.getRange();
25634
28762
  const elementList = this.draw.getElementList();