From 32ac24caed0c7004ac1347274b478ecb51daa1c3 Mon Sep 17 00:00:00 2001 From: Liyuan Li Date: Sat, 23 May 2020 15:12:03 +0800 Subject: [PATCH] :bug: fix #429 --- CHANGELOG.md | 1 + src/ts/ir/process.ts | 4 ++-- src/ts/sv/inputEvent.ts | 3 ++- src/ts/ui/initUI.ts | 3 ++- src/ts/util/compatibility.ts | 8 ++++++++ src/ts/wysiwyg/afterRenderEvent.ts | 4 ++-- 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3c833f5a..e220325e0 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/src/ts/ir/process.ts b/src/ts/ir/process.ts index 37518ac58..3769bc2b3 100644 --- a/src/ts/ir/process.ts +++ b/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); diff --git a/src/ts/sv/inputEvent.ts b/src/ts/sv/inputEvent.ts index 33be4c8d0..8aa7e8fbd 100644 --- a/src/ts/sv/inputEvent.ts +++ b/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); diff --git a/src/ts/ui/initUI.ts b/src/ts/ui/initUI.ts index 0e6bf5352..09f6628ed 100644 --- a/src/ts/ui/initUI.ts +++ b/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; diff --git a/src/ts/util/compatibility.ts b/src/ts/util/compatibility.ts index 2ca8d4552..d52a8bbb7 100644 --- a/src/ts/util/compatibility.ts +++ b/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) { diff --git a/src/ts/wysiwyg/afterRenderEvent.ts b/src/ts/wysiwyg/afterRenderEvent.ts index 236f68bd1..ff61c50db 100644 --- a/src/ts/wysiwyg/afterRenderEvent.ts +++ b/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);