-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78ffb4d
commit 2867c92
Showing
10 changed files
with
344 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {defer, merge, Observable, of, switchMap} from 'rxjs'; | ||
|
||
declare global { | ||
interface Window { | ||
/** | ||
* query local fonts | ||
* | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/API/Window/queryLocalFonts MDN Reference} | ||
*/ | ||
queryLocalFonts?: (options?: { | ||
postscriptNames: string[]; | ||
}) => Promise<FontData[]>; | ||
} | ||
|
||
interface FontData { | ||
/** | ||
* the family of the font face | ||
*/ | ||
readonly family: string; | ||
/** | ||
* the full name of the font face | ||
*/ | ||
readonly fullName: string; | ||
/** | ||
* the PostScript name of the font face | ||
*/ | ||
readonly postscriptName: string; | ||
/** | ||
* the style of the font face | ||
*/ | ||
readonly style: string; | ||
/** | ||
* get a Promise that fulfills with a Blob containing the raw bytes of the underlying font file | ||
*/ | ||
readonly blob: () => Promise<Blob>; | ||
} | ||
} | ||
|
||
export function isLocalFontsFeatureSupported() { | ||
const {queryLocalFonts} = window; | ||
return !!queryLocalFonts; | ||
} | ||
|
||
export async function checkLocalFontPermission() { | ||
const {navigator} = window; | ||
return navigator.permissions.query({ | ||
name: 'local-fonts', | ||
// TODO: extend dom interfaces? | ||
} as unknown as PermissionDescriptor); | ||
} | ||
|
||
export const checkLocalFontPermission$ = defer(() => | ||
checkLocalFontPermission(), | ||
).pipe( | ||
switchMap(permission => { | ||
const permissionStateChange$ = new Observable<PermissionState>(observer => { | ||
permission.onchange = function (this) { | ||
observer.next(this.state); | ||
}; | ||
}); | ||
return merge(of(permission.state), permissionStateChange$); | ||
}), | ||
); |
Oops, something went wrong.