| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 | 
							- import VditorMethod from "./method";
 
- import {Constants, VDITOR_VERSION} from "./ts/constants";
 
- import {DevTools} from "./ts/devtools";
 
- import {Hint} from "./ts/hint/index";
 
- import {i18n} from "./ts/i18n";
 
- import {IR} from "./ts/ir";
 
- import {input as irInput} from "./ts/ir/input";
 
- import {processAfterRender} from "./ts/ir/process";
 
- import {getHTML} from "./ts/markdown/getHTML";
 
- import {getMarkdown} from "./ts/markdown/getMarkdown";
 
- import {setLute} from "./ts/markdown/setLute";
 
- import {Outline} from "./ts/outline";
 
- import {Preview} from "./ts/preview/index";
 
- import {Resize} from "./ts/resize/index";
 
- import {Editor} from "./ts/sv/index";
 
- import {inputEvent} from "./ts/sv/inputEvent";
 
- import {processAfterRender as processSVAfterRender} from "./ts/sv/process";
 
- import {Tip} from "./ts/tip";
 
- import {Toolbar} from "./ts/toolbar/index";
 
- import {disableToolbar, hidePanel} from "./ts/toolbar/setToolbar";
 
- import {enableToolbar} from "./ts/toolbar/setToolbar";
 
- import {initUI} from "./ts/ui/initUI";
 
- import {setCodeTheme} from "./ts/ui/setCodeTheme";
 
- import {setContentTheme} from "./ts/ui/setContentTheme";
 
- import {setPreviewMode} from "./ts/ui/setPreviewMode";
 
- import {setTheme} from "./ts/ui/setTheme";
 
- import {Undo} from "./ts/undo";
 
- import {Upload} from "./ts/upload/index";
 
- import {addScript, addScriptSync} from "./ts/util/addScript";
 
- import {getSelectText} from "./ts/util/getSelectText";
 
- import {Options} from "./ts/util/Options";
 
- import {processCodeRender} from "./ts/util/processCode";
 
- import {getCursorPosition, getEditorRange} from "./ts/util/selection";
 
- import {WYSIWYG} from "./ts/wysiwyg";
 
- import {afterRenderEvent} from "./ts/wysiwyg/afterRenderEvent";
 
- import {input} from "./ts/wysiwyg/input";
 
- import {renderDomByMd} from "./ts/wysiwyg/renderDomByMd";
 
- class Vditor extends VditorMethod {
 
-     public readonly version: string;
 
-     public vditor: IVditor;
 
-     /**
 
-      * @param id 要挂载 Vditor 的元素或者元素 ID。
 
-      * @param options Vditor 参数
 
-      */
 
-     constructor(id: string | HTMLElement, options?: IOptions) {
 
-         super();
 
-         this.version = VDITOR_VERSION;
 
-         if (typeof id === "string") {
 
-             if (!options) {
 
-                 options = {
 
-                     cache: {
 
-                         id: `vditor${id}`,
 
-                     },
 
-                 };
 
-             } else if (!options.cache) {
 
-                 options.cache = {id: `vditor${id}`};
 
-             } else if (!options.cache.id) {
 
-                 options.cache.id = `vditor${id}`;
 
-             }
 
-             id = document.getElementById(id);
 
-         }
 
-         const getOptions = new Options(options);
 
-         const mergedOptions = getOptions.merge();
 
-         if (!["en_US", "ja_JP", "ko_KR", "zh_CN"].includes(mergedOptions.lang)) {
 
-             throw new Error("options.lang error, see https://ld246.com/article/1549638745630#options");
 
-         }
 
-         this.vditor = {
 
-             currentMode: mergedOptions.mode,
 
-             element: id,
 
-             hint: new Hint(mergedOptions.hint.extend),
 
-             lute: undefined,
 
-             options: mergedOptions,
 
-             originalInnerHTML: id.innerHTML,
 
-             outline: new Outline(i18n[mergedOptions.lang].outline),
 
-             tip: new Tip(),
 
-         };
 
-         this.vditor.sv = new Editor(this.vditor);
 
-         this.vditor.undo = new Undo();
 
-         this.vditor.wysiwyg = new WYSIWYG(this.vditor);
 
-         this.vditor.ir = new IR(this.vditor);
 
-         this.vditor.toolbar = new Toolbar(this.vditor);
 
-         if (mergedOptions.resize.enable) {
 
-             this.vditor.resize = new Resize(this.vditor);
 
-         }
 
-         if (this.vditor.toolbar.elements.devtools) {
 
-             this.vditor.devtools = new DevTools();
 
-         }
 
-         if (mergedOptions.upload.url || mergedOptions.upload.handler) {
 
-             this.vditor.upload = new Upload();
 
-         }
 
-         addScript(options._lutePath || `${mergedOptions.cdn}/dist/js/lute/lute.min.js`, "vditorLuteScript")
 
-             .then(() => {
 
-                 this.vditor.lute = setLute({
 
-                     autoSpace: this.vditor.options.preview.markdown.autoSpace,
 
-                     chinesePunct: this.vditor.options.preview.markdown.chinesePunct,
 
-                     codeBlockPreview: this.vditor.options.preview.markdown.codeBlockPreview,
 
-                     emojiSite: this.vditor.options.hint.emojiPath,
 
-                     emojis: this.vditor.options.hint.emoji,
 
-                     fixTermTypo: this.vditor.options.preview.markdown.fixTermTypo,
 
-                     footnotes: this.vditor.options.preview.markdown.footnotes,
 
-                     headingAnchor: false,
 
-                     inlineMathDigit: this.vditor.options.preview.math.inlineDigit,
 
-                     linkBase: this.vditor.options.preview.markdown.linkBase,
 
-                     linkPrefix: this.vditor.options.preview.markdown.linkPrefix,
 
-                     listStyle: this.vditor.options.preview.markdown.listStyle,
 
-                     mark: this.vditor.options.preview.markdown.mark,
 
-                     mathBlockPreview: this.vditor.options.preview.markdown.mathBlockPreview,
 
-                     paragraphBeginningSpace: this.vditor.options.preview.markdown.paragraphBeginningSpace,
 
-                     sanitize: this.vditor.options.preview.markdown.sanitize,
 
-                     toc: this.vditor.options.preview.markdown.toc,
 
-                 });
 
-                 this.vditor.preview = new Preview(this.vditor);
 
-                 initUI(this.vditor);
 
-                 if (mergedOptions.after) {
 
-                     mergedOptions.after();
 
-                 }
 
-                 if (mergedOptions.icon) {
 
-                     // 防止初始化 2 个编辑器时加载 2 次
 
-                     addScriptSync(`${mergedOptions.cdn}/dist/js/icons/${mergedOptions.icon}.js`, "vditorIconScript");
 
-                 }
 
-             });
 
-     }
 
-     /** 设置主题 */
 
-     public setTheme(theme: "dark" | "classic", contentTheme?: string, codeTheme?: string, contentThemePath?: string) {
 
-         this.vditor.options.theme = theme;
 
-         setTheme(this.vditor);
 
-         if (contentTheme) {
 
-             this.vditor.options.preview.theme.current = contentTheme;
 
-             setContentTheme(contentTheme, contentThemePath || this.vditor.options.preview.theme.path);
 
-         }
 
-         if (codeTheme) {
 
-             this.vditor.options.preview.hljs.style = codeTheme;
 
-             setCodeTheme(codeTheme, this.vditor.options.cdn);
 
-         }
 
-     }
 
-     /** 获取 Markdown 内容 */
 
-     public getValue() {
 
-         return getMarkdown(this.vditor);
 
-     }
 
-     /** 获取编辑器当前编辑模式 */
 
-     public getCurrentMode() {
 
-         return this.vditor.currentMode;
 
-     }
 
-     /** 聚焦到编辑器 */
 
-     public focus() {
 
-         if (this.vditor.currentMode === "sv") {
 
-             this.vditor.sv.element.focus();
 
-         } else if (this.vditor.currentMode === "wysiwyg") {
 
-             this.vditor.wysiwyg.element.focus();
 
-         } else if (this.vditor.currentMode === "ir") {
 
-             this.vditor.ir.element.focus();
 
-         }
 
-     }
 
-     /** 让编辑器失焦 */
 
-     public blur() {
 
-         if (this.vditor.currentMode === "sv") {
 
-             this.vditor.sv.element.blur();
 
-         } else if (this.vditor.currentMode === "wysiwyg") {
 
-             this.vditor.wysiwyg.element.blur();
 
-         } else if (this.vditor.currentMode === "ir") {
 
-             this.vditor.ir.element.blur();
 
-         }
 
-     }
 
-     /** 禁用编辑器 */
 
-     public disabled() {
 
-         hidePanel(this.vditor, ["subToolbar", "hint", "popover"]);
 
-         disableToolbar(this.vditor.toolbar.elements, Constants.EDIT_TOOLBARS.concat(["undo", "redo", "fullscreen",
 
-             "edit-mode"]));
 
-         this.vditor[this.vditor.currentMode].element.setAttribute("contenteditable", "false");
 
-     }
 
-     /** 解除编辑器禁用 */
 
-     public enable() {
 
-         enableToolbar(this.vditor.toolbar.elements, Constants.EDIT_TOOLBARS.concat(["undo", "redo", "fullscreen",
 
-             "edit-mode"]));
 
-         this.vditor.undo.resetIcon(this.vditor);
 
-         this.vditor[this.vditor.currentMode].element.setAttribute("contenteditable", "true");
 
-     }
 
-     /** 返回选中的字符串 */
 
-     public getSelection() {
 
-         if (this.vditor.currentMode === "wysiwyg") {
 
-             return getSelectText(this.vditor.wysiwyg.element);
 
-         } else if (this.vditor.currentMode === "sv") {
 
-             return getSelectText(this.vditor.sv.element);
 
-         } else if (this.vditor.currentMode === "ir") {
 
-             return getSelectText(this.vditor.ir.element);
 
-         }
 
-     }
 
-     /** 设置预览区域内容 */
 
-     public renderPreview(value?: string) {
 
-         this.vditor.preview.render(this.vditor, value);
 
-     }
 
-     /** 获取焦点位置 */
 
-     public getCursorPosition() {
 
-         return getCursorPosition(this.vditor[this.vditor.currentMode].element);
 
-     }
 
-     /** 上传是否还在进行中 */
 
-     public isUploading() {
 
-         return this.vditor.upload.isUploading;
 
-     }
 
-     /** 清除缓存 */
 
-     public clearCache() {
 
-         localStorage.removeItem(this.vditor.options.cache.id);
 
-     }
 
-     /** 禁用缓存 */
 
-     public disabledCache() {
 
-         this.vditor.options.cache.enable = false;
 
-     }
 
-     /** 启用缓存 */
 
-     public enableCache() {
 
-         if (!this.vditor.options.cache.id) {
 
-             throw new Error("need options.cache.id, see https://ld246.com/article/1549638745630#options");
 
-             return;
 
-         }
 
-         this.vditor.options.cache.enable = true;
 
-     }
 
-     /** HTML 转 md */
 
-     public html2md(value: string) {
 
-         return this.vditor.lute.HTML2Md(value);
 
-     }
 
-     /** 获取 HTML */
 
-     public getHTML() {
 
-         return getHTML(this.vditor);
 
-     }
 
-     /** 消息提示。time 为 0 将一直显示 */
 
-     public tip(text: string, time?: number) {
 
-         this.vditor.tip.show(text, time);
 
-     }
 
-     /** 设置预览模式 */
 
-     public setPreviewMode(mode: "both" | "editor") {
 
-         setPreviewMode(mode, this.vditor);
 
-     }
 
-     /** 删除选中内容 */
 
-     public deleteValue() {
 
-         if (window.getSelection().isCollapsed) {
 
-             return;
 
-         }
 
-         document.execCommand("delete", false);
 
-     }
 
-     /** 更新选中内容 */
 
-     public updateValue(value: string) {
 
-         document.execCommand("insertHTML", false, value);
 
-     }
 
-     /** 在焦点处插入内容,并默认进行 Markdown 渲染 */
 
-     public insertValue(value: string, render = true) {
 
-         const range = getEditorRange(this.vditor[this.vditor.currentMode].element);
 
-         range.collapse(true);
 
-         // https://github.com/Vanessa219/vditor/issues/716 需使用 insertText,否则需要重写方法,不能使用 execCommand
 
-         if (this.vditor.currentMode === "sv") {
 
-             this.vditor.sv.preventInput = true;
 
-             document.execCommand("insertText", false, value);
 
-             if (render) {
 
-                 inputEvent(this.vditor);
 
-             }
 
-         } else if (this.vditor.currentMode === "wysiwyg") {
 
-             this.vditor.wysiwyg.preventInput = true;
 
-             document.execCommand("insertText", false, value);
 
-             if (render) {
 
-                 input(this.vditor, getSelection().getRangeAt(0));
 
-             }
 
-         } else if (this.vditor.currentMode === "ir") {
 
-             this.vditor.ir.preventInput = true;
 
-             document.execCommand("insertText", false, value);
 
-             if (render) {
 
-                 irInput(this.vditor, getSelection().getRangeAt(0), true);
 
-             }
 
-         }
 
-     }
 
-     /** 设置编辑器内容 */
 
-     public setValue(markdown: string, clearStack = false) {
 
-         if (this.vditor.currentMode === "sv") {
 
-             this.vditor.sv.element.innerHTML = this.vditor.lute.SpinVditorSVDOM(markdown);
 
-             processSVAfterRender(this.vditor, {
 
-                 enableAddUndoStack: clearStack,
 
-                 enableHint: false,
 
-                 enableInput: false,
 
-             });
 
-         } else if (this.vditor.currentMode === "wysiwyg") {
 
-             renderDomByMd(this.vditor, markdown, {
 
-                 enableAddUndoStack: clearStack,
 
-                 enableHint: false,
 
-                 enableInput: false,
 
-             });
 
-         } else {
 
-             this.vditor.ir.element.innerHTML = this.vditor.lute.Md2VditorIRDOM(markdown);
 
-             this.vditor.ir.element.querySelectorAll(".vditor-ir__preview[data-render='2']").forEach(
 
-                 (item: HTMLElement) => {
 
-                     processCodeRender(item, this.vditor);
 
-                 });
 
-             processAfterRender(this.vditor, {
 
-                 enableAddUndoStack: clearStack,
 
-                 enableHint: false,
 
-                 enableInput: false,
 
-             });
 
-         }
 
-         this.vditor.outline.render(this.vditor);
 
-         if (!markdown) {
 
-             hidePanel(this.vditor, ["emoji", "headings", "submenu", "hint"]);
 
-             if (this.vditor.wysiwyg.popover) {
 
-                 this.vditor.wysiwyg.popover.style.display = "none";
 
-             }
 
-             this.clearCache();
 
-         }
 
-         if (clearStack) {
 
-             this.clearStack();
 
-         }
 
-     }
 
-     /** 清空 undo & redo 栈 */
 
-     public clearStack() {
 
-         this.vditor.undo.clearStack(this.vditor);
 
-         this.vditor.undo.addToUndoStack(this.vditor);
 
-     }
 
-     /** 销毁编辑器 */
 
-     public destroy() {
 
-         this.vditor.element.innerHTML = this.vditor.originalInnerHTML;
 
-         this.vditor.element.classList.remove("vditor");
 
-         this.vditor.element.removeAttribute("style");
 
-         document.getElementById("vditorIconScript").remove();
 
-         this.clearCache();
 
-     }
 
-     /** 获取评论 ID */
 
-     public getCommentIds() {
 
-         if (this.vditor.currentMode !== "wysiwyg") {
 
-             return [];
 
-         }
 
-         return this.vditor.wysiwyg.getComments(this.vditor);
 
-     }
 
-     /** 高亮评论 */
 
-     public hlCommentIds(ids: string[]) {
 
-         if (this.vditor.currentMode !== "wysiwyg") {
 
-             return;
 
-         }
 
-         const hlItem = (item: Element) => {
 
-             item.classList.remove("vditor-comment--hover");
 
-             ids.forEach((id) => {
 
-                 if (item.getAttribute("data-cmtids").indexOf(id) > -1) {
 
-                     item.classList.add("vditor-comment--hover");
 
-                 }
 
-             });
 
-         };
 
-         this.vditor.wysiwyg.element.querySelectorAll(".vditor-comment").forEach((item) => {
 
-             hlItem(item);
 
-         });
 
-         if (this.vditor.preview.element.style.display !== "none") {
 
-             this.vditor.preview.element.querySelectorAll(".vditor-comment").forEach((item) => {
 
-                 hlItem(item);
 
-             });
 
-         }
 
-     }
 
-     /** 取消评论高亮 */
 
-     public unHlCommentIds(ids: string[]) {
 
-         if (this.vditor.currentMode !== "wysiwyg") {
 
-             return;
 
-         }
 
-         const unHlItem = (item: Element) => {
 
-             ids.forEach((id) => {
 
-                 if (item.getAttribute("data-cmtids").indexOf(id) > -1) {
 
-                     item.classList.remove("vditor-comment--hover");
 
-                 }
 
-             });
 
-         };
 
-         this.vditor.wysiwyg.element.querySelectorAll(".vditor-comment").forEach((item) => {
 
-             unHlItem(item);
 
-         });
 
-         if (this.vditor.preview.element.style.display !== "none") {
 
-             this.vditor.preview.element.querySelectorAll(".vditor-comment").forEach((item) => {
 
-                 unHlItem(item);
 
-             });
 
-         }
 
-     }
 
-     /** 删除评论 */
 
-     public removeCommentIds(removeIds: string[]) {
 
-         if (this.vditor.currentMode !== "wysiwyg") {
 
-             return;
 
-         }
 
-         const removeItem = (item: Element, removeId: string) => {
 
-             const ids = item.getAttribute("data-cmtids").split(" ");
 
-             ids.find((id, index) => {
 
-                 if (id === removeId) {
 
-                     ids.splice(index, 1);
 
-                     return true;
 
-                 }
 
-             });
 
-             if (ids.length === 0) {
 
-                 item.outerHTML = item.innerHTML;
 
-                 getEditorRange(this.vditor.element).collapse(true);
 
-             } else {
 
-                 item.setAttribute("data-cmtids", ids.join(" "));
 
-             }
 
-         };
 
-         removeIds.forEach((removeId) => {
 
-             this.vditor.wysiwyg.element.querySelectorAll(".vditor-comment").forEach((item) => {
 
-                 removeItem(item, removeId);
 
-             });
 
-             if (this.vditor.preview.element.style.display !== "none") {
 
-                 this.vditor.preview.element.querySelectorAll(".vditor-comment").forEach((item) => {
 
-                     removeItem(item, removeId);
 
-                 });
 
-             }
 
-         });
 
-         afterRenderEvent(this.vditor, {
 
-             enableAddUndoStack: true,
 
-             enableHint: false,
 
-             enableInput: false,
 
-         });
 
-     }
 
- }
 
- export default Vditor;
 
 
  |