-
Notifications
You must be signed in to change notification settings - Fork 3
/
TransitionalPopup.ts
255 lines (203 loc) · 9.12 KB
/
TransitionalPopup.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { TransitionalView } from "./TransitionalView";
export class TransitionalPopup {
readonly transitionalView: TransitionalView;
private onClose: (() => void) | null;
private initialPositionRelativeToAnnotationMask: "above" | "below" | null;
private popupNode: HTMLElement;
private backgroundNode: HTMLElement | null;
private frameNode: HTMLElement | null;
private titleBarNode: HTMLElement | null;
private contentNode: HTMLElement | null;
private errorContainer: HTMLElement | null;
private unavailabilityErrorNode: HTMLElement | null;
private isDisplayingAnError: boolean;
constructor(view: TransitionalView, onClose?: () => void) {
this.transitionalView = view;
this.onClose = onClose ?? null;
this.initialPositionRelativeToAnnotationMask = null;
// The popup node is the root container of the popup in the DOM
this.popupNode = document.createElement("div");
this.popupNode.classList.add("transitional-popup");
this.backgroundNode = null;
this.frameNode = null;
this.titleBarNode = null;
this.contentNode = null;
this.errorContainer = null;
this.unavailabilityErrorNode = null;
this.isDisplayingAnError = false;
this.init();
}
private init(): void {
this.createBackground();
this.createFrame();
this.createTitleBar();
this.createContent();
this.createErrors();
this.startHandlingBackgroundClicks();
this.open();
}
createBackground() {
this.backgroundNode = document.createElement("div");
this.backgroundNode.classList.add("popup-background");
this.popupNode.append(this.backgroundNode);
}
createFrame() {
this.frameNode = document.createElement("div");
this.frameNode.classList.add("popup-frame");
this.updateFramePosition();
this.popupNode.append(this.frameNode);
}
updateFramePosition(): void {
const maskCoordinates = this.transitionalView.context.annotationMaskCoordinates
?? this.transitionalView.context.initialAnnotationMaskCoordinates;
// Reset the CSS attributes that position the frame
this.frameNode!.style.removeProperty("bottom");
this.frameNode!.style.removeProperty("top");
// The first time this method is called, decide to position the frame
// either above or below the annotation mask of the transitional
// (depending on the mask position within the current viewport)
const maskTop = maskCoordinates[1];
if (this.initialPositionRelativeToAnnotationMask === null) {
this.initialPositionRelativeToAnnotationMask =
maskTop > window.scrollY + (window.innerHeight / 2)
? "above"
: "below";
}
// Then/the next times, pdate the right CSS attribute depending on whether the popup
// should be displayed below or above the annotation mask of the transitional
if (this.initialPositionRelativeToAnnotationMask === "above") {
const maskTopToWebpageBottom = document.documentElement.clientHeight - maskTop;
this.frameNode!.style.bottom = `${maskTopToWebpageBottom + 20}px`;
}
else {
const maskBottom = maskCoordinates[3];
this.frameNode!.style.top = `${maskBottom + 20}px`;
}
}
createTitleBar() {
// Create an empty title bar
this.titleBarNode = document.createElement("div");
this.titleBarNode.classList.add("popup-title-bar");
this.frameNode!.append(this.titleBarNode);
// Add the name and the location of the transitional as a title
const titleNode = document.createElement("span");
titleNode.classList.add("title");
this.titleBarNode.append(titleNode);
// Add the name (of the visualised command/environement) to the title
const nameNode = document.createElement("span");
nameNode.classList.add("name");
titleNode.append(nameNode);
nameNode.textContent = this.transitionalView.title;
// Add the location (in the code) to the title
const locationNode = document.createElement("span");
locationNode.classList.add("location");
titleNode.append(locationNode);
locationNode.innerHTML = this.getTransitionalLocationInSourceCode();
// Reveal the code of the transitional when the title is clicked
titleNode.addEventListener("click", event => {
this.transitionalView.revealInSourceDocument();
});
// Add a button to close the popup
const closeButtonNode = document.createElement("button");
closeButtonNode.classList.add("close-button");
this.titleBarNode.append(closeButtonNode);
// Close the popup and save the document on click
closeButtonNode.addEventListener("click", event => {
this.close();
});
}
// Must be called in case the transitional is updated
updateTitleBar() {
this.titleBarNode!.querySelector(".location")!.innerHTML = this.getTransitionalLocationInSourceCode();
}
createContent() {
this.contentNode = document.createElement("div");
this.contentNode.classList.add("popup-content");
this.contentNode.setAttribute("data-transitional-name", this.transitionalView.name);
this.frameNode!.append(this.contentNode);
// Render the view inside the popup content
this.contentNode.append(this.transitionalView.render());
}
// Must be called in case the transitional is updated
updateContent() {
this.contentNode!.innerHTML = "";
this.contentNode!.append(this.transitionalView.render());
}
createErrors(): void {
this.errorContainer = document.createElement("div");
this.frameNode!.append(this.errorContainer);
// Error message in case the transitional becomes unavailable (while its view is displayed)
this.unavailabilityErrorNode = document.createElement("div");
this.unavailabilityErrorNode.classList.add("popup-error", "unavailable-transitional-error");
this.unavailabilityErrorNode.innerHTML = `
<span class="error-title">This transitional is currently not available :(</span>
<p class="error-message">
This is often caused by a syntax error or an edit iLaTeX could not understand.
You can try to:
</p>
<ul class="error-suggestions">
<li><strong>check the syntax</strong> of the code <span class="red-highlight">highlighted in red</span></li>
<li><strong>recompile the document</strong> if it persists (to refresh all the transitionals)</li>
</ul>
`;
this.errorContainer!.append(this.unavailabilityErrorNode);
}
updateErrors(): void {
// Display an error if the transitional is unavailable
if (!this.transitionalView.isAvailable) {
this.transitionalView.onBeforeTransitionalErrorDisplay();
this.popupNode.classList.add("error", "error-unavailable");
this.transitionalView.onAfterTransitionalErrorDisplay();
this.isDisplayingAnError = true;
}
// Otherwise, hide any displayed error and restore the content of the popup
else if (this.isDisplayingAnError) {
this.transitionalView.onBeforeTransitionalErrorRemoval();
this.popupNode.classList.remove("error", "error-unavailable");
this.transitionalView.onAfterTransitionalErrorRemoval();
this.isDisplayingAnError = false;
}
}
getTransitionalLocationInSourceCode() {
const fileName = this.transitionalView.sourceFileName;
const range = this.transitionalView.sourceFileCodeRange;
return range.from.line === range.to.line
? `(${fileName} · line ${range.from.line + 1})`
: `(${fileName} · lines ${range.from.line + 1} – ${range.to.line + 1})`;
}
startHandlingBackgroundClicks() {
this.backgroundNode!.addEventListener("click", event => {
if (event.target !== this.backgroundNode) {
return;
}
this.close();
});
}
onAfterVisualationContentUpdate(): void {
this.updateContent();
}
onAfterTransitionalMetadataUpdate(): void {
this.updateTitleBar();
this.updateErrors();
}
onBeforePdfResize(): void {
this.transitionalView.onBeforePdfResize();
}
onAfterPdfResize(): void {
this.updateFramePosition();
this.transitionalView.onAfterPdfResize();
}
open() {
this.transitionalView.onBeforeTransitionalDisplay();
document.body.prepend(this.popupNode);
this.transitionalView.onAfterTransitionalDisplay();
}
close() {
if (this.onClose) {
this.onClose();
}
this.transitionalView.onBeforeTransitionalRemoval();
this.popupNode.remove();
this.transitionalView.onAfterTransitionalRemoval();
}
}