aden-editor-vue 0.1.7 → 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/README.md CHANGED
@@ -41,7 +41,7 @@ const content = ref('<p>欢迎使用 AdenEditor</p>');
41
41
 
42
42
  默认工具栏和样式会自动加载。未传 `options` 时,文字编辑、列表、引用、表格等内置功能仍可直接使用;无需单独导入 CSS,也无需自己处理创建、挂载和卸载。
43
43
 
44
- 从 Word 复制的 Office HTML 会以可编辑的富文本片段导入,尽量保留 Word 的段落、表格、内联格式和可安全转换的样式;这不是嵌入 Word 文档。Word 剪贴板若只提供本机临时图片路径、没有提供图片文件数据,浏览器无法读取该路径,编辑器会提示重新复制图片或配置 `uploadImage`。
44
+ 带 HTML 格式的粘贴会以可编辑的富文本片段导入,尽量保留段落、表格、内联格式和可安全转换的样式;Word 内容不是嵌入的 Word 文档。若剪贴板只提供本机临时图片路径、没有提供图片文件数据,浏览器无法读取该路径,编辑器会提示重新复制图片或配置 `uploadImage`。
45
45
 
46
46
  ## 保存内容
47
47
 
package/dist/index.cjs CHANGED
@@ -5974,14 +5974,62 @@ var STYLE_PROPERTIES = /* @__PURE__ */ new Set([
5974
5974
  "padding-top",
5975
5975
  "page-break-after",
5976
5976
  "page-break-before",
5977
+ "page-break-inside",
5978
+ "orphans",
5979
+ "widows",
5980
+ "letter-spacing",
5981
+ "word-spacing",
5982
+ "word-break",
5983
+ "overflow-wrap",
5977
5984
  "text-align",
5978
5985
  "text-decoration",
5979
5986
  "text-indent",
5980
5987
  "text-transform",
5988
+ "text-justify",
5981
5989
  "vertical-align",
5982
5990
  "white-space",
5983
5991
  "width"
5984
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
+ }
5985
6033
  function sanitizeWordHtml(input) {
5986
6034
  var _a, _b, _c;
5987
6035
  const source = input.slice(0, 2e6);
@@ -6066,7 +6114,7 @@ function inlineOfficeStyles(root) {
6066
6114
  if (!declarations) continue;
6067
6115
  for (const rawSelector of rule[1].split(",")) {
6068
6116
  const selector = rawSelector.trim();
6069
- if (!/^(?:[a-z][\w-]*)?(?:\.[\w-]+)+$/i.test(selector)) continue;
6117
+ if (!/^(?:[a-z][\w-]*)?(?:\.[\w-]+)*(?:\s+(?:[a-z][\w-]*)?(?:\.[\w-]+)*)*$/i.test(selector)) continue;
6070
6118
  try {
6071
6119
  for (const element of Array.from(root.querySelectorAll(selector))) {
6072
6120
  const current = (_b = element.getAttribute("style")) != null ? _b : "";
@@ -6086,7 +6134,7 @@ function sanitizeInlineStyle(raw) {
6086
6134
  if (separator < 1) continue;
6087
6135
  const property = declaration.slice(0, separator).trim().toLowerCase();
6088
6136
  const value = sanitizeStyleValue(declaration.slice(separator + 1));
6089
- if (!STYLE_PROPERTIES.has(property) || !value || /url\s*\(|expression\s*\(|javascript:|[{}<>]/i.test(value)) continue;
6137
+ if (!(STYLE_PROPERTIES.has(property) || /^mso-[a-z0-9-]+$/i.test(property)) || !value || /url\s*\(|expression\s*\(|javascript:|[{}<>]/i.test(value)) continue;
6090
6138
  if (property === "display" && !/^(inline|inline-block|block|table|table-row|table-cell|table-caption|table-row-group|table-header-group|table-footer-group|list-item)$/i.test(value)) continue;
6091
6139
  declarations.push(`${property}:${value}`);
6092
6140
  }
@@ -9648,16 +9696,17 @@ var WebAdapter = class {
9648
9696
  }
9649
9697
  };
9650
9698
  this.handlePaste = (e) => {
9651
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
9699
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
9652
9700
  if (this.opts.readonly) return;
9653
9701
  const html = (_b = (_a2 = e.clipboardData) == null ? void 0 : _a2.getData("text/html")) != null ? _b : "";
9654
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 : "";
9655
9704
  this.syncAllFromDom();
9656
9705
  let pasteHtml2 = html;
9657
9706
  let pasteText = text;
9658
9707
  const max = this.getMaxLength();
9659
9708
  if (max !== null && (html || text)) {
9660
- 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 : "";
9661
9710
  const baseLength = Math.max(0, this.countChars() - countNonWhitespace(selectedText));
9662
9711
  let plainText = text;
9663
9712
  if (!plainText && html) {
@@ -9669,22 +9718,22 @@ var WebAdapter = class {
9669
9718
  }
9670
9719
  const attemptedLength = baseLength + countNonWhitespace(plainText);
9671
9720
  if (attemptedLength > max) {
9672
- (_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);
9673
9722
  pasteText = truncateTextAtLimit(plainText, Math.max(0, max - baseLength));
9674
9723
  pasteHtml2 = "";
9675
9724
  }
9676
9725
  }
9677
9726
  for (const plugin of [...this.plugins].reverse()) {
9678
- 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 })) {
9679
9728
  e.preventDefault();
9680
9729
  this.restoreSelectionFromModel();
9681
9730
  return;
9682
9731
  }
9683
9732
  }
9684
9733
  const clipboard = e.clipboardData;
9685
- 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/"));
9686
9735
  const knownFiles = new Set(images.map((file) => `${file.name}:${file.size}:${file.lastModified}:${file.type}`));
9687
- 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 : [])) {
9688
9737
  if (item.kind !== "file" || !item.type.startsWith("image/")) continue;
9689
9738
  const file = item.getAsFile();
9690
9739
  if (!file) continue;
@@ -9693,9 +9742,17 @@ var WebAdapter = class {
9693
9742
  knownFiles.add(key);
9694
9743
  images.push(file);
9695
9744
  }
9696
- if (shouldPreserveRichHtmlPaste(pasteHtml2)) {
9745
+ const advertisesRichClipboard = Array.from((_p = clipboard == null ? void 0 : clipboard.types) != null ? _p : []).some(
9746
+ (type) => /text\/html|text\/rtf|application\/rtf/i.test(type)
9747
+ );
9748
+ if (!pasteHtml2 && advertisesRichClipboard) {
9697
9749
  e.preventDefault();
9698
- void this.pasteOfficeHtml(pasteHtml2, images);
9750
+ void this.pasteRichClipboardFallback(text, images);
9751
+ return;
9752
+ }
9753
+ if (pasteHtml2) {
9754
+ e.preventDefault();
9755
+ void this.pasteRichHtml(pasteHtml2, images, rtf);
9699
9756
  return;
9700
9757
  }
9701
9758
  if (images.length) {
@@ -10213,9 +10270,10 @@ var WebAdapter = class {
10213
10270
  cur = cur.nextElementSibling;
10214
10271
  }
10215
10272
  }
10216
- async pasteOfficeHtml(html, files) {
10273
+ async pasteRichHtml(html, files, rtf = "") {
10217
10274
  const template = document.createElement("template");
10218
10275
  let safe = sanitizeWordHtml(html);
10276
+ if (safe.missingImages && rtf) files = [...files, ...extractRtfImageFiles(rtf)];
10219
10277
  if (safe.missingImages && !files.length) files = await this.readClipboardImageFiles();
10220
10278
  template.innerHTML = safe.html;
10221
10279
  const imageEls = Array.from(template.content.querySelectorAll("img[data-aden-missing-image]"));
@@ -10238,11 +10296,34 @@ var WebAdapter = class {
10238
10296
  }
10239
10297
  }
10240
10298
  if (safe.missingImages > files.length) {
10241
- 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);
10242
10300
  }
10243
10301
  safe = sanitizeWordHtml(template.innerHTML);
10244
10302
  this.runCommand((cmd) => cmd.pasteHtmlFragment(this.editor, safe.html, safe.text));
10245
10303
  }
10304
+ async pasteRichClipboardFallback(plainText, files) {
10305
+ var _a;
10306
+ try {
10307
+ const read = (_a = navigator.clipboard) == null ? void 0 : _a.read;
10308
+ if (typeof read === "function") {
10309
+ const items = await read.call(navigator.clipboard);
10310
+ for (const item of items) {
10311
+ if (!item.types.includes("text/html")) continue;
10312
+ const html = await (await item.getType("text/html")).text();
10313
+ if (html) {
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);
10317
+ return;
10318
+ }
10319
+ }
10320
+ }
10321
+ } catch {
10322
+ }
10323
+ showToast("\u672A\u80FD\u8BFB\u53D6 Word \u7684\u683C\u5F0F\u5316\u5185\u5BB9\uFF0C\u5F53\u524D\u526A\u8D34\u677F\u53EA\u63D0\u4F9B\u7EAF\u6587\u672C\uFF0C\u56E0\u6B64\u65E0\u6CD5\u4FDD\u7559\u539F\u6392\u7248\u3002\u8BF7\u5728 Word \u4E2D\u91CD\u65B0\u590D\u5236\u540E\u7C98\u8D34\uFF0C\u6216\u68C0\u67E5\u6D4F\u89C8\u5668\u526A\u8D34\u677F\u6743\u9650\u3002", 7e3);
10324
+ if (plainText) this.runCommand((cmd) => cmd.pastePlainText(this.editor, plainText));
10325
+ for (const file of files) void this.imageCtrl.insertFile(file);
10326
+ }
10246
10327
  async readClipboardImageFiles() {
10247
10328
  var _a, _b;
10248
10329
  try {
@@ -10792,9 +10873,6 @@ var WebAdapter = class {
10792
10873
  function countNonWhitespace(text) {
10793
10874
  return text.replace(/\s/g, "").length;
10794
10875
  }
10795
- function shouldPreserveRichHtmlPaste(html) {
10796
- return /<(?:table|img|style)\b|\bstyle\s*=|\bclass\s*=|mso-[\w-]+|Microsoft Word|urn:schemas-microsoft-com|w:worddocument|v:(?:shape|imagedata)|StartFragment/i.test(html);
10797
- }
10798
10876
  function truncateTextAtLimit(text, remaining) {
10799
10877
  let used = 0;
10800
10878
  let result = "";