|
| 1 | +import { Command } from "../types"; |
| 2 | +import { buildCommandForConfigKey } from "../util"; |
| 3 | +import FileStorage from "../../utils/file-storage"; |
| 4 | +import { applyFontFamily } from "../../controllers/theme-controller"; |
| 5 | +import { updateUI } from "../../elements/settings/custom-font-picker"; |
| 6 | +import * as Notifications from "../../elements/notifications"; |
| 7 | +import Config, * as UpdateConfig from "../../config"; |
| 8 | + |
| 9 | +const fromMeta = buildCommandForConfigKey("fontFamily"); |
| 10 | + |
| 11 | +if (fromMeta.subgroup) { |
| 12 | + fromMeta.subgroup.list.push({ |
| 13 | + id: "customFont", |
| 14 | + display: "Custom font...", |
| 15 | + icon: "fa-font", |
| 16 | + alias: "custom font options", |
| 17 | + subgroup: { |
| 18 | + title: "Custom font...", |
| 19 | + list: [ |
| 20 | + { |
| 21 | + id: "customFontName", |
| 22 | + display: "Custom name...", |
| 23 | + icon: "fa-font", |
| 24 | + alias: "custom font name", |
| 25 | + input: true, |
| 26 | + defaultValue: (): string => { |
| 27 | + return Config.fontFamily.replace(/_/g, " "); |
| 28 | + }, |
| 29 | + exec: ({ input }): void => { |
| 30 | + if (input === undefined || input === "") return; |
| 31 | + const fontName = input.replaceAll(/ /g, "_"); |
| 32 | + UpdateConfig.setFontFamily(fontName); |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + id: "customLocalFont", |
| 37 | + display: "Local font...", |
| 38 | + icon: "fa-file-import fa-fw", |
| 39 | + alias: "upload font", |
| 40 | + available: async (): Promise<boolean> => { |
| 41 | + return !(await FileStorage.hasFile("LocalFontFamilyFile")); |
| 42 | + }, |
| 43 | + exec: async (): Promise<void> => { |
| 44 | + const inputElement = document.createElement("input"); |
| 45 | + inputElement.type = "file"; |
| 46 | + inputElement.accept = "font/woff,font/woff2,font/ttf,font/otf"; |
| 47 | + inputElement.style.display = "none"; |
| 48 | + document.body.appendChild(inputElement); |
| 49 | + |
| 50 | + const cleanup = (): void => { |
| 51 | + document.body.removeChild(inputElement); |
| 52 | + }; |
| 53 | + |
| 54 | + inputElement.onchange = async (event) => { |
| 55 | + const file = (event.target as HTMLInputElement).files?.[0]; |
| 56 | + if (!file) { |
| 57 | + cleanup(); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // check type |
| 62 | + if ( |
| 63 | + !file.type.match(/font\/(woff|woff2|ttf|otf)/) && |
| 64 | + !file.name.match(/\.(woff|woff2|ttf|otf)$/i) |
| 65 | + ) { |
| 66 | + Notifications.add( |
| 67 | + "Unsupported font format, must be woff, woff2, ttf or otf.", |
| 68 | + 0 |
| 69 | + ); |
| 70 | + cleanup(); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const reader = new FileReader(); |
| 75 | + reader.onload = async (readerEvent) => { |
| 76 | + const dataUrl = readerEvent.target?.result as string; |
| 77 | + try { |
| 78 | + await FileStorage.storeFile("LocalFontFamilyFile", dataUrl); |
| 79 | + await applyFontFamily(); |
| 80 | + await updateUI(); |
| 81 | + } catch (e) { |
| 82 | + Notifications.add( |
| 83 | + "Error uploading font: " + (e as Error).message, |
| 84 | + 0 |
| 85 | + ); |
| 86 | + } |
| 87 | + cleanup(); |
| 88 | + }; |
| 89 | + reader.onerror = cleanup; |
| 90 | + reader.readAsDataURL(file); |
| 91 | + }; |
| 92 | + inputElement.click(); |
| 93 | + }, |
| 94 | + }, |
| 95 | + { |
| 96 | + id: "removeLocalFont", |
| 97 | + display: "Remove local font", |
| 98 | + icon: "fa-trash", |
| 99 | + alias: "remove font", |
| 100 | + available: async (): Promise<boolean> => { |
| 101 | + return await FileStorage.hasFile("LocalFontFamilyFile"); |
| 102 | + }, |
| 103 | + exec: async (): Promise<void> => { |
| 104 | + try { |
| 105 | + await FileStorage.deleteFile("LocalFontFamilyFile"); |
| 106 | + await updateUI(); |
| 107 | + await applyFontFamily(); |
| 108 | + } catch (e) { |
| 109 | + Notifications.add( |
| 110 | + "Error removing font: " + (e as Error).message, |
| 111 | + 0 |
| 112 | + ); |
| 113 | + } |
| 114 | + }, |
| 115 | + }, |
| 116 | + ], |
| 117 | + }, |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +const commands: Command[] = [fromMeta]; |
| 122 | + |
| 123 | +export default commands; |
0 commit comments