diff --git a/src/lib/components/ImageShowFullscreenButton.js b/src/lib/components/ImageShowFullscreenButton.js index 7a2fc09..f0cd8c7 100644 --- a/src/lib/components/ImageShowFullscreenButton.js +++ b/src/lib/components/ImageShowFullscreenButton.js @@ -1,6 +1,6 @@ import {BaseComponent} from "$lib/components/base/BaseComponent.js"; import {getComponent} from "$lib/components/base/ComponentUtils.js"; -import MiscSettings from "$lib/extension/settings/MiscSettings.js"; +import MiscSettings from "$lib/extension/settings/MiscSettings.ts"; import {FullscreenViewer} from "$lib/components/FullscreenViewer.js"; export class ImageShowFullscreenButton extends BaseComponent { diff --git a/src/lib/components/MaintenancePopup.js b/src/lib/components/MaintenancePopup.js index 7abb794..0267304 100644 --- a/src/lib/components/MaintenancePopup.js +++ b/src/lib/components/MaintenancePopup.js @@ -1,4 +1,4 @@ -import MaintenanceSettings from "$lib/extension/settings/MaintenanceSettings.js"; +import MaintenanceSettings from "$lib/extension/settings/MaintenanceSettings.ts"; import MaintenanceProfile from "$entities/MaintenanceProfile.ts"; import {BaseComponent} from "$lib/components/base/BaseComponent.js"; import {getComponent} from "$lib/components/base/ComponentUtils.js"; diff --git a/src/lib/components/SearchWrapper.js b/src/lib/components/SearchWrapper.js index 43720db..d674851 100644 --- a/src/lib/components/SearchWrapper.js +++ b/src/lib/components/SearchWrapper.js @@ -1,6 +1,6 @@ import {BaseComponent} from "$lib/components/base/BaseComponent.js"; import {QueryLexer, QuotedTermToken, TermToken, Token} from "$lib/booru/search/QueryLexer.js"; -import SearchSettings from "$lib/extension/settings/SearchSettings.js"; +import SearchSettings from "$lib/extension/settings/SearchSettings.ts"; export class SearchWrapper extends BaseComponent { /** @type {HTMLInputElement|null} */ diff --git a/src/lib/components/TagDropdownWrapper.js b/src/lib/components/TagDropdownWrapper.js index 0eb5e3c..78961a3 100644 --- a/src/lib/components/TagDropdownWrapper.js +++ b/src/lib/components/TagDropdownWrapper.js @@ -1,6 +1,6 @@ import {BaseComponent} from "$lib/components/base/BaseComponent.js"; import MaintenanceProfile from "$entities/MaintenanceProfile.ts"; -import MaintenanceSettings from "$lib/extension/settings/MaintenanceSettings.js"; +import MaintenanceSettings from "$lib/extension/settings/MaintenanceSettings.ts"; import {getComponent} from "$lib/components/base/ComponentUtils.js"; const isTagEditorProcessedKey = Symbol(); diff --git a/src/lib/extension/base/CacheableSettings.js b/src/lib/extension/base/CacheableSettings.ts similarity index 58% rename from src/lib/extension/base/CacheableSettings.js rename to src/lib/extension/base/CacheableSettings.ts index 6627297..d5bb6e7 100644 --- a/src/lib/extension/base/CacheableSettings.js +++ b/src/lib/extension/base/CacheableSettings.ts @@ -1,20 +1,20 @@ import ConfigurationController from "$lib/extension/ConfigurationController.js"; -export default class CacheableSettings { - /** @type {ConfigurationController} */ - #controller; - /** @type {Map} */ - #cachedValues = new Map(); - /** @type {function[]} */ - #disposables = []; +export default class CacheableSettings { + #controller: ConfigurationController; + #cachedValues: Map = new Map(); + #disposables: Function[] = []; - constructor(settingsNamespace) { + constructor(settingsNamespace: string) { this.#controller = new ConfigurationController(settingsNamespace); this.#disposables.push( this.#controller.subscribeToChanges(settings => { for (const key of Object.keys(settings)) { - this.#cachedValues.set(key, settings[key]); + this.#cachedValues.set( + key as keyof Fields, + settings[key] + ); } }) ); @@ -27,12 +27,12 @@ export default class CacheableSettings { * @return {Promise} * @protected */ - async _resolveSetting(settingName, defaultValue) { + protected async _resolveSetting(settingName: Key, defaultValue: Fields[Key]): Promise { if (this.#cachedValues.has(settingName)) { return this.#cachedValues.get(settingName); } - const settingValue = await this.#controller.readSetting(settingName, defaultValue); + const settingValue = await this.#controller.readSetting(settingName as string, defaultValue); this.#cachedValues.set(settingName, settingValue); @@ -40,13 +40,12 @@ export default class CacheableSettings { } /** - * @param {string} settingName Name of the setting to write. - * @param {*} value Value to pass. - * @param {boolean} [force=false] Ignore the cache and force the update. - * @return {Promise} + * @param settingName Name of the setting to write. + * @param value Value to pass. + * @param force Ignore the cache and force the update. * @protected */ - async _writeSetting(settingName, value, force = false) { + async _writeSetting(settingName: Key, value: Fields[Key], force: boolean = false): Promise { if ( !force && this.#cachedValues.has(settingName) @@ -55,7 +54,10 @@ export default class CacheableSettings { return; } - return this.#controller.writeSetting(settingName, value); + return this.#controller.writeSetting( + settingName as string, + value + ); } /** @@ -63,8 +65,8 @@ export default class CacheableSettings { * @param {function(Object): void} callback Callback which will receive list of settings. * @return {function(): void} Unsubscribe function. */ - subscribe(callback) { - const unsubscribeCallback = this.#controller.subscribeToChanges(callback); + subscribe(callback: (settings: Partial) => void): () => void { + const unsubscribeCallback = this.#controller.subscribeToChanges(callback as (fields: Record) => void); this.#disposables.push(unsubscribeCallback); diff --git a/src/lib/extension/settings/MaintenanceSettings.js b/src/lib/extension/settings/MaintenanceSettings.js deleted file mode 100644 index ad0da60..0000000 --- a/src/lib/extension/settings/MaintenanceSettings.js +++ /dev/null @@ -1,63 +0,0 @@ -import ConfigurationController from "$lib/extension/ConfigurationController.js"; -import MaintenanceProfile from "$entities/MaintenanceProfile.ts"; -import CacheableSettings from "$lib/extension/base/CacheableSettings.js"; - -export default class MaintenanceSettings extends CacheableSettings { - constructor() { - super("maintenance"); - } - - /** - * Set the active maintenance profile. - * - * @return {Promise} - */ - async resolveActiveProfileId() { - return this._resolveSetting("activeProfile", null); - } - - /** - * Get the active maintenance profile if it is set. - * - * @return {Promise} - */ - async resolveActiveProfileAsObject() { - const resolvedProfileId = await this.resolveActiveProfileId(); - - return (await MaintenanceProfile.readAll()) - .find(profile => profile.id === resolvedProfileId) || null; - } - - /** - * Set the active maintenance profile. - * - * @param {string|null} profileId ID of the profile to set as active. If `null`, the active profile will be considered - * unset. - * - * @return {Promise} - */ - async setActiveProfileId(profileId) { - await this._writeSetting("activeProfile", profileId); - } - - /** - * Subscribe to the changes in the maintenance-related settings. - * - * @param {function(MaintenanceSettingsObject): void} callback Callback to call when the settings change. The new - * settings are passed as an argument. - * - * @return {function(): void} Unsubscribe function. - */ - subscribe(callback) { - return super.subscribe(settings => { - callback({ - activeProfile: settings.activeProfile || null, - }); - }); - } -} - -/** - * @typedef {Object} MaintenanceSettingsObject - * @property {string|null} activeProfile - */ diff --git a/src/lib/extension/settings/MaintenanceSettings.ts b/src/lib/extension/settings/MaintenanceSettings.ts new file mode 100644 index 0000000..94b9b85 --- /dev/null +++ b/src/lib/extension/settings/MaintenanceSettings.ts @@ -0,0 +1,39 @@ +import MaintenanceProfile from "$entities/MaintenanceProfile.ts"; +import CacheableSettings from "$lib/extension/base/CacheableSettings.ts"; + +interface MaintenanceSettingsFields { + activeProfile: string | null; +} + +export default class MaintenanceSettings extends CacheableSettings { + constructor() { + super("maintenance"); + } + + /** + * Set the active maintenance profile. + */ + async resolveActiveProfileId() { + return this._resolveSetting("activeProfile", null); + } + + /** + * Get the active maintenance profile if it is set. + */ + async resolveActiveProfileAsObject(): Promise { + const resolvedProfileId = await this.resolveActiveProfileId(); + + return (await MaintenanceProfile.readAll()) + .find(profile => profile.id === resolvedProfileId) || null; + } + + /** + * Set the active maintenance profile. + * + * @param profileId ID of the profile to set as active. If `null`, the active profile will be considered + * unset. + */ + async setActiveProfileId(profileId: string | null): Promise { + await this._writeSetting("activeProfile", profileId); + } +} diff --git a/src/lib/extension/settings/MiscSettings.js b/src/lib/extension/settings/MiscSettings.js deleted file mode 100644 index fd49f18..0000000 --- a/src/lib/extension/settings/MiscSettings.js +++ /dev/null @@ -1,39 +0,0 @@ -import CacheableSettings from "$lib/extension/base/CacheableSettings.js"; - -export default class MiscSettings extends CacheableSettings { - constructor() { - super("misc"); - } - - /** - * @return {Promise} - */ - async resolveFullscreenViewerEnabled() { - return this._resolveSetting("fullscreenViewer", true); - } - - /** - * @param {boolean} isEnabled - * @return {Promise} - */ - async setFullscreenViewerEnabled(isEnabled) { - return this._writeSetting("fullscreenViewer", isEnabled); - } - - /** - * @param {function(MiscSettingsObject): void} callback - * @return {function(): void} - */ - subscribe(callback) { - return super.subscribe(settings => { - callback({ - fullscreenViewer: settings.fullscreenViewer ?? true, - }) - }); - } -} - -/** - * @typedef {Object} MiscSettingsObject - * @property {boolean} fullscreenViewer - */ diff --git a/src/lib/extension/settings/MiscSettings.ts b/src/lib/extension/settings/MiscSettings.ts new file mode 100644 index 0000000..94ea7d2 --- /dev/null +++ b/src/lib/extension/settings/MiscSettings.ts @@ -0,0 +1,19 @@ +import CacheableSettings from "$lib/extension/base/CacheableSettings.ts"; + +interface MiscSettingsFields { + fullscreenViewer: boolean; +} + +export default class MiscSettings extends CacheableSettings { + constructor() { + super("misc"); + } + + async resolveFullscreenViewerEnabled() { + return this._resolveSetting("fullscreenViewer", true); + } + + async setFullscreenViewerEnabled(isEnabled: boolean) { + return this._writeSetting("fullscreenViewer", isEnabled); + } +} diff --git a/src/lib/extension/settings/SearchSettings.js b/src/lib/extension/settings/SearchSettings.js deleted file mode 100644 index e5de971..0000000 --- a/src/lib/extension/settings/SearchSettings.js +++ /dev/null @@ -1,42 +0,0 @@ -import CacheableSettings from "$lib/extension/base/CacheableSettings.js"; - -export default class SearchSettings extends CacheableSettings { - constructor() { - super("search"); - } - - async resolvePropertiesSuggestionsEnabled() { - return this._resolveSetting("suggestProperties", false); - } - - async resolvePropertiesSuggestionsPosition() { - return this._resolveSetting("suggestPropertiesPosition", "start"); - } - - async setPropertiesSuggestions(isEnabled) { - return this._writeSetting("suggestProperties", isEnabled); - } - - async setPropertiesSuggestionsPosition(position) { - return this._writeSetting("suggestPropertiesPosition", position); - } - - /** - * @param {function(SearchSettingsObject): void} callback - * @return {function(): void} - */ - subscribe(callback) { - return super.subscribe(rawSettings => { - callback({ - suggestProperties: rawSettings.suggestProperties ?? false, - suggestPropertiesPosition: rawSettings.suggestPropertiesPosition ?? "start", - }); - }); - } -} - -/** - * @typedef {Object} SearchSettingsObject - * @property {boolean} suggestProperties - * @property {"start"|"end"} suggestPropertiesPosition - */ diff --git a/src/lib/extension/settings/SearchSettings.ts b/src/lib/extension/settings/SearchSettings.ts new file mode 100644 index 0000000..13a7bce --- /dev/null +++ b/src/lib/extension/settings/SearchSettings.ts @@ -0,0 +1,28 @@ +import CacheableSettings from "$lib/extension/base/CacheableSettings.ts"; + +interface SearchSettingsFields { + suggestProperties: boolean; + suggestPropertiesPosition: "start" | "end"; +} + +export default class SearchSettings extends CacheableSettings { + constructor() { + super("search"); + } + + async resolvePropertiesSuggestionsEnabled() { + return this._resolveSetting("suggestProperties", false); + } + + async resolvePropertiesSuggestionsPosition() { + return this._resolveSetting("suggestPropertiesPosition", "start"); + } + + async setPropertiesSuggestions(isEnabled: boolean) { + return this._writeSetting("suggestProperties", isEnabled); + } + + async setPropertiesSuggestionsPosition(position: "start" | "end") { + return this._writeSetting("suggestPropertiesPosition", position); + } +} diff --git a/src/stores/maintenance-profiles-store.js b/src/stores/maintenance-profiles-store.js index 95e1a70..db0ce85 100644 --- a/src/stores/maintenance-profiles-store.js +++ b/src/stores/maintenance-profiles-store.js @@ -1,6 +1,6 @@ import {writable} from "svelte/store"; import MaintenanceProfile from "$entities/MaintenanceProfile.ts"; -import MaintenanceSettings from "$lib/extension/settings/MaintenanceSettings.js"; +import MaintenanceSettings from "$lib/extension/settings/MaintenanceSettings.ts"; /** * Store for working with maintenance profiles in the Svelte popup. diff --git a/src/stores/misc-preferences.js b/src/stores/misc-preferences.js index 22eb309..53c7c04 100644 --- a/src/stores/misc-preferences.js +++ b/src/stores/misc-preferences.js @@ -1,5 +1,5 @@ import {writable} from "svelte/store"; -import MiscSettings from "$lib/extension/settings/MiscSettings.js"; +import MiscSettings from "$lib/extension/settings/MiscSettings.ts"; export const fullScreenViewerEnabled = writable(true); @@ -13,6 +13,6 @@ Promise.allSettled([ }); miscSettings.subscribe(settings => { - fullScreenViewerEnabled.set(settings.fullscreenViewer); + fullScreenViewerEnabled.set(Boolean(settings.fullscreenViewer)); }); }); diff --git a/src/stores/search-preferences.js b/src/stores/search-preferences.js index 5d01486..6d35192 100644 --- a/src/stores/search-preferences.js +++ b/src/stores/search-preferences.js @@ -1,5 +1,5 @@ import {writable} from "svelte/store"; -import SearchSettings from "$lib/extension/settings/SearchSettings.js"; +import SearchSettings from "$lib/extension/settings/SearchSettings.ts"; export const searchPropertiesSuggestionsEnabled = writable(false); @@ -23,7 +23,7 @@ Promise.allSettled([ }); searchSettings.subscribe(settings => { - searchPropertiesSuggestionsEnabled.set(settings.suggestProperties); - searchPropertiesSuggestionsPosition.set(settings.suggestPropertiesPosition); + searchPropertiesSuggestionsEnabled.set(Boolean(settings.suggestProperties)); + searchPropertiesSuggestionsPosition.set(settings.suggestPropertiesPosition || 'start'); }); })