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 openEditor
global styleViaAPI
@ -15,6 +14,7 @@ window.API_METHODS = Object.assign(window.API_METHODS || {}, {
toggleStyle: styleManager.toggleStyle,
deleteStyle: styleManager.deleteStyle,
getStylesInfoByUrl: styleManager.getStylesInfoByUrl,
installStyle: styleManager.installStyle,
getStyleFromDB: id =>
db.exec('get', id).then(event => event.target.result),
@ -306,7 +306,7 @@ function webNavUsercssInstallerFF(data) {
getTab(tabId),
]).then(([pong, tab]) => {
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;
}
function saveStyle(style) {
}
function deleteStyle({id, notify = true}) {
id = Number(id);
return dbExec('delete', id).then(() => {

View File

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

View File

@ -1,12 +1,17 @@
/* global API_METHODS usercss saveStyle getStyles chromeLocal cachedStyles */
/* global API_METHODS usercss chromeLocal */
'use strict';
(() => {
API_METHODS.saveUsercss = style => save(style, false);
API_METHODS.saveUsercssUnsafe = style => save(style, true);
// API_METHODS.saveUsercss = style => save(style, false);
// 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.installUsercss = install;
API_METHODS.openUsercssInstallPage
// API_METHODS.installUsercss = install;
API_METHODS.parseUsercss = parse;
API_METHODS.findUsercss = find;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -41,7 +41,7 @@ function importFromFile({fileTypeFilter, file} = {}) {
importFromString(text) :
getOwnTab().then(tab => {
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(numStyles => {

View File

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