-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathmain.js
More file actions
139 lines (124 loc) · 4.5 KB
/
Copy pathmain.js
File metadata and controls
139 lines (124 loc) · 4.5 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
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
/// Import stylesheets for different components
// todo: refactor them, but we don't touch them in this PR
import "./typst.css";
import "./styles/toolbar.css";
import "./styles/layout.css";
import "./styles/help-panel.css";
import "./styles/outline.css";
import { wsMain, PreviewMode } from "./ws";
import { setupDrag } from "./drag";
const windowElem = document.getElementById("typst-container");
windowElem.documents = [];
/// Main entry point of the frontend program.
main();
function main() {
const wsArgs = retrieveWsArgs();
const { nextWs } = buildWs();
window.onload = () => nextWs(wsArgs);
setupVscodeChannel(nextWs);
setupDrag();
}
/// Placeholders for typst-preview program initializing frontend
/// arguments.
function retrieveWsArgs() {
/// The string `preview-arg:previewMode:Doc` is a placeholder
/// It will be replaced by the actual preview mode.
/// ```rs
/// let frontend_html = frontend_html.replace(
/// "preview-arg:previewMode:Doc", ...);
/// ```
let mode = "preview-arg:previewMode:Doc";
/// Remove the placeholder prefix.
mode = mode.replace("preview-arg:previewMode:", "");
let previewMode = PreviewMode[mode];
/// The string `ws://127.0.0.1:23625` is a placeholder
/// Also, it is the default url to connect to.
/// Note that we must resolve the url to an absolute url as
/// the websocket connection requires an absolute url.
///
/// See [WebSocket and relative URLs](https://github.com/whatwg/websockets/issues/20)
let url = "ws://127.0.0.1:23625";
if (!url) {
return { url: "", previewMode, isContentPreview: false };
}
let urlObject = new URL(url, window.location.href);
/// Rewrite the protocol to websocket.
urlObject.protocol = urlObject.protocol.replace("https:", "wss:").replace("http:", "ws:");
if (location.href.startsWith("https://")) {
urlObject.protocol = urlObject.protocol.replace("ws:", "wss:");
}
/// Return a `WsArgs` object.
return { url: urlObject.href, previewMode, isContentPreview: false };
}
/// `buildWs` returns a object, which keeps track of websocket
/// connections.
function buildWs() {
let previousDispose = Promise.resolve(() => {});
/// `nextWs` will always hold a global unique websocket connection
/// to the preview backend.
function nextWs(nextWsArgs) {
const previous = previousDispose;
previousDispose = new Promise(async (resolve) => {
/// Dispose the previous websocket connection.
await previous.then((d) => d());
/// Reset app mode before creating a new websocket connection.
resetAppMode(nextWsArgs);
/// Create a new websocket connection.
resolve(wsMain(nextWsArgs));
});
}
return { nextWs };
function resetAppMode({ previewMode: mode, isContentPreview }) {
const app = document.getElementById("typst-container");
const helpPanel = document.getElementById("typst-help-panel");
/// Set the root css selector to the content preview mode.
app.classList.remove("content-preview");
if (isContentPreview) {
app.classList.add("content-preview");
}
/// Set the root css selector to the preview mode.
app.classList.remove("mode-slide");
app.classList.remove("mode-doc");
helpPanel?.classList.remove("mode-slide");
helpPanel?.classList.remove("mode-doc");
if (mode === PreviewMode.Slide) {
app.classList.add("mode-slide");
helpPanel?.classList.add("mode-slide");
} else if (mode === PreviewMode.Doc) {
app.classList.add("mode-doc");
helpPanel?.classList.add("mode-doc");
} else {
throw new Error(`Unknown preview mode: ${mode}`);
}
}
}
/// A frontend will try to setup a vscode channel if it is running
/// in vscode.
function setupVscodeChannel(nextWs) {
const vscodeAPI = typeof acquireVsCodeApi !== "undefined" && acquireVsCodeApi();
if (vscodeAPI?.postMessage) {
vscodeAPI.postMessage({ type: "started" });
}
if (vscodeAPI?.setState && window.vscode_state) {
vscodeAPI.setState(window.vscode_state);
}
// Handle messages sent from the extension to the webview
window.addEventListener("message", (event) => {
const message = event.data; // The json data that the extension sent
switch (message.type) {
case "reconnect": {
console.log("reconnect", message);
nextWs({
url: message.url,
previewMode: PreviewMode[message.mode],
isContentPreview: message.isContentPreview,
});
break;
}
case "outline": {
console.log("outline", message);
break;
}
}
});
}