-
-
Notifications
You must be signed in to change notification settings - Fork 821
/
EditMode.ts
195 lines (172 loc) · 7.63 KB
/
EditMode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {Constants} from "../constants";
import {processAfterRender} from "../ir/process";
import {getMarkdown} from "../markdown/getMarkdown";
import {mathRender} from "../markdown/mathRender";
import {processAfterRender as processSVAfterRender, processSpinVditorSVDOM} from "../sv/process";
import {setPadding, setTypewriterPosition} from "../ui/initUI";
import {getEventName, updateHotkeyTip} from "../util/compatibility";
import {highlightToolbar} from "../util/highlightToolbar";
import {processCodeRender} from "../util/processCode";
import {renderToc} from "../util/toc";
import {renderDomByMd} from "../wysiwyg/renderDomByMd";
import {MenuItem} from "./MenuItem";
import {
disableToolbar,
enableToolbar,
hidePanel,
hideToolbar,
removeCurrentToolbar,
showToolbar, toggleSubMenu,
} from "./setToolbar";
import { combineFootnote } from "../util/combineFootnote"
export const setEditMode = (vditor: IVditor, type: string, event: Event | string) => {
let markdownText;
if (typeof event !== "string") {
hidePanel(vditor, ["subToolbar", "hint"]);
event.preventDefault();
markdownText = getMarkdown(vditor);
} else {
markdownText = event;
}
if (vditor.currentMode === type && typeof event !== "string") {
return;
}
if (vditor.devtools) {
vditor.devtools.renderEchart(vditor);
}
if (vditor.options.preview.mode === "both" && type === "sv") {
vditor.preview.element.style.display = "block";
} else {
vditor.preview.element.style.display = "none";
}
enableToolbar(vditor.toolbar.elements, Constants.EDIT_TOOLBARS);
removeCurrentToolbar(vditor.toolbar.elements, Constants.EDIT_TOOLBARS);
disableToolbar(vditor.toolbar.elements, ["outdent", "indent"]);
if (type === "ir") {
hideToolbar(vditor.toolbar.elements, ["both"]);
showToolbar(vditor.toolbar.elements, ["outdent", "indent", "outline", "insert-before", "insert-after"]);
vditor.sv.element.style.display = "none";
vditor.wysiwyg.element.parentElement.style.display = "none";
vditor.ir.element.parentElement.style.display = "block";
vditor.lute.SetVditorIR(true);
vditor.lute.SetVditorWYSIWYG(false);
vditor.lute.SetVditorSV(false);
vditor.currentMode = "ir";
vditor.ir.element.innerHTML = vditor.lute.Md2VditorIRDOM(markdownText);
processAfterRender(vditor, {
enableAddUndoStack: true,
enableHint: false,
enableInput: false,
});
setPadding(vditor);
vditor.ir.element.querySelectorAll(".vditor-ir__preview[data-render='2']").forEach((item: HTMLElement) => {
processCodeRender(item, vditor);
});
vditor.ir.element.querySelectorAll(".vditor-toc").forEach((item: HTMLElement) => {
mathRender(item, {
cdn: vditor.options.cdn,
math: vditor.options.preview.math,
});
});
} else if (type === "wysiwyg") {
hideToolbar(vditor.toolbar.elements, ["both"]);
showToolbar(vditor.toolbar.elements, ["outdent", "indent", "outline", "insert-before", "insert-after"]);
vditor.sv.element.style.display = "none";
vditor.wysiwyg.element.parentElement.style.display = "block";
vditor.ir.element.parentElement.style.display = "none";
vditor.lute.SetVditorIR(false);
vditor.lute.SetVditorWYSIWYG(true);
vditor.lute.SetVditorSV(false);
vditor.currentMode = "wysiwyg";
setPadding(vditor);
renderDomByMd(vditor, markdownText, {
enableAddUndoStack: true,
enableHint: false,
enableInput: false,
});
vditor.wysiwyg.element.querySelectorAll(".vditor-toc").forEach((item: HTMLElement) => {
mathRender(item, {
cdn: vditor.options.cdn,
math: vditor.options.preview.math,
});
});
vditor.wysiwyg.popover.style.display = "none";
} else if (type === "sv") {
showToolbar(vditor.toolbar.elements, ["both"]);
hideToolbar(vditor.toolbar.elements, ["outdent", "indent", "outline", "insert-before", "insert-after"]);
vditor.wysiwyg.element.parentElement.style.display = "none";
vditor.ir.element.parentElement.style.display = "none";
if (vditor.options.preview.mode === "both") {
vditor.sv.element.style.display = "block";
} else if (vditor.options.preview.mode === "editor") {
vditor.sv.element.style.display = "block";
}
vditor.lute.SetVditorIR(false);
vditor.lute.SetVditorWYSIWYG(false);
vditor.lute.SetVditorSV(true);
vditor.currentMode = "sv";
let svHTML = processSpinVditorSVDOM(markdownText, vditor);
if (svHTML === "<div data-block='0'></div>") {
// https://github.com/Vanessa219/vditor/issues/654 SV 模式 Placeholder 显示问题
svHTML = "";
}
vditor.sv.element.innerHTML = svHTML;
combineFootnote(vditor.sv.element)
processSVAfterRender(vditor, {
enableAddUndoStack: true,
enableHint: false,
enableInput: false,
});
setPadding(vditor);
}
vditor.undo.resetIcon(vditor);
if (typeof event !== "string") {
// 初始化不 focus
vditor[vditor.currentMode].element.focus();
highlightToolbar(vditor);
}
renderToc(vditor);
setTypewriterPosition(vditor);
if (vditor.toolbar.elements["edit-mode"]) {
vditor.toolbar.elements["edit-mode"].querySelectorAll("button").forEach((item) => {
item.classList.remove("vditor-menu--current");
});
vditor.toolbar.elements["edit-mode"].querySelector(`button[data-mode="${vditor.currentMode}"]`).classList.add("vditor-menu--current");
}
vditor.outline.toggle(vditor, vditor.currentMode !== "sv" && vditor.options.outline.enable, typeof event !== "string");
};
export class EditMode extends MenuItem {
public element: HTMLElement;
constructor(vditor: IVditor, menuItem: IMenuItem) {
super(vditor, menuItem);
const panelElement = document.createElement("div");
panelElement.className = `vditor-hint${menuItem.level === 2 ? "" : " vditor-panel--arrow"}`;
panelElement.innerHTML = `<button data-mode="wysiwyg">${window.VditorI18n.wysiwyg} <${updateHotkeyTip("⌥⌘7")}></button>
<button data-mode="ir">${window.VditorI18n.instantRendering} <${updateHotkeyTip("⌥⌘8")}></button>
<button data-mode="sv">${window.VditorI18n.splitView} <${updateHotkeyTip("⌥⌘9")}></button>`;
this.element.appendChild(panelElement);
this._bindEvent(vditor, panelElement, menuItem);
}
public _bindEvent(vditor: IVditor, panelElement: HTMLElement, menuItem: IMenuItem) {
const actionBtn = this.element.children[0] as HTMLElement;
toggleSubMenu(vditor, panelElement, actionBtn, menuItem.level);
panelElement.children.item(0).addEventListener(getEventName(), (event: Event) => {
// wysiwyg
setEditMode(vditor, "wysiwyg", event);
event.preventDefault();
event.stopPropagation();
});
panelElement.children.item(1).addEventListener(getEventName(), (event: Event) => {
// ir
setEditMode(vditor, "ir", event);
event.preventDefault();
event.stopPropagation();
});
panelElement.children.item(2).addEventListener(getEventName(), (event: Event) => {
// markdown
setEditMode(vditor, "sv", event);
event.preventDefault();
event.stopPropagation();
});
}
}