Skip to content

Build your personal knowledge base with Trilium Notes

fix(canvas): loading of fonts under dev mode

Elian Doran89d32db6017c
diff --git a/apps/client/src/types-assets.d.ts b/apps/client/src/types-assets.d.ts
index 34a964be84..010ec6b44b 100644
--- a/apps/client/src/types-assets.d.ts
+++ b/apps/client/src/types-assets.d.ts
@@ -3,7 +3,7 @@ declare module "*.png" {
export default path;
}
-declare module "@triliumnext/ckeditor5/emoji_definitions/en.json?url" {
+declare module "*?url" {
var path: string;
export default path;
}
diff --git a/apps/client/src/types.d.ts b/apps/client/src/types.d.ts
index 113b94d769..be42284c78 100644
--- a/apps/client/src/types.d.ts
+++ b/apps/client/src/types.d.ts
@@ -57,6 +57,8 @@ declare global {
process?: ElectronProcess;
glob?: CustomGlobals;
+
+ EXCALIDRAW_ASSET_PATH?: string;
}
interface AutoCompleteConfig {
diff --git a/apps/client/src/widgets/type_widgets/canvas.ts b/apps/client/src/widgets/type_widgets/canvas.ts
index a018c7dcf8..a15715c648 100644
--- a/apps/client/src/widgets/type_widgets/canvas.ts
+++ b/apps/client/src/widgets/type_widgets/canvas.ts
@@ -113,10 +113,6 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
constructor() {
super();
- // currently required by excalidraw, in order to allows self-hosting fonts locally.
- // this avoids making excalidraw load the fonts from an external CDN.
- (window as any).EXCALIDRAW_ASSET_PATH = `${window.location.pathname}/node_modules/@excalidraw/excalidraw/dist/prod`;
-
// temporary vars
this.currentNoteId = "";
@@ -187,6 +183,8 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
this.saveData();
},
});
+
+ await setupFonts();
this.canvasInstance.renderCanvas(renderElement);
}
@@ -377,3 +375,22 @@ export default class ExcalidrawTypeWidget extends TypeWidget {
}
}
+
+async function setupFonts() {
+ if (window.EXCALIDRAW_ASSET_PATH) {
+ return;
+ }
+
+ // currently required by excalidraw, in order to allows self-hosting fonts locally.
+ // this avoids making excalidraw load the fonts from an external CDN.
+ let path: string;
+ if (!glob.isDev) {
+ path = `${window.location.pathname}/node_modules/@excalidraw/excalidraw/dist/prod`;
+ } else {
+ path = (await import("../../../node_modules/@excalidraw/excalidraw/dist/prod/fonts/Excalifont/Excalifont-Regular-a88b72a24fb54c9f94e3b5fdaa7481c9.woff2?url")).default;
+ let pathComponents = path.split("/");
+ path = pathComponents.slice(0, pathComponents.length - 2).join("/");
+ }
+
+ window.EXCALIDRAW_ASSET_PATH = path;
+}