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