| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- import insertAfterSVG from "../../assets/icons/after.svg";
- import outlinerSVG from "../../assets/icons/align-center.svg";
- import insertBeforeSVG from "../../assets/icons/before.svg";
- import boldSVG from "../../assets/icons/bold.svg";
- import bothSVG from "../../assets/icons/both.svg";
- import bugSVG from "../../assets/icons/bug.svg";
- import checkSVG from "../../assets/icons/check.svg";
- import codeThemeSVG from "../../assets/icons/code-theme.svg";
- import codeSVG from "../../assets/icons/code.svg";
- import editSVG from "../../assets/icons/edit.svg";
- import emojiSVG from "../../assets/icons/emoji.svg";
- import fullscreenSVG from "../../assets/icons/fullscreen.svg";
- import headingsSVG from "../../assets/icons/headings.svg";
- import helpSVG from "../../assets/icons/help.svg";
- import indentSVG from "../../assets/icons/indent.svg";
- import infoSVG from "../../assets/icons/info.svg";
- import inlineCodeSVG from "../../assets/icons/inline-code.svg";
- import italicSVG from "../../assets/icons/italic.svg";
- import lineSVG from "../../assets/icons/line.svg";
- import linkSVG from "../../assets/icons/link.svg";
- import listSVG from "../../assets/icons/list.svg";
- import moreSVG from "../../assets/icons/more.svg";
- import orderedListVG from "../../assets/icons/ordered-list.svg";
- import formatSVG from "../../assets/icons/outdent.svg";
- import outdentSVG from "../../assets/icons/outdent.svg";
- import previewSVG from "../../assets/icons/preview.svg";
- import quoteSVG from "../../assets/icons/quote.svg";
- import recordSVG from "../../assets/icons/record.svg";
- import redoSVG from "../../assets/icons/redo.svg";
- import strikekSVG from "../../assets/icons/strike.svg";
- import tableSVG from "../../assets/icons/table.svg";
- import contentThemeSVG from "../../assets/icons/theme.svg";
- import undoSVG from "../../assets/icons/undo.svg";
- import uploadSVG from "../../assets/icons/upload.svg";
- import {VDITOR_VERSION} from "../constants";
- export class Options {
- public options: IOptions;
- private defaultOptions: IOptions = {
- after: undefined,
- cache: {
- enable: true,
- },
- cdn: `https://cdn.jsdelivr.net/npm/vditor@${VDITOR_VERSION}`,
- classes: {
- preview: "",
- },
- counter: {
- enable: false,
- type: "markdown",
- },
- debugger: false,
- height: "auto",
- hint: {
- delay: 200,
- emoji: {
- "+1": "👍",
- "-1": "👎",
- "confused": "😕",
- "eyes": "👀️",
- "heart": "❤️",
- "rocket": "🚀️",
- "smile": "😄",
- "tada": "🎉️",
- },
- emojiPath: `https://cdn.jsdelivr.net/npm/vditor@${VDITOR_VERSION}/dist/images/emoji`,
- },
- keymap: {
- deleteLine: "⌘-Backspace",
- duplicate: "⌘-D",
- },
- lang: "zh_CN",
- mode: "wysiwyg",
- placeholder: "",
- preview: {
- delay: 1000,
- hljs: {
- enable: true,
- lineNumber: false,
- style: "github",
- },
- markdown: {
- autoSpace: false,
- chinesePunct: false,
- codeBlockPreview: true,
- fixTermTypo: false,
- footnotes: true,
- setext: true,
- theme: "light",
- toc: false,
- },
- math: {
- engine: "KaTeX",
- inlineDigit: false,
- macros: {},
- },
- maxWidth: 800,
- mode: "both",
- },
- resize: {
- enable: false,
- position: "bottom",
- },
- theme: "classic",
- 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",
- "|",
- {
- name: "more",
- toolbar: [
- "fullscreen",
- "edit-mode",
- "both",
- "format",
- "preview",
- "outline",
- "content-theme",
- "code-theme",
- "devtools",
- "info",
- "help",
- ],
- }],
- toolbarConfig: {
- hide: false,
- pin: false,
- },
- typewriterMode: false,
- upload: {
- filename: (name: string) => name.replace(/\W/g, ""),
- linkToImgUrl: "",
- max: 10 * 1024 * 1024,
- url: "",
- withCredentials: false,
- },
- value: "",
- width: "auto",
- };
- private toolbarItem = [{
- hotkey: "⌘-E",
- icon: emojiSVG,
- name: "emoji",
- tipPosition: "ne",
- }, {
- hotkey: "⌘-H",
- icon: headingsSVG,
- name: "headings",
- tipPosition: "ne",
- }, {
- hotkey: "⌘-B",
- icon: boldSVG,
- name: "bold",
- prefix: "**",
- suffix: "**",
- tipPosition: "ne",
- }, {
- hotkey: "⌘-I",
- icon: italicSVG,
- name: "italic",
- prefix: "*",
- suffix: "*",
- tipPosition: "ne",
- }, {
- hotkey: "⌘-S",
- icon: strikekSVG,
- name: "strike",
- prefix: "~~",
- suffix: "~~",
- tipPosition: "ne",
- }, {
- hotkey: "⌘-K",
- icon: linkSVG,
- name: "link",
- prefix: "[",
- suffix: "](https://)",
- tipPosition: "n",
- }, {
- name: "|",
- }, {
- hotkey: "⌘-L",
- icon: listSVG,
- name: "list",
- prefix: "* ",
- tipPosition: "n",
- }, {
- hotkey: "⌘-O",
- icon: orderedListVG,
- name: "ordered-list",
- prefix: "1. ",
- tipPosition: "n",
- }, {
- hotkey: "⌘-J",
- icon: checkSVG,
- name: "check",
- prefix: "* [ ] ",
- tipPosition: "n",
- }, {
- hotkey: "⌘-⇧-I",
- icon: outdentSVG,
- name: "outdent",
- tipPosition: "n",
- }, {
- hotkey: "⌘-⇧-O",
- icon: indentSVG,
- name: "indent",
- tipPosition: "n",
- }, {
- name: "|",
- }, {
- hotkey: "⌘-;",
- icon: quoteSVG,
- name: "quote",
- prefix: "> ",
- tipPosition: "n",
- }, {
- hotkey: "⌘-⇧-H",
- icon: lineSVG,
- name: "line",
- prefix: "---",
- tipPosition: "n",
- }, {
- hotkey: "⌘-U",
- icon: codeSVG,
- name: "code",
- prefix: "```\n",
- suffix: "\n```",
- tipPosition: "n",
- }, {
- hotkey: "⌘-G",
- icon: inlineCodeSVG,
- name: "inline-code",
- prefix: "`",
- suffix: "`",
- tipPosition: "n",
- }, {
- hotkey: "⌘-⇧-B",
- icon: insertBeforeSVG,
- name: "insert-before",
- tipPosition: "n",
- }, {
- hotkey: "⌘-⇧-E",
- icon: insertAfterSVG,
- name: "insert-after",
- tipPosition: "n",
- }, {
- name: "|",
- }, {
- icon: uploadSVG,
- name: "upload",
- tipPosition: "n",
- }, {
- icon: recordSVG,
- name: "record",
- tipPosition: "n",
- }, {
- hotkey: "⌘-M",
- icon: tableSVG,
- name: "table",
- prefix: "| col1",
- suffix: " | col2 | col3 |\n| --- | --- | --- |\n| | | |\n| | | |",
- tipPosition: "n",
- }, {
- name: "|",
- }, {
- hotkey: "⌘-Z",
- icon: undoSVG,
- name: "undo",
- tipPosition: "nw",
- }, {
- hotkey: "⌘-Y",
- icon: redoSVG,
- name: "redo",
- tipPosition: "nw",
- }, {
- name: "|",
- }, {
- icon: moreSVG,
- name: "more",
- tipPosition: "e",
- }, {
- hotkey: "⌘-'",
- icon: fullscreenSVG,
- name: "fullscreen",
- tipPosition: "nw",
- }, {
- icon: editSVG,
- name: "edit-mode",
- tipPosition: "nw",
- }, {
- hotkey: "⌘-P",
- icon: bothSVG,
- name: "both",
- tipPosition: "nw",
- }, {
- icon: previewSVG,
- name: "preview",
- tipPosition: "nw",
- }, {
- hotkey: "⌘-⇧-F",
- icon: formatSVG,
- name: "format",
- tipPosition: "nw",
- }, {
- icon: outlinerSVG,
- name: "outline",
- tipPosition: "nw",
- }, {
- icon: contentThemeSVG,
- name: "content-theme",
- tipPosition: "nw",
- }, {
- icon: codeThemeSVG,
- name: "code-theme",
- tipPosition: "nw",
- }, {
- icon: bugSVG,
- name: "devtools",
- tipPosition: "nw",
- }, {
- icon: infoSVG,
- name: "info",
- tipPosition: "nw",
- }, {
- icon: helpSVG,
- name: "help",
- tipPosition: "nw",
- }, {
- name: "br",
- }];
- constructor(options: IOptions) {
- this.options = options;
- }
- public merge(): IOptions {
- if (this.options) {
- if (this.options.upload) {
- this.options.upload = Object.assign({}, this.defaultOptions.upload, this.options.upload);
- }
- if (this.options.cache) {
- this.options.cache = Object.assign({}, this.defaultOptions.cache, this.options.cache);
- }
- if (this.options.classes) {
- this.options.classes = Object.assign({}, this.defaultOptions.classes, this.options.classes);
- }
- if (this.options.keymap) {
- this.options.keymap = Object.assign({}, this.defaultOptions.keymap, this.options.keymap);
- }
- if (this.options.preview) {
- this.options.preview = Object.assign({}, this.defaultOptions.preview, this.options.preview);
- if (this.options.preview.hljs) {
- this.options.preview.hljs =
- Object.assign({}, this.defaultOptions.preview.hljs, this.options.preview.hljs);
- }
- if (this.options.preview.math) {
- this.options.preview.math =
- Object.assign({}, this.defaultOptions.preview.math, this.options.preview.math);
- }
- if (this.options.preview.markdown) {
- if (!this.options.preview.markdown.theme && this.options.theme && this.options.theme === "dark") {
- this.options.preview.markdown.theme = "dark";
- }
- this.options.preview.markdown =
- Object.assign({}, this.defaultOptions.preview.markdown, this.options.preview.markdown);
- }
- }
- if (this.options.hint) {
- this.options.hint = Object.assign({}, this.defaultOptions.hint, this.options.hint);
- }
- if (this.options.resize) {
- this.options.resize = Object.assign({}, this.defaultOptions.resize, this.options.resize);
- }
- if (this.options.counter) {
- this.options.counter = Object.assign({}, this.defaultOptions.counter, this.options.counter);
- }
- if (this.options.toolbarConfig) {
- this.options.toolbarConfig =
- Object.assign({}, this.defaultOptions.toolbarConfig, this.options.toolbarConfig);
- }
- if (this.options.toolbar) {
- this.options.toolbar = this.mergeToolbar(this.options.toolbar);
- } else {
- this.options.toolbar = this.mergeToolbar(this.defaultOptions.toolbar);
- }
- }
- const mergedOptions = Object.assign({}, this.defaultOptions, this.options);
- if (mergedOptions.cache.enable && !mergedOptions.cache.id) {
- throw new Error("need options.cache.id, see https://hacpai.com/article/1549638745630#options");
- }
- return mergedOptions;
- }
- private mergeToolbar(toolbar: Array<string | IMenuItem>) {
- const toolbarResult: IMenuItem[] = [];
- toolbar.forEach((menuItem: IMenuItem) => {
- let currentMenuItem = menuItem;
- this.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;
- }
- }
|