-
Notifications
You must be signed in to change notification settings - Fork 8
/
underleaf.ts
211 lines (182 loc) · 5.92 KB
/
underleaf.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import "./underleaf.css";
import "./typst.css";
import "./typst.ts";
import "../loader.mjs";
import van from "vanjs-core";
const { div, button } = van.tags;
import { Editor } from "./underleaf-editor";
import { DirectoryView, FsItemState } from "./underleaf-fs";
import { Preview } from "./underleaf-preview";
import { TypstDocument } from "typst-dom/typst-doc.mjs";
import { IncrementalServer } from "@myriaddreamin/typst.ts/dist/esm/compiler.mjs";
let $typst = window.$typst;
/// Checks if the browser is in dark mode
const isDarkMode = () =>
window.matchMedia?.("(prefers-color-scheme: dark)").matches;
/// Exports the document
const ExportButton = (title: string, onclick: () => void) =>
button({
onclick,
textContent: title,
});
const App = () => {
/// External status
const /// Captures compiler load status
compilerLoaded = van.state(false),
/// Captures font load status
fontLoaded = van.state(false),
/// Binds to filesystem reload event
reloadBell = van.state(false);
const mainFilePath = "/repo/fixtures/underleaf/ieee/main.typ";
/// Component status
const /// The incremental server
incrServer = van.state<IncrementalServer | undefined>(undefined),
/// The document in memory
typstDoc = van.state<TypstDocument | undefined>(undefined),
/// request to change focus file
changeFocusFile = van.state<FsItemState | undefined>(undefined),
/// The current focus file
focusFile = van.state<FsItemState | undefined>(undefined);
/// Styles and outputs
const /// The source code state
error = van.state(""),
/// The dark mode style
darkMode = van.state(isDarkMode());
/// Checks compiler status
window.$typst$script.then(async () => {
$typst = window.$typst;
await $typst.getCompiler();
compilerLoaded.val = true;
await $typst.svg({ mainContent: "" });
fontLoaded.val = true;
(await $typst.getCompiler()).withIncrementalServer(
(srv: IncrementalServer) => {
return new Promise((disposeServer) => {
incrServer.val = srv;
/// Let it leak.
void disposeServer;
});
}
);
});
/// Listens to dark mode change
window
.matchMedia?.("(prefers-color-scheme: dark)")
.addEventListener("change", (event) => (darkMode.val = event.matches));
/// Triggers compilation when precondition is met or changed
van.derive(async () => {
try {
if (
/// Compiler with fonts should be loaded
fontLoaded.val &&
/// Incremental server should be loaded
incrServer.val &&
/// Typst document should be loaded
typstDoc.val &&
/// Filesystem should be loaded
reloadBell.val &&
/// recompile If focus file changed
focusFile.val &&
/// recompile If focus file content changed
focusFile.val.data.val &&
/// recompile If dark mode changed
darkMode
) {
console.log("recompilation");
setTypstTheme(darkMode.val);
const v = await (
await $typst.getCompiler()
).compile({
mainFilePath,
incrementalServer: incrServer.val,
});
// todo: incremental update
typstDoc.val.addChangement(["diff-v1", v as any]);
}
error.val = "";
} catch (e) {
error.val = e as string;
}
});
const exportAs = (data: string | Uint8Array, mime: string) => {
var fileBlob = new Blob([data], { type: mime });
// Create element with <a> tag
const link = document.createElement("a");
// name
link.download =
mime === "application/pdf"
? "A typesetting system to untangle the scientific writing process.pdf"
: "A typesetting system to untangle the scientific writing process.html";
// Add file content in the object URL
link.href = URL.createObjectURL(fileBlob);
// Add file name
link.target = "_blank";
// Add click event to <a> tag to save file.
link.click();
URL.revokeObjectURL(link.href);
};
const exportPdf = () => {
setTypstTheme(false);
const pdfData = $typst.pdf({ mainFilePath });
return pdfData.then((pdfData: string) =>
exportAs(pdfData, "application/pdf")
);
};
const exportHtml = () => {
setTypstTheme(false);
const svgData = $typst.svg({
mainFilePath,
data_selection: { body: true, defs: true, css: true },
});
return svgData.then((svgData: string) =>
exportAs(
`<!DOCTYPE html>
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>A typesetting system to untangle the scientific writing process</title></head>
<body>${svgData}</body>
</html>
`,
"text/html"
)
);
};
return div(
{ class: "mitex-main flex-column" },
div(
{
class: "flex-row",
style: "justify-content: space-between; margin-bottom: 10px",
},
div(
{
style:
"display: flex; align-items: center; text-align: center; text-decoration: underline; padding-left: 10px",
},
"A typesetting system to untangle the scientific writing process"
),
div(
{ class: "mitex-toolbar-row flex-row" },
div({ class: "error", textContent: error }),
ExportButton("Export to PDF", exportPdf),
div({ style: "width: 5px" }),
ExportButton("HTML", exportHtml)
)
),
div(
{ class: "mitex-edit-row flex-row" },
DirectoryView({ compilerLoaded, changeFocusFile, focusFile, reloadBell }),
Editor(darkMode, changeFocusFile, focusFile),
Preview({ darkMode, compilerLoaded, fontLoaded, typstDoc })
)
);
async function setTypstTheme(darkMode: boolean) {
let styling = darkMode
? `#let prefer-theme = "dark";`
: `#let prefer-theme = "light";`;
await $typst.addSource(
"/repo/fixtures/underleaf/ieee/styling.typ",
styling
);
}
};
van.add(document.querySelector("#app")!, App());