Skip to content

Commit

Permalink
Remember font per book collection
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvire committed Dec 4, 2024
1 parent 9ebd58f commit 42e04ca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/lib/components/FontSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Font Selector component.
<script>
import Modal from './Modal.svelte';
import FontList from './FontList.svelte';
import { convertStyle, currentFont, s, t } from '$lib/data/stores';
import { convertStyle, currentFont, currentFonts, refs, s, t } from '$lib/data/stores';
const modalId = 'fontSelector';
let modal;
Expand All @@ -17,7 +17,10 @@ Font Selector component.
}
function handleOk() {
$currentFont = fontList.selectedFont;
currentFonts.update((fonts) => {
fonts[$refs.collection] = fontList.selectedFont;
return fonts;
});
}
</script>

Expand Down
30 changes: 23 additions & 7 deletions src/lib/data/stores/scripture.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,29 @@ export const glossary = derived(docSet, async ($docSet) => {
}
return glossaryResults;
});
export const currentFont = writable(config.fonts[0].family);

function getDefaultCurrentFonts() {
const currentFonts = {};
for (let collection of config.bookCollections) {

Check failure on line 152 in src/lib/data/stores/scripture.js

View workflow job for this annotation

GitHub Actions / test

src/lib/scripts/scripture-reference-utils.test.ts

TypeError: default.bookCollections is not iterable ❯ getDefaultCurrentFonts src/lib/data/stores/scripture.js:152:35 ❯ src/lib/data/stores/scripture.js:162:50 ❯ src/lib/data/stores/annotation.js:2:31

Check failure on line 152 in src/lib/data/stores/scripture.js

View workflow job for this annotation

GitHub Actions / test

src/lib/search/data/test/search-config-repository-impl.test.ts

TypeError: default.bookCollections is not iterable ❯ getDefaultCurrentFonts src/lib/data/stores/scripture.js:152:35 ❯ src/lib/data/stores/scripture.js:162:50 ❯ src/lib/data/stores/annotation.js:2:31
// Sometimes, the collection.style.font doesn't exist in the array of fonts!
currentFonts[collection.id] =
collection.style.font &&
config.fonts.some((font) => font.family === collection.style.font)
? collection.style.font
: config.fonts[0].family;
}
return currentFonts;
}
setDefaultStorage('currentFonts', JSON.stringify(getDefaultCurrentFonts()));

export const currentFonts = writable(JSON.parse(localStorage.currentFonts));
currentFonts.subscribe((fonts) => (localStorage.currentFonts = JSON.stringify(fonts)));

export const currentFont = derived([refs, currentFonts], ([$refs, $currentFonts]) => {
if (!$refs.initialized) return config.fonts[0].family;
return $currentFonts[$refs.collection];
});

export const fontChoices = derived(refs, ($refs) => {
if (!$refs.initialized) return [];
const bookFonts = config.bookCollections
Expand All @@ -156,12 +178,6 @@ export const fontChoices = derived(refs, ($refs) => {
const allFonts = [...new Set(config.fonts.map((x) => x.family))];
const currentFonts =
bookFonts?.length > 0 ? bookFonts : colFonts.length > 0 ? colFonts : allFonts;
currentFont.update((current) => {
if (currentFonts.indexOf(current) === -1) {
return currentFonts[0];
}
return current;
});
return currentFonts;
});

Expand Down

0 comments on commit 42e04ca

Please sign in to comment.