-
Notifications
You must be signed in to change notification settings - Fork 18
/
preview.html
93 lines (84 loc) · 2.87 KB
/
preview.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Svg Document</title>
<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>
<button id="export">Export To PDF</button>
<textarea id="input">Hello, Typst!</textarea>
<div id="content"></div>
<script>
const input = document.getElementById('input');
const contentDiv = document.getElementById('content');
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);
});
};
const exportPdf = mainContent =>
$typst.pdf({ mainContent }).then(pdfData => {
var pdfFile = new Blob([pdfData], { type: 'application/pdf' });
// Create element with <a> tag
const link = document.createElement('a');
// Add file content in the object URL
link.href = URL.createObjectURL(pdfFile);
// Add file name
link.target = '_blank';
// Add click event to <a> tag to save file.
link.click();
URL.revokeObjectURL(link.href);
});
document.getElementById('typst').addEventListener('load', function () {
$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',
});
document.getElementById('export').onclick = () => exportPdf(input.value);
input.oninput = () => {
previewSvg(input.value);
input.style.height = '5px';
input.style.height = input.scrollHeight + 'px';
};
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>