Add preliminary support for profiles
This commit is contained in:
parent
5877861277
commit
c8171f5ec7
@ -165,7 +165,15 @@ class Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getOptionsSync(optionsContext) {
|
getOptionsSync(optionsContext) {
|
||||||
return this.options;
|
return this.getProfileSync(optionsContext).options;
|
||||||
|
}
|
||||||
|
|
||||||
|
getProfileSync(optionsContext) {
|
||||||
|
const profiles = this.options.profiles;
|
||||||
|
if (typeof optionsContext.index === 'number') {
|
||||||
|
return profiles[optionsContext.index];
|
||||||
|
}
|
||||||
|
return this.options.profiles[this.options.profileCurrent];
|
||||||
}
|
}
|
||||||
|
|
||||||
setExtensionBadgeBackgroundColor(color) {
|
setExtensionBadgeBackgroundColor(color) {
|
||||||
|
@ -17,7 +17,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
function optionsApplyUpdates(options, updates) {
|
/*
|
||||||
|
* Generic options functions
|
||||||
|
*/
|
||||||
|
|
||||||
|
function optionsGenericApplyUpdates(options, updates) {
|
||||||
const targetVersion = updates.length;
|
const targetVersion = updates.length;
|
||||||
const currentVersion = options.version;
|
const currentVersion = options.version;
|
||||||
if (typeof currentVersion === 'number' && Number.isFinite(currentVersion)) {
|
if (typeof currentVersion === 'number' && Number.isFinite(currentVersion)) {
|
||||||
@ -33,7 +37,12 @@ function optionsApplyUpdates(options, updates) {
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
const optionsVersionUpdates = [
|
|
||||||
|
/*
|
||||||
|
* Per-profile options
|
||||||
|
*/
|
||||||
|
|
||||||
|
const profileOptionsVersionUpdates = [
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
@ -48,7 +57,7 @@ const optionsVersionUpdates = [
|
|||||||
options.scanning.modifier = options.scanning.requireShift ? 'shift' : 'none';
|
options.scanning.modifier = options.scanning.requireShift ? 'shift' : 'none';
|
||||||
},
|
},
|
||||||
(options) => {
|
(options) => {
|
||||||
const fieldTemplatesDefault = optionsFieldTemplates();
|
const fieldTemplatesDefault = profileOptionsGetDefaultFieldTemplates();
|
||||||
options.general.resultOutputMode = options.general.groupResults ? 'group' : 'split';
|
options.general.resultOutputMode = options.general.groupResults ? 'group' : 'split';
|
||||||
options.anki.fieldTemplates = (
|
options.anki.fieldTemplates = (
|
||||||
(utilStringHashCode(options.anki.fieldTemplates) !== -805327496) ?
|
(utilStringHashCode(options.anki.fieldTemplates) !== -805327496) ?
|
||||||
@ -58,17 +67,17 @@ const optionsVersionUpdates = [
|
|||||||
},
|
},
|
||||||
(options) => {
|
(options) => {
|
||||||
if (utilStringHashCode(options.anki.fieldTemplates) === 1285806040) {
|
if (utilStringHashCode(options.anki.fieldTemplates) === 1285806040) {
|
||||||
options.anki.fieldTemplates = optionsFieldTemplates();
|
options.anki.fieldTemplates = profileOptionsGetDefaultFieldTemplates();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
(options) => {
|
(options) => {
|
||||||
if (utilStringHashCode(options.anki.fieldTemplates) === -250091611) {
|
if (utilStringHashCode(options.anki.fieldTemplates) === -250091611) {
|
||||||
options.anki.fieldTemplates = optionsFieldTemplates();
|
options.anki.fieldTemplates = profileOptionsGetDefaultFieldTemplates();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
function optionsFieldTemplates() {
|
function profileOptionsGetDefaultFieldTemplates() {
|
||||||
return `
|
return `
|
||||||
{{#*inline "glossary-single"}}
|
{{#*inline "glossary-single"}}
|
||||||
{{~#unless brief~}}
|
{{~#unless brief~}}
|
||||||
@ -234,7 +243,7 @@ function optionsFieldTemplates() {
|
|||||||
`.trim();
|
`.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
function optionsCreateDefaults() {
|
function profileOptionsCreateDefaults() {
|
||||||
return {
|
return {
|
||||||
general: {
|
general: {
|
||||||
enable: true,
|
enable: true,
|
||||||
@ -286,13 +295,13 @@ function optionsCreateDefaults() {
|
|||||||
screenshot: {format: 'png', quality: 92},
|
screenshot: {format: 'png', quality: 92},
|
||||||
terms: {deck: '', model: '', fields: {}},
|
terms: {deck: '', model: '', fields: {}},
|
||||||
kanji: {deck: '', model: '', fields: {}},
|
kanji: {deck: '', model: '', fields: {}},
|
||||||
fieldTemplates: optionsFieldTemplates()
|
fieldTemplates: profileOptionsGetDefaultFieldTemplates()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function optionsSetDefaults(options) {
|
function profileOptionsSetDefaults(options) {
|
||||||
const defaults = optionsCreateDefaults();
|
const defaults = profileOptionsCreateDefaults();
|
||||||
|
|
||||||
const combine = (target, source) => {
|
const combine = (target, source) => {
|
||||||
for (const key in source) {
|
for (const key in source) {
|
||||||
@ -312,9 +321,59 @@ function optionsSetDefaults(options) {
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
function optionsVersion(options) {
|
function profileOptionsUpdateVersion(options) {
|
||||||
optionsSetDefaults(options);
|
profileOptionsSetDefaults(options);
|
||||||
return optionsApplyUpdates(options, optionsVersionUpdates);
|
return optionsGenericApplyUpdates(options, profileOptionsVersionUpdates);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Global options
|
||||||
|
*/
|
||||||
|
|
||||||
|
const optionsVersionUpdates = [];
|
||||||
|
|
||||||
|
function optionsUpdateVersion(options, defaultProfileOptions) {
|
||||||
|
// Ensure profiles is an array
|
||||||
|
if (!Array.isArray(options.profiles)) {
|
||||||
|
options.profiles = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove invalid
|
||||||
|
const profiles = options.profiles;
|
||||||
|
for (let i = profiles.length - 1; i >= 0; --i) {
|
||||||
|
if (!utilIsObject(profiles[i])) {
|
||||||
|
profiles.splice(i, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Require at least one profile
|
||||||
|
if (profiles.length === 0) {
|
||||||
|
profiles.push({
|
||||||
|
name: 'Default',
|
||||||
|
options: defaultProfileOptions
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure profileCurrent is valid
|
||||||
|
const profileCurrent = options.profileCurrent;
|
||||||
|
if (!(
|
||||||
|
typeof profileCurrent === 'number' &&
|
||||||
|
Number.isFinite(profileCurrent) &&
|
||||||
|
Math.floor(profileCurrent) === profileCurrent &&
|
||||||
|
profileCurrent >= 0 &&
|
||||||
|
profileCurrent < profiles.length
|
||||||
|
)) {
|
||||||
|
options.profileCurrent = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update profile options
|
||||||
|
for (const profile of profiles) {
|
||||||
|
profile.options = profileOptionsUpdateVersion(profile.options);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generic updates
|
||||||
|
return optionsGenericApplyUpdates(options, optionsVersionUpdates);
|
||||||
}
|
}
|
||||||
|
|
||||||
function optionsLoad() {
|
function optionsLoad() {
|
||||||
@ -338,7 +397,11 @@ function optionsLoad() {
|
|||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
return {};
|
return {};
|
||||||
}).then(options => {
|
}).then(options => {
|
||||||
return optionsVersion(options);
|
return (
|
||||||
|
Array.isArray(options.profiles) ?
|
||||||
|
optionsUpdateVersion(options, {}) :
|
||||||
|
optionsUpdateVersion({}, options)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -643,7 +643,7 @@ async function onAnkiFieldTemplatesReset(e) {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const optionsContext = getOptionsContext();
|
const optionsContext = getOptionsContext();
|
||||||
const options = await apiOptionsGet(optionsContext);
|
const options = await apiOptionsGet(optionsContext);
|
||||||
const fieldTemplates = optionsFieldTemplates();
|
const fieldTemplates = profileOptionsGetDefaultFieldTemplates();
|
||||||
options.anki.fieldTemplates = fieldTemplates;
|
options.anki.fieldTemplates = fieldTemplates;
|
||||||
$('#field-templates').val(fieldTemplates);
|
$('#field-templates').val(fieldTemplates);
|
||||||
await settingsSaveOptions();
|
await settingsSaveOptions();
|
||||||
|
Loading…
Reference in New Issue
Block a user