Skip to content

Commit 7ff80d1

Browse files
committed
font caching
1 parent 06407db commit 7ff80d1

File tree

8 files changed

+344
-289
lines changed

8 files changed

+344
-289
lines changed

bun.lock

Lines changed: 64 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 202 additions & 195 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
"devDependencies": {
1616
"@iconify-json/tabler": "^1.2.19",
1717
"@sveltejs/adapter-auto": "^6.0.1",
18-
"@sveltejs/kit": "^2.22.0",
18+
"@sveltejs/kit": "^2.22.2",
1919
"@sveltejs/vite-plugin-svelte": "^5.1.0",
20-
"@tailwindcss/vite": "^4.1.10",
20+
"@tailwindcss/vite": "^4.1.11",
2121
"@testing-library/jest-dom": "^6.6.3",
2222
"@testing-library/svelte": "^5.2.8",
23-
"@tiptap/extension-color": "^2.22.3",
24-
"@tiptap/extension-font-family": "^2.22.3",
25-
"@tiptap/extension-text-style": "^2.22.3",
26-
"@tiptap/extension-underline": "^2.22.3",
27-
"@types/node": "^24.0.3",
23+
"@tiptap/extension-color": "^2.23.0",
24+
"@tiptap/extension-font-family": "^2.23.0",
25+
"@tiptap/extension-text-style": "^2.23.0",
26+
"@tiptap/extension-underline": "^2.23.0",
27+
"@types/node": "^24.0.4",
2828
"file-type": "^21.0.0",
2929
"jsdom": "^26.1.0",
30-
"prettier": "^3.6.0",
30+
"prettier": "^3.6.1",
3131
"prettier-plugin-svelte": "^3.4.0",
3232
"prettier-plugin-tailwindcss": "^0.6.13",
33-
"svelte": "^5.34.7",
33+
"svelte": "^5.34.8",
3434
"svelte-check": "^4.2.2",
35-
"tailwindcss": "^4.1.10",
35+
"tailwindcss": "^4.1.11",
3636
"tslib": "^2.8.1",
3737
"typescript": "^5.8.3",
3838
"unplugin-icons": "^22.1.0",
@@ -41,10 +41,11 @@
4141
},
4242
"type": "module",
4343
"dependencies": {
44-
"@tiptap/core": "^2.22.3",
45-
"@tiptap/extension-placeholder": "^2.22.3",
46-
"@tiptap/pm": "^2.22.3",
47-
"@tiptap/starter-kit": "^2.22.3",
44+
"@tiptap/core": "^2.23.0",
45+
"@tiptap/extension-placeholder": "^2.23.0",
46+
"@tiptap/pm": "^2.23.0",
47+
"@tiptap/starter-kit": "^2.23.0",
48+
"idb": "^8.0.3",
4849
"svelte-awesome-color-picker": "^4.0.2",
4950
"tippy.js": "^6.3.7",
5051
"typescript-color-gradient": "^4.0.1"

src/lib/components/modals/FontPickerModal.svelte

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,17 @@
5858
To use a custom font in the editor, upload or select one below.
5959
</p>
6060
<div class="flex flex-col space-y-1">
61-
{#key fontLUT}
62-
{#each fontLUT as [identifier, _]}
63-
<button
64-
onclick={() => {
65-
editor?.chain().focus().setFont(identifier).run();
66-
fontDialog?.close();
67-
}}
68-
class="flex h-full w-full cursor-pointer items-center space-x-2 rounded-md bg-zinc-900 p-2 hover:bg-black/50">
69-
<IconUploadFont />
70-
<span class="font-mono text-white">{identifier}</span>
71-
</button>
72-
{/each}
73-
{/key}
61+
{#each fontLUT as [identifier, _]}
62+
<button
63+
onclick={() => {
64+
editor?.chain().focus().setFont(identifier).run();
65+
fontDialog?.close();
66+
}}
67+
class="flex h-full w-full cursor-pointer items-center space-x-2 rounded-md bg-zinc-900 p-2 hover:bg-black/50">
68+
<IconUploadFont />
69+
<span class="font-mono text-white">{identifier}</span>
70+
</button>
71+
{/each}
7472
<button
7573
onclick={() => {
7674
fontDialog!.close();

src/lib/components/modals/FontUploadModal.svelte

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import Modal from "$lib/components/Modal.svelte";
3+
import { openDataStore } from "$lib/db";
34
import { fontLUT } from "$lib/tiptap/extensions/fonts";
45
import { fileTypeFromBlob } from "file-type";
56
import IconUpload from "~icons/tabler/file-upload";
@@ -8,6 +9,7 @@
89
let step = $state(1);
910
let identifier = $state("");
1011
let fontAlias = $state("");
12+
let fontData: Blob
1113
1214
async function dropHandler(
1315
e: DragEvent & {
@@ -53,14 +55,25 @@
5355
document.fonts.add(font);
5456
fontAlias = fileName;
5557
await font.load();
58+
fontData = file
5659
}
5760
step = 2;
5861
}
5962
60-
function addToFontLUT() {
63+
async function addToFontLUT() {
6164
fontLUT.set(identifier, fontAlias);
6265
fontUploadModal.close();
6366
step = 1;
67+
68+
const db = await openDataStore()
69+
await db.put("fonts", {
70+
alias: fontAlias,
71+
identifier,
72+
data: fontData
73+
})
74+
75+
identifier = "";
76+
fontAlias = "";
6477
}
6578
</script>
6679

src/lib/db.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { openDB } from "idb";
2+
3+
export async function openDataStore() {
4+
const db = await openDB("dph_data-store", 1, {
5+
upgrade(db, _old, _new, _) {
6+
db.createObjectStore("fonts", {
7+
keyPath: "alias",
8+
});
9+
},
10+
});
11+
12+
return db;
13+
}
14+

src/lib/tiptap/extensions/fonts.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Extension } from "@tiptap/core";
2+
import { SvelteMap } from "svelte/reactivity"
23

3-
export let fontLUT: Map<string, string> = new Map();
4+
export let fontLUT: SvelteMap<string, string> = new SvelteMap();
45

56
export const FontsExtension = Extension.create({
67
name: "font",
@@ -43,9 +44,7 @@ export const FontsExtension = Extension.create({
4344
}
4445

4546
return {
46-
style: `
47-
font-family: "${font}";
48-
`,
47+
style: `font-family: "${font}", monospace;`,
4948
};
5049
},
5150
},

src/routes/+page.svelte

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import UnicodeSelectorModal from "$lib/components/modals/UnicodeSelectorModal.svelte";
6262
import ToolbarButton from "$lib/components/ToolbarButton.svelte";
6363
import { tooltip } from "$lib/tooltip";
64+
import { openDataStore } from "$lib/db";
65+
import { fontLUT } from "$lib/tiptap/extensions/fonts";
6466
6567
let tiptapJSON: JSONContent = $state()!;
6668
@@ -120,7 +122,7 @@
120122
importDialog?.close();
121123
}
122124
123-
onMount(() => {
125+
async function loadData() {
124126
if (localStorage.getItem("content")) {
125127
tiptapJSON = JSON.parse(localStorage.getItem("content")!);
126128
} else {
@@ -135,6 +137,24 @@
135137
localStorage.setItem("snapshots", "");
136138
}
137139
140+
const db = await openDataStore();
141+
const fontStore = await db.getAll("fonts");
142+
143+
type FontStoreSchema = {
144+
identifier: string;
145+
alias: string;
146+
data: File;
147+
};
148+
149+
fontStore.forEach(async ({ identifier, alias, data }: FontStoreSchema) => {
150+
fontLUT.set(identifier, alias);
151+
document.fonts.add(new FontFace(identifier, await data.arrayBuffer()));
152+
});
153+
}
154+
155+
onMount(() => {
156+
loadData();
157+
138158
editor = new Editor({
139159
element: element,
140160
content: tiptapJSON,

0 commit comments

Comments
 (0)