Liyuan Li 5 years ago
parent
commit
ff6d81346b
3 changed files with 12 additions and 7 deletions
  1. 2 2
      demo/index.js
  2. 6 2
      src/index.ts
  3. 4 3
      src/ts/util/Options.ts

+ 2 - 2
demo/index.js

@@ -11,8 +11,8 @@ window.vditor = new Vditor('vditor', {
       toc: true,
     },
     hljs: {
-      style: 'native'
-    }
+      style: 'native',
+    },
   },
   counter: 100,
   height: 500,

+ 6 - 2
src/index.ts

@@ -46,10 +46,14 @@ class Vditor extends VditorMethod {
         this.version = VDITOR_VERSION;
 
         if (typeof id === "string") {
-            id = document.getElementById(id);
-            if (!options.cache.id) {
+            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);

+ 4 - 3
src/ts/util/Options.ts

@@ -237,9 +237,6 @@ export class Options {
 
             if (this.options.cache) {
                 this.options.cache = Object.assign({}, this.defaultOptions.cache, this.options.cache);
-                if (this.options.cache && !this.options.cache.id) {
-                    throw new Error("need options.cache.id, see https://hacpai.com/article/1549638745630#options");
-                }
             }
 
             if (this.options.classes) {
@@ -281,6 +278,10 @@ export class Options {
             mergedOptions.toolbar = toolbar;
         }
 
+        if (mergedOptions.cache.enable && !mergedOptions.cache.id) {
+            throw new Error("need options.cache.id, see https://hacpai.com/article/1549638745630#options");
+        }
+
         return mergedOptions;
     }
 }