From 8c7f7b81f88e078f0f48ec5602e256cdec84c9c1 Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 20 Mar 2017 04:58:55 +0300 Subject: [PATCH] Don't recreate editors after save --- edit.js | 26 +++++++++++++++++--------- storage.js | 6 ++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/edit.js b/edit.js index faffc91a..fcf4f0cb 100644 --- a/edit.js +++ b/edit.js @@ -1088,15 +1088,23 @@ function init() { // This is an edit tE("heading", "editStyleHeading", null, false); getStylesSafe({id: params.id}).then(styles => { - styleId = styles[0].id; - initWithStyle(styles[0]); + const style = styles[0]; + styleId = style.id; + initWithStyle({style}); }); } -function initWithStyle(style) { +function initWithStyle({style, codeIsUpdated}) { document.getElementById("name").value = style.name; document.getElementById("enabled").checked = style.enabled; document.getElementById("url").href = style.url; + + if (codeIsUpdated === false) { + setCleanGlobal(); + updateTitle(); + return; + } + // if this was done in response to an update, we need to clear existing sections getSections().forEach(function(div) { div.remove(); }); var queue = style.sections.length ? style.sections.slice() : [{code: ""}]; @@ -1247,14 +1255,14 @@ function save() { } var name = document.getElementById("name").value; var enabled = document.getElementById("enabled").checked; - var request = { - method: "saveStyle", + saveStyle({ id: styleId, name: name, enabled: enabled, + reason: 'editSave', sections: getSectionsHashes() - }; - chrome.runtime.sendMessage(request, saveComplete); + }) + .then(saveComplete); } function getSectionsHashes() { @@ -1621,8 +1629,8 @@ function getParams() { chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { switch (request.method) { case "styleUpdated": - if (styleId && styleId == request.id) { - initWithStyle(request.style); + if (styleId && styleId == request.style.id && request.reason != 'editSave') { + initWithStyle(request); } break; case "styleDeleted": diff --git a/storage.js b/storage.js index 5c627d18..1696ad1e 100644 --- a/storage.js +++ b/storage.js @@ -213,7 +213,9 @@ function saveStyle(style, {notify = true} = {}) { const tx = db.transaction(['styles'], 'readwrite'); const os = tx.objectStore('styles'); + const reason = style.reason; delete style.method; + delete style.reason; // Update if (style.id) { @@ -227,7 +229,7 @@ function saveStyle(style, {notify = true} = {}) { style.id = style.id || eventPut.target.result; invalidateCache(notify, {updated: style}); if (notify) { - notifyAllTabs({method: 'styleUpdated', style, codeIsUpdated}); + notifyAllTabs({method: 'styleUpdated', style, codeIsUpdated, reason}); } resolve(style); }; @@ -250,7 +252,7 @@ function saveStyle(style, {notify = true} = {}) { // Give it the ID that was generated style.id = event.target.result; invalidateCache(true, {added: style}); - notifyAllTabs({method: 'styleAdded', style}); + notifyAllTabs({method: 'styleAdded', style, reason}); resolve(style); }; });