Sfoglia il codice sorgente

:refactor: https://github.com/Vanessa219/vditor/pull/1348

Vanessa 2 anni fa
parent
commit
4ccd6fc047
8 ha cambiato i file con 36 aggiunte e 17 eliminazioni
  1. 3 0
      CHANGELOG.md
  2. 3 8
      README.md
  3. 7 0
      README_en_US.md
  4. 6 0
      demo/index.js
  5. 6 7
      src/ts/ir/index.ts
  6. 3 0
      src/ts/util/Options.ts
  7. 7 1
      src/ts/wysiwyg/index.ts
  8. 1 1
      types/index.d.ts

+ 3 - 0
CHANGELOG.md

@@ -1,6 +1,8 @@
 ## Vditor change log
 
 ### 升级
+* 3.9
+  * 添加 option.link 
 * 3.8
   * 添加 plantumlRender 方法
   * options.input 移除 previewElement 参数
@@ -105,6 +107,7 @@
 
 ### v3.9.0 / 2023-01
 
+* [1348](https://github.com/Vanessa219/vditor/pull/1348) 新增链接和图片处理接口 `引入特性`
 * [1342](https://github.com/Vanessa219/vditor/pull/1342) 初始化后不自动聚焦 `改进功能`
 * [1341](https://github.com/Vanessa219/vditor/pull/1341) 支持 markmap `引入特性`
 * [1335](https://github.com/Vanessa219/vditor/issues/1335) 嵌入 Iframe 时无法导出 PDF `修复缺陷`

+ 3 - 8
README.md

@@ -360,16 +360,11 @@ previewImage: (img: HTMLImageElement) => void;
 对原图片双击预览的拦截,对图片的扩展操作。
 
 #### options.link
-``` ts
-link?: {
-  open?: boolean;
-  trigger?: (href: string) => void;
-}
-```
+
 |   | 说明 | 默认值 |
 | - | - | - |
-| open | 是否点击打开(window.open)地址 | - |
-| trigger | 地址点击触发 | - |
+| open | 是否打开链接地址 | true |
+| trigger(bom: Element) => void | 点击链接触发事件 | - |
 
 #### options.hint
 

+ 7 - 0
README_en_US.md

@@ -327,6 +327,13 @@ Default: ["desktop", "tablet", "mobile", "mp-wechat", "zhihu"]
 | className | Button Class | - |
 | click(key: string) | Click Event | - |
 
+#### options.link
+
+|   | Explanation | Default |
+| - | - | - |
+| open | Whether to open the link address | true |
+| trigger(bom: Element) => void | Click link trigger event | - |
+
 #### options.hint
 
 |   | Explanation | Default |

+ 6 - 0
demo/index.js

@@ -55,6 +55,12 @@ const initVditor = (language) => {
     cdn: 'http://localhost:9000',
     toolbar,
     lang: language,
+    link: {
+      open: false,
+      trigger(bom){
+        console.log(bom)
+      }
+    },
     mode: 'wysiwyg',
     height: window.innerHeight + 100,
     outline: {

+ 6 - 7
src/ts/ir/index.ts

@@ -93,9 +93,9 @@ class IR {
             if (this.preventInput) {
                 this.preventInput = false;
                 processAfterRender(vditor, {
-                  enableAddUndoStack: true,
-                  enableHint: true,
-                  enableInput: true,
+                    enableAddUndoStack: true,
+                    enableHint: true,
+                    enableInput: true,
                 });
                 return;
             }
@@ -149,10 +149,9 @@ class IR {
             // 打开链接
             const aElement = hasClosestByAttribute(event.target, "data-type", "a");
             if (aElement && (!aElement.classList.contains("vditor-ir__node--expand"))) {
-                if (vditor.options.link && vditor.options.link.trigger) {
-                    vditor.options.link.trigger(aElement.querySelector(":scope > .vditor-ir__marker--link").textContent);
-                }
-                if (vditor.options.link && vditor.options.link.open) {
+                if (vditor.options.link.trigger) {
+                    vditor.options.link.trigger(aElement.querySelector(":scope > .vditor-ir__marker--link"));
+                } else if (vditor.options.link.open) {
                     window.open(aElement.querySelector(":scope > .vditor-ir__marker--link").textContent);
                 }
                 return;

+ 3 - 0
src/ts/util/Options.ts

@@ -59,6 +59,9 @@ export class Options {
             mode: "both",
             theme: Constants.THEME_OPTIONS,
         },
+        link: {
+            open: true,
+        },
         resize: {
             enable: false,
             position: "bottom",

+ 7 - 1
src/ts/wysiwyg/index.ts

@@ -425,7 +425,13 @@ class WYSIWYG {
 
             // 打开链接
             if (event.target.tagName === "A") {
-                window.open(event.target.getAttribute("href"));
+                if (vditor.options.link.trigger) {
+                    vditor.options.link.trigger(event.target);
+                } else if (vditor.options.link.open) {
+                    window.open(event.target.getAttribute("href"));
+                }
+                event.preventDefault();
+                return;
             }
 
             const range = getEditorRange(vditor);

+ 1 - 1
types/index.d.ts

@@ -623,7 +623,7 @@ interface IOptions {
     previewImage?: (img: HTMLImageElement) => void;
     link?: {
         open?: boolean;
-        trigger?: (href: string) => void;
+        trigger?: (bom: Element) => void;
     },
     /** @link https://ld246.com/article/1549638745630#options-hint */
     hint?: IHint;