cmat 0.0.112 → 0.0.114

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/fesm2022/cmat.mjs CHANGED
@@ -85,9 +85,6 @@ import { CmatFilesUtilService as CmatFilesUtilService$1, CmatUploadQueueComponen
85
85
  import BlotFormatter from "@enzedonline/quill-blot-formatter2";
86
86
  import { CmatConfigService as CmatConfigService$1, CmatUrlStateConfigService as CmatUrlStateConfigService$1 } from "cmat/services/config";
87
87
  import { CmatUtilsService as CmatUtilsService$1 } from "cmat/services/utils";
88
- import { AlignmentType, BorderStyle, Document, HeadingLevel, HighlightColor, ImageRun, Packer, Paragraph, TextRun } from "docx";
89
- import * as FileSaver from "file-saver-es";
90
- import { saveAs } from "file-saver-es";
91
88
  import * as i2$1 from "ngx-quill";
92
89
  import { QuillModule } from "ngx-quill";
93
90
  import Quill from "quill";
@@ -122,6 +119,8 @@ import { MatTreeModule } from "@angular/material/tree";
122
119
  import * as i1 from "@angular/material/button-toggle";
123
120
  import { MatButtonToggleModule } from "@angular/material/button-toggle";
124
121
  import { CmatBytesPipe as CmatBytesPipe$1 } from "cmat/pipes/bytes";
122
+ import * as FileSaver from "file-saver-es";
123
+ import { saveAs } from "file-saver-es";
125
124
  import { Graph, Markup, Node, NodeView, ObjectExt } from "@antv/x6";
126
125
  import * as XLSX from "xlsx";
127
126
  import { TranslateService } from "@ngx-translate/core";
@@ -8504,299 +8503,15 @@ var CmatQuillTypeComponent = class CmatQuillTypeComponent extends FieldType$2 {
8504
8503
  this.isExporting.set(true);
8505
8504
  try {
8506
8505
  const delta = editor.getContents();
8507
- const children = await this._convertDeltaToDocxChildren(delta);
8508
- const doc = new Document({ sections: [{
8509
- properties: {},
8510
- children
8511
- }] });
8512
- const blob = await Packer.toBlob(doc);
8513
- FileSaver.saveAs(blob, `${this.placeholder ?? "文档"}_${dayjs().format("YYYYMMDD_HHmmss")}.docx`);
8506
+ const filename = `${this.placeholder ?? "文档"}_${dayjs().format("YYYYMMDD_HHmmss")}.docx`;
8507
+ const { exportDeltaToDocx } = await import("./cmat-quill-docx-exporter-D9Eo73Wq.mjs");
8508
+ await exportDeltaToDocx(delta, filename);
8514
8509
  } catch (error) {
8515
8510
  console.error("Word 导出失败:", error);
8516
8511
  } finally {
8517
8512
  this.isExporting.set(false);
8518
8513
  }
8519
8514
  }
8520
- async _convertDeltaToDocxChildren(delta) {
8521
- const children = [];
8522
- const imageCache = /* @__PURE__ */ new Map();
8523
- const imageOps = delta.ops.filter((op) => typeof op.insert !== "string" && op.insert?.image);
8524
- await Promise.all(imageOps.map(async (op) => {
8525
- if (typeof op.insert === "object" && op.insert !== null && "image" in op.insert && typeof op.insert.image === "string") {
8526
- const url = op.insert.image;
8527
- if (!imageCache.has(url)) {
8528
- const buffer = await this._getImageBuffer(url);
8529
- const dims = await this._getImageDimensions(url);
8530
- let type = "png";
8531
- if (url.toLowerCase().endsWith(".jpg") || url.toLowerCase().endsWith(".jpeg")) type = "jpg";
8532
- else if (url.toLowerCase().endsWith(".gif")) type = "gif";
8533
- else if (url.toLowerCase().endsWith(".svg")) type = "svg";
8534
- imageCache.set(url, {
8535
- buffer,
8536
- width: dims.width,
8537
- height: dims.height,
8538
- type
8539
- });
8540
- }
8541
- }
8542
- }));
8543
- let currentRun = [];
8544
- let currentListLevel = 0;
8545
- let isList = false;
8546
- for (const op of delta.ops) if (typeof op.insert !== "string" && op.insert?.image) {
8547
- if (currentRun.length > 0) {
8548
- children.push(new Paragraph({ children: currentRun }));
8549
- currentRun = [];
8550
- }
8551
- const imgData = imageCache.get(op.insert.image);
8552
- if (imgData) {
8553
- const imageRun = this._createImageRunFromData(imgData);
8554
- children.push(new Paragraph({
8555
- children: [imageRun],
8556
- alignment: AlignmentType.CENTER
8557
- }));
8558
- }
8559
- } else if (typeof op.insert === "string") {
8560
- const text = op.insert;
8561
- if (text === "\n") {
8562
- if (currentRun.length > 0) {
8563
- children.push(this._createParagraph(currentRun, op.attributes, isList, currentListLevel));
8564
- currentRun = [];
8565
- } else children.push(new Paragraph({ children: [new TextRun({ text: "" })] }));
8566
- if (!op.attributes?.list) {
8567
- isList = false;
8568
- currentListLevel = 0;
8569
- }
8570
- } else {
8571
- const textRun = this._createTextRun(text, op.attributes);
8572
- currentRun.push(textRun);
8573
- if (op.attributes?.list) {
8574
- isList = true;
8575
- currentListLevel = op.attributes.list === "bullet" ? 0 : op.attributes.list === "order" ? 0 : currentListLevel;
8576
- }
8577
- }
8578
- }
8579
- if (currentRun.length > 0) children.push(this._createParagraph(currentRun, void 0, isList, currentListLevel));
8580
- return children;
8581
- }
8582
- _createImageRunFromData(data) {
8583
- const scale = Math.min(500 / data.width, 400 / data.height, 1);
8584
- const imageType = data.type;
8585
- return new ImageRun({
8586
- data: data.buffer,
8587
- transformation: {
8588
- width: Math.round(data.width * scale),
8589
- height: Math.round(data.height * scale)
8590
- },
8591
- type: imageType,
8592
- fallback: {
8593
- type: imageType !== "svg" ? imageType : "png",
8594
- data: data.buffer
8595
- }
8596
- });
8597
- }
8598
- _createTextRun(text, attributes) {
8599
- return new TextRun({
8600
- text,
8601
- bold: attributes?.bold ?? false,
8602
- italics: attributes?.italic ?? false,
8603
- underline: attributes?.underline ? { type: BorderStyle.SINGLE } : void 0,
8604
- size: this._mapFontSize(attributes?.size),
8605
- font: attributes?.font ?? void 0,
8606
- color: attributes?.color ? this._parseColor(attributes.color) : void 0,
8607
- highlight: attributes?.background ? this._mapHighlightColor(attributes.background) : void 0
8608
- });
8609
- }
8610
- _createParagraph(runs, attributes, isList, listLevel) {
8611
- const headerLevel = attributes?.header ? typeof attributes.header === "number" ? attributes.header : parseInt(attributes.header) : void 0;
8612
- const paragraphOptions = {
8613
- children: runs,
8614
- alignment: this._mapAlignment(attributes?.align),
8615
- ...headerLevel && headerLevel >= 1 && headerLevel <= 6 ? { heading: HeadingLevel[`HEADING_${headerLevel}`] } : {},
8616
- ...isList && attributes?.list ? { bullet: { level: listLevel ?? 0 } } : {}
8617
- };
8618
- return new Paragraph(paragraphOptions);
8619
- }
8620
- async _getImageBuffer(imageUrl) {
8621
- if (imageUrl.startsWith("data:image")) {
8622
- const base64 = imageUrl.split(",")[1];
8623
- return this._base64ToUint8Array(base64);
8624
- }
8625
- const controller = new AbortController();
8626
- const timeoutId = setTimeout(() => controller.abort(), 1e4);
8627
- try {
8628
- const response = await fetch(imageUrl, {
8629
- method: "GET",
8630
- mode: "cors",
8631
- credentials: "omit",
8632
- signal: controller.signal
8633
- });
8634
- clearTimeout(timeoutId);
8635
- if (!response.ok) throw new Error(`图片获取失败: ${imageUrl}`);
8636
- const arrayBuffer = await response.arrayBuffer();
8637
- return new Uint8Array(arrayBuffer);
8638
- } catch (error) {
8639
- clearTimeout(timeoutId);
8640
- console.error("图片加载失败:", imageUrl, error);
8641
- return /* @__PURE__ */ new Uint8Array(0);
8642
- }
8643
- }
8644
- async _getImageDimensions(imageUrl) {
8645
- return new Promise((resolve) => {
8646
- const img = new Image();
8647
- const timeout = setTimeout(() => {
8648
- img.onload = null;
8649
- img.onerror = null;
8650
- resolve({
8651
- width: 400,
8652
- height: 300
8653
- });
8654
- }, 5e3);
8655
- img.onload = () => {
8656
- clearTimeout(timeout);
8657
- resolve({
8658
- width: img.width,
8659
- height: img.height
8660
- });
8661
- };
8662
- img.onerror = () => {
8663
- clearTimeout(timeout);
8664
- resolve({
8665
- width: 400,
8666
- height: 300
8667
- });
8668
- };
8669
- img.src = imageUrl;
8670
- });
8671
- }
8672
- _base64ToUint8Array(base64) {
8673
- const binaryString = atob(base64);
8674
- const bytes = new Uint8Array(binaryString.length);
8675
- for (let i = 0; i < binaryString.length; i++) bytes[i] = binaryString.charCodeAt(i);
8676
- return bytes;
8677
- }
8678
- _mapFontSize(size) {
8679
- if (!size) return void 0;
8680
- return {
8681
- "small": 12,
8682
- "normal": 14,
8683
- "large": 18,
8684
- "huge": 24
8685
- }[size] || 14;
8686
- }
8687
- _mapAlignment(align) {
8688
- switch (align) {
8689
- case "center": return AlignmentType.CENTER;
8690
- case "right": return AlignmentType.RIGHT;
8691
- case "justify": return AlignmentType.JUSTIFIED;
8692
- default: return AlignmentType.LEFT;
8693
- }
8694
- }
8695
- _parseColor(color) {
8696
- if (!color) return void 0;
8697
- if (color.startsWith("#")) return color.substring(1).toUpperCase();
8698
- const rgbMatch = /rgba?\((\d+),\s*(\d+),\s*(\d+)/.exec(color);
8699
- if (rgbMatch) return `${parseInt(rgbMatch[1]).toString(16).padStart(2, "0")}${parseInt(rgbMatch[2]).toString(16).padStart(2, "0")}${parseInt(rgbMatch[3]).toString(16).padStart(2, "0")}`.toUpperCase();
8700
- }
8701
- _mapHighlightColor(color) {
8702
- const hex = this._parseColor(color);
8703
- if (!hex) return HighlightColor.NONE;
8704
- const r = parseInt(hex.substring(0, 2), 16);
8705
- const g = parseInt(hex.substring(2, 4), 16);
8706
- const b = parseInt(hex.substring(4, 6), 16);
8707
- const colorMap = {
8708
- [HighlightColor.YELLOW]: [
8709
- 255,
8710
- 255,
8711
- 0
8712
- ],
8713
- [HighlightColor.GREEN]: [
8714
- 0,
8715
- 128,
8716
- 0
8717
- ],
8718
- [HighlightColor.CYAN]: [
8719
- 0,
8720
- 255,
8721
- 255
8722
- ],
8723
- [HighlightColor.RED]: [
8724
- 255,
8725
- 0,
8726
- 0
8727
- ],
8728
- [HighlightColor.BLUE]: [
8729
- 0,
8730
- 0,
8731
- 255
8732
- ],
8733
- [HighlightColor.MAGENTA]: [
8734
- 255,
8735
- 0,
8736
- 255
8737
- ],
8738
- [HighlightColor.DARK_BLUE]: [
8739
- 0,
8740
- 0,
8741
- 139
8742
- ],
8743
- [HighlightColor.DARK_CYAN]: [
8744
- 0,
8745
- 139,
8746
- 139
8747
- ],
8748
- [HighlightColor.DARK_GREEN]: [
8749
- 0,
8750
- 100,
8751
- 0
8752
- ],
8753
- [HighlightColor.DARK_MAGENTA]: [
8754
- 139,
8755
- 0,
8756
- 139
8757
- ],
8758
- [HighlightColor.DARK_RED]: [
8759
- 139,
8760
- 0,
8761
- 0
8762
- ],
8763
- [HighlightColor.DARK_YELLOW]: [
8764
- 128,
8765
- 128,
8766
- 0
8767
- ],
8768
- [HighlightColor.DARK_GRAY]: [
8769
- 169,
8770
- 169,
8771
- 169
8772
- ],
8773
- [HighlightColor.LIGHT_GRAY]: [
8774
- 211,
8775
- 211,
8776
- 211
8777
- ],
8778
- [HighlightColor.BLACK]: [
8779
- 0,
8780
- 0,
8781
- 0
8782
- ],
8783
- [HighlightColor.WHITE]: [
8784
- 255,
8785
- 255,
8786
- 255
8787
- ]
8788
- };
8789
- let closestColor = HighlightColor.NONE;
8790
- let minDistance = Infinity;
8791
- for (const [name, [cr, cg, cb]] of Object.entries(colorMap)) {
8792
- const distance = (r - cr) ** 2 + (g - cg) ** 2 + (b - cb) ** 2;
8793
- if (distance < minDistance) {
8794
- minDistance = distance;
8795
- closestColor = name;
8796
- }
8797
- }
8798
- return closestColor;
8799
- }
8800
8515
  _dropFileHandler(_dataUrl, _type, fileData) {
8801
8516
  this.fileChangeEvent(fileData.toFile());
8802
8517
  }