@kubohiroya/turbowarp-plus-editor-project-folder 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,124 @@
1
+ import {EditorView, ViewPlugin, type ViewUpdate} from '@codemirror/view';
2
+ import type {Extension} from '@codemirror/state';
3
+
4
+ // Sample at most one document line per canvas row, including lines outside CodeMirror's viewport.
5
+ export const textMinimap = (label: string): Extension => ViewPlugin.fromClass(class {
6
+ readonly dom: HTMLDivElement;
7
+ readonly canvas: HTMLCanvasElement;
8
+ readonly viewport: HTMLDivElement;
9
+ readonly observer: ResizeObserver;
10
+ private drawnDoc?: EditorView['state']['doc'];
11
+ private drawnColor = '';
12
+ private drawnWidth = 0;
13
+ private drawnHeight = 0;
14
+ private disposed = false;
15
+
16
+ constructor(readonly view: EditorView) {
17
+ const document = view.dom.ownerDocument;
18
+ this.dom = document.createElement('div');
19
+ this.dom.className = 'twpf-text-minimap';
20
+ this.dom.tabIndex = 0;
21
+ this.dom.setAttribute('role', 'slider');
22
+ this.dom.setAttribute('aria-label', label);
23
+ this.dom.setAttribute('aria-orientation', 'vertical');
24
+ this.dom.setAttribute('aria-valuemin', '1');
25
+ this.canvas = document.createElement('canvas');
26
+ this.canvas.setAttribute('aria-hidden', 'true');
27
+ this.viewport = document.createElement('div');
28
+ this.viewport.className = 'twpf-text-minimap-viewport';
29
+ this.dom.append(this.canvas, this.viewport);
30
+ view.dom.append(this.dom);
31
+ this.dom.addEventListener('pointerdown', event => {
32
+ if (event.button !== 0) return;
33
+ event.preventDefault();
34
+ this.dom.focus({preventScroll: true});
35
+ this.dom.setPointerCapture(event.pointerId);
36
+ this.seek(event);
37
+ });
38
+ this.dom.addEventListener('pointermove', event => {
39
+ if (this.dom.hasPointerCapture(event.pointerId)) this.seek(event);
40
+ });
41
+ this.dom.addEventListener('pointerup', event => {
42
+ if (this.dom.hasPointerCapture(event.pointerId)) this.dom.releasePointerCapture(event.pointerId);
43
+ });
44
+ this.dom.addEventListener('keydown', event => {
45
+ const scroll = view.scrollDOM;
46
+ const line = view.defaultLineHeight;
47
+ const destinations: Record<string, number> = {
48
+ ArrowUp: scroll.scrollTop - line, ArrowDown: scroll.scrollTop + line,
49
+ PageUp: scroll.scrollTop - scroll.clientHeight, PageDown: scroll.scrollTop + scroll.clientHeight,
50
+ Home: 0, End: scroll.scrollHeight
51
+ };
52
+ if (!(event.key in destinations)) return;
53
+ event.preventDefault();
54
+ event.stopPropagation();
55
+ scroll.scrollTop = destinations[event.key];
56
+ });
57
+ view.scrollDOM.addEventListener('scroll', this.measure, {passive: true});
58
+ const browser = document.defaultView!;
59
+ this.observer = new browser.ResizeObserver(this.measure);
60
+ this.observer.observe(this.dom);
61
+ this.measure();
62
+ }
63
+
64
+ private seek(event: PointerEvent) {
65
+ const bounds = this.dom.getBoundingClientRect();
66
+ if (!bounds.height) return;
67
+ const scroll = this.view.scrollDOM;
68
+ scroll.scrollTop = (event.clientY - bounds.top) / bounds.height * scroll.scrollHeight - scroll.clientHeight / 2;
69
+ }
70
+
71
+ readonly measure = () => {
72
+ if (this.disposed) return;
73
+ this.view.requestMeasure({
74
+ key: this,
75
+ read: view => ({
76
+ width: this.dom.clientWidth, height: this.dom.clientHeight,
77
+ top: view.scrollDOM.scrollTop, total: view.scrollDOM.scrollHeight, visible: view.scrollDOM.clientHeight,
78
+ color: view.dom.ownerDocument.defaultView!.getComputedStyle(view.contentDOM).color
79
+ }),
80
+ write: ({width, height, top, total, visible, color}, view) => {
81
+ if (this.disposed || !width || !height || !total) return;
82
+ const doc = view.state.doc;
83
+ if (doc !== this.drawnDoc || width !== this.drawnWidth || height !== this.drawnHeight || color !== this.drawnColor) {
84
+ this.canvas.width = width;
85
+ this.canvas.height = height;
86
+ const context = this.canvas.getContext('2d');
87
+ if (context) {
88
+ context.fillStyle = color;
89
+ context.globalAlpha = .65;
90
+ const rows = Math.min(doc.lines, height);
91
+ const rowHeight = Math.min(2, height / rows);
92
+ for (let row = 0; row < rows; row++) {
93
+ const line = doc.line(1 + Math.floor(row * doc.lines / rows));
94
+ // Reading a bounded slice also keeps a large, single-line JSON file cheap to draw.
95
+ const text = doc.sliceString(line.from, Math.min(line.to, line.from + width));
96
+ for (let column = 0; column < text.length; column++) {
97
+ if (!/\s/u.test(text[column])) context.fillRect(column, row * rowHeight, 1, rowHeight);
98
+ }
99
+ }
100
+ }
101
+ this.drawnDoc = doc;
102
+ this.drawnWidth = width;
103
+ this.drawnHeight = height;
104
+ this.drawnColor = color;
105
+ }
106
+ this.viewport.style.top = `${top / total * 100}%`;
107
+ this.viewport.style.height = `${Math.min(1, visible / total) * 100}%`;
108
+ this.dom.setAttribute('aria-valuemax', String(doc.lines));
109
+ this.dom.setAttribute('aria-valuenow', String(Math.min(doc.lines, 1 + Math.floor(top / total * doc.lines))));
110
+ }
111
+ });
112
+ };
113
+
114
+ update(update: ViewUpdate) {
115
+ if (update.docChanged || update.viewportChanged || update.geometryChanged) this.measure();
116
+ }
117
+
118
+ destroy() {
119
+ this.disposed = true;
120
+ this.observer.disconnect();
121
+ this.view.scrollDOM.removeEventListener('scroll', this.measure);
122
+ this.dom.remove();
123
+ }
124
+ });