Van 6 年之前
父節點
當前提交
5f846a27cc
共有 5 個文件被更改,包括 17 次插入12 次删除
  1. 6 4
      src/ts/editor/index.ts
  2. 6 7
      src/ts/hint/index.ts
  3. 1 1
      src/ts/markdown/index.ts
  4. 2 0
      src/ts/types/index.d.ts
  5. 2 0
      src/ts/util/OptionsClass.ts

+ 6 - 4
src/ts/editor/index.ts

@@ -22,9 +22,7 @@ class Editor {
                 vditor.counter.render(this.element.value.length, vditor.options.counter)
             }
 
-            if (vditor.options.input) {
-                vditor.options.input(this.element.value, vditor.markdown && vditor.markdown.element)
-            }
+            vditor.options.input && vditor.options.input(this.element.value, vditor.markdown && vditor.markdown.element)
 
             vditor.hint && vditor.hint.render()
 
@@ -32,7 +30,7 @@ class Editor {
                 localStorage.setItem(`vditor${vditor.id}`, vditor.editor.element.value)
             }
 
-            vditor.markdown.render(vditor)
+            vditor.markdown && vditor.markdown.render(vditor)
         })
 
         this.element.addEventListener('focus', () => {
@@ -224,6 +222,10 @@ const insertText = (textarea: HTMLTextAreaElement, prefix: string, suffix: strin
                     }
                 }
             }
+
+            const event = document.createEvent('HTMLEvents');
+            event.initEvent('input', true, false);
+            textarea.dispatchEvent(event);
         } else {
             if (startPos === endPos) {
                 // no selection

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

@@ -7,8 +7,11 @@ export class Hint {
     element: HTMLUListElement
     atUser: { (value: string): Array<any> }
     commonEmoji: any
+    hintDelay: number
 
     constructor(vditor: Vditor) {
+        this.timeId = -1
+        this.hintDelay = vditor.options.hintDelay
         this.editorElement = vditor.editor.element
         this.atUser = vditor.options.atUser
         this.commonEmoji = vditor.options.commonEmoji
@@ -27,17 +30,13 @@ export class Hint {
 
         if (atKey === undefined && emojiKey === undefined) {
             this.element.style.display = 'none'
-            if (this.timeId) {
-                clearTimeout(this.timeId)
-            }
+            clearTimeout(this.timeId)
         } else {
             if (atKey !== undefined && this.atUser) {
-                if (this.timeId) {
-                    clearTimeout(this.timeId)
-                }
+                clearTimeout(this.timeId)
                 this.timeId = setTimeout(() => {
                     this.genHTML(this.atUser(atKey))
-                }, 100)
+                }, this.hintDelay)
             }
             if (emojiKey !== undefined) {
                 import(/* webpackChunkName: "allEmoji" */ '../emoji/allEmoji.js')

+ 1 - 1
src/ts/markdown/index.ts

@@ -34,7 +34,7 @@ export class Markdown {
                 xhr.send(JSON.stringify({
                     markdownText: vditor.editor.element.value,
                 }))
-            }, 1000)
+            }, vditor.options.previewDelay)
         } else {
             this.element.innerHTML = vditor.editor.element.value
         }

+ 2 - 0
src/ts/types/index.d.ts

@@ -37,6 +37,8 @@ interface MenuItem {
 }
 
 interface Options {
+    previewDelay: number
+    hintDelay: number
     parseMarkdown?: { (element: HTMLElement): void }
     markdownUrl?: string
     height?: number | string

+ 2 - 0
src/ts/util/OptionsClass.ts

@@ -13,6 +13,8 @@ export class OptionsClass {
         draggable: false,
         previewShow: false,
         counter: 0,
+        previewDelay: 1000,
+        hintDelay: 500,
         markdownUrl: '',
         upload: {
             url: '',