Hook up live preview

This commit is contained in:
eight 2018-10-08 18:16:45 +08:00
parent 15efafff3c
commit 7b5e7c96d5
4 changed files with 39 additions and 11 deletions

View File

@ -81,16 +81,24 @@
sourceCode, sourceCode,
checkDup, checkDup,
metaOnly, metaOnly,
vars,
}) { }) {
const task = buildMeta({sourceCode}); return usercss.buildMeta(sourceCode)
return (metaOnly ? task : task.then(usercss.buildCode)) .then(style =>
.then(style => { Promise.all([
if (!checkDup) { metaOnly ? style : doBuild(style),
return {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);
} }
return find(style)
.then(dup => ({style, dup}));
});
} }
// Parse the source, apply customizations, report fatal/syntax errors // Parse the source, apply customizations, report fatal/syntax errors

View File

@ -30,6 +30,7 @@
<script src="content/apply.js"></script> <script src="content/apply.js"></script>
<script src="edit/util.js"></script> <script src="edit/util.js"></script>
<script src="edit/regexp-tester.js"></script> <script src="edit/regexp-tester.js"></script>
<script src="edit/live-preview.js"></script>
<script src="edit/applies-to-line-widget.js"></script> <script src="edit/applies-to-line-widget.js"></script>
<script src="edit/source-editor.js"></script> <script src="edit/source-editor.js"></script>
<script src="edit/colorpicker-helper.js"></script> <script src="edit/colorpicker-helper.js"></script>

View File

@ -4,7 +4,6 @@ function createLivePreview() {
let previewer; let previewer;
return {update}; return {update};
// {id, enabled, sourceCode, sections}
function update(data) { function update(data) {
if (!previewer) { if (!previewer) {
if (!data.id || !data.enabled) { if (!data.id || !data.enabled) {

View File

@ -5,7 +5,7 @@ global createAppliesToLineWidget messageBox
global sectionsToMozFormat global sectionsToMozFormat
global exclusions global exclusions
global beforeUnload global beforeUnload
global createMetaCompiler linter global createMetaCompiler linter createLivePreview
*/ */
'use strict'; 'use strict';
@ -37,14 +37,19 @@ function createSourceEditor(style) {
editors.push(cm); editors.push(cm);
const livePreview = createLivePreview();
livePreview.enable(Boolean(style.id));
$('#enabled').onchange = function () { $('#enabled').onchange = function () {
const value = this.checked; const value = this.checked;
dirty.modify('enabled', style.enabled, value); dirty.modify('enabled', style.enabled, value);
style.enabled = value; style.enabled = value;
updateLivePreview();
}; };
cm.on('changes', () => { cm.on('changes', () => {
dirty.modify('sourceGeneration', savedGeneration, cm.changeGeneration()); dirty.modify('sourceGeneration', savedGeneration, cm.changeGeneration());
updateLivePreview();
}); });
CodeMirror.commands.prevEditor = cm => nextPrevMozDocument(cm, -1); 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() { function initAppliesToLineWidget() {
const PREF_NAME = 'editor.appliesToLineWidget'; const PREF_NAME = 'editor.appliesToLineWidget';
const widget = createAppliesToLineWidget(cm); const widget = createAppliesToLineWidget(cm);
@ -204,6 +223,7 @@ function createSourceEditor(style) {
styleId = style.id; styleId = style.id;
$('#preview-label').classList.remove('hidden'); $('#preview-label').classList.remove('hidden');
updateMeta(); updateMeta();
livePreview.enable(Boolean(style.id));
} }
} }