From b0bc59d8a9e9550fa47e9b1530711fbfb504681a Mon Sep 17 00:00:00 2001 From: Liyuan Li Date: Tue, 28 Jul 2020 23:30:31 +0800 Subject: [PATCH] :bug: #619 --- demo/index.js | 4 ++-- src/ts/undo/index.ts | 39 +++++++++++++++++---------------------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/demo/index.js b/demo/index.js index 5ed097933..c75e000c2 100644 --- a/demo/index.js +++ b/demo/index.js @@ -50,8 +50,8 @@ if (window.innerWidth < 768) { } window.vditor = new Vditor('vditor', { - // _lutePath: `http://192.168.0.107:9090/lute.min.js?${new Date().getTime()}`, - _lutePath: 'src/js/lute/lute.min.js', + _lutePath: `http://192.168.0.107:9090/lute.min.js?${new Date().getTime()}`, + // _lutePath: 'src/js/lute/lute.min.js', toolbar, mode: 'ir', height: window.innerHeight + 100, diff --git a/src/ts/undo/index.ts b/src/ts/undo/index.ts index c62bd7cd4..55c5327ce 100644 --- a/src/ts/undo/index.ts +++ b/src/ts/undo/index.ts @@ -93,24 +93,21 @@ class Undo { // Safari keydown 在 input 之后,不需要重复记录历史 return; } - const caretObj = this.addCaret(vditor); - this[vditor.currentMode].undoStack[0][0].diffs[0][1] = caretObj.text; - this[vditor.currentMode].lastText = caretObj.text; + const text = this.addCaret(vditor); + this[vditor.currentMode].undoStack[0][0].diffs[0][1] = text; + this[vditor.currentMode].lastText = text; // 不能添加 setSelectionFocus(cloneRange); 否则 windows chrome 首次输入会烂 } public addToUndoStack(vditor: IVditor) { // afterRenderEvent.ts 已经 debounce - const caretObj = this.addCaret(vditor); - if (caretObj.cloneRange) { - setSelectionFocus(caretObj.cloneRange); - } - const diff = this.dmp.diff_main(caretObj.text, this[vditor.currentMode].lastText, true); - const patchList = this.dmp.patch_make(caretObj.text, this[vditor.currentMode].lastText, diff); + const text = this.addCaret(vditor, true); + const diff = this.dmp.diff_main(text, this[vditor.currentMode].lastText, true); + const patchList = this.dmp.patch_make(text, this[vditor.currentMode].lastText, diff); if (patchList.length === 0 && this[vditor.currentMode].undoStack.length > 0) { return; } - this[vditor.currentMode].lastText = caretObj.text; + this[vditor.currentMode].lastText = text; this[vditor.currentMode].undoStack.push(patchList); if (this[vditor.currentMode].undoStack.length > this.stackSize) { this[vditor.currentMode].undoStack.shift(); @@ -201,27 +198,25 @@ class Undo { }; } - private addCaret(vditor: IVditor) { + private addCaret(vditor: IVditor, setFocus = false) { let cloneRange: Range; if (getSelection().rangeCount !== 0 && !vditor[vditor.currentMode].element.querySelector("wbr")) { const range = getSelection().getRangeAt(0); if (vditor[vditor.currentMode].element.contains(range.startContainer)) { cloneRange = range.cloneRange(); - const wbrTempElement = document.createElement("span"); - wbrTempElement.className = "vditor-wbr"; - range.insertNode(wbrTempElement); + const wbrElement = document.createElement("span"); + wbrElement.className = "vditor-wbr"; + range.insertNode(wbrElement); } } - const text = vditor[vditor.currentMode].element.innerHTML; - const wbrElement = vditor[vditor.currentMode].element.querySelector(".wbr"); - if (wbrElement) { - wbrElement.remove(); + vditor[vditor.currentMode].element.querySelectorAll(".vditor-wbr").forEach((item) => { + item.outerHTML = ""; + }); + if (setFocus && cloneRange) { + setSelectionFocus(cloneRange); } - return { - cloneRange, - text: text.replace('', ""), - }; + return text.replace('', ""); } }