Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add events for config value changes #144

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const CONFIG_KEY = 'ytaf-configuration';

export const configOptions = new Map([
const configOptions = new Map([
['enableAdBlock', { default: true, desc: 'Enable ad blocking' }],
['enableSponsorBlock', { default: true, desc: 'Enable SponsorBlock' }],
[
Expand Down Expand Up @@ -40,6 +40,15 @@ const defaultConfig = (() => {
return ret;
})();

/** @type {Record<string, DocumentFragment>} as const */
const configFrags = (() => {
let ret = {};
for (const k of configOptions.keys()) {
ret[k] = new DocumentFragment();
}
return ret;
})();

function loadStoredConfig() {
const storage = window.localStorage.getItem(CONFIG_KEY);

Expand All @@ -63,7 +72,7 @@ function configExists(key) {
return configOptions.has(key);
}

export function getConfigDesc(key) {
export function configGetDesc(key) {
if (!configExists(key)) {
throw new Error('tried to get desc for unknown config key: ' + key);
}
Expand Down Expand Up @@ -95,7 +104,38 @@ export function configWrite(key, value) {
throw new Error('tried to write unknown config key: ' + key);
}

console.info('Setting key', key, 'to', value);
const oldValue =
localConfig[key] !== undefined ? localConfig[key] : defaultConfig[key];

console.info('Changing key', key, 'from', oldValue, 'to', value);
localConfig[key] = value;
window.localStorage[CONFIG_KEY] = JSON.stringify(localConfig);

configFrags[key].dispatchEvent(
new CustomEvent('ytafConfigChange', {
detail: { key, newValue: value, oldValue }
})
);
}

/**
* Add a listener for changes in the value of a specified config option
* @param {string} key Config option to monitor
* @param {(evt: Event) => void} callback Function to be called on change
*/
export function configAddChangeListener(key, callback) {
const frag = configFrags[key];

frag.addEventListener('ytafConfigChange', callback);
}

/**
* Remove a listener for changes in the value of a specified config option
* @param {string} key Config option to monitor
* @param {(evt: Event) => void} callback Function to be called on change
*/
export function configRemoveChangeListener(key, callback) {
const frag = configFrags[key];

frag.removeEventListener('ytafConfigChange', callback);
}
14 changes: 11 additions & 3 deletions src/ui.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*global navigate*/
import './spatial-navigation-polyfill.js';
import './ui.css';
import { configRead, configWrite, getConfigDesc } from './config.js';
import { configRead, configWrite, configGetDesc } from './config.js';

// We handle key events ourselves.
window.__spatialNavigation__.keyMode = 'NONE';
Expand Down Expand Up @@ -41,14 +41,22 @@ function createConfigCheckbox(key) {
const elmInput = document.createElement('input');
elmInput.type = 'checkbox';
elmInput.checked = configRead(key);
elmInput.addEventListener('change', (evt) => {

/** @type {(evt: Event) => void} */
const changeHandler = (evt) => {
configWrite(key, evt.target.checked);
};

elmInput.addEventListener('change', changeHandler);

configAddChangeListener(key, (evt) => {
elmInput.checked = evt.detail.newValue;
});

const elmLabel = document.createElement('label');
elmLabel.appendChild(elmInput);
// Use non-breaking space (U+00A0)
elmLabel.appendChild(document.createTextNode('\u00A0' + getConfigDesc(key)));
elmLabel.appendChild(document.createTextNode('\u00A0' + configGetDesc(key)));

return elmLabel;
}
Expand Down