From 8bcd7f60c5c9c0e3aabc0d19e4d5293dc2c0b22c Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 25 Mar 2017 05:35:54 +0300 Subject: [PATCH] Restore implicit handleUpdate in saveStyle --- backup/fileSaveLoad.js | 13 ++++++---- manage.js | 55 +++++++++++++++++++++--------------------- storage.js | 16 +++++++++++- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/backup/fileSaveLoad.js b/backup/fileSaveLoad.js index 57f69e30..7d7528ae 100644 --- a/backup/fileSaveLoad.js +++ b/backup/fileSaveLoad.js @@ -85,8 +85,10 @@ function importFromString(jsonString) { stats.unchanged.ids.push(oldStyle.id); continue; } - saveStyle(item, {notify: false}).then(style => { - handleUpdate(style, {reason: 'import'}); + saveStyle(Object.assign(item, { + reason: 'import', + notify: false, + })).then(style => { setTimeout(proceed, 0, resolve); if (!oldStyle) { stats.added.names.push(style.name); @@ -160,11 +162,12 @@ function importFromString(jsonString) { } const id = newIds[index++]; deleteStyle(id, {notify: false}).then(id => { - handleDelete(id); const oldStyle = oldStylesById.get(id); if (oldStyle) { - saveStyle(Object.assign(oldStyle, {reason: 'undoImport'}), {notify: false}) - .then(handleUpdate) + saveStyle(Object.assign(oldStyle, { + reason: 'undoImport', + notify: false, + })) .then(() => setTimeout(undoNextId, 0, resolve)); } else { setTimeout(undoNextId, 0, resolve); diff --git a/manage.js b/manage.js index 16ab39a8..3852d937 100644 --- a/manage.js +++ b/manage.js @@ -82,7 +82,7 @@ function showStyles(styles = []) { const t0 = performance.now(); while (index < sorted.length && (shouldRenderAll || performance.now() - t0 < 10)) { renderBin.appendChild(createStyleElement(sorted[index++].style)); - } + } if ($('#search').value) { // re-apply filtering on history Back searchStyles(true, renderBin); @@ -122,8 +122,8 @@ function createStyleElement(style) { const homepage = template.styleHomepage.cloneNode(true); homepage.href = style.url; styleName.appendChild(document.createTextNode(' ')); - styleName.appendChild(homepage); - } + styleName.appendChild(homepage); + } const targets = new Map(TARGET_TYPES.map(t => [t, new Set()])); const decorations = { @@ -138,9 +138,9 @@ function createStyleElement(style) { (decorations[name + 'Before'] || '') + targetValue.trim() + (decorations[name + 'After'] || '')); - } - } - } + } + } + } const appliesTo = $('.applies-to', entry); appliesTo.firstElementChild.textContent = TARGET_LABEL; const targetsList = Array.prototype.concat.apply([], @@ -154,18 +154,18 @@ function createStyleElement(style) { for (let target of targetsList) { if (index > 0) { container.appendChild(template.appliesToSeparator.cloneNode(true)); - } + } if (++index == TARGET_LIMIT) { container = appliesTo.appendChild(template.extraAppliesTo.cloneNode(true)); - } + } const item = template.appliesToTarget.cloneNode(true); item.textContent = target; container.appendChild(item); - } } + } - const editLink = $('.style-edit-link', entry); - editLink.href = editLink.getAttribute('href') + style.id; + const editLink = $('.style-edit-link', entry); + editLink.href = editLink.getAttribute('href') + style.id; editLink.onclick = EntryOnClick.edit; $('.enable', entry).onclick = EntryOnClick.toggle; @@ -215,22 +215,21 @@ class EntryOnClick { } static update(event) { - const element = getClickedStyleElement(event); - const updatedCode = element.updatedCode; + const updatedCode = getClickedStyleElement(event).updatedCode; // update everything but name - delete updatedCode.name; - updatedCode.id = element.styleId; - updatedCode.reason = 'update'; - saveStyle(updatedCode) - .then(style => handleUpdate(style, {reason: 'update'})); - } + saveStyle(Object.assign(updatedCode, { + id: element.styleId, + name: null, + reason: 'update', + })); + } static delete(event) { if (confirm(t('deleteStyleConfirm'))) { deleteStyle(getClickedStyleId(event)) .then(handleDelete); - } } + } } @@ -242,7 +241,7 @@ function handleUpdate(style, {reason} = {}) { if (!oldElement) { installed.appendChild(element); } else { - installed.replaceChild(element, oldElement); + installed.replaceChild(element, oldElement); if (reason == 'update') { element.classList.add('update-done'); $('.update-note', element).innerHTML = t('updateCompleted'); @@ -270,10 +269,10 @@ function applyUpdateAll() { [...document.querySelectorAll('.can-update .update')] .forEach(button => { - // align to the bottom of the visible area if wasn't visible - button.scrollIntoView(false); - button.click(); - }); + // align to the bottom of the visible area if wasn't visible + button.scrollIntoView(false); + button.click(); + }); } @@ -381,8 +380,8 @@ class Updater { } else { this.element.classList.add('no-update'); $('.update-note', this.element).innerHTML = message || t('updateCheckSucceededNoUpdate'); - } } + } download(url) { return new Promise((resolve, reject) => { @@ -489,7 +488,7 @@ function rememberScrollPosition() { function $(selector, base = document) { if (selector.startsWith('#') && /^#[^,\s]+$/.test(selector)) { return document.getElementById(selector.slice(1)); - } else { + } else { return base.querySelector(selector); - } + } } diff --git a/storage.js b/storage.js index 2085fd30..6a492f58 100644 --- a/storage.js +++ b/storage.js @@ -250,7 +250,7 @@ function cleanupCachedFilters({force = false} = {}) { } -function saveStyle(style, {notify = true} = {}) { +function saveStyle(style) { return new Promise(resolve => { getDatabase(db => { const tx = db.transaction(['styles'], 'readwrite'); @@ -258,8 +258,13 @@ function saveStyle(style, {notify = true} = {}) { const id = style.id !== undefined && style.id !== null ? Number(style.id) : null; const reason = style.reason; + const notify = style.notify !== false; delete style.method; delete style.reason; + delete style.notify; + if (!style.name) { + delete style.name; + } // Update if (id != null) { @@ -279,6 +284,9 @@ function saveStyle(style, {notify = true} = {}) { style, codeIsUpdated, reason, }); } + if (typeof handleUpdate != 'undefined') { + handleUpdate(style, {reason}); + } resolve(style); }; }; @@ -303,6 +311,9 @@ function saveStyle(style, {notify = true} = {}) { if (notify) { notifyAllTabs({method: 'styleAdded', style, reason}); } + if (typeof handleUpdate != 'undefined') { + handleUpdate(style, {reason}); + } resolve(style); }; }); @@ -337,6 +348,9 @@ function deleteStyle(id, {notify = true} = {}) { if (notify) { notifyAllTabs({method: 'styleDeleted', id}); } + if (typeof handleDelete != 'undefined') { + handleDelete(id); + } resolve(id); }; }));