@yltrcc/vditor 0.0.1 → 0.0.3
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.css +15 -2
- package/dist/index.js +183 -4
- package/dist/index.min.js +1 -1
- package/dist/method.js +3 -3
- package/dist/method.min.js +1 -1
- package/dist/ts/markdown/docLink.d.ts +33 -0
- package/dist/types/index.d.ts +13 -0
- package/package.json +3 -3
- package/src/assets/less/_content.less +16 -0
- package/src/ts/constants.ts +1 -1
- package/src/ts/markdown/docLink.ts +171 -0
- package/src/ts/preview/index.ts +7 -0
- package/src/ts/toolbar/Info.ts +2 -2
- package/src/ts/undo/index.ts +270 -270
- package/src/ts/wysiwyg/afterRenderEvent.ts +6 -0
- package/src/ts/wysiwyg/index.ts +14 -0
package/src/ts/undo/index.ts
CHANGED
|
@@ -1,270 +1,270 @@
|
|
|
1
|
-
import * as DiffMatchPatch from "diff-match-patch";
|
|
2
|
-
import {disableToolbar, enableToolbar, hidePanel} from "../toolbar/setToolbar";
|
|
3
|
-
import {isFirefox, isSafari} from "../util/compatibility";
|
|
4
|
-
import {scrollCenter} from "../util/editorCommonEvent";
|
|
5
|
-
import {execAfterRender} from "../util/fixBrowserBehavior";
|
|
6
|
-
import {highlightToolbar} from "../util/highlightToolbar";
|
|
7
|
-
import {processCodeRender} from "../util/processCode";
|
|
8
|
-
import {setRangeByWbr, setSelectionFocus} from "../util/selection";
|
|
9
|
-
import {renderToc} from "../util/toc";
|
|
10
|
-
|
|
11
|
-
interface IUndo {
|
|
12
|
-
hasUndo: boolean;
|
|
13
|
-
lastText: string;
|
|
14
|
-
redoStack: DiffMatchPatch.patch_obj[][];
|
|
15
|
-
undoStack: DiffMatchPatch.patch_obj[][];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
class Undo {
|
|
19
|
-
private stackSize = 50;
|
|
20
|
-
private dmp: any;
|
|
21
|
-
private wysiwyg: IUndo;
|
|
22
|
-
private ir: IUndo;
|
|
23
|
-
private sv: IUndo;
|
|
24
|
-
|
|
25
|
-
constructor() {
|
|
26
|
-
this.resetStack();
|
|
27
|
-
// @ts-ignore
|
|
28
|
-
this.dmp = new DiffMatchPatch();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public clearStack(vditor: IVditor) {
|
|
32
|
-
this.resetStack();
|
|
33
|
-
this.resetIcon(vditor);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public resetIcon(vditor: IVditor) {
|
|
37
|
-
if (!vditor.toolbar) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (this[vditor.currentMode].undoStack.length > 1) {
|
|
42
|
-
enableToolbar(vditor.toolbar.elements, ["undo"]);
|
|
43
|
-
} else {
|
|
44
|
-
disableToolbar(vditor.toolbar.elements, ["undo"]);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (this[vditor.currentMode].redoStack.length !== 0) {
|
|
48
|
-
enableToolbar(vditor.toolbar.elements, ["redo"]);
|
|
49
|
-
} else {
|
|
50
|
-
disableToolbar(vditor.toolbar.elements, ["redo"]);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public undo(vditor: IVditor) {
|
|
55
|
-
if (vditor[vditor.currentMode].element.getAttribute("contenteditable") === "false") {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
if (this[vditor.currentMode].undoStack.length < 2) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const state = this[vditor.currentMode].undoStack.pop();
|
|
62
|
-
if (!state) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
this[vditor.currentMode].redoStack.push(state);
|
|
66
|
-
this.renderDiff(state, vditor);
|
|
67
|
-
this[vditor.currentMode].hasUndo = true;
|
|
68
|
-
// undo 操作后,需要关闭 hint
|
|
69
|
-
hidePanel(vditor, ["hint"]);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
public redo(vditor: IVditor) {
|
|
73
|
-
if (vditor[vditor.currentMode].element.getAttribute("contenteditable") === "false") {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
const state = this[vditor.currentMode].redoStack.pop();
|
|
77
|
-
if (!state) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
this[vditor.currentMode].undoStack.push(state);
|
|
81
|
-
this.renderDiff(state, vditor, true);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public recordFirstPosition(vditor: IVditor, event: KeyboardEvent) {
|
|
85
|
-
if (getSelection().rangeCount === 0) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
if (this[vditor.currentMode].undoStack.length !== 1 || this[vditor.currentMode].undoStack[0].length === 0 ||
|
|
89
|
-
this[vditor.currentMode].redoStack.length > 0) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
if (isFirefox() && event.key === "Backspace") {
|
|
93
|
-
// Firefox 第一次删除无效
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
if (isSafari()) {
|
|
97
|
-
// Safari keydown 在 input 之后,不需要重复记录历史
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
const text = this.addCaret(vditor);
|
|
101
|
-
if (text.replace("<wbr>", "").replace(" vditor-ir__node--expand", "")
|
|
102
|
-
!== this[vditor.currentMode].undoStack[0][0].diffs[0][1].replace("<wbr>", "")) {
|
|
103
|
-
// 当还不没有存入 undo 栈时,按下 ctrl 后会覆盖 lastText
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
this[vditor.currentMode].undoStack[0][0].diffs[0][1] = text;
|
|
107
|
-
this[vditor.currentMode].lastText = text;
|
|
108
|
-
// 不能添加 setSelectionFocus(cloneRange); 否则 windows chrome 首次输入会烂
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
public addToUndoStack(vditor: IVditor) {
|
|
112
|
-
// afterRenderEvent.ts 已经 debounce
|
|
113
|
-
const text = this.addCaret(vditor, true);
|
|
114
|
-
const diff = this.dmp.diff_main(text, this[vditor.currentMode].lastText, true);
|
|
115
|
-
const patchList = this.dmp.patch_make(text, this[vditor.currentMode].lastText, diff);
|
|
116
|
-
if (patchList.length === 0 && this[vditor.currentMode].undoStack.length > 0) {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
this[vditor.currentMode].lastText = text;
|
|
120
|
-
this[vditor.currentMode].undoStack.push(patchList);
|
|
121
|
-
if (this[vditor.currentMode].undoStack.length > this.stackSize) {
|
|
122
|
-
this[vditor.currentMode].undoStack.shift();
|
|
123
|
-
}
|
|
124
|
-
if (this[vditor.currentMode].hasUndo) {
|
|
125
|
-
this[vditor.currentMode].redoStack = [];
|
|
126
|
-
this[vditor.currentMode].hasUndo = false;
|
|
127
|
-
disableToolbar(vditor.toolbar.elements, ["redo"]);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (this[vditor.currentMode].undoStack.length > 1) {
|
|
131
|
-
enableToolbar(vditor.toolbar.elements, ["undo"]);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
private renderDiff(state: DiffMatchPatch.patch_obj[], vditor: IVditor, isRedo: boolean = false) {
|
|
136
|
-
let text;
|
|
137
|
-
if (isRedo) {
|
|
138
|
-
const redoPatchList = this.dmp.patch_deepCopy(state).reverse();
|
|
139
|
-
redoPatchList.forEach((patch: DiffMatchPatch.patch_obj) => {
|
|
140
|
-
patch.diffs.forEach((diff: any) => {
|
|
141
|
-
diff[0] = -diff[0];
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
text = this.dmp.patch_apply(redoPatchList, this[vditor.currentMode].lastText)[0];
|
|
145
|
-
} else {
|
|
146
|
-
text = this.dmp.patch_apply(state, this[vditor.currentMode].lastText)[0];
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
this[vditor.currentMode].lastText = text;
|
|
150
|
-
vditor[vditor.currentMode].element.innerHTML = text;
|
|
151
|
-
if (vditor.currentMode !== "sv") {
|
|
152
|
-
vditor[vditor.currentMode].element.querySelectorAll(`.vditor-${vditor.currentMode}__preview`)
|
|
153
|
-
.forEach((blockElement: HTMLElement) => {
|
|
154
|
-
if (blockElement.parentElement.querySelector(".language-echarts")) {
|
|
155
|
-
if (vditor.currentMode === "ir") {
|
|
156
|
-
blockElement.parentElement.outerHTML = vditor.lute.SpinVditorIRDOM(blockElement.parentElement.outerHTML);
|
|
157
|
-
} else {
|
|
158
|
-
blockElement.parentElement.outerHTML = vditor.lute.SpinVditorDOM(blockElement.parentElement.outerHTML);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
vditor[vditor.currentMode].element.querySelectorAll(`.vditor-${vditor.currentMode}__preview[data-render='2']`)
|
|
163
|
-
.forEach((blockElement: HTMLElement) => {
|
|
164
|
-
processCodeRender(blockElement, vditor);
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (!vditor[vditor.currentMode].element.querySelector("wbr")) {
|
|
169
|
-
// Safari 第一次输入没有光标,需手动定位到结尾
|
|
170
|
-
const range = getSelection().getRangeAt(0);
|
|
171
|
-
range.setEndBefore(vditor[vditor.currentMode].element);
|
|
172
|
-
range.collapse(false);
|
|
173
|
-
} else {
|
|
174
|
-
setRangeByWbr(
|
|
175
|
-
vditor[vditor.currentMode].element, vditor[vditor.currentMode].element.ownerDocument.createRange());
|
|
176
|
-
scrollCenter(vditor);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
renderToc(vditor);
|
|
180
|
-
|
|
181
|
-
execAfterRender(vditor, {
|
|
182
|
-
enableAddUndoStack: false,
|
|
183
|
-
enableHint: false,
|
|
184
|
-
enableInput: true,
|
|
185
|
-
});
|
|
186
|
-
highlightToolbar(vditor);
|
|
187
|
-
|
|
188
|
-
vditor[vditor.currentMode].element.querySelectorAll(`.vditor-${vditor.currentMode}__preview[data-render='2']`)
|
|
189
|
-
.forEach((item: HTMLElement) => {
|
|
190
|
-
processCodeRender(item, vditor);
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
if (this[vditor.currentMode].undoStack.length > 1) {
|
|
194
|
-
enableToolbar(vditor.toolbar.elements, ["undo"]);
|
|
195
|
-
} else {
|
|
196
|
-
disableToolbar(vditor.toolbar.elements, ["undo"]);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (this[vditor.currentMode].redoStack.length !== 0) {
|
|
200
|
-
enableToolbar(vditor.toolbar.elements, ["redo"]);
|
|
201
|
-
} else {
|
|
202
|
-
disableToolbar(vditor.toolbar.elements, ["redo"]);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
private resetStack() {
|
|
207
|
-
this.ir = {
|
|
208
|
-
hasUndo: false,
|
|
209
|
-
lastText: "",
|
|
210
|
-
redoStack: [],
|
|
211
|
-
undoStack: [],
|
|
212
|
-
};
|
|
213
|
-
this.sv = {
|
|
214
|
-
hasUndo: false,
|
|
215
|
-
lastText: "",
|
|
216
|
-
redoStack: [],
|
|
217
|
-
undoStack: [],
|
|
218
|
-
};
|
|
219
|
-
this.wysiwyg = {
|
|
220
|
-
hasUndo: false,
|
|
221
|
-
lastText: "",
|
|
222
|
-
redoStack: [],
|
|
223
|
-
undoStack: [],
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
private addCaret(vditor: IVditor, setFocus = false) {
|
|
228
|
-
let cloneRange: Range;
|
|
229
|
-
if (getSelection().rangeCount !== 0 && !vditor[vditor.currentMode].element.querySelector("wbr")) {
|
|
230
|
-
const range = getSelection().getRangeAt(0);
|
|
231
|
-
if (vditor[vditor.currentMode].element.contains(range.startContainer)) {
|
|
232
|
-
cloneRange = range.cloneRange();
|
|
233
|
-
const wbrElement = document.createElement("span");
|
|
234
|
-
wbrElement.className = "vditor-wbr";
|
|
235
|
-
range.insertNode(wbrElement);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
// 移除数学公式、echart 渲染 https://github.com/Vanessa219/vditor/issues/1738
|
|
239
|
-
const cloneElement = vditor[vditor.currentMode].element.cloneNode(true) as HTMLElement;
|
|
240
|
-
cloneElement.querySelectorAll(`.vditor-${vditor.currentMode}__preview[data-render='1']`)
|
|
241
|
-
.forEach((item: HTMLElement) => {
|
|
242
|
-
if (!item.firstElementChild) {
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
if (item.firstElementChild.classList.contains("language-echarts") ||
|
|
246
|
-
item.firstElementChild.classList.contains("language-plantuml") ||
|
|
247
|
-
item.firstElementChild.classList.contains("language-mindmap")) {
|
|
248
|
-
item.firstElementChild.removeAttribute("_echarts_instance_");
|
|
249
|
-
item.firstElementChild.removeAttribute("data-processed");
|
|
250
|
-
item.firstElementChild.innerHTML = item.previousElementSibling.firstElementChild.innerHTML;
|
|
251
|
-
item.setAttribute("data-render", "2");
|
|
252
|
-
} else if (item.firstElementChild.classList.contains("language-math")) {
|
|
253
|
-
item.setAttribute("data-render", "2");
|
|
254
|
-
item.firstElementChild.textContent = item.firstElementChild.getAttribute("data-math");
|
|
255
|
-
item.firstElementChild.removeAttribute("data-math");
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
const text = cloneElement.innerHTML;
|
|
259
|
-
vditor[vditor.currentMode].element.querySelectorAll(".vditor-wbr").forEach((item) => {
|
|
260
|
-
item.remove();
|
|
261
|
-
// 使用 item.outerHTML = "" 会产生 https://github.com/Vanessa219/vditor/pull/686;
|
|
262
|
-
});
|
|
263
|
-
if (setFocus && cloneRange) {
|
|
264
|
-
setSelectionFocus(cloneRange);
|
|
265
|
-
}
|
|
266
|
-
return text.replace('<span class="vditor-wbr"></span>', "<wbr>");
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
export {Undo};
|
|
1
|
+
import * as DiffMatchPatch from "diff-match-patch";
|
|
2
|
+
import {disableToolbar, enableToolbar, hidePanel} from "../toolbar/setToolbar";
|
|
3
|
+
import {isFirefox, isSafari} from "../util/compatibility";
|
|
4
|
+
import {scrollCenter} from "../util/editorCommonEvent";
|
|
5
|
+
import {execAfterRender} from "../util/fixBrowserBehavior";
|
|
6
|
+
import {highlightToolbar} from "../util/highlightToolbar";
|
|
7
|
+
import {processCodeRender} from "../util/processCode";
|
|
8
|
+
import {setRangeByWbr, setSelectionFocus} from "../util/selection";
|
|
9
|
+
import {renderToc} from "../util/toc";
|
|
10
|
+
|
|
11
|
+
interface IUndo {
|
|
12
|
+
hasUndo: boolean;
|
|
13
|
+
lastText: string;
|
|
14
|
+
redoStack: DiffMatchPatch.patch_obj[][];
|
|
15
|
+
undoStack: DiffMatchPatch.patch_obj[][];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class Undo {
|
|
19
|
+
private stackSize = 50;
|
|
20
|
+
private dmp: any;
|
|
21
|
+
private wysiwyg: IUndo;
|
|
22
|
+
private ir: IUndo;
|
|
23
|
+
private sv: IUndo;
|
|
24
|
+
|
|
25
|
+
constructor() {
|
|
26
|
+
this.resetStack();
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
this.dmp = new DiffMatchPatch();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public clearStack(vditor: IVditor) {
|
|
32
|
+
this.resetStack();
|
|
33
|
+
this.resetIcon(vditor);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public resetIcon(vditor: IVditor) {
|
|
37
|
+
if (!vditor.toolbar) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (this[vditor.currentMode].undoStack.length > 1) {
|
|
42
|
+
enableToolbar(vditor.toolbar.elements, ["undo"]);
|
|
43
|
+
} else {
|
|
44
|
+
disableToolbar(vditor.toolbar.elements, ["undo"]);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (this[vditor.currentMode].redoStack.length !== 0) {
|
|
48
|
+
enableToolbar(vditor.toolbar.elements, ["redo"]);
|
|
49
|
+
} else {
|
|
50
|
+
disableToolbar(vditor.toolbar.elements, ["redo"]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public undo(vditor: IVditor) {
|
|
55
|
+
if (vditor[vditor.currentMode].element.getAttribute("contenteditable") === "false") {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (this[vditor.currentMode].undoStack.length < 2) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const state = this[vditor.currentMode].undoStack.pop();
|
|
62
|
+
if (!state) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
this[vditor.currentMode].redoStack.push(state);
|
|
66
|
+
this.renderDiff(state, vditor);
|
|
67
|
+
this[vditor.currentMode].hasUndo = true;
|
|
68
|
+
// undo 操作后,需要关闭 hint
|
|
69
|
+
hidePanel(vditor, ["hint"]);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public redo(vditor: IVditor) {
|
|
73
|
+
if (vditor[vditor.currentMode].element.getAttribute("contenteditable") === "false") {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const state = this[vditor.currentMode].redoStack.pop();
|
|
77
|
+
if (!state) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this[vditor.currentMode].undoStack.push(state);
|
|
81
|
+
this.renderDiff(state, vditor, true);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public recordFirstPosition(vditor: IVditor, event: KeyboardEvent) {
|
|
85
|
+
if (getSelection().rangeCount === 0) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (this[vditor.currentMode].undoStack.length !== 1 || this[vditor.currentMode].undoStack[0].length === 0 ||
|
|
89
|
+
this[vditor.currentMode].redoStack.length > 0) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (isFirefox() && event.key === "Backspace") {
|
|
93
|
+
// Firefox 第一次删除无效
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (isSafari()) {
|
|
97
|
+
// Safari keydown 在 input 之后,不需要重复记录历史
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const text = this.addCaret(vditor);
|
|
101
|
+
if (text.replace("<wbr>", "").replace(" vditor-ir__node--expand", "")
|
|
102
|
+
!== this[vditor.currentMode].undoStack[0][0].diffs[0][1].replace("<wbr>", "")) {
|
|
103
|
+
// 当还不没有存入 undo 栈时,按下 ctrl 后会覆盖 lastText
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
this[vditor.currentMode].undoStack[0][0].diffs[0][1] = text;
|
|
107
|
+
this[vditor.currentMode].lastText = text;
|
|
108
|
+
// 不能添加 setSelectionFocus(cloneRange); 否则 windows chrome 首次输入会烂
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public addToUndoStack(vditor: IVditor) {
|
|
112
|
+
// afterRenderEvent.ts 已经 debounce
|
|
113
|
+
const text = this.addCaret(vditor, true);
|
|
114
|
+
const diff = this.dmp.diff_main(text, this[vditor.currentMode].lastText, true);
|
|
115
|
+
const patchList = this.dmp.patch_make(text, this[vditor.currentMode].lastText, diff);
|
|
116
|
+
if (patchList.length === 0 && this[vditor.currentMode].undoStack.length > 0) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
this[vditor.currentMode].lastText = text;
|
|
120
|
+
this[vditor.currentMode].undoStack.push(patchList);
|
|
121
|
+
if (this[vditor.currentMode].undoStack.length > this.stackSize) {
|
|
122
|
+
this[vditor.currentMode].undoStack.shift();
|
|
123
|
+
}
|
|
124
|
+
if (this[vditor.currentMode].hasUndo) {
|
|
125
|
+
this[vditor.currentMode].redoStack = [];
|
|
126
|
+
this[vditor.currentMode].hasUndo = false;
|
|
127
|
+
disableToolbar(vditor.toolbar.elements, ["redo"]);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (this[vditor.currentMode].undoStack.length > 1) {
|
|
131
|
+
enableToolbar(vditor.toolbar.elements, ["undo"]);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private renderDiff(state: DiffMatchPatch.patch_obj[], vditor: IVditor, isRedo: boolean = false) {
|
|
136
|
+
let text;
|
|
137
|
+
if (isRedo) {
|
|
138
|
+
const redoPatchList = this.dmp.patch_deepCopy(state).reverse();
|
|
139
|
+
redoPatchList.forEach((patch: DiffMatchPatch.patch_obj) => {
|
|
140
|
+
patch.diffs.forEach((diff: any) => {
|
|
141
|
+
diff[0] = -diff[0];
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
text = this.dmp.patch_apply(redoPatchList, this[vditor.currentMode].lastText)[0];
|
|
145
|
+
} else {
|
|
146
|
+
text = this.dmp.patch_apply(state, this[vditor.currentMode].lastText)[0];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
this[vditor.currentMode].lastText = text;
|
|
150
|
+
vditor[vditor.currentMode].element.innerHTML = text;
|
|
151
|
+
if (vditor.currentMode !== "sv") {
|
|
152
|
+
vditor[vditor.currentMode].element.querySelectorAll(`.vditor-${vditor.currentMode}__preview`)
|
|
153
|
+
.forEach((blockElement: HTMLElement) => {
|
|
154
|
+
if (blockElement.parentElement.querySelector(".language-echarts")) {
|
|
155
|
+
if (vditor.currentMode === "ir") {
|
|
156
|
+
blockElement.parentElement.outerHTML = vditor.lute.SpinVditorIRDOM(blockElement.parentElement.outerHTML);
|
|
157
|
+
} else {
|
|
158
|
+
blockElement.parentElement.outerHTML = vditor.lute.SpinVditorDOM(blockElement.parentElement.outerHTML);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
vditor[vditor.currentMode].element.querySelectorAll(`.vditor-${vditor.currentMode}__preview[data-render='2']`)
|
|
163
|
+
.forEach((blockElement: HTMLElement) => {
|
|
164
|
+
processCodeRender(blockElement, vditor);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (!vditor[vditor.currentMode].element.querySelector("wbr")) {
|
|
169
|
+
// Safari 第一次输入没有光标,需手动定位到结尾
|
|
170
|
+
const range = getSelection().getRangeAt(0);
|
|
171
|
+
range.setEndBefore(vditor[vditor.currentMode].element);
|
|
172
|
+
range.collapse(false);
|
|
173
|
+
} else {
|
|
174
|
+
setRangeByWbr(
|
|
175
|
+
vditor[vditor.currentMode].element, vditor[vditor.currentMode].element.ownerDocument.createRange());
|
|
176
|
+
scrollCenter(vditor);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
renderToc(vditor);
|
|
180
|
+
|
|
181
|
+
execAfterRender(vditor, {
|
|
182
|
+
enableAddUndoStack: false,
|
|
183
|
+
enableHint: false,
|
|
184
|
+
enableInput: true,
|
|
185
|
+
});
|
|
186
|
+
highlightToolbar(vditor);
|
|
187
|
+
|
|
188
|
+
vditor[vditor.currentMode].element.querySelectorAll(`.vditor-${vditor.currentMode}__preview[data-render='2']`)
|
|
189
|
+
.forEach((item: HTMLElement) => {
|
|
190
|
+
processCodeRender(item, vditor);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
if (this[vditor.currentMode].undoStack.length > 1) {
|
|
194
|
+
enableToolbar(vditor.toolbar.elements, ["undo"]);
|
|
195
|
+
} else {
|
|
196
|
+
disableToolbar(vditor.toolbar.elements, ["undo"]);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (this[vditor.currentMode].redoStack.length !== 0) {
|
|
200
|
+
enableToolbar(vditor.toolbar.elements, ["redo"]);
|
|
201
|
+
} else {
|
|
202
|
+
disableToolbar(vditor.toolbar.elements, ["redo"]);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private resetStack() {
|
|
207
|
+
this.ir = {
|
|
208
|
+
hasUndo: false,
|
|
209
|
+
lastText: "",
|
|
210
|
+
redoStack: [],
|
|
211
|
+
undoStack: [],
|
|
212
|
+
};
|
|
213
|
+
this.sv = {
|
|
214
|
+
hasUndo: false,
|
|
215
|
+
lastText: "",
|
|
216
|
+
redoStack: [],
|
|
217
|
+
undoStack: [],
|
|
218
|
+
};
|
|
219
|
+
this.wysiwyg = {
|
|
220
|
+
hasUndo: false,
|
|
221
|
+
lastText: "",
|
|
222
|
+
redoStack: [],
|
|
223
|
+
undoStack: [],
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private addCaret(vditor: IVditor, setFocus = false) {
|
|
228
|
+
let cloneRange: Range;
|
|
229
|
+
if (getSelection().rangeCount !== 0 && !vditor[vditor.currentMode].element.querySelector("wbr")) {
|
|
230
|
+
const range = getSelection().getRangeAt(0);
|
|
231
|
+
if (vditor[vditor.currentMode].element.contains(range.startContainer)) {
|
|
232
|
+
cloneRange = range.cloneRange();
|
|
233
|
+
const wbrElement = document.createElement("span");
|
|
234
|
+
wbrElement.className = "vditor-wbr";
|
|
235
|
+
range.insertNode(wbrElement);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
// 移除数学公式、echart 渲染 https://github.com/Vanessa219/vditor/issues/1738
|
|
239
|
+
const cloneElement = vditor[vditor.currentMode].element.cloneNode(true) as HTMLElement;
|
|
240
|
+
cloneElement.querySelectorAll(`.vditor-${vditor.currentMode}__preview[data-render='1']`)
|
|
241
|
+
.forEach((item: HTMLElement) => {
|
|
242
|
+
if (!item.firstElementChild) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (item.firstElementChild.classList.contains("language-echarts") ||
|
|
246
|
+
item.firstElementChild.classList.contains("language-plantuml") ||
|
|
247
|
+
item.firstElementChild.classList.contains("language-mindmap")) {
|
|
248
|
+
item.firstElementChild.removeAttribute("_echarts_instance_");
|
|
249
|
+
item.firstElementChild.removeAttribute("data-processed");
|
|
250
|
+
item.firstElementChild.innerHTML = item.previousElementSibling.firstElementChild.innerHTML;
|
|
251
|
+
item.setAttribute("data-render", "2");
|
|
252
|
+
} else if (item.firstElementChild.classList.contains("language-math")) {
|
|
253
|
+
item.setAttribute("data-render", "2");
|
|
254
|
+
item.firstElementChild.textContent = item.firstElementChild.getAttribute("data-math");
|
|
255
|
+
item.firstElementChild.removeAttribute("data-math");
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
const text = cloneElement.innerHTML;
|
|
259
|
+
vditor[vditor.currentMode].element.querySelectorAll(".vditor-wbr").forEach((item) => {
|
|
260
|
+
item.remove();
|
|
261
|
+
// 使用 item.outerHTML = "" 会产生 https://github.com/Vanessa219/vditor/pull/686;
|
|
262
|
+
});
|
|
263
|
+
if (setFocus && cloneRange) {
|
|
264
|
+
setSelectionFocus(cloneRange);
|
|
265
|
+
}
|
|
266
|
+
return text.replace('<span class="vditor-wbr"></span>', "<wbr>");
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export {Undo};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {processDocLinkInWYSIWYG} from "../markdown/docLink";
|
|
1
2
|
import {getMarkdown} from "../markdown/getMarkdown";
|
|
2
3
|
import {accessLocalStorage} from "../util/compatibility";
|
|
3
4
|
|
|
@@ -37,5 +38,10 @@ export const afterRenderEvent = (vditor: IVditor, options = {
|
|
|
37
38
|
if (options.enableAddUndoStack) {
|
|
38
39
|
vditor.undo.addToUndoStack(vditor);
|
|
39
40
|
}
|
|
41
|
+
|
|
42
|
+
// 处理文档链接渲染
|
|
43
|
+
if (vditor.wysiwyg.element) {
|
|
44
|
+
processDocLinkInWYSIWYG(vditor.wysiwyg.element, vditor);
|
|
45
|
+
}
|
|
40
46
|
}, vditor.options.undoDelay);
|
|
41
47
|
};
|
package/src/ts/wysiwyg/index.ts
CHANGED
|
@@ -447,6 +447,20 @@ class WYSIWYG {
|
|
|
447
447
|
return;
|
|
448
448
|
}
|
|
449
449
|
|
|
450
|
+
// 处理文档链接点击
|
|
451
|
+
const docLink = hasClosestByClassName(event.target, "vditor-doclink");
|
|
452
|
+
if (docLink) {
|
|
453
|
+
const docId = docLink.getAttribute("data-doc-id");
|
|
454
|
+
const docText = docLink.getAttribute("data-doc-text") || '';
|
|
455
|
+
if (docId) {
|
|
456
|
+
import("../markdown/docLink").then((docLinkModule) => {
|
|
457
|
+
docLinkModule.handleDocLinkClick(docId, docText, vditor);
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
event.preventDefault();
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
|
|
450
464
|
const range = getEditorRange(vditor);
|
|
451
465
|
if (event.target.isEqualNode(this.element) && this.element.lastElementChild && range.collapsed) {
|
|
452
466
|
const lastRect = this.element.lastElementChild.getBoundingClientRect();
|