Skip to content

Commit 6ca46ec

Browse files
fix: Font upload UX
- Ensure font is loaded properly - Show some feedback on font upload
1 parent efb4758 commit 6ca46ec

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

frontend/src/components/Controls/FontUploader.vue

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import userFont from "@/data/userFonts";
1111
import blockController from "@/utils/blockController";
1212
import { getFontName } from "@/utils/helpers";
1313
import { FileUploadHandler } from "frappe-ui";
14+
import { nextTick } from "vue";
15+
import { toast } from "vue-sonner";
1416
const emit = defineEmits(["change"]);
1517

1618
type FileDoc = {
@@ -22,28 +24,34 @@ const openFileSelector = () => {
2224
const input = document.createElement("input");
2325
input.type = "file";
2426
input.accept = ".woff2,.woff,.ttf,.otf";
25-
input.onchange = (e: Event) => {
26-
const file = (e.target as HTMLInputElement)?.files?.[0];
27-
if (file) {
28-
const fileUploadHandler = new FileUploadHandler();
29-
fileUploadHandler
30-
.upload(file, {
31-
private: false,
32-
folder: "Home/Builder Uploads/Fonts",
33-
})
34-
.then((fileDoc: FileDoc) => {
35-
uploadFont(fileDoc);
36-
});
37-
}
38-
};
27+
input.onchange = (e) =>
28+
toast.promise(
29+
new Promise(async (resolve) => {
30+
const file = (e.target as HTMLInputElement)?.files?.[0];
31+
if (file) {
32+
const fileUploadHandler = new FileUploadHandler();
33+
const fileDoc = await fileUploadHandler.upload(file, {
34+
private: false,
35+
folder: "Home/Builder Uploads/Fonts",
36+
});
37+
await uploadFont(fileDoc);
38+
}
39+
resolve(null);
40+
}),
41+
{
42+
loading: "Uploading font...",
43+
success: () => "Font uploaded",
44+
},
45+
);
3946
input.click();
4047
};
4148

4249
const uploadFont = async (file: FileDoc) => {
4350
const fontFamilyName = await getFontName(file.file_url);
4451
const fontURL = file.file_url;
4552
const fontFace = new FontFace(fontFamilyName, `url("${fontURL}")`);
46-
await fontFace.load();
53+
const loadedFont = await fontFace.load();
54+
document.fonts.add(loadedFont);
4755
try {
4856
await userFont.insert.submit({
4957
font_name: fontFace.family,
@@ -55,9 +63,11 @@ const uploadFont = async (file: FileDoc) => {
5563
console.log("Font already exists");
5664
}
5765
}
66+
await userFont.fetch();
67+
emit("change");
68+
await nextTick();
5869
if (blockController.isBLockSelected()) {
5970
blockController.setFontFamily(fontFace.family);
6071
}
61-
emit("change");
6272
};
6373
</script>

frontend/src/utils/fontManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const setFontRequested = (font: string) => {
1212
};
1313

1414
const setFont = (font: string | null, weight: string | null) => {
15-
let fontId = "";
15+
let fontId = font || "";
1616
return new Promise((resolve) => {
1717
if (!font) {
1818
return resolve(font);

0 commit comments

Comments
 (0)