terrazzo-terminal 0.2.7

A simple web-based terminal emulator built on Terrazzo.
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
let pdfjsLibPromise;

function loadPdfJs() {
    if (!pdfjsLibPromise) {
        pdfjsLibPromise = import("/static/pdfjs/pdf.min.mjs").then((pdfjsLib) => {
            pdfjsLib.GlobalWorkerOptions.workerSrc = "/static/pdfjs/pdf.worker.min.mjs";
            return pdfjsLib;
        });
    }
    return pdfjsLibPromise;
}

const MIN_ZOOM = 0.1;
const MAX_ZOOM = 10;
const MIN_ZOOM_SLIDER_VALUE = Math.log10(MIN_ZOOM);
const MAX_ZOOM_SLIDER_VALUE = Math.log10(MAX_ZOOM);
const ZOOM_RENDER_DELAY_MS = 80;

class PdfJsImpl {
    element;
    documentElement;
    loadingTask;
    pdfjsLib;
    pdf;
    generation;
    zoom;
    zoomRenderTimeout;
    zoomRenderStartedAt;
    zoomAnchor;
    zoomControl;
    zoomSlider;
    zoomValue;
    activePageTasks;
    onWheel;

    constructor(element, data) {
        this.element = element;
        this.documentElement = null;
        this.generation = 0;
        this.zoom = 1;
        this.zoomRenderTimeout = null;
        this.zoomRenderStartedAt = 0;
        this.zoomAnchor = null;
        this.zoomControl = null;
        this.zoomSlider = null;
        this.zoomValue = null;
        this.activePageTasks = new Set();
        this.onWheel = (event) => this.handleWheel(event);
        this.element.addEventListener("wheel", this.onWheel, { passive: false });
        this.set_content(data);
    }

    destroy() {
        this.generation++;
        this.clearZoomRender();
        this.cancelPageTasks();
        this.element.removeEventListener("wheel", this.onWheel);
        if (this.loadingTask) {
            this.loadingTask.destroy();
            this.loadingTask = null;
        }
        this.pdfjsLib = null;
        this.pdf = null;
        this.documentElement = null;
        this.zoomControl = null;
        this.zoomSlider = null;
        this.zoomValue = null;
        this.element.replaceChildren();
    }

    set_content(data) {
        this.zoom = 1;
        this.render(data);
    }

    async render(data) {
        const generation = ++this.generation;
        this.clearZoomRender();
        this.cancelPageTasks();
        if (this.loadingTask) {
            this.loadingTask.destroy();
            this.loadingTask = null;
        }
        this.pdfjsLib = null;
        this.pdf = null;
        this.showStatus("Loading PDF...");

        try {
            const pdfjsLib = await loadPdfJs();
            if (generation !== this.generation) return;

            this.loadingTask = pdfjsLib.getDocument({
                data,
            });
            const pdf = await this.loadingTask.promise;
            if (generation !== this.generation) return;

            this.pdfjsLib = pdfjsLib;
            this.pdf = pdf;
            await this.renderDocument(generation);
            this.zoomRenderStartedAt = window.performance.now();
        } catch (error) {
            if (generation === this.generation) {
                this.showStatus(`Failed to load PDF: ${error}`);
            }
        }
    }

    async renderDocument(generation, preserveZoomControl = false) {
        this.cancelPageTasks();
        const previousDocumentElement = this.documentElement;
        const documentElement = this.createDocumentElement();
        this.documentElement = documentElement;
        const zoomControl = preserveZoomControl && this.zoomControl
            ? this.zoomControl
            : this.createZoomControl();

        if (preserveZoomControl && previousDocumentElement?.parentElement === this.element) {
            previousDocumentElement.replaceWith(documentElement);
            if (zoomControl.parentElement !== this.element) {
                this.element.appendChild(zoomControl);
            }
        } else {
            this.element.replaceChildren(documentElement, zoomControl);
        }
        this.updateZoomControl();
        for (let pageNumber = 1; pageNumber <= this.pdf.numPages; pageNumber++) {
            if (generation !== this.generation) return;
            const page = await this.pdf.getPage(pageNumber);
            await this.renderPage(generation, page, pageNumber);
        }
        this.restoreZoomAnchor();
    }

    async renderPage(generation, page, pageNumber) {
        const unscaledViewport = page.getViewport({ scale: 1 });
        const availableWidth = Math.max(this.scrollElement.clientWidth - 32, 320);
        const scale = Math.min(Math.max(availableWidth / unscaledViewport.width, 0.5), 2) * this.zoom;
        const viewport = page.getViewport({ scale });

        const pageElement = document.createElement("div");
        pageElement.dataset.pageNumber = `${pageNumber}`;
        pageElement.style.width = `${viewport.width}px`;
        pageElement.style.height = `${viewport.height}px`;
        pageElement.style.setProperty("--total-scale-factor", `${this.totalScaleFactor(viewport)}`);

        const canvas = document.createElement("canvas");
        canvas.dataset.pageNumber = `${pageNumber}`;
        canvas.width = Math.floor(viewport.width);
        canvas.height = Math.floor(viewport.height);
        canvas.style.width = `${viewport.width}px`;
        canvas.style.height = `${viewport.height}px`;

        const textLayerElement = document.createElement("div");
        textLayerElement.dataset.layer = "text";

        const annotationLayerElement = document.createElement("div");
        annotationLayerElement.dataset.layer = "annotations";

        pageElement.append(canvas, textLayerElement, annotationLayerElement);
        this.documentElement.appendChild(pageElement);

        const canvasContext = canvas.getContext("2d");
        const renderTask = page.render({ canvasContext, viewport });
        const textLayer = new this.pdfjsLib.TextLayer({
            textContentSource: page.streamTextContent({ includeMarkedContent: true }),
            container: textLayerElement,
            viewport,
        });
        const annotations = page
            .getAnnotations({ intent: "display" })
            .then((annotations) => this.renderLinks(annotationLayerElement, annotations, viewport));
        const tasks = { renderTask, textLayer };
        this.activePageTasks.add(tasks);

        try {
            await Promise.all([renderTask.promise, textLayer.render(), annotations]);
        } catch (error) {
            if (generation === this.generation) {
                throw error;
            }
        } finally {
            this.activePageTasks.delete(tasks);
        }
    }

    renderLinks(layerElement, annotations, viewport) {
        const linkType = this.pdfjsLib.AnnotationType.LINK;
        for (const annotation of annotations) {
            if (annotation.annotationType !== linkType || !annotation.rect) {
                continue;
            }

            const externalHref = annotation.url ?? annotation.unsafeUrl;
            const href = externalHref ?? this.destinationHref(annotation.dest);
            if (!href) {
                continue;
            }

            const rect = viewport.convertToViewportRectangle(annotation.rect);
            const left = Math.min(rect[0], rect[2]);
            const top = Math.min(rect[1], rect[3]);
            const width = Math.abs(rect[0] - rect[2]);
            const height = Math.abs(rect[1] - rect[3]);
            if (!width || !height) {
                continue;
            }

            const link = document.createElement("a");
            link.href = href;
            link.title = href;
            link.style.left = `${left}px`;
            link.style.top = `${top}px`;
            link.style.width = `${width}px`;
            link.style.height = `${height}px`;
            if (externalHref) {
                link.target = "_blank";
                link.rel = "noopener noreferrer";
            } else {
                link.addEventListener("click", (event) => {
                    event.preventDefault();
                    this.scrollToDestination(annotation.dest);
                });
            }
            layerElement.appendChild(link);
        }
    }

    async scrollToDestination(destination) {
        const explicitDestination = typeof destination === "string"
            ? await this.pdf.getDestination(destination)
            : destination;
        if (!explicitDestination?.length) {
            return;
        }

        const pageNumber = await this.destinationPageNumber(explicitDestination[0]);
        if (!pageNumber) {
            return;
        }

        const pageElement = this.documentElement?.querySelector(`div[data-page-number="${pageNumber}"]`);
        if (!pageElement) {
            return;
        }

        const scrollTop = await this.destinationScrollTop(pageElement, pageNumber, explicitDestination);
        this.scrollElement.scrollTo({ top: scrollTop, behavior: "smooth" });
    }

    async destinationPageNumber(pageReference) {
        if (Number.isInteger(pageReference)) {
            return pageReference + 1;
        }

        try {
            return await this.pdf.getPageIndex(pageReference) + 1;
        } catch (_) {
            return null;
        }
    }

    async destinationScrollTop(pageElement, pageNumber, explicitDestination) {
        const destinationKind = explicitDestination[1]?.name;
        const destinationTop = explicitDestination[3];
        if ((destinationKind !== "XYZ" && destinationKind !== "FitH" && destinationKind !== "FitBH")
            || typeof destinationTop !== "number") {
            return pageElement.offsetTop;
        }

        const page = await this.pdf.getPage(pageNumber);
        const unscaledViewport = page.getViewport({ scale: 1 });
        const scale = pageElement.getBoundingClientRect().width / unscaledViewport.width;
        const viewport = page.getViewport({ scale });
        return pageElement.offsetTop + viewport.convertToViewportPoint(0, destinationTop)[1];
    }

    destinationHref(destination) {
        if (!destination) {
            return null;
        }
        if (typeof destination === "string") {
            return `#${encodeURIComponent(destination)}`;
        }
        return `#${encodeURIComponent(JSON.stringify(destination))}`;
    }

    totalScaleFactor(viewport) {
        const { pageWidth, pageHeight } = viewport.rawDims;
        const rotated = viewport.rotation % 180 !== 0;
        return rotated ? viewport.width / pageHeight : viewport.width / pageWidth;
    }

    createDocumentElement() {
        const documentElement = document.createElement("div");
        documentElement.dataset.layer = "pages";
        return documentElement;
    }

    createZoomControl() {
        const control = document.createElement("div");
        control.dataset.control = "zoom";

        const slider = document.createElement("input");
        slider.type = "range";
        slider.min = `${MIN_ZOOM_SLIDER_VALUE}`;
        slider.max = `${MAX_ZOOM_SLIDER_VALUE}`;
        slider.step = "any";
        slider.setAttribute("aria-label", "PDF zoom");
        slider.addEventListener("input", () => {
            this.setZoom(this.zoomFromSliderValue(Number(slider.value)), this.makeCenterZoomAnchor());
        });

        const value = document.createElement("output");
        value.setAttribute("aria-live", "polite");

        control.append(slider, value);
        this.zoomControl = control;
        this.zoomSlider = slider;
        this.zoomValue = value;
        return control;
    }

    handleWheel(event) {
        if (!this.pdf || !this.pdfjsLib || !(event.ctrlKey || event.metaKey)) {
            return;
        }
        event.preventDefault();
        event.stopPropagation();

        const factor = Math.max(0.8, Math.min(1.25, Math.exp(-event.deltaY * 0.002)));
        this.setZoom(this.zoom * factor, this.makeZoomAnchor(event));
    }

    setZoom(zoom, anchor) {
        const nextZoom = Math.min(Math.max(zoom, MIN_ZOOM), MAX_ZOOM);
        if (Math.abs(nextZoom - this.zoom) < 0.001) {
            return;
        }
        this.zoom = nextZoom;
        this.zoomAnchor = anchor;
        this.updateZoomControl();
        this.scheduleZoomRender();
    }

    updateZoomControl() {
        const percent = Math.round(this.zoom * 100);
        if (this.zoomSlider) {
            this.zoomSlider.value = `${this.sliderValueFromZoom(this.zoom)}`;
        }
        if (this.zoomValue) {
            this.zoomValue.value = `${percent}%`;
            this.zoomValue.textContent = `${percent}%`;
        }
    }

    zoomFromSliderValue(value) {
        return 10 ** value;
    }

    sliderValueFromZoom(zoom) {
        return Math.log10(Math.min(Math.max(zoom, MIN_ZOOM), MAX_ZOOM));
    }

    scheduleZoomRender() {
        this.clearZoomRender();
        const now = window.performance.now();
        const delay = this.zoomRenderStartedAt
            ? Math.max(0, ZOOM_RENDER_DELAY_MS - (now - this.zoomRenderStartedAt))
            : ZOOM_RENDER_DELAY_MS;
        this.zoomRenderTimeout = window.setTimeout(() => {
            this.zoomRenderTimeout = null;
            if (!this.pdf || !this.pdfjsLib) {
                return;
            }
            this.zoomRenderStartedAt = window.performance.now();
            const generation = ++this.generation;
            this.renderDocument(generation, true).catch((error) => {
                if (generation === this.generation) {
                    this.showStatus(`Failed to zoom PDF: ${error}`);
                }
            });
        }, delay);
    }

    clearZoomRender() {
        if (this.zoomRenderTimeout) {
            window.clearTimeout(this.zoomRenderTimeout);
            this.zoomRenderTimeout = null;
        }
    }

    cancelPageTasks() {
        for (const { renderTask, textLayer } of this.activePageTasks) {
            renderTask.cancel();
            textLayer.cancel();
        }
        this.activePageTasks.clear();
    }

    makeZoomAnchor(event) {
        const scrollElement = this.scrollElement;
        const rect = scrollElement.getBoundingClientRect();
        const pointerY = event.clientY - rect.top;
        const scrollHeight = Math.max(scrollElement.scrollHeight, 1);
        return {
            pointerY,
            ratio: (scrollElement.scrollTop + pointerY) / scrollHeight,
        };
    }

    makeCenterZoomAnchor() {
        const scrollElement = this.scrollElement;
        const pointerY = scrollElement.clientHeight / 2;
        const scrollHeight = Math.max(scrollElement.scrollHeight, 1);
        return {
            pointerY,
            ratio: (scrollElement.scrollTop + pointerY) / scrollHeight,
        };
    }

    restoreZoomAnchor() {
        const anchor = this.zoomAnchor;
        this.zoomAnchor = null;
        if (!anchor) return;
        const scrollElement = this.scrollElement;
        scrollElement.scrollTop = anchor.ratio * scrollElement.scrollHeight - anchor.pointerY;
    }

    get scrollElement() {
        return this.documentElement ?? this.element;
    }

    showStatus(message) {
        this.documentElement = null;
        this.zoomControl = null;
        this.zoomSlider = null;
        this.zoomValue = null;
        const status = document.createElement("div");
        status.className = "pdf-status";
        status.textContent = message;
        status.style.alignSelf = "stretch";
        status.style.padding = "var(--padding)";
        this.element.replaceChildren(status);
    }
}

export {
    PdfJsImpl
};