Quellcode durchsuchen

fix incorrect font rendered on late load (#1555)

David Luzar vor 5 Jahren
Ursprung
Commit
8c8458ceb0
3 geänderte Dateien mit 16 neuen und 19 gelöschten Zeilen
  1. 12 18
      src/components/App.tsx
  2. 4 0
      src/global.d.ts
  3. 0 1
      src/time_constants.ts

+ 12 - 18
src/components/App.tsx

@@ -114,7 +114,6 @@ import {
   ENV,
 } from "../constants";
 import {
-  FONT_LOAD_THRESHOLD,
   INITAL_SCENE_UPDATE_TIMEOUT,
   TAP_TWICE_TIMEOUT,
 } from "../time_constants";
@@ -303,6 +302,15 @@ class App extends React.Component<any, AppState> {
     event.preventDefault();
   };
 
+  private onFontLoaded = () => {
+    globalSceneState.getElementsIncludingDeleted().forEach((element) => {
+      if (isTextElement(element)) {
+        invalidateShapeForElement(element);
+      }
+    });
+    this.onSceneUpdated();
+  };
+
   private initializeScene = async () => {
     const searchParams = new URLSearchParams(window.location.search);
     const id = searchParams.get("id");
@@ -327,23 +335,6 @@ class App extends React.Component<any, AppState> {
       }
     }
 
-    // rerender text elements on font load to fix #637
-    try {
-      await Promise.race([
-        document.fonts?.ready?.then(() => {
-          globalSceneState.getElementsIncludingDeleted().forEach((element) => {
-            if (isTextElement(element)) {
-              invalidateShapeForElement(element);
-            }
-          });
-        }),
-        // if fonts don't load in 1s for whatever reason, don't block the UI
-        new Promise((resolve) => setTimeout(resolve, FONT_LOAD_THRESHOLD)),
-      ]);
-    } catch (error) {
-      console.error(error);
-    }
-
     if (this.state.isLoading) {
       this.setState({ isLoading: false });
     }
@@ -402,6 +393,9 @@ class App extends React.Component<any, AppState> {
     window.addEventListener(EVENT.DRAG_OVER, this.disableEvent, false);
     window.addEventListener(EVENT.DROP, this.disableEvent, false);
 
+    // rerender text elements on font load to fix #637 && #1553
+    document.fonts?.addEventListener?.("loadingdone", this.onFontLoaded);
+
     // Safari-only desktop pinch zoom
     document.addEventListener(
       EVENT.GESTURE_START,

+ 4 - 0
src/global.d.ts

@@ -1,6 +1,10 @@
 interface Document {
   fonts?: {
     ready?: Promise<void>;
+    addEventListener?(
+      type: "loading" | "loadingdone" | "loadingerror",
+      listener: (this: Document, ev: Event) => any,
+    ): void;
   };
 }
 

+ 0 - 1
src/time_constants.ts

@@ -1,4 +1,3 @@
 // time in milliseconds
-export const FONT_LOAD_THRESHOLD = 1000;
 export const TAP_TWICE_TIMEOUT = 300;
 export const INITAL_SCENE_UPDATE_TIMEOUT = 5000;