From 7b5e7c96d59df10531bbf81fe3bb682f9593aabf Mon Sep 17 00:00:00 2001 From: eight Date: Mon, 8 Oct 2018 18:16:45 +0800 Subject: [PATCH] Hook up live preview --- background/usercss-helper.js | 26 +++++++++++++++++--------- edit.html | 1 + edit/live-preview.js | 1 - edit/source-editor.js | 22 +++++++++++++++++++++- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/background/usercss-helper.js b/background/usercss-helper.js index be4e5716..e3d3d91f 100644 --- a/background/usercss-helper.js +++ b/background/usercss-helper.js @@ -81,16 +81,24 @@ sourceCode, checkDup, metaOnly, + vars, }) { - const task = buildMeta({sourceCode}); - return (metaOnly ? task : task.then(usercss.buildCode)) - .then(style => { - if (!checkDup) { - return {style}; - } - return find(style) - .then(dup => ({style, dup})); - }); + return usercss.buildMeta(sourceCode) + .then(style => + Promise.all([ + metaOnly ? style : doBuild(style), + checkDup ? find(style) : undefined + ]) + ) + .then(([style, dup]) => ({style, dup})); + + function doBuild(style) { + if (vars) { + const oldStyle = {usercssData: {vars}}; + usercss.assignVars(style, oldStyle); + } + return usercss.buildCode(style); + } } // Parse the source, apply customizations, report fatal/syntax errors diff --git a/edit.html b/edit.html index 01c91b83..4fe6708b 100644 --- a/edit.html +++ b/edit.html @@ -30,6 +30,7 @@ + diff --git a/edit/live-preview.js b/edit/live-preview.js index fe81de0b..c18cdca7 100644 --- a/edit/live-preview.js +++ b/edit/live-preview.js @@ -4,7 +4,6 @@ function createLivePreview() { let previewer; return {update}; - // {id, enabled, sourceCode, sections} function update(data) { if (!previewer) { if (!data.id || !data.enabled) { diff --git a/edit/source-editor.js b/edit/source-editor.js index 56278681..22d10002 100644 --- a/edit/source-editor.js +++ b/edit/source-editor.js @@ -5,7 +5,7 @@ global createAppliesToLineWidget messageBox global sectionsToMozFormat global exclusions global beforeUnload -global createMetaCompiler linter +global createMetaCompiler linter createLivePreview */ 'use strict'; @@ -37,14 +37,19 @@ function createSourceEditor(style) { editors.push(cm); + const livePreview = createLivePreview(); + livePreview.enable(Boolean(style.id)); + $('#enabled').onchange = function () { const value = this.checked; dirty.modify('enabled', style.enabled, value); style.enabled = value; + updateLivePreview(); }; cm.on('changes', () => { dirty.modify('sourceGeneration', savedGeneration, cm.changeGeneration()); + updateLivePreview(); }); CodeMirror.commands.prevEditor = cm => nextPrevMozDocument(cm, -1); @@ -87,6 +92,20 @@ function createSourceEditor(style) { }); }); + function updateLivePreview() { + if (!style.id) { + return; + } + API.buildUsercss({ + sourceCode: cm.getValue(), + vars: style.usercssData.vars + }) + .then(newStyle => { + delete newStyle.enabled; + livePreview.update(Object.assign({}, style, newStyle)); + }); + } + function initAppliesToLineWidget() { const PREF_NAME = 'editor.appliesToLineWidget'; const widget = createAppliesToLineWidget(cm); @@ -204,6 +223,7 @@ function createSourceEditor(style) { styleId = style.id; $('#preview-label').classList.remove('hidden'); updateMeta(); + livePreview.enable(Boolean(style.id)); } }