aden-editor-vue 0.1.8 → 0.1.9

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.cjs CHANGED
@@ -5990,6 +5990,46 @@ var STYLE_PROPERTIES = /* @__PURE__ */ new Set([
5990
5990
  "white-space",
5991
5991
  "width"
5992
5992
  ]);
5993
+ function extractRtfImageFiles(rtf) {
5994
+ var _a, _b;
5995
+ if (!rtf || rtf.length > 2e7) return [];
5996
+ const files = [];
5997
+ const pictStart = /\\pict\b/gi;
5998
+ let match;
5999
+ while (match = pictStart.exec(rtf)) {
6000
+ const groupStart = rtf.lastIndexOf("{", match.index);
6001
+ if (groupStart < 0) continue;
6002
+ let depth = 0;
6003
+ let groupEnd = -1;
6004
+ for (let i = groupStart; i < rtf.length; i += 1) {
6005
+ const char = rtf[i];
6006
+ if (char === "\\") {
6007
+ i += 1;
6008
+ continue;
6009
+ }
6010
+ if (char === "{") depth += 1;
6011
+ else if (char === "}" && --depth === 0) {
6012
+ groupEnd = i;
6013
+ break;
6014
+ }
6015
+ }
6016
+ if (groupEnd < 0) continue;
6017
+ const group = rtf.slice(match.index, groupEnd);
6018
+ const kind = (_b = (_a = /\\(pngblip|jpegblip|jpgblip)\b/i.exec(group)) == null ? void 0 : _a[1]) == null ? void 0 : _b.toLowerCase();
6019
+ const mime = kind === "pngblip" ? "image/png" : kind ? "image/jpeg" : "";
6020
+ if (!mime) continue;
6021
+ const hex = group.replace(/\\'[0-9a-f]{2}/gi, "").replace(/\\[a-z]+-?\d* ?/gi, "").replace(/\\[^a-z]/gi, "").replace(/[\s{}]/g, "");
6022
+ if (hex.length < 16 || hex.length % 2 !== 0 || /[^0-9a-f]/i.test(hex) || hex.length > 2e7) continue;
6023
+ const bytes = new Uint8Array(hex.length / 2);
6024
+ for (let i = 0; i < bytes.length; i += 1) bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
6025
+ const validSignature = mime === "image/png" ? bytes.length >= 8 && bytes[0] === 137 && bytes[1] === 80 && bytes[2] === 78 && bytes[3] === 71 : bytes.length >= 3 && bytes[0] === 255 && bytes[1] === 216 && bytes[2] === 255;
6026
+ if (!validSignature) continue;
6027
+ const extension = mime === "image/png" ? "png" : "jpg";
6028
+ files.push(new File([bytes], `word-clipboard-${files.length + 1}.${extension}`, { type: mime }));
6029
+ pictStart.lastIndex = groupEnd + 1;
6030
+ }
6031
+ return files;
6032
+ }
5993
6033
  function sanitizeWordHtml(input) {
5994
6034
  var _a, _b, _c;
5995
6035
  const source = input.slice(0, 2e6);
@@ -9656,16 +9696,17 @@ var WebAdapter = class {
9656
9696
  }
9657
9697
  };
9658
9698
  this.handlePaste = (e) => {
9659
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
9699
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
9660
9700
  if (this.opts.readonly) return;
9661
9701
  const html = (_b = (_a2 = e.clipboardData) == null ? void 0 : _a2.getData("text/html")) != null ? _b : "";
9662
9702
  const text = (_d = (_c = e.clipboardData) == null ? void 0 : _c.getData("text/plain")) != null ? _d : "";
9703
+ const rtf = (_h = (_g = (_e = e.clipboardData) == null ? void 0 : _e.getData("text/rtf")) != null ? _g : (_f = e.clipboardData) == null ? void 0 : _f.getData("application/rtf")) != null ? _h : "";
9663
9704
  this.syncAllFromDom();
9664
9705
  let pasteHtml2 = html;
9665
9706
  let pasteText = text;
9666
9707
  const max = this.getMaxLength();
9667
9708
  if (max !== null && (html || text)) {
9668
- const selectedText = (_f = (_e = window.getSelection()) == null ? void 0 : _e.toString()) != null ? _f : "";
9709
+ const selectedText = (_j = (_i = window.getSelection()) == null ? void 0 : _i.toString()) != null ? _j : "";
9669
9710
  const baseLength = Math.max(0, this.countChars() - countNonWhitespace(selectedText));
9670
9711
  let plainText = text;
9671
9712
  if (!plainText && html) {
@@ -9677,22 +9718,22 @@ var WebAdapter = class {
9677
9718
  }
9678
9719
  const attemptedLength = baseLength + countNonWhitespace(plainText);
9679
9720
  if (attemptedLength > max) {
9680
- (_h = (_g = this.opts).onMaxLength) == null ? void 0 : _h.call(_g, attemptedLength, max);
9721
+ (_l = (_k = this.opts).onMaxLength) == null ? void 0 : _l.call(_k, attemptedLength, max);
9681
9722
  pasteText = truncateTextAtLimit(plainText, Math.max(0, max - baseLength));
9682
9723
  pasteHtml2 = "";
9683
9724
  }
9684
9725
  }
9685
9726
  for (const plugin of [...this.plugins].reverse()) {
9686
- if ((_i = plugin.handlePaste) == null ? void 0 : _i.call(plugin, this.editor, { html: pasteHtml2, text: pasteText })) {
9727
+ if ((_m = plugin.handlePaste) == null ? void 0 : _m.call(plugin, this.editor, { html: pasteHtml2, text: pasteText })) {
9687
9728
  e.preventDefault();
9688
9729
  this.restoreSelectionFromModel();
9689
9730
  return;
9690
9731
  }
9691
9732
  }
9692
9733
  const clipboard = e.clipboardData;
9693
- const images = Array.from((_j = clipboard == null ? void 0 : clipboard.files) != null ? _j : []).filter((file) => file.type.startsWith("image/"));
9734
+ const images = Array.from((_n = clipboard == null ? void 0 : clipboard.files) != null ? _n : []).filter((file) => file.type.startsWith("image/"));
9694
9735
  const knownFiles = new Set(images.map((file) => `${file.name}:${file.size}:${file.lastModified}:${file.type}`));
9695
- for (const item of Array.from((_k = clipboard == null ? void 0 : clipboard.items) != null ? _k : [])) {
9736
+ for (const item of Array.from((_o = clipboard == null ? void 0 : clipboard.items) != null ? _o : [])) {
9696
9737
  if (item.kind !== "file" || !item.type.startsWith("image/")) continue;
9697
9738
  const file = item.getAsFile();
9698
9739
  if (!file) continue;
@@ -9701,7 +9742,7 @@ var WebAdapter = class {
9701
9742
  knownFiles.add(key);
9702
9743
  images.push(file);
9703
9744
  }
9704
- const advertisesRichClipboard = Array.from((_l = clipboard == null ? void 0 : clipboard.types) != null ? _l : []).some(
9745
+ const advertisesRichClipboard = Array.from((_p = clipboard == null ? void 0 : clipboard.types) != null ? _p : []).some(
9705
9746
  (type) => /text\/html|text\/rtf|application\/rtf/i.test(type)
9706
9747
  );
9707
9748
  if (!pasteHtml2 && advertisesRichClipboard) {
@@ -9711,7 +9752,7 @@ var WebAdapter = class {
9711
9752
  }
9712
9753
  if (pasteHtml2) {
9713
9754
  e.preventDefault();
9714
- void this.pasteRichHtml(pasteHtml2, images);
9755
+ void this.pasteRichHtml(pasteHtml2, images, rtf);
9715
9756
  return;
9716
9757
  }
9717
9758
  if (images.length) {
@@ -10229,9 +10270,10 @@ var WebAdapter = class {
10229
10270
  cur = cur.nextElementSibling;
10230
10271
  }
10231
10272
  }
10232
- async pasteRichHtml(html, files) {
10273
+ async pasteRichHtml(html, files, rtf = "") {
10233
10274
  const template = document.createElement("template");
10234
10275
  let safe = sanitizeWordHtml(html);
10276
+ if (safe.missingImages && rtf) files = [...files, ...extractRtfImageFiles(rtf)];
10235
10277
  if (safe.missingImages && !files.length) files = await this.readClipboardImageFiles();
10236
10278
  template.innerHTML = safe.html;
10237
10279
  const imageEls = Array.from(template.content.querySelectorAll("img[data-aden-missing-image]"));
@@ -10254,7 +10296,7 @@ var WebAdapter = class {
10254
10296
  }
10255
10297
  }
10256
10298
  if (safe.missingImages > files.length) {
10257
- showToast("Word \u5185\u5BB9\u4E2D\u6709\u56FE\u7247\u672A\u80FD\u4ECE\u526A\u8D34\u677F\u8BFB\u53D6\u3002\u8BF7\u91CD\u65B0\u590D\u5236\u56FE\u7247\uFF0C\u6216\u914D\u7F6E uploadImage \u4E0A\u4F20\u63A5\u53E3\u540E\u518D\u8BD5\u3002", 6500);
10299
+ showToast("Word \u5185\u5BB9\u4E2D\u7684\u56FE\u7247\u6570\u636E\u672A\u5305\u542B\u5728\u526A\u8D34\u677F\u53EF\u8BFB\u53D6\u683C\u5F0F\u4E2D\uFF0C\u6682\u65F6\u65E0\u6CD5\u81EA\u52A8\u6062\u590D\u3002\u8BF7\u5C1D\u8BD5\u5728 Word \u4E2D\u53F3\u952E\u56FE\u7247\u9009\u62E9\u201C\u590D\u5236\u56FE\u7247\u201D\u540E\u5355\u72EC\u7C98\u8D34\uFF0C\u6216\u5C06\u56FE\u7247\u53E6\u5B58\u4E3A\u6587\u4EF6\u518D\u63D2\u5165\u3002", 7e3);
10258
10300
  }
10259
10301
  safe = sanitizeWordHtml(template.innerHTML);
10260
10302
  this.runCommand((cmd) => cmd.pasteHtmlFragment(this.editor, safe.html, safe.text));
@@ -10269,7 +10311,9 @@ var WebAdapter = class {
10269
10311
  if (!item.types.includes("text/html")) continue;
10270
10312
  const html = await (await item.getType("text/html")).text();
10271
10313
  if (html) {
10272
- await this.pasteRichHtml(html, files);
10314
+ const rtfType = item.types.find((type) => /(?:text|application)\/rtf/i.test(type));
10315
+ const rtf = rtfType ? await (await item.getType(rtfType)).text() : "";
10316
+ await this.pasteRichHtml(html, files, rtf);
10273
10317
  return;
10274
10318
  }
10275
10319
  }