fix incorrect font rendered on late load (#1555)

This commit is contained in:
David Luzar 2020-05-08 10:42:51 +02:00 committed by GitHub
parent 4696c9ee0e
commit 8c8458ceb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 19 deletions

View File

@ -114,7 +114,6 @@ import {
ENV, ENV,
} from "../constants"; } from "../constants";
import { import {
FONT_LOAD_THRESHOLD,
INITAL_SCENE_UPDATE_TIMEOUT, INITAL_SCENE_UPDATE_TIMEOUT,
TAP_TWICE_TIMEOUT, TAP_TWICE_TIMEOUT,
} from "../time_constants"; } from "../time_constants";
@ -303,6 +302,15 @@ class App extends React.Component<any, AppState> {
event.preventDefault(); event.preventDefault();
}; };
private onFontLoaded = () => {
globalSceneState.getElementsIncludingDeleted().forEach((element) => {
if (isTextElement(element)) {
invalidateShapeForElement(element);
}
});
this.onSceneUpdated();
};
private initializeScene = async () => { private initializeScene = async () => {
const searchParams = new URLSearchParams(window.location.search); const searchParams = new URLSearchParams(window.location.search);
const id = searchParams.get("id"); 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) { if (this.state.isLoading) {
this.setState({ isLoading: false }); 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.DRAG_OVER, this.disableEvent, false);
window.addEventListener(EVENT.DROP, 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 // Safari-only desktop pinch zoom
document.addEventListener( document.addEventListener(
EVENT.GESTURE_START, EVENT.GESTURE_START,

4
src/global.d.ts vendored
View File

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

View File

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