From 9fd067c6e33e0956d90988e19424b8bdefdcd586 Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 21 Mar 2017 00:35:51 +0300 Subject: [PATCH] Switch editor to add style mode when id=nonexistentstyle --- edit.js | 6 +++++- storage.js | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/edit.js b/edit.js index f948ecbb..a17fabdb 100644 --- a/edit.js +++ b/edit.js @@ -1087,7 +1087,11 @@ function init() { // This is an edit tE("heading", "editStyleHeading", null, false); getStylesSafe({id: params.id}).then(styles => { - const style = styles[0]; + let style = styles[0]; + if (!style) { + style = {id: null, sections: []}; + history.replaceState({}, document.title, location.pathname); + } styleId = style.id; setStyleMeta(style); onload = () => { diff --git a/storage.js b/storage.js index 1696ad1e..9d7fd5cd 100644 --- a/storage.js +++ b/storage.js @@ -182,9 +182,13 @@ function filterStyles(options = {}) { const styles = id == null ? (code ? cachedStyles.list : cachedStyles.noCode) - : [code ? cachedStyles.byId.get(id).style : cachedStyles.byId.get(id).noCode]; + : [(cachedStyles.byId.get(id) || {})[code ? 'style' : 'noCode']]; const filtered = asHash ? {} : []; - + if (!styles) { + // may happen when users [accidentally] reopen an old URL + // of edit.html with a non-existent style id parameter + return filtered; + } for (let i = 0, style; (style = styles[i]); i++) { if ((enabled == null || style.enabled == enabled) && (url == null || style.url == url)