allow errors in usercss when saving in editor and live-reloading

This commit is contained in:
tophf 2018-01-07 16:36:30 +03:00
parent 3df4a86428
commit 022e588c97
4 changed files with 53 additions and 43 deletions

View File

@ -4,6 +4,7 @@
(() => { (() => {
API_METHODS.saveUsercss = save; API_METHODS.saveUsercss = save;
API_METHODS.saveUsercssUnsafe = style => save(style, true);
API_METHODS.buildUsercss = build; API_METHODS.buildUsercss = build;
API_METHODS.installUsercss = install; API_METHODS.installUsercss = install;
@ -47,35 +48,34 @@
} }
} }
function buildCode(style) {
return usercss.buildCode(style);
}
// Parse the source and find the duplication // Parse the source and find the duplication
function build({sourceCode, checkDup = false}) { function build({sourceCode, checkDup = false}) {
return buildMeta({sourceCode}) return buildMeta({sourceCode})
.then(style => Promise.all([ .then(usercss.buildCode)
buildCode(style), .then(style => ({
checkDup && findDup(style) style,
])) dup: checkDup && findDup(style),
.then(([style, dup]) => ({style, dup})); }));
} }
function save(style) { function save(style, allowErrors = false) {
// restore if stripped by getStyleWithNoCode // restore if stripped by getStyleWithNoCode
if (typeof style.sourceCode !== 'string') { if (typeof style.sourceCode !== 'string') {
style.sourceCode = cachedStyles.byId.get(style.id).sourceCode; style.sourceCode = cachedStyles.byId.get(style.id).sourceCode;
} }
return buildMeta(style) return buildMeta(style)
.then(assignVars) .then(assignVars)
.then(buildCode) .then(style => usercss.buildCode(style, allowErrors))
.then(saveStyle); .then(result =>
allowErrors ?
saveStyle(result.style).then(style => ({style, errors: result.errors})) :
saveStyle(result));
function assignVars(style) { function assignVars(style) {
if (style.reason === 'config' && style.id) { if (style.reason === 'config' && style.id) {
return style; return style;
} }
return findDup(style).then(dup => { const dup = findDup(style);
if (dup) { if (dup) {
style.id = dup.id; style.id = dup.id;
if (style.reason !== 'config') { if (style.reason !== 'config') {
@ -84,23 +84,20 @@
} }
} }
return style; return style;
});
} }
} }
function findDup(style) { function findDup(style) {
if (style.id) { if (style.id) return cachedStyles.byId.get(style.id);
return getStyles({id: style.id}).then(s => s[0]); const {name, namespace} = style.usercssData;
for (const dup of cachedStyles.list) {
const data = dup.usercssData;
if (!data) continue;
if (data.name === name &&
data.namespace === namespace) {
return dup;
} }
return getStyles().then(styles =>
styles.find(target => {
if (!target.usercssData) {
return false;
} }
return target.usercssData.name === style.usercssData.name &&
target.usercssData.namespace === style.usercssData.namespace;
})
);
} }
function install({url, direct, downloaded, tab}, sender) { function install({url, direct, downloaded, tab}, sender) {

View File

@ -192,13 +192,16 @@ function createSourceEditor(style) {
if (!dirty.isDirty()) return; if (!dirty.isDirty()) return;
const code = cm.getValue(); const code = cm.getValue();
return ( return (
API.saveUsercss({ API.saveUsercssUnsafe({
reason: 'editSave',
id: style.id, id: style.id,
reason: 'editSave',
enabled: style.enabled, enabled: style.enabled,
sourceCode: code, sourceCode: code,
})) }))
.then(replaceStyle) .then(({style, errors}) => {
replaceStyle(style);
if (errors) return Promise.reject(errors);
})
.catch(err => { .catch(err => {
if (err.message === t('styleMissingMeta', 'name')) { if (err.message === t('styleMissingMeta', 'name')) {
messageBox.confirm(t('usercssReplaceTemplateConfirmation')).then(ok => ok && messageBox.confirm(t('usercssReplaceTemplateConfirmation')).then(ok => ok &&

View File

@ -68,13 +68,14 @@
cm.setCursor(cursor); cm.setCursor(cursor);
cm.scrollTo(scrollInfo.left, scrollInfo.top); cm.scrollTo(scrollInfo.left, scrollInfo.top);
return sendMessage({ API.saveUsercssUnsafe({
id: (installed || installedDup).id, id: (installed || installedDup).id,
method: 'saveUsercss',
reason: 'update', reason: 'update',
sourceCode sourceCode
}).then(updateMeta) }).then(({style, errors}) => {
.catch(showError); updateMeta(style);
if (errors) return Promise.reject(errors);
}).catch(showError);
}); });
} }

View File

@ -471,7 +471,13 @@ var usercss = (() => {
return version; return version;
} }
function buildCode(style) { /**
* @param {Object} style
* @param {Boolean} [allowErrors=false]
* @returns {(Style | {style: Style, errors: (false|String[])})} - style object
* when allowErrors is falsy or {style, errors} object when allowErrors is truthy
*/
function buildCode(style, allowErrors) {
const {usercssData: {preprocessor, vars}, sourceCode} = style; const {usercssData: {preprocessor, vars}, sourceCode} = style;
let builder; let builder;
if (preprocessor) { if (preprocessor) {
@ -494,11 +500,14 @@ var usercss = (() => {
styleId: style.id, styleId: style.id,
code: mozStyle, code: mozStyle,
})) }))
.then(({sections, errors}) => sections.length && sections || Promise.reject(errors)) .then(({sections, errors}) => {
.then(sections => { if (!errors.length) errors = false;
if (!sections.length || errors && !allowErrors) {
return Promise.reject(errors);
}
style.sections = sections; style.sections = sections;
if (builder.postprocess) builder.postprocess(style.sections, sVars); if (builder.postprocess) builder.postprocess(style.sections, sVars);
return style; return allowErrors ? {style, errors} : style;
})); }));
} }