import { withGlobalRenderer } from "@myriaddreamin/typst.ts/dist/esm/contrib/global-renderer.mjs";
import * as typst from "@myriaddreamin/typst.ts";
import { createEffect, createSignal } from "solid-js";
export interface TypstDocumentProps {
let moduleInitOptions: typst.InitOptions = {
"_build/node_modules/@myriaddreamin/typst-ts-renderer/pkg/typst_ts_renderer_bg.wasm",
export const TypstDocument = ({
}: TypstDocumentProps) => {
/// --- beg: manipulate permission --- ///
// todo: acquire permission.
const [permission, setPermissionInternal] = createSignal(false);
const setPermissionAndOk = (status: PermissionStatus) => {
if (status.state === "granted") {
setPermissionInternal(true);
setPermissionInternal(false);
/// only works in chromium
navigator.permissions.query({ name: 'local-fonts' as PermissionName }).then(status => {
if (setPermissionAndOk(status)) {
status.addEventListener('change', event => {
console.log(event, status);
setPermissionAndOk(status);
/// --- end: manipulate permission --- ///
/// --- beg: update document --- ///
const [displayDivRef, setDisplayDivRef] = createSignal<
HTMLDivElement | undefined
const doRender = (renderer: typst.TypstRenderer) => {
const divElem = displayDivRef();
return renderer.renderToCanvas({
artifactContent: artifact || new Uint8Array(0),
/// get display layer div
const divElem = displayDivRef();
/// we allow empty artifact
console.log(displayDivRef());
withGlobalRenderer(typst.createTypstRenderer, moduleInitOptions, doRender);
}, [permission, displayDivRef, fill, artifact, format]);
/// --- end: update document --- ///
return <div ref={setDisplayDivRef}></div>;
TypstDocument.setWasmModuleInitOptions = (opts: typst.InitOptions) => {
moduleInitOptions = opts;