-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathpreview.html
More file actions
105 lines (94 loc) · 3.55 KB
/
preview.html
File metadata and controls
105 lines (94 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tiny Typst Previewer Example</title>
<!-- Loads the full bundle or the lite version from jsdelivr -->
<!-- src="https://cdn.jsdelivr.net/npm/@myriaddreamin/[email protected]/dist/esm/index.js" -->
<script type="module"
src="https://cdn.jsdelivr.net/npm/@myriaddreamin/typst.ts/dist/esm/contrib/all-in-one-lite.bundle.js"
id="typst"></script>
</head>
<body>
<!-- Provides a button to export to PDF after finish editing -->
<button id="export">Export To PDF</button>
<!-- Accepts user input using a text area element -->
<textarea id="input">Hello, Typst!</textarea>
<div id="content"></div>
<script>
const input = document.getElementById('input');
const contentDiv = document.getElementById('content');
// Exports SVG and puts it into the `contentDiv`
const previewSvg = mainContent => {
$typst.svg({ mainContent }).then(svg => {
console.log(`rendered! SvgElement { len: ${svg.length} }`);
// append svg text
contentDiv.innerHTML = svg;
const svgElem = contentDiv.firstElementChild;
const width = Number.parseFloat(svgElem.getAttribute('width'));
const height = Number.parseFloat(svgElem.getAttribute('height'));
const cw = document.body.clientWidth - 40;
svgElem.setAttribute('width', cw);
svgElem.setAttribute('height', (height * cw) / width);
});
};
// Exports PDF and downloads it
const exportPdf = mainContent =>
$typst.pdf({ mainContent }).then(pdfData => {
var pdfFile = new Blob([pdfData], { type: 'application/pdf' });
// Creates element with <a> tag
const link = document.createElement('a');
// Sets file content in the object URL
link.href = URL.createObjectURL(pdfFile);
// Sets file name
link.target = '_blank';
// Triggers a click event to <a> tag to save file.
link.click();
URL.revokeObjectURL(link.href);
});
/// Listens the 'load' event to initialize after loaded the bundle file from CDN (jsdelivr).
document.getElementById('typst').addEventListener('load', function () {
/// Initializes the Typst compiler and renderer. Since we use "all-in-one-lite.bundle.js" instead of
/// "all-in-one.bundle.js" we need to tell that the wasm module files can be loaded from CDN (jsdelivr).
$typst.setCompilerInitOptions({
getModule: () =>
'https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-web-compiler/pkg/typst_ts_web_compiler_bg.wasm',
});
$typst.setRendererInitOptions({
getModule: () =>
'https://cdn.jsdelivr.net/npm/@myriaddreamin/typst-ts-renderer/pkg/typst_ts_renderer_bg.wasm',
});
/// Binds exportPdf action to the button
document.getElementById('export').onclick = () => exportPdf(input.value);
/// Binds previewSvg action to the textarea
input.oninput = () => {
previewSvg(input.value);
input.style.height = '5px';
input.style.height = input.scrollHeight + 'px';
};
/// Triggers the first preview.
previewSvg(input.value);
});
</script>
<style>
body {
margin: 0px;
display: flex;
flex-direction: column;
align-items: center;
}
#export {
width: 100%;
height: 20px;
}
#input {
width: 100%;
margin: 0;
padding: 0;
border-width: 0;
height: auto;
}
</style>
</body>
</html>