Update usercss API

This commit is contained in:
eight 2018-10-07 22:59:31 +08:00
parent 69cae02381
commit 057111b171
11 changed files with 31 additions and 36 deletions

View File

@ -1,5 +1,4 @@
/* /*
global dbExec getStyles saveStyle deleteStyle
global handleCssTransitionBug detectSloppyRegexps global handleCssTransitionBug detectSloppyRegexps
global openEditor global openEditor
global styleViaAPI global styleViaAPI
@ -15,6 +14,7 @@ window.API_METHODS = Object.assign(window.API_METHODS || {}, {
toggleStyle: styleManager.toggleStyle, toggleStyle: styleManager.toggleStyle,
deleteStyle: styleManager.deleteStyle, deleteStyle: styleManager.deleteStyle,
getStylesInfoByUrl: styleManager.getStylesInfoByUrl, getStylesInfoByUrl: styleManager.getStylesInfoByUrl,
installStyle: styleManager.installStyle,
getStyleFromDB: id => getStyleFromDB: id =>
db.exec('get', id).then(event => event.target.result), db.exec('get', id).then(event => event.target.result),
@ -306,7 +306,7 @@ function webNavUsercssInstallerFF(data) {
getTab(tabId), getTab(tabId),
]).then(([pong, tab]) => { ]).then(([pong, tab]) => {
if (pong !== true && tab.url !== 'about:blank') { if (pong !== true && tab.url !== 'about:blank') {
window.API_METHODS.installUsercss({direct: true}, {tab}); window.API_METHODS.openUsercssInstallPage({direct: true}, {tab});
} }
}); });
} }

View File

@ -149,11 +149,6 @@ function filterStylesInternal({
: filtered; : filtered;
} }
function saveStyle(style) {
}
function deleteStyle({id, notify = true}) { function deleteStyle({id, notify = true}) {
id = Number(id); id = Number(id);
return dbExec('delete', id).then(() => { return dbExec('delete', id).then(() => {

View File

@ -1,5 +1,5 @@
/* /*
global getStyles saveStyle styleSectionsEqual global styleSectionsEqual
global calcStyleDigest cachedStyles getStyleWithNoCode global calcStyleDigest cachedStyles getStyleWithNoCode
global usercss semverCompare global usercss semverCompare
global API_METHODS styleManager global API_METHODS styleManager
@ -173,7 +173,7 @@ global API_METHODS styleManager
json.id = style.id; json.id = style.id;
json.updateDate = Date.now(); json.updateDate = Date.now();
json.reason = 'update'; // json.reason = 'update';
// keep current state // keep current state
delete json.enabled; delete json.enabled;
@ -185,10 +185,11 @@ global API_METHODS styleManager
json.originalName = json.name; json.originalName = json.name;
} }
const newStyle = Object.assign({}, style, json);
if (styleSectionsEqual(json, style, {checkSource: true})) { if (styleSectionsEqual(json, style, {checkSource: true})) {
// update digest even if save === false as there might be just a space added etc. // update digest even if save === false as there might be just a space added etc.
json.reason = 'update-digest'; // json.reason = 'update-digest';
return saveStyle(json) return styleManager.installStyle(newStyle)
.then(saved => { .then(saved => {
style.originalDigest = saved.originalDigest; style.originalDigest = saved.originalDigest;
return Promise.reject(STATES.SAME_CODE); return Promise.reject(STATES.SAME_CODE);
@ -200,8 +201,8 @@ global API_METHODS styleManager
} }
return save ? return save ?
API_METHODS[json.usercssData ? 'saveUsercss' : 'saveStyle'](json) : API_METHODS[json.usercssData ? 'installUsercss' : 'installStyle'](newStyle) :
json; newStyle;
} }
function styleJSONseemsValid(json) { function styleJSONseemsValid(json) {

View File

@ -1,12 +1,17 @@
/* global API_METHODS usercss saveStyle getStyles chromeLocal cachedStyles */ /* global API_METHODS usercss chromeLocal */
'use strict'; 'use strict';
(() => { (() => {
API_METHODS.saveUsercss = style => save(style, false); // API_METHODS.saveUsercss = style => save(style, false);
API_METHODS.saveUsercssUnsafe = style => save(style, true); // API_METHODS.saveUsercssUnsafe = style => save(style, true);
API_METHODS.installUsercss
API_METHODS.editSaveUsercss
API_METHODS.configUsercssVars = (id, vars) => newVars
API_METHODS.buildUsercss = build; API_METHODS.buildUsercss = build;
API_METHODS.installUsercss = install; API_METHODS.openUsercssInstallPage
// API_METHODS.installUsercss = install;
API_METHODS.parseUsercss = parse; API_METHODS.parseUsercss = parse;
API_METHODS.findUsercss = find; API_METHODS.findUsercss = find;

View File

@ -128,8 +128,7 @@
&& event.data.type === 'ouc-install-usercss' && event.data.type === 'ouc-install-usercss'
&& allowedOrigins.includes(event.origin) && allowedOrigins.includes(event.origin)
) { ) {
API.saveUsercss({ API.installUsercss({
reason: 'install',
name: event.data.title, name: event.data.title,
sourceCode: event.data.code, sourceCode: event.data.code,
}).then(style => { }).then(style => {

View File

@ -16,7 +16,7 @@
let sourceCode, port, timer; let sourceCode, port, timer;
chrome.runtime.onConnect.addListener(onConnected); chrome.runtime.onConnect.addListener(onConnected);
API.installUsercss({url}) API.openUsercssInstallPage({url})
.catch(err => alert(err)); .catch(err => alert(err));
function onConnected(newPort) { function onConnected(newPort) {

View File

@ -224,17 +224,13 @@ function createSourceEditor(style) {
exclusionList exclusionList
}); });
return ensureUniqueStyle(code) return ensureUniqueStyle(code)
.then(() => API.saveUsercssUnsafe({ .then(() => API.editSaveUsercss({
id: style.id, id: style.id,
reason: 'editSave',
enabled: style.enabled, enabled: style.enabled,
sourceCode: code, sourceCode: code,
exclusions: exclusionList exclusions: exclusionList
})) }))
.then(({style, errors}) => { .then(replaceStyle)
replaceStyle(style);
if (errors) return Promise.reject(errors);
})
.catch(err => { .catch(err => {
if (err.handled) return; if (err.handled) return;
if (err.message === t('styleMissingMeta', 'name')) { if (err.message === t('styleMissingMeta', 'name')) {

View File

@ -86,13 +86,12 @@
cm.setCursor(cursor); cm.setCursor(cursor);
cm.scrollTo(scrollInfo.left, scrollInfo.top); cm.scrollTo(scrollInfo.left, scrollInfo.top);
API.saveUsercssUnsafe({ API.installUsercss({
id: (installed || installedDup).id, id: (installed || installedDup).id,
reason: 'update', reason: 'update',
sourceCode sourceCode
}).then(({style, errors}) => { }).then(style => {
updateMeta(style); updateMeta(style);
if (errors) return Promise.reject(errors);
}).catch(showError); }).catch(showError);
}); });
} }
@ -301,7 +300,7 @@
data.version, data.version,
])) ]))
).then(ok => ok && ).then(ok => ok &&
API.saveUsercss(Object.assign(style, dup && {reason: 'update'})) API.installUsercss(style)
.then(install) .then(install)
.catch(err => messageBox.alert(t('styleInstallFailed', err), 'pre')) .catch(err => messageBox.alert(t('styleInstallFailed', err), 'pre'))
); );

View File

@ -171,9 +171,9 @@ function configDialog(style) {
return; return;
} }
saving = true; saving = true;
return API.saveUsercss(style) return API.configUsercssVar(style.id, style.usercssData.vars)
.then(saved => { .then(newVars => {
varsInitial = getInitialValues(saved.usercssData.vars); varsInitial = getInitialValues(newVars);
vars.forEach(va => onchange({target: va.input, justSaved: true})); vars.forEach(va => onchange({target: va.input, justSaved: true}));
renderValues(); renderValues();
updateButtons(); updateButtons();

View File

@ -41,7 +41,7 @@ function importFromFile({fileTypeFilter, file} = {}) {
importFromString(text) : importFromString(text) :
getOwnTab().then(tab => { getOwnTab().then(tab => {
tab.url = URL.createObjectURL(new Blob([text], {type: 'text/css'})); tab.url = URL.createObjectURL(new Blob([text], {type: 'text/css'}));
return API.installUsercss({direct: true, tab}) return API.openUsercssInstallPage({direct: true, tab})
.then(() => URL.revokeObjectURL(tab.url)); .then(() => URL.revokeObjectURL(tab.url));
}) })
).then(numStyles => { ).then(numStyles => {

View File

@ -415,8 +415,8 @@ Object.assign(handleEvent, {
event.preventDefault(); event.preventDefault();
const json = entry.updatedCode; const json = entry.updatedCode;
json.id = entry.styleId; json.id = entry.styleId;
json.reason = 'update'; // json.reason = 'update';
API[json.usercssData ? 'saveUsercss' : 'saveStyle'](json); API[json.usercssData ? 'installUsercss' : 'installStyle'](json);
}, },
delete(event, entry) { delete(event, entry) {