Skip to content

Commit

Permalink
♻️ editor -> sv
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 authored and stevapple committed Apr 8, 2020
1 parent 8f3f7be commit 82f1bb7
Show file tree
Hide file tree
Showing 19 changed files with 47 additions and 47 deletions.
18 changes: 9 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Vditor {
this.vditor.counter = new Counter(this.vditor);
}

this.vditor.editor = new Editor(this.vditor);
this.vditor.sv = new Editor(this.vditor);
this.vditor.undo = new Undo();
this.vditor.wysiwyg = new WYSIWYG(this.vditor);
this.vditor.wysiwygUndo = new WysiwygUndo();
Expand Down Expand Up @@ -112,7 +112,7 @@ class Vditor {

loadLuteJs(this.vditor);

if (this.vditor.editor && (this.vditor.toolbar.elements.preview || this.vditor.toolbar.elements.both)) {
if (this.vditor.sv && (this.vditor.toolbar.elements.preview || this.vditor.toolbar.elements.both)) {
this.vditor.preview = new Preview(this.vditor);
}

Expand All @@ -137,7 +137,7 @@ class Vditor {

public focus() {
if (this.vditor.currentMode === "sv") {
this.vditor.editor.element.focus();
this.vditor.sv.element.focus();
} else if (this.vditor.currentMode === "wysiwyg") {
this.vditor.wysiwyg.element.focus();
} else if (this.vditor.currentMode === "ir") {
Expand All @@ -147,7 +147,7 @@ class Vditor {

public blur() {
if (this.vditor.currentMode === "sv") {
this.vditor.editor.element.blur();
this.vditor.sv.element.blur();
} else if (this.vditor.currentMode === "wysiwyg") {
this.vditor.wysiwyg.element.blur();
} else if (this.vditor.currentMode === "ir") {
Expand All @@ -159,7 +159,7 @@ class Vditor {
disableToolbar(this.vditor.toolbar.elements, ["emoji", "headings", "bold", "italic", "strike", "link",
"list", "ordered-list", "check", "quote", "line", "code", "inline-code", "upload", "record", "table",
"undo", "redo", "wysiwyg"]);
this.vditor.editor.element.setAttribute("contenteditable", "false");
this.vditor.sv.element.setAttribute("contenteditable", "false");
this.vditor.wysiwyg.element.setAttribute("contenteditable", "false");
this.vditor.wysiwyg.element.setAttribute("contenteditable", "false");
}
Expand All @@ -168,7 +168,7 @@ class Vditor {
enableToolbar(this.vditor.toolbar.elements, ["emoji", "headings", "bold", "italic", "strike", "link",
"list", "ordered-list", "check", "quote", "line", "code", "inline-code", "upload", "record", "table", "wysiwyg"]);
this.vditor.undo.enableIcon(this.vditor);
this.vditor.editor.element.setAttribute("contenteditable", "true");
this.vditor.sv.element.setAttribute("contenteditable", "true");
this.vditor.wysiwygUndo.enableIcon(this.vditor);
this.vditor.wysiwyg.element.setAttribute("contenteditable", "true");
this.vditor.irUndo.enableIcon(this.vditor);
Expand All @@ -179,15 +179,15 @@ class Vditor {
if (this.vditor.currentMode !== "sv") {
console.error("所见即所得模式暂不支持该方法");
} else {
setSelectionByPosition(start, end, this.vditor.editor.element);
setSelectionByPosition(start, end, this.vditor.sv.element);
}
}

public getSelection() {
if (this.vditor.currentMode === "wysiwyg") {
return getSelectText(this.vditor.wysiwyg.element);
} else if (this.vditor.currentMode === "sv") {
return getSelectText(this.vditor.editor.element);
return getSelectText(this.vditor.sv.element);
} else if (this.vditor.currentMode === "ir") {
return getSelectText(this.vditor.ir.element);
}
Expand All @@ -203,7 +203,7 @@ class Vditor {
if (this.vditor.currentMode === "wysiwyg") {
return getCursorPosition(this.vditor.wysiwyg.element);
} else if (this.vditor.currentMode === "sv") {
return getCursorPosition(this.vditor.editor.element);
return getCursorPosition(this.vditor.sv.element);
} else if (this.vditor.currentMode === "ir") {
return getCursorPosition(this.vditor.ir.element);
}
Expand Down
8 changes: 4 additions & 4 deletions src/ts/hint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Hint {
return;
}
const position = getSelectPosition(vditor.currentMode === "wysiwyg" ?
vditor.wysiwyg.element : vditor.editor.element);
vditor.wysiwyg.element : vditor.sv.element);
let currentLineValue: string;
if (vditor.currentMode !== "sv") {
const range = getSelection().getRangeAt(0);
Expand All @@ -52,7 +52,7 @@ export class Hint {
this.timeId = window.setTimeout(() => {
this.genHTML(vditor.options.hint.at(key), key,
vditor.currentMode === "wysiwyg" ?
vditor.wysiwyg.element : vditor.editor.element, vditor);
vditor.wysiwyg.element : vditor.sv.element, vditor);
}, vditor.options.hint.delay);
}
if (!isAt) {
Expand All @@ -74,7 +74,7 @@ export class Hint {
}
});
this.genHTML(matchEmojiData, key, vditor.currentMode === "wysiwyg" ?
vditor.wysiwyg.element : vditor.editor.element, vditor);
vditor.wysiwyg.element : vditor.sv.element, vditor);
}
}
}
Expand Down Expand Up @@ -109,7 +109,7 @@ export class Hint {
processAfterRender(vditor);
}
} else {
const position = getSelectPosition(vditor.editor.element, range);
const position = getSelectPosition(vditor.sv.element, range);
const text = getMarkdown(vditor);
const preText = text.substring(0, text.substring(0, position.start).lastIndexOf(splitChar));
formatRender(vditor, preText + value + text.substring(position.start),
Expand Down
2 changes: 1 addition & 1 deletion src/ts/ir/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {expandMarker} from "./expandMarker";
import {input} from "./input";

class IR {
public element: HTMLElement;
public element: HTMLPreElement;
public composingLock: boolean = false;

constructor(vditor: IVditor) {
Expand Down
2 changes: 1 addition & 1 deletion src/ts/ir/processKeydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {
if (event.isComposing) {
return false;
}
}
};
4 changes: 2 additions & 2 deletions src/ts/resize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export class Resize {
vditorElement.style.height = Math.max(minHeight, height + (moveEvent.clientY - y)) + "px";
}
if (vditor.options.typewriterMode) {
vditor.editor.element.style.paddingBottom =
vditor.editor.element.parentElement.offsetHeight / 2 + "px";
vditor.sv.element.style.paddingBottom =
vditor.sv.element.parentElement.offsetHeight / 2 + "px";
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/ts/sv/formatRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export const formatRender = (vditor: IVditor, content: string, position?: { star

if (textList.length <= 2 && isEmpty) {
// 当内容等于空或 \n 时把编辑器内部元素置空,显示 placeholder 文字
vditor.editor.element.innerHTML = "";
vditor.sv.element.innerHTML = "";
} else {
// TODO: 使用虚拟 Dom
vditor.editor.element.innerHTML = html || newLine;
vditor.sv.element.innerHTML = html || newLine;
}

if (position) {
setSelectionByPosition(position.start, position.end, vditor.editor.element);
setSelectionByPosition(position.start, position.end, vditor.sv.element);
}

inputEvent(vditor, {
Expand Down
2 changes: 1 addition & 1 deletion src/ts/sv/html2md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const html2md = (vditor: IVditor, textHTML: string, textPlain?: string) =
return;
}
const original = responseJSON.data.originalURL;
setSelectionByInlineText(original, vditor.editor.element.childNodes);
setSelectionByInlineText(original, vditor.sv.element.childNodes);
insertText(vditor, responseJSON.data.url, "", true);
} else {
vditor.tip.show(responseJSON.msg);
Expand Down
4 changes: 2 additions & 2 deletions src/ts/sv/insertText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {formatRender} from "./formatRender";

export const insertText = (vditor: IVditor, prefix: string, suffix: string, replace: boolean = false,
toggle: boolean = false) => {
const range = getEditorRange(vditor.editor.element);
const range = getEditorRange(vditor.sv.element);

const position = getSelectPosition(vditor.editor.element, range);
const position = getSelectPosition(vditor.sv.element, range);
const content = getMarkdown(vditor);

// select none || select something and need replace
Expand Down
2 changes: 1 addition & 1 deletion src/ts/sv/processKeydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {insertText} from "./insertText";
export const processKeydown = (vditor: IVditor, event: KeyboardEvent) => {
vditor.undo.recordFirstPosition(vditor);

const editorElement = vditor.editor.element;
const editorElement = vditor.sv.element;
const position = getSelectPosition(editorElement);
const text = getMarkdown(vditor);
// tab and shift + tab
Expand Down
10 changes: 5 additions & 5 deletions src/ts/toolbar/EditMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const setEditMode = (vditor: IVditor, type: string, event?: Event) => {

if (type === "ir") {
vditor.toolbar.element.style.display = "none";
vditor.editor.element.style.display = "none";
vditor.sv.element.style.display = "none";
vditor.preview.element.style.display = "none";
vditor.wysiwyg.element.parentElement.style.display = "none";
vditor.ir.element.parentElement.style.display = "block";
Expand All @@ -43,7 +43,7 @@ export const setEditMode = (vditor: IVditor, type: string, event?: Event) => {
} else if (type === "wysiwyg") {
hideToolbar(vditor.toolbar.elements, ["format", "both", "preview"]);
vditor.toolbar.element.style.display = "block";
vditor.editor.element.style.display = "none";
vditor.sv.element.style.display = "none";
vditor.preview.element.style.display = "none";
vditor.wysiwyg.element.parentElement.style.display = "block";
vditor.ir.element.parentElement.style.display = "none";
Expand All @@ -62,18 +62,18 @@ export const setEditMode = (vditor: IVditor, type: string, event?: Event) => {
vditor.wysiwyg.element.parentElement.style.display = "none";
vditor.ir.element.parentElement.style.display = "none";
if (vditor.currentPreviewMode === "both") {
vditor.editor.element.style.display = "block";
vditor.sv.element.style.display = "block";
vditor.preview.element.style.display = "block";
} else if (vditor.currentPreviewMode === "preview") {
vditor.preview.element.style.display = "block";
} else if (vditor.currentPreviewMode === "editor") {
vditor.editor.element.style.display = "block";
vditor.sv.element.style.display = "block";
}

const wysiwygMD = getMarkdown(vditor);
vditor.currentMode = "sv";
formatRender(vditor, wysiwygMD, undefined);
vditor.editor.element.focus();
vditor.sv.element.focus();
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/ts/toolbar/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Format extends MenuItem {
this.element.children[0].innerHTML = menuItem.icon || formatSVG;
this.element.children[0].addEventListener(getEventName(), (event) => {
formatRender(vditor, vditor.lute.FormatMd( getMarkdown(vditor)),
getSelectPosition(vditor.editor.element, getSelection().getRangeAt(0)));
getSelectPosition(vditor.sv.element, getSelection().getRangeAt(0)));
event.preventDefault();
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/ts/toolbar/Record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Record extends MenuItem {
return;
}
event.preventDefault();
const editorElement = vditor.currentMode === "wysiwyg" ? vditor.wysiwyg.element : vditor.editor.element;
const editorElement = vditor.currentMode === "wysiwyg" ? vditor.wysiwyg.element : vditor.sv.element;
if (!mediaRecorder) {
navigator.mediaDevices.getUserMedia({audio: true}).then((mediaStream: MediaStream) => {
mediaRecorder = new RecordMedia(mediaStream);
Expand Down
4 changes: 2 additions & 2 deletions src/ts/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ interface IVditor {
element: HTMLElement
render(vditor: IVditor, value?: string): void,
};
editor?: {
sv?: {
element: HTMLPreElement,
};
counter?: {
Expand Down Expand Up @@ -351,7 +351,7 @@ interface IVditor {
composingLock: boolean,
};
ir?: {
element: HTMLElement,
element: HTMLPreElement,
composingLock: boolean,
};
}
2 changes: 1 addition & 1 deletion src/ts/ui/initUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const initUI = (vditor: IVditor) => {

contentElement.appendChild(vditor.wysiwyg.element.parentElement);

contentElement.appendChild(vditor.editor.element);
contentElement.appendChild(vditor.sv.element);

contentElement.appendChild(vditor.ir.element.parentElement);

Expand Down
8 changes: 4 additions & 4 deletions src/ts/ui/setPreviewMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const setPreviewMode = (mode: keyof IPreviewMode, vditor: IVditor) => {

switch (mode) {
case "both":
vditor.editor.element.style.display = "block";
vditor.sv.element.style.display = "block";
vditor.preview.element.style.display = "block";
vditor.preview.render(vditor);

Expand All @@ -18,18 +18,18 @@ export const setPreviewMode = (mode: keyof IPreviewMode, vditor: IVditor) => {

break;
case "editor":
vditor.editor.element.style.display = "block";
vditor.sv.element.style.display = "block";
vditor.preview.element.style.display = "none";

removeCurrentToolbar(vditor.toolbar.elements, ["preview"]);
removeCurrentToolbar(vditor.toolbar.elements, ["both"]);

break;
case "preview":
vditor.editor.element.style.display = "none";
vditor.sv.element.style.display = "none";
vditor.preview.element.style.display = "block";
vditor.preview.render(vditor);
vditor.editor.element.blur();
vditor.sv.element.blur();

setCurrentToolbar(vditor.toolbar.elements, ["preview"]);
removeCurrentToolbar(vditor.toolbar.elements, ["both"]);
Expand Down
10 changes: 5 additions & 5 deletions src/ts/undo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ class Undo {

public recordFirstPosition(vditor: IVditor) {
if (this.undoStack.length === 1) {
this.undoStack[0].end = getSelectPosition(vditor.editor.element).end;
this.undoStack[0].end = getSelectPosition(vditor.sv.element).end;
}
}

public undo(vditor: IVditor) {
if (vditor.editor.element.getAttribute("contenteditable") === "false") {
if (vditor.sv.element.getAttribute("contenteditable") === "false") {
return;
}
if (this.undoStack.length < 2) {
Expand All @@ -57,7 +57,7 @@ class Undo {
}

public redo(vditor: IVditor) {
if (vditor.editor.element.getAttribute("contenteditable") === "false") {
if (vditor.sv.element.getAttribute("contenteditable") === "false") {
return;
}
const state = this.redoStack.pop();
Expand All @@ -78,7 +78,7 @@ class Undo {
return;
}
this.lastText = text;
this.undoStack.push({patchList, end: getSelectPosition(vditor.editor.element).end});
this.undoStack.push({patchList, end: getSelectPosition(vditor.sv.element).end});
if (this.undoStack.length > this.stackSize) {
this.undoStack.shift();
}
Expand Down Expand Up @@ -127,7 +127,7 @@ class Undo {
enableInput: true,
});

scrollCenter(vditor.editor.element);
scrollCenter(vditor.sv.element);

if (this.undoStack.length > 1) {
enableToolbar(vditor.toolbar.elements, ["undo"]);
Expand Down
2 changes: 1 addition & 1 deletion src/ts/upload/getElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export const getElement = (vditor: IVditor) => {
case "wysiwyg":
return vditor.wysiwyg.element;
case "sv":
return vditor.editor.element;
return vditor.sv.element;
}
};
4 changes: 2 additions & 2 deletions src/ts/util/editorCommenEvent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {processKeydown as irProcessKeydown} from "../ir/processKeydown";
import {getSelectText} from "../sv/getSelectText";
import {insertText} from "../sv/insertText";
import {processKeydown as mdProcessKeydown} from "../sv/processKeydown";
import {processKeydown as irProcessKeydown} from "../ir/processKeydown";
import {setEditMode} from "../toolbar/EditMode";
import {hidePanel} from "../toolbar/setToolbar";
import {getCursorPosition} from "../util/selection";
Expand Down Expand Up @@ -195,7 +195,7 @@ export const selectEvent = (vditor: IVditor, editorElement: HTMLElement) => {
editorElement.addEventListener("selectstart", (event: IHTMLInputEvent) => {
editorElement.onmouseup = () => {
const element = vditor.currentMode === "wysiwyg" ?
vditor.wysiwyg.element : vditor.editor.element;
vditor.wysiwyg.element : vditor.sv.element;
const selectText = getSelectText(element);
vditor.options.select(selectText);
};
Expand Down
2 changes: 1 addition & 1 deletion src/ts/util/getMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {addP2Li} from "../wysiwyg/addP2Li";
export const getMarkdown = (vditor: IVditor) => {
if (vditor.currentMode === "sv") {
// last char must be a `\n`.
return code160to32(`${vditor.editor.element.textContent}\n`.replace(/\n\n$/, "\n"));
return code160to32(`${vditor.sv.element.textContent}\n`.replace(/\n\n$/, "\n"));
} else if (vditor.currentMode === "wysiwyg") {
const tempEditorElement = vditor.wysiwyg.element.cloneNode(true) as HTMLElement;
addP2Li(tempEditorElement);
Expand Down

0 comments on commit 82f1bb7

Please sign in to comment.