Skip to content

Commit

Permalink
implement custom font family text input (#141)
Browse files Browse the repository at this point in the history
Co-authored-by: April Sylph <[email protected]>
  • Loading branch information
marcustyphoon and AprilSylph authored Jun 2, 2024
1 parent bb0c956 commit 272e51d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/browser_action/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ main > div + div {
border-top: 1px dotted rgb(var(--passive-grey));
}

h4 {
h4, h5 {
margin: 0;
}

Expand All @@ -66,6 +66,10 @@ label {
min-width: max-content;
}

label ~ label {
margin-top: 1ch;
}

button {
padding: 0;
border: none;
Expand Down Expand Up @@ -116,3 +120,7 @@ a {
:visited {
color: rgb(var(--accent));
}

select:not([data-value="custom"]) ~ :is([for="font-family-custom"], [id="font-family-custom"]) {
display: none;
}
3 changes: 3 additions & 0 deletions src/browser_action/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ <h4>Change Palette</h4>
<option value="cursive">Cursive</option>
<option value="fantasy">Fantasy</option>
</optgroup>
<option value="custom">Custom</option>
</select>
<label for="font-family-custom"><h5>Custom <code>font-family</code> String</h4></label>
<input id="font-family-custom" type="text" value="">
</div>
<div>
<label for="font-size"><h4>Change Base Font Size</h4></label>
Expand Down
14 changes: 13 additions & 1 deletion src/browser_action/render_font_family.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
const fontFamilySelect = document.getElementById('font-family');
const customFontFamilyInput = document.getElementById('font-family-custom');

const writeFontFamily = async function ({ target: { value } }) {
fontFamilySelect.dataset.value = value;
browser.storage.local.set({ fontFamily: value });
};

const writeCustomFontFamily = async function ({ target: { value } }) {
browser.storage.local.set({ customFontFamily: value });
};

const renderFontFamily = async function () {
const { fontFamily = '' } = await browser.storage.local.get('fontFamily');
const fontFamilySelect = document.getElementById('font-family');
const { customFontFamily = '' } = await browser.storage.local.get('customFontFamily');

fontFamilySelect.addEventListener('input', writeFontFamily);
customFontFamilyInput.addEventListener('input', writeCustomFontFamily);

[...fontFamilySelect.options].forEach(option => {
option.selected = option.value === fontFamily;
});
fontFamilySelect.dataset.value = fontFamily;

customFontFamilyInput.value = customFontFamily;
};

renderFontFamily();
11 changes: 8 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ const applyCurrentPalette = async function () {

const applyFontFamily = async function () {
const { fontFamily = '' } = await browser.storage.local.get('fontFamily');
document.documentElement.style.setProperty('--font-family', fontFamily);
const { customFontFamily = '' } = await browser.storage.local.get('customFontFamily');

document.documentElement.style.setProperty(
'--font-family',
fontFamily === 'custom' ? customFontFamily : fontFamily
);
};

const applyFontSize = async function () {
Expand All @@ -53,13 +58,13 @@ const onStorageChanged = async function (changes, areaName) {
return;
}

const { currentPalette, fontFamily, fontSize } = changes;
const { currentPalette, fontFamily, customFontFamily, fontSize } = changes;

if (currentPalette || Object.keys(changes).some(key => key.startsWith('palette:'))) {
applyCurrentPalette();
}

if (fontFamily) applyFontFamily();
if (fontFamily || customFontFamily) applyFontFamily();
if (fontSize) applyFontSize();
};

Expand Down

0 comments on commit 272e51d

Please sign in to comment.