menu: store theme outside profile, reload env on profile change

This commit is contained in:
dragonwocky 2021-10-03 15:08:42 +11:00
parent 42cfc69357
commit 9dcbfc213f
4 changed files with 13 additions and 9 deletions

View File

@ -25,6 +25,7 @@ export const localPath = fs.localPath;
* fetch and parse a json file's contents * fetch and parse a json file's contents
* @type {function} * @type {function}
* @param {string} path - a url or within-the-enhancer filepath * @param {string} path - a url or within-the-enhancer filepath
* @param {object} [opts] - the second argument of a fetch() request
* @returns {object} the json value of the requested file as a js object * @returns {object} the json value of the requested file as a js object
*/ */
export const getJSON = fs.getJSON; export const getJSON = fs.getJSON;
@ -33,6 +34,7 @@ export const getJSON = fs.getJSON;
* fetch a text file's contents * fetch a text file's contents
* @type {function} * @type {function}
* @param {string} path - a url or within-the-enhancer filepath * @param {string} path - a url or within-the-enhancer filepath
* @param {object} [opts] - the second argument of a fetch() request
* @returns {string} the text content of the requested file * @returns {string} the text content of the requested file
*/ */
export const getText = fs.getText; export const getText = fs.getText;

10
extension/env/fs.mjs vendored
View File

@ -21,18 +21,20 @@ export const localPath = chrome.runtime.getURL;
/** /**
* fetch and parse a json file's contents * fetch and parse a json file's contents
* @param {string} path - a url or within-the-enhancer filepath * @param {string} path - a url or within-the-enhancer filepath
* @param {object} [opts] - the second argument of a fetch() request
* @returns {object} the json value of the requested file as a js object * @returns {object} the json value of the requested file as a js object
*/ */
export const getJSON = (path) => export const getJSON = (path, opts = {}) =>
fetch(path.startsWith('http') ? path : localPath(path)).then((res) => res.json()); fetch(path.startsWith('http') ? path : localPath(path), opts).then((res) => res.json());
/** /**
* fetch a text file's contents * fetch a text file's contents
* @param {string} path - a url or within-the-enhancer filepath * @param {string} path - a url or within-the-enhancer filepath
* @param {object} [opts] - the second argument of a fetch() request
* @returns {string} the text content of the requested file * @returns {string} the text content of the requested file
*/ */
export const getText = (path) => export const getText = (path, opts = {}) =>
fetch(path.startsWith('http') ? path : localPath(path)).then((res) => res.text()); fetch(path.startsWith('http') ? path : localPath(path), opts).then((res) => res.text());
/** /**
* check if a file exists * check if a file exists

View File

@ -12,7 +12,7 @@ export default async function (api, db) {
web.addHotkeyListener(await db.get(['hotkey']), env.focusMenu); web.addHotkeyListener(await db.get(['hotkey']), env.focusMenu);
const updateTheme = () => const updateTheme = () =>
db.set(['theme'], document.querySelector('.notion-dark-theme') ? 'dark' : 'light'); storage.set(['theme'], document.querySelector('.notion-dark-theme') ? 'dark' : 'light');
web.addDocumentObserver((mutation) => { web.addDocumentObserver((mutation) => {
if (mutation.target === document.body && document.hasFocus()) updateTheme(); if (mutation.target === document.body && document.hasFocus()) updateTheme();
}); });

View File

@ -25,7 +25,7 @@ web.addHotkeyListener(await db.get(['hotkey']), env.focusNotion);
const loadTheme = async () => { const loadTheme = async () => {
document.documentElement.className = document.documentElement.className =
(await db.get(['theme'], 'light')) === 'dark' ? 'dark' : ''; (await storage.get(['theme'], 'light')) === 'dark' ? 'dark' : '';
}; };
document.addEventListener('visibilitychange', loadTheme); document.addEventListener('visibilitychange', loadTheme);
loadTheme(); loadTheme();
@ -110,7 +110,7 @@ $profile.addEventListener('click', async (event) => {
const profileUpload = JSON.parse(progress.currentTarget.result); const profileUpload = JSON.parse(progress.currentTarget.result);
if (!profileUpload) throw Error; if (!profileUpload) throw Error;
await storage.set(['profiles', $select.value], profileUpload); await storage.set(['profiles', $select.value], profileUpload);
location.reload(); env.reload();
} catch { } catch {
web.render(web.empty($error), 'Invalid JSON uploaded.'); web.render(web.empty($error), 'Invalid JSON uploaded.');
} }
@ -151,7 +151,7 @@ $profile.addEventListener('click', async (event) => {
); );
await storage.set(['profiles', $select.value], undefined); await storage.set(['profiles', $select.value], undefined);
} }
location.reload(); env.reload();
}); });
$delete.addEventListener('click', async (event) => { $delete.addEventListener('click', async (event) => {
await storage.set(['profiles', $select.value], undefined); await storage.set(['profiles', $select.value], undefined);
@ -159,7 +159,7 @@ $profile.addEventListener('click', async (event) => {
['currentprofile'], ['currentprofile'],
profileNames.find((profile) => profile !== $select.value) || 'default' profileNames.find((profile) => profile !== $select.value) || 'default'
); );
location.reload(); env.reload();
}); });
_$profileConfig = web.render( _$profileConfig = web.render(