import {Constants} from "../constants"; import {merge} from "./merge"; export class Options { public options: IOptions; private defaultOptions: IOptions = { after: undefined, cache: { enable: true, }, cdn: Constants.CDN, classes: { preview: "", }, comment: { enable: false, }, counter: { enable: false, type: "markdown", }, debugger: false, fullscreen: { index: 90, }, height: "auto", hint: { delay: 200, emoji: { "+1": "👍", "-1": "👎", "confused": "😕", "eyes": "👀️", "heart": "❤️", "rocket": "🚀️", "smile": "😄", "tada": "🎉️", }, emojiPath: `${Constants.CDN}/dist/images/emoji`, extend: [], parse: true, }, icon: "ant", lang: "zh_CN", mode: "ir", outline: { enable: false, position: "left", }, placeholder: "", preview: { actions: ["desktop", "tablet", "mobile", "mp-wechat", "zhihu"], delay: 1000, hljs: Constants.HLJS_OPTIONS, markdown: Constants.MARKDOWN_OPTIONS, math: Constants.MATH_OPTIONS, maxWidth: 800, mode: "both", theme: Constants.THEME_OPTIONS, }, resize: { enable: false, position: "bottom", }, theme: "classic", bubbleToolbar: [ // 气泡工具栏 "bold", "italic", "strike", ], toolbar: [ "emoji", "headings", "bold", "italic", "strike", "link", "|", "list", "ordered-list", "check", "outdent", "indent", "|", "quote", "line", "code", "inline-code", "insert-before", "insert-after", "|", "upload", "record", "table", "|", "undo", "redo", "|", "fullscreen", "edit-mode", { name: "more", toolbar: [ "both", "code-theme", "content-theme", "export", "outline", "preview", "devtools", "info", "help", ], }], toolbarConfig: { hide: false, pin: false, bubbleHide: false, // 气泡工具栏是否显示 }, typewriterMode: false, undoDelay: 800, upload: { extraData: {}, fieldName: "file[]", filename: (name: string) => name.replace(/\W/g, ""), linkToImgUrl: "", max: 10 * 1024 * 1024, multiple: true, url: "", withCredentials: false, }, value: "", width: "auto", }; constructor(options: IOptions) { this.options = options; } public merge(): IOptions { if (this.options) { if (this.options.toolbar) { this.options.toolbar = this.mergeToolbar(this.options.toolbar); } else { this.options.toolbar = this.mergeToolbar(this.defaultOptions.toolbar); } if (this.options.bubbleToolbar) { this.options.bubbleToolbar = this.mergeToolbar(this.options.bubbleToolbar); } else { this.options.bubbleToolbar = this.mergeToolbar(this.defaultOptions.bubbleToolbar) } if (this.options.preview?.theme?.list) { this.defaultOptions.preview.theme.list = this.options.preview.theme.list; } if (this.options.hint?.emoji) { this.defaultOptions.hint.emoji = this.options.hint.emoji; } if (this.options.comment) { this.defaultOptions.comment = this.options.comment; } } const mergedOptions = merge(this.defaultOptions, this.options); if (mergedOptions.cache.enable && !mergedOptions.cache.id) { throw new Error("need options.cache.id, see https://ld246.com/article/1549638745630#options"); } return mergedOptions; } private mergeToolbar(toolbar: Array) { const toolbarItem = [{ icon: '', name: "export", tipPosition: "ne", }, { hotkey: "⌘E", icon: '', name: "emoji", tipPosition: "ne", }, { hotkey: "⌘H", icon: '', name: "headings", tipPosition: "ne", }, { hotkey: "⌘B", icon: '', name: "bold", prefix: "**", suffix: "**", tipPosition: "ne", }, { hotkey: "⌘I", icon: '', name: "italic", prefix: "*", suffix: "*", tipPosition: "ne", }, { hotkey: "⌘D", icon: '', name: "strike", prefix: "~~", suffix: "~~", tipPosition: "ne", }, { hotkey: "⌘K", icon: '', name: "link", prefix: "[", suffix: "](https://)", tipPosition: "n", }, { name: "|", }, { hotkey: "⌘L", icon: '', name: "list", prefix: "* ", tipPosition: "n", }, { hotkey: "⌘O", icon: '', name: "ordered-list", prefix: "1. ", tipPosition: "n", }, { hotkey: "⌘J", icon: '', name: "check", prefix: "* [ ] ", tipPosition: "n", }, { hotkey: "⇧⌘I", icon: '', name: "outdent", tipPosition: "n", }, { hotkey: "⇧⌘O", icon: '', name: "indent", tipPosition: "n", }, { name: "|", }, { hotkey: "⌘;", icon: '', name: "quote", prefix: "> ", tipPosition: "n", }, { hotkey: "⇧⌘H", icon: '', name: "line", prefix: "---", tipPosition: "n", }, { hotkey: "⌘U", icon: '', name: "code", prefix: "```", suffix: "\n```", tipPosition: "n", }, { hotkey: "⌘G", icon: '', name: "inline-code", prefix: "`", suffix: "`", tipPosition: "n", }, { hotkey: "⇧⌘B", icon: '', name: "insert-before", tipPosition: "n", }, { hotkey: "⇧⌘E", icon: '', name: "insert-after", tipPosition: "n", }, { name: "|", }, { icon: '', name: "upload", tipPosition: "n", }, { icon: '', name: "record", tipPosition: "n", }, { hotkey: "⌘M", icon: '', name: "table", prefix: "| col1", suffix: " | col2 | col3 |\n| --- | --- | --- |\n| | | |\n| | | |", tipPosition: "n", }, { name: "|", }, { hotkey: "⌘Z", icon: '', name: "undo", tipPosition: "nw", }, { hotkey: "⌘Y", icon: '', name: "redo", tipPosition: "nw", }, { name: "|", }, { icon: '', name: "more", tipPosition: "e", }, { hotkey: "⌘'", icon: '', name: "fullscreen", tipPosition: "nw", }, { icon: '', name: "edit-mode", tipPosition: "nw", }, { hotkey: "⌘P", icon: '', name: "both", tipPosition: "nw", }, { icon: '', name: "preview", tipPosition: "nw", }, { icon: '', name: "outline", tipPosition: "nw", }, { icon: '', name: "content-theme", tipPosition: "nw", }, { icon: '', name: "code-theme", tipPosition: "nw", }, { icon: '', name: "devtools", tipPosition: "nw", }, { icon: '', name: "info", tipPosition: "nw", }, { icon: '', name: "help", tipPosition: "nw", }, { name: "br", }]; const toolbarResult: IMenuItem[] = []; toolbar.forEach((menuItem: IMenuItem) => { let currentMenuItem = menuItem; toolbarItem.forEach((defaultMenuItem: IMenuItem) => { if (typeof menuItem === "string" && defaultMenuItem.name === menuItem) { currentMenuItem = defaultMenuItem; } if (typeof menuItem === "object" && defaultMenuItem.name === menuItem.name) { currentMenuItem = Object.assign({}, defaultMenuItem, menuItem); } }); if (menuItem.toolbar) { currentMenuItem.toolbar = this.mergeToolbar(menuItem.toolbar); } toolbarResult.push(currentMenuItem); }); return toolbarResult; } }