Liyuan Li пре 5 година
родитељ
комит
32ac24caed

+ 1 - 0
CHANGELOG.md

@@ -66,6 +66,7 @@
 
 ### v3.2.10 / 2020-05-xx
 
+* [429](https://github.com/Vanessa219/vditor/issues/429) localStorage被禁用时,报错 `修复缺陷`
 * [427](https://github.com/Vanessa219/vditor/issues/427) headings 大小调整 `改进功能`
 
 ### v3.2.9 / 2020-05-22

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

@@ -1,6 +1,6 @@
 import {Constants} from "../constants";
 import {getMarkdown} from "../markdown/getMarkdown";
-import {isSafari} from "../util/compatibility";
+import {accessLocalStorage, isSafari} from "../util/compatibility";
 import {listToggle, renderToc} from "../util/fixBrowserBehavior";
 import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName, hasClosestByMatchTag} from "../util/hasClosest";
 import {getEditorRange, getSelectPosition, setRangeByWbr} from "../util/selection";
@@ -57,7 +57,7 @@ export const processAfterRender = (vditor: IVditor, options = {
             vditor.counter.render(vditor, text);
         }
 
-        if (vditor.options.cache.enable) {
+        if (vditor.options.cache.enable && accessLocalStorage()) {
             localStorage.setItem(vditor.options.cache.id, text);
             if (vditor.options.cache.after) {
                 vditor.options.cache.after(text);

+ 2 - 1
src/ts/sv/inputEvent.ts

@@ -1,4 +1,5 @@
 import {getMarkdown} from "../markdown/getMarkdown";
+import {accessLocalStorage} from "../util/compatibility";
 
 export const inputEvent = (vditor: IVditor, options = {
     enableAddUndoStack: true,
@@ -15,7 +16,7 @@ export const inputEvent = (vditor: IVditor, options = {
     if (options.enableHint) {
         vditor.hint.render(vditor);
     }
-    if (vditor.options.cache.enable) {
+    if (vditor.options.cache.enable && accessLocalStorage()) {
         localStorage.setItem(vditor.options.cache.id, text);
         if (vditor.options.cache.after) {
             vditor.options.cache.after(text);

+ 2 - 1
src/ts/ui/initUI.ts

@@ -1,6 +1,7 @@
 import {Constants} from "../constants";
 import {html2md} from "../sv/html2md";
 import {setEditMode} from "../toolbar/EditMode";
+import {accessLocalStorage} from "../util/compatibility";
 import {setContentTheme} from "./setContentTheme";
 import {setTheme} from "./setTheme";
 
@@ -141,7 +142,7 @@ const afterRender = (vditor: IVditor, contentElement: HTMLElement) => {
     });
 
     // set default value
-    let initValue = localStorage.getItem(vditor.options.cache.id);
+    let initValue = accessLocalStorage() && localStorage.getItem(vditor.options.cache.id);
     if (!vditor.options.cache.enable || !initValue) {
         if (vditor.options.value) {
             initValue = vditor.options.value;

+ 8 - 0
src/ts/util/compatibility.ts

@@ -6,6 +6,14 @@ export const isFirefox = () => {
     return navigator.userAgent.toLowerCase().indexOf("firefox") > -1;
 };
 
+export const accessLocalStorage = () => {
+    try {
+       return typeof localStorage !== "undefined";
+    } catch (e) {
+        return false;
+    }
+};
+
 // 用户 iPhone 点击延迟/需要双击的处理
 export const getEventName = () => {
     if (navigator.userAgent.indexOf("iPhone") > -1) {

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

@@ -1,5 +1,5 @@
 import {getMarkdown} from "../markdown/getMarkdown";
-import {isSafari} from "../util/compatibility";
+import {accessLocalStorage, isSafari} from "../util/compatibility";
 
 export const afterRenderEvent = (vditor: IVditor, options = {
     enableAddUndoStack: true,
@@ -24,7 +24,7 @@ export const afterRenderEvent = (vditor: IVditor, options = {
             vditor.counter.render(vditor, text);
         }
 
-        if (vditor.options.cache.enable) {
+        if (vditor.options.cache.enable && accessLocalStorage()) {
             localStorage.setItem(vditor.options.cache.id, text);
             if (vditor.options.cache.after) {
                 vditor.options.cache.after(text);