1
0
Liyuan Li 5 жил өмнө
parent
commit
0aa0e6fa74

+ 7 - 1
CHANGELOG.md

@@ -56,11 +56,12 @@
 ### TODO
 
 * [open issues](https://github.com/Vanessa219/vditor/issues)
+* [324](https://github.com/Vanessa219/vditor/issues/324) 支持多款主题预览 `引入特性`
 
 ### v3.1.16 / 2020-04-2x
 
+* [316](https://github.com/Vanessa219/vditor/issues/316) 支持多端预览 `引入特性`
 * [337](https://github.com/Vanessa219/vditor/issues/337) insertValue 光标错误 `修复缺陷`
-* [324](https://github.com/Vanessa219/vditor/issues/324) 支持多款主题预览 `引入特性`
 
 ### v3.1.15 / 2020-04-21
 
@@ -118,6 +119,11 @@
   * `setTheme` 方法添加 `conentTheme`, `codeTheme` 参数
   * `options.preview.markdonw` 添加 `theme`, `setext` 配置
   * IPreviewOptions 添加 `after`,`lazyLoadImage`, `markdown.theme`,移除 `theme`
+  
+  * `options.preview.mode` 移除 `preview`
+  * `setPreviewMode` 移除 `preview`
+  * 移除预览快捷键
+  * 更新 html demo
 
 ### v3.0.12 / 2020-04-06
 

+ 1 - 1
README.md

@@ -369,7 +369,7 @@ xhr.send(JSON.stringify({url: src})); // src is the address of the image outside
 | enableCache() | Enable caching |
 | html2md(value: string) | HTML to md |
 | tip(text: string, time: number) | notification. time is 0 will always display |
-| setPreviewMode(mode: "both" \| "editor" \| "preview") | Set preview mode |
+| setPreviewMode(mode: "both" \| "editor") | Set preview mode |
 | setTheme(theme: "dark" \| "classic", contentTheme?: string, codeTheme?: string) | Set theme |
 | getCurrentMode(): string | Get the editor's current editing mode |
 

+ 1 - 1
demo/index.js

@@ -39,8 +39,8 @@ if (window.innerWidth < 768) {
       toolbar: [
         "fullscreen",
         "both",
-        "preview",
         "format",
+        "preview",
         "info",
         "help",
       ],

+ 52 - 7
demo/static.html

@@ -34,8 +34,8 @@
     <meta name="twitter:url" content="https://hacpai.com/tag/vditor"/>
     <meta property="og:image" content="https://cdn.jsdelivr.net/npm/vditor/dist/images/logo.png"/>
     <meta name="twitter:image" content="https://cdn.jsdelivr.net/npm/vditor/dist/images/logo.png"/>
-    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]2/dist/index.css"/>
-    <script src="https://cdn.jsdelivr.net/npm/[email protected]2/dist/index.min.js"></script>
+    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]6/dist/index.css"/>
+    <script src="https://cdn.jsdelivr.net/npm/[email protected]6/dist/index.min.js"></script>
 </head>
 <body>
 <h2><a href="https://hacpai.com/article/1549638745630?r=Vanessa" target="_blank">Doc</a></h2>
@@ -46,8 +46,8 @@
 </h2>
 <h2>
     Vditor for you
-    <button onclick="vditor.setTheme('dark')">Dark</button>
-    <button onclick="vditor.setTheme('light')">Light</button>
+    <button onclick="window.vditor.setTheme('dark', 'dark',  'native')">Dark</button>
+    <button onclick="window.vditor.setTheme('light', 'light', 'github')">Light</button>
 </h2>
 <div id="vditor">
 <h1>Vditor</h1>
@@ -57,7 +57,55 @@
 </ul>
 </div>
 <script>
+
+  let toolbar
+  if (window.innerWidth < 768) {
+    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",
+      "|",
+      "edit-mode",
+      "content-theme",
+      "code-theme",
+      {
+        name: "more",
+        toolbar: [
+          "fullscreen",
+          "both",
+          "format",
+          "preview",
+          "info",
+          "help",
+        ],
+      }]
+  }
+
   window.vditor = new Vditor('vditor', {
+    toolbar,
     debugger: true,
     typewriterMode: true,
     placeholder: 'placeholder',
@@ -65,9 +113,6 @@
       markdown: {
         toc: true,
       },
-      hljs: {
-        style: 'native',
-      },
     },
     toolbarConfig: {
       pin: true,

+ 9 - 0
src/assets/scss/_content.scss

@@ -93,6 +93,15 @@
       padding: 10px;
       margin: 0 auto;
     }
+
+    &--only {
+      position: absolute;
+      left: 0;
+      right: 0;
+      bottom: 0;
+      top: 0;
+      background-color: #fff;
+    }
   }
 
   &-devtools {

+ 1 - 5
src/assets/scss/_hint.scss

@@ -28,17 +28,13 @@
     cursor: pointer;
     white-space: nowrap;
 
-    &:last-child {
-      border-bottom: 0;
-    }
-
     &:focus {
       outline: none;
     }
   }
 
   &--current,
-  button:hover {
+  button:not(.vditor-menu--disabled):hover {
     background-color: var(--toolbar-background-color) !important;
     color: var(--toolbar-icon-hover-color) !important;
   }

+ 1 - 1
src/assets/scss/_toolbar.scss

@@ -86,7 +86,7 @@
 
     &--disabled {
       color: var(--second-color) !important;
-      cursor: not-allowed;
+      cursor: not-allowed !important;
     }
   }
 

+ 7 - 5
src/index.ts

@@ -67,7 +67,6 @@ class Vditor extends VditorMethod {
 
         this.vditor = {
             currentMode: mergedOptions.mode,
-            currentPreviewMode: mergedOptions.preview.mode,
             element: id,
             hint: new Hint(),
             lute: undefined,
@@ -99,9 +98,12 @@ class Vditor extends VditorMethod {
             this.vditor.upload = new Upload();
         }
 
-        // addScript("src/js/lute/lute.min.js", "vditorLuteScript").then(() => {
-        // addScript(`http://192.168.2.248:9090/lute.min.js?${new Date().getTime()}`, "vditorLuteScript").then(() => {
-        addScript(`${mergedOptions.cdn}/dist/js/lute/lute.min.js`, "vditorLuteScript").then(() => {
+        let lutePath = `http://192.168.2.248:9090/lute.min.js?${new Date().getTime()}`
+        // let lutePath = "src/js/lute/lute.min.js";
+        if (!options.debugger) {
+            lutePath = `${mergedOptions.cdn}/dist/js/lute/lute.min.js`;
+        }
+        addScript(lutePath, "vditorLuteScript").then(() => {
             this.vditor.lute = setLute({
                 autoSpace: this.vditor.options.preview.markdown.autoSpace,
                 chinesePunct: this.vditor.options.preview.markdown.chinesePunct,
@@ -279,7 +281,7 @@ class Vditor extends VditorMethod {
     }
 
     /** 设置预览模式 */
-    public setPreviewMode(mode: keyof IPreviewMode) {
+    public setPreviewMode(mode: "both" | "editor") {
         setPreviewMode(mode, this.vditor);
     }
 

+ 4 - 4
src/ts/constants.ts

@@ -7,10 +7,10 @@ export {_VDITOR_VERSION as VDITOR_VERSION};
 export abstract class Constants {
     public static readonly ZWSP: string = "\u200b";
     public static readonly CLASS_MENU_DISABLED: string = "vditor-menu--disabled";
-    public static readonly TOOLBARS: string[] = ["emoji", "headings", "bold", "italic", "strike", "link", "list",
-        "ordered-list", "outdent", "indent", "check", "line", "quote", "code", "inline-code", "upload", "record", "table"];
-    public static readonly CONTENT_THEME: string[] =  ["dark", "light"];
-    public static readonly CODE_THEME: string[] =  ["abap", "algol", "algol_nu", "arduino", "autumn", "borland", "bw", "colorful", "dracula",
+    public static readonly EDIT_TOOLBARS: string[] = ["emoji", "headings", "bold", "italic", "strike", "link", "list",
+        "ordered-list", "outdent", "indent", "check", "line", "quote", "code", "inline-code", "insert-after", "insert-before", "upload", "record", "table"];
+    public static readonly CONTENT_THEME: string[] = ["dark", "light"];
+    public static readonly CODE_THEME: string[] = ["abap", "algol", "algol_nu", "arduino", "autumn", "borland", "bw", "colorful", "dracula",
         "emacs", "friendly", "fruity", "github", "igor", "lovelace", "manni", "monokai", "monokailight", "murphy",
         "native", "paraiso-dark", "paraiso-light", "pastie", "perldoc", "pygments", "rainbow_dash", "rrt",
         "solarized-dark", "solarized-dark256", "solarized-light", "swapoff", "tango", "trac", "vim", "vs", "xcode"];

+ 2 - 2
src/ts/ir/highlightToolbar.ts

@@ -14,8 +14,8 @@ export const highlightToolbar = (vditor: IVditor) => {
             return;
         }
 
-        removeCurrentToolbar(vditor.toolbar.elements, Constants.TOOLBARS);
-        enableToolbar(vditor.toolbar.elements, Constants.TOOLBARS);
+        removeCurrentToolbar(vditor.toolbar.elements, Constants.EDIT_TOOLBARS);
+        enableToolbar(vditor.toolbar.elements, Constants.EDIT_TOOLBARS);
 
         const range = getEditorRange(vditor.ir.element);
         let typeElement = range.startContainer as HTMLElement;

+ 6 - 1
src/ts/toolbar/Both.ts

@@ -1,3 +1,4 @@
+import {Constants} from "../constants";
 import {setPreviewMode} from "../ui/setPreviewMode";
 import {getEventName} from "../util/compatibility";
 import {MenuItem} from "./MenuItem";
@@ -9,11 +10,15 @@ export class Both extends MenuItem {
             this.element.children[0].classList.add("vditor-menu--current");
         }
         this.element.children[0].addEventListener(getEventName(), (event) => {
+            const btnElement = this.element.firstElementChild;
+            if (btnElement.classList.contains(Constants.CLASS_MENU_DISABLED)) {
+                return;
+            }
             event.preventDefault();
             if (vditor.currentMode !== "sv") {
                 return;
             }
-            if (vditor.currentPreviewMode === "both") {
+            if (vditor.options.preview.mode === "both") {
                 setPreviewMode("editor", vditor);
             } else {
                 setPreviewMode("both", vditor);

+ 10 - 4
src/ts/toolbar/Devtools.ts

@@ -1,3 +1,4 @@
+import {Constants} from "../constants";
 import {setPadding} from "../ui/initUI";
 import {getEventName} from "../util/compatibility";
 import {MenuItem} from "./MenuItem";
@@ -5,14 +6,19 @@ import {MenuItem} from "./MenuItem";
 export class Devtools extends MenuItem {
     constructor(vditor: IVditor, menuItem: IMenuItem) {
         super(vditor, menuItem);
-        this.element.addEventListener(getEventName(), (event) => {
+        this.element.firstElementChild.addEventListener(getEventName(), (event) => {
+            const btnElement = this.element.firstElementChild;
+            if (btnElement.classList.contains(Constants.CLASS_MENU_DISABLED)) {
+                return;
+            }
+
             event.preventDefault();
-            if (this.element.children[0].classList.contains("vditor-menu--current")) {
-                this.element.children[0].classList.remove("vditor-menu--current");
+            if (btnElement.classList.contains("vditor-menu--current")) {
+                btnElement.classList.remove("vditor-menu--current");
                 vditor.devtools.element.style.display = "none";
                 setPadding(vditor);
             } else {
-                this.element.children[0].classList.add("vditor-menu--current");
+                btnElement.classList.add("vditor-menu--current");
                 vditor.devtools.element.style.display = "block";
                 setPadding(vditor);
                 vditor.devtools.renderEchart(vditor);

+ 8 - 10
src/ts/toolbar/EditMode.ts

@@ -36,19 +36,19 @@ export const setEditMode = (vditor: IVditor, type: string, event: Event | string
         vditor.devtools.renderEchart(vditor);
     }
     if (vditor.preview) {
-        if ((vditor.currentPreviewMode === "both" || vditor.currentPreviewMode === "preview") && type === "sv") {
+        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.TOOLBARS);
-    removeCurrentToolbar(vditor.toolbar.elements, Constants.TOOLBARS);
+    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, ["format", "both", "preview"]);
+        hideToolbar(vditor.toolbar.elements, ["format", "both"]);
         showToolbar(vditor.toolbar.elements, ["outdent", "indent", "outline", "insert-before", "insert-after"]);
         vditor.irUndo.resetIcon(vditor);
         vditor.sv.element.style.display = "none";
@@ -80,7 +80,7 @@ export const setEditMode = (vditor: IVditor, type: string, event: Event | string
             IRHighlightToolbar(vditor);
         }
     } else if (type === "wysiwyg") {
-        hideToolbar(vditor.toolbar.elements, ["format", "both", "preview"]);
+        hideToolbar(vditor.toolbar.elements, ["format", "both"]);
         showToolbar(vditor.toolbar.elements, ["outdent", "indent", "outline", "insert-before", "insert-after"]);
         vditor.wysiwygUndo.resetIcon(vditor);
         vditor.sv.element.style.display = "none";
@@ -104,16 +104,14 @@ export const setEditMode = (vditor: IVditor, type: string, event: Event | string
         }
         vditor.wysiwyg.popover.style.display = "none";
     } else if (type === "sv") {
-        showToolbar(vditor.toolbar.elements, ["format", "both", "preview"]);
+        showToolbar(vditor.toolbar.elements, ["format", "both"]);
         hideToolbar(vditor.toolbar.elements, ["outdent", "indent", "outline", "insert-before", "insert-after"]);
         vditor.undo.resetIcon(vditor);
         vditor.wysiwyg.element.parentElement.style.display = "none";
         vditor.ir.element.parentElement.style.display = "none";
-        if (vditor.currentPreviewMode === "both") {
+        if (vditor.options.preview.mode === "both") {
             vditor.sv.element.style.display = "block";
-        } else if (vditor.currentPreviewMode === "preview") {
-            vditor.sv.element.style.display = "none";
-        } else if (vditor.currentPreviewMode === "editor") {
+        } else if (vditor.options.preview.mode === "editor") {
             vditor.sv.element.style.display = "block";
         }
         vditor.currentMode = "sv";

+ 20 - 8
src/ts/toolbar/Preview.ts

@@ -1,26 +1,38 @@
-import {setPreviewMode} from "../ui/setPreviewMode";
+import {Constants} from "../constants";
 import {getEventName} from "../util/compatibility";
 import {MenuItem} from "./MenuItem";
+import {disableToolbar, enableToolbar, hidePanel} from "./setToolbar";
 
 export class Preview extends MenuItem {
     constructor(vditor: IVditor, menuItem: IMenuItem) {
         super(vditor, menuItem);
-        if (vditor.currentPreviewMode === "preview") {
-            this.element.children[0].classList.add("vditor-menu--current");
-        }
         this._bindEvent(vditor);
     }
 
     public _bindEvent(vditor: IVditor) {
         this.element.children[0].addEventListener(getEventName(), (event) => {
             event.preventDefault();
-            if (vditor.currentMode === "wysiwyg") {
+            const btnElement = this.element.firstElementChild;
+            if (btnElement.classList.contains(Constants.CLASS_MENU_DISABLED)) {
                 return;
             }
-            if (vditor.currentPreviewMode === "preview") {
-                setPreviewMode("editor", vditor);
+
+            const toolbars = Constants.EDIT_TOOLBARS.concat(["both", "format", "edit-mode", "outline", "devtools"]);
+            if (btnElement.classList.contains("vditor-menu--current")) {
+                vditor.preview.element.classList.remove("vditor-preview--only");
+                btnElement.classList.remove("vditor-menu--current");
+                if (vditor.currentMode !== "sv" ||
+                    (vditor.currentMode === "sv" && vditor.options.preview.mode !== "both")) {
+                    vditor.preview.element.style.display = "none";
+                }
+                enableToolbar(vditor.toolbar.elements, toolbars);
             } else {
-                setPreviewMode("preview", vditor);
+                disableToolbar(vditor.toolbar.elements, toolbars);
+                vditor.preview.element.style.display = "block";
+                vditor.preview.render(vditor);
+                vditor.preview.element.classList.add("vditor-preview--only");
+                btnElement.classList.add("vditor-menu--current");
+                hidePanel(vditor, ["subToolbar", "hint", "popover"]);
             }
         });
     }

+ 3 - 17
src/ts/ui/setPreviewMode.ts

@@ -1,11 +1,11 @@
 import {removeCurrentToolbar} from "../toolbar/setToolbar";
 import {setCurrentToolbar} from "../toolbar/setToolbar";
 
-export const setPreviewMode = (mode: keyof IPreviewMode, vditor: IVditor) => {
-    if (vditor.currentPreviewMode === mode) {
+export const setPreviewMode = (mode: "both" | "editor", vditor: IVditor) => {
+    if (vditor.options.preview.mode === mode) {
         return;
     }
-    vditor.currentPreviewMode = mode;
+    vditor.options.preview.mode = mode;
 
     switch (mode) {
         case "both":
@@ -14,26 +14,12 @@ export const setPreviewMode = (mode: keyof IPreviewMode, vditor: IVditor) => {
             vditor.preview.render(vditor);
 
             setCurrentToolbar(vditor.toolbar.elements, ["both"]);
-            removeCurrentToolbar(vditor.toolbar.elements, ["preview"]);
-
             break;
         case "editor":
             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.sv.element.style.display = "none";
-            vditor.preview.element.style.display = "block";
-            vditor.preview.render(vditor);
-            vditor.sv.element.blur();
-
-            setCurrentToolbar(vditor.toolbar.elements, ["preview"]);
             removeCurrentToolbar(vditor.toolbar.elements, ["both"]);
-
             break;
         default:
             break;

+ 1 - 2
src/ts/util/Options.ts

@@ -136,8 +136,8 @@ export class Options {
                     "fullscreen",
                     "edit-mode",
                     "both",
-                    "preview",
                     "format",
+                    "preview",
                     "outline",
                     "content-theme",
                     "code-theme",
@@ -318,7 +318,6 @@ export class Options {
         name: "both",
         tipPosition: "nw",
     }, {
-        hotkey: "⌘-⇧-P",
         icon: previewSVG,
         name: "preview",
         tipPosition: "nw",

+ 2 - 2
src/ts/wysiwyg/highlightToolbar.ts

@@ -34,8 +34,8 @@ export const highlightToolbar = (vditor: IVditor) => {
             return;
         }
 
-        removeCurrentToolbar(vditor.toolbar.elements, Constants.TOOLBARS);
-        enableToolbar(vditor.toolbar.elements, Constants.TOOLBARS);
+        removeCurrentToolbar(vditor.toolbar.elements, Constants.EDIT_TOOLBARS);
+        enableToolbar(vditor.toolbar.elements, Constants.EDIT_TOOLBARS);
 
         const range = getSelection().getRangeAt(0);
         let typeElement = range.startContainer as HTMLElement;

+ 1 - 8
types/index.d.ts

@@ -194,12 +194,6 @@ interface IMenuItem {
     click?(status?: boolean): void;
 }
 
-interface IPreviewMode {
-    both: string;
-    preview: string;
-    editor: string;
-}
-
 /** @link https://hacpai.com/article/1549638745630#options-preview-hljs */
 interface IHljs {
     /** 是否启用行号。默认值: false */
@@ -247,7 +241,7 @@ interface IPreview {
     /** 预览区域最大宽度。默认值: 768 */
     maxWidth?: number;
     /** 显示模式。默认值: 'both' */
-    mode?: keyof IPreviewMode;
+    mode?: "both" | "editor";
     /** md 解析请求 */
     url?: string;
     /** @link https://hacpai.com/article/1549638745630#options-preview-hljs */
@@ -397,7 +391,6 @@ interface IVditor {
     originalInnerHTML: string;
     lute: ILute;
     currentMode: "sv" | "wysiwyg" | "ir";
-    currentPreviewMode: keyof IPreviewMode;
     devtools?: {
         element: HTMLDivElement,
         renderEchart(vditor: IVditor): void,