[css-font-loading-module] Make FontFaceSet extend EventTarget and add FontFaceSetLoadEvent (#43396)

This commit is contained in:
horsenit 2020-03-31 17:47:56 -04:00 committed by GitHub
parent 480921ca85
commit 2892d81ccb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View File

@ -18,4 +18,12 @@ contexts.forEach(context => {
const b: boolean = context.fonts.check("12px Example", "ß");
const c: Promise<FontFace[]> = context.fonts.load("12px MyFont", "ß").then();
const d: Promise<typeof context.fonts> = context.fonts.ready.then();
const e: FontFaceSetLoadEvent = new FontFaceSetLoadEvent('loading', {fontfaces: []});
context.fonts.addEventListener('loading', (evt) => {
evt.fontfaces;
});
context.fonts.onloadingdone = (evt) => {
evt.fontfaces;
};
context.fonts.dispatchEvent(e);
});

View File

@ -6,7 +6,6 @@
export type FontFaceLoadStatus = 'unloaded' | 'loading' | 'loaded' | 'error';
export type FontFaceSetLoadStatus = 'loading' | 'loaded';
export type BinaryData = ArrayBuffer | ArrayBufferView;
export type EventHandler = (event: Event) => void;
export interface FontFaceDescriptors {
style?: string;
@ -17,11 +16,27 @@ export interface FontFaceDescriptors {
featureSettings?: string;
}
export interface FontFaceSet extends Set<FontFace> {
export interface FontFaceSetLoadEventInit extends EventInit {
fontfaces?: FontFace[];
}
export interface FontFaceSetEventMap {
"loading": (this: FontFaceSet, event: FontFaceSetLoadEvent) => any;
"loadingdone": (this: FontFaceSet, event: FontFaceSetLoadEvent) => any;
"loadingerror": (this: FontFaceSet, event: FontFaceSetLoadEvent) => any;
}
export interface FontFaceSet extends Set<FontFace>, EventTarget {
// events for when loading state changes
onloading: EventHandler;
onloadingdone: EventHandler;
onloadingerror: EventHandler;
onloading: ((this: FontFaceSet, event: FontFaceSetLoadEvent) => any) | null;
onloadingdone: ((this: FontFaceSet, event: FontFaceSetLoadEvent) => any) | null;
onloadingerror: ((this: FontFaceSet, event: FontFaceSetLoadEvent) => any) | null;
// EventTarget
addEventListener<K extends keyof FontFaceSetEventMap>(type: K, listener: FontFaceSetEventMap[K], options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof FontFaceSetEventMap>(type: K, listener: FontFaceSetEventMap[K], options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
// check and start loads if appropriate
// and fulfill promise when all loads complete
@ -55,6 +70,11 @@ declare global {
readonly loaded: Promise<FontFace>;
}
class FontFaceSetLoadEvent extends Event {
constructor(type: string, eventInitDict?: FontFaceSetLoadEventInit);
readonly fontfaces: FontFace[];
}
interface Document {
fonts: FontFaceSet;
}