-
-
Notifications
You must be signed in to change notification settings - Fork 1
Support packages from Typst universe #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6fefddc
Implement package registry access and cached font middleware
Splines ae3b13b
Move font cache file
Splines e8302d5
Extract registry request to new file
Splines 15e7f94
Add docstring
Splines 08b1721
Increase max height of preview pane
Splines 954de19
Add preview Typst fill color toggle
Splines 9109a64
Only allow to preview typst fill if fill is unchecked
Splines a208e55
Normalize SVG transparency
Splines c1adbb1
Don't update the preview during fill color change
Splines 6f8e359
Reuse constants
Splines 9f48b9e
Validate alpha value
Splines 1ea8408
Catch request.send()
Splines 802455d
Sync preview fill toggle state
Splines c57c2a9
Update Readme
Splines 0a4fca8
Add docs to font cache
Splines 9729823
Also check for the SVG root element in alpha normalization
Splines 31515d4
Handle missing browser cache
Splines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,57 @@ | ||
| /** | ||
| * Module for font downloading and caching in the browser. | ||
| * | ||
| * Inspired by the cached font middleware in the typst.ts compiler tempalte: | ||
| * https://github.com/Myriad-Dreamin/typst.ts/blob/2a8b32d8cca70cc4d105fef074d2f35fc7546450/templates/compiler-wasm-cjs/src/cached-font-middleware.cts#L1-L52 | ||
| */ | ||
|
|
||
| import { preloadFontAssets } from "@myriaddreamin/typst.ts/dist/esm/options.init.mjs"; | ||
|
|
||
| const FONT_CACHE_NAME = "typst-font-assets-v1"; | ||
|
|
||
| /** | ||
| * A fetch wrapper that caches font assets in the browser's Cache API. | ||
| */ | ||
| async function cachedFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> { | ||
| const request = input instanceof Request ? input : new Request(input, init); | ||
|
|
||
| if (!("caches" in globalThis) || request.method.toUpperCase() !== "GET") { | ||
| return fetch(request); | ||
| } | ||
|
|
||
| let cache: Cache | null = null; | ||
| try { | ||
| cache = await caches.open(FONT_CACHE_NAME); | ||
| const cached = await cache.match(request); | ||
| if (cached) { | ||
| // 🎈 Cached response | ||
| return cached; | ||
| } | ||
| } catch { | ||
| // No cache access possible | ||
| return fetch(request); | ||
| } | ||
|
|
||
| // 🎈 No cached response | ||
| const response = await fetch(request); | ||
| if (response.ok) { | ||
| try { | ||
| await cache.put(request, response.clone()); | ||
| } catch { | ||
| // Ignore cache write failures and keep network response. | ||
| } | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| export function cachedFontInitOptions() { | ||
| return { | ||
| beforeBuild: [ | ||
| preloadFontAssets({ | ||
| assets: ["text", "cjk", "emoji"], | ||
| fetcher: cachedFetch, | ||
| }), | ||
| ], | ||
| }; | ||
| } |
This file contains hidden or 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,41 @@ | ||
| interface RegistryResponse { | ||
| statusCode: number; | ||
| getBody: (_encoding?: unknown) => Uint8Array; | ||
| } | ||
|
|
||
| /** | ||
| * Performs a synchronous HTTP request to the given URL and returns the response as a Uint8Array. | ||
| * | ||
| * Note: This function uses XMLHttpRequest in synchronous mode, which is generally | ||
| * discouraged in web development due to potential UI blocking. However, it is | ||
| * used here to meet the requirements of the Typst package registry interface, | ||
| * which expects a synchronous response. | ||
| */ | ||
| export function registryRequest(method: string, url: string): RegistryResponse { | ||
| const request = new XMLHttpRequest(); | ||
| request.open(method, url, false); | ||
| // Sync XHR from a document cannot use non-text responseType | ||
| request.overrideMimeType("text/plain; charset=x-user-defined"); | ||
| try { | ||
| request.send(); | ||
| } catch (error) { | ||
| console.error(`Registry request to ${url} failed:`, error); | ||
|
|
||
| // If the request fails synchronously (e.g. CORS/network issues), | ||
| // return a non-2xx status code with an empty body instead of throwing | ||
| const emptyBody = new Uint8Array(); | ||
| return { | ||
| statusCode: 0, | ||
| getBody: () => emptyBody, | ||
| }; | ||
| } | ||
|
|
||
| const response = request.response as unknown; | ||
| const responseText = typeof response === "string" ? response : ""; | ||
| const body = Uint8Array.from(responseText, char => char.charCodeAt(0) & 0xff); | ||
|
|
||
| return { | ||
| statusCode: request.status, | ||
| getBody: () => body, | ||
| }; | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.