restty 0.1.17 → 0.1.18

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.
@@ -1,226 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __export = (target, all) => {
3
- for (var name in all)
4
- __defProp(target, name, {
5
- get: all[name],
6
- enumerable: true,
7
- configurable: true,
8
- set: (newValue) => all[name] = () => newValue
9
- });
10
- };
11
- // src/grid/grid.ts
12
- function fontHeightUnits(font) {
13
- if (!font)
14
- return 0;
15
- const height = font.height;
16
- if (height !== undefined && Number.isFinite(height) && height > 0)
17
- return height;
18
- const asc = font.ascender ?? 0;
19
- const desc = font.descender ?? 0;
20
- const fallback = asc - desc;
21
- if (Number.isFinite(fallback) && fallback > 0)
22
- return fallback;
23
- return font.upem || 1000;
24
- }
25
- function computeCellMetrics(font, config, dpr, shapeCluster) {
26
- if (!font)
27
- return null;
28
- const fontSizePx = Math.max(1, Math.round(config.fontSize * dpr));
29
- const scale = font.scaleForSize(fontSizePx, config.sizeMode);
30
- const glyphId = font.glyphIdForChar("M");
31
- const advanceUnits = glyphId !== undefined && glyphId !== null ? font.advanceWidth(glyphId) : shapeCluster("M").advance;
32
- const cellW = Math.max(1, Math.round(advanceUnits * scale));
33
- const lineHeight = fontHeightUnits(font) * scale;
34
- const cellH = Math.max(1, Math.round(lineHeight));
35
- const baselineOffset = font.ascender * scale;
36
- const yPad = Math.max(0, (cellH - lineHeight) * 0.5);
37
- return { cellW, cellH, fontSizePx, scale, lineHeight, baselineOffset, yPad };
38
- }
39
- function createGridState() {
40
- return {
41
- cols: 0,
42
- rows: 0,
43
- cellW: 0,
44
- cellH: 0,
45
- fontSizePx: 0,
46
- scale: 1,
47
- lineHeight: 0,
48
- baselineOffset: 0,
49
- yPad: 0
50
- };
51
- }
52
- function updateGridState(state, metrics, canvasWidth, canvasHeight) {
53
- const cols = Math.max(1, Math.floor(canvasWidth / metrics.cellW));
54
- const rows = Math.max(1, Math.floor(canvasHeight / metrics.cellH));
55
- if (!Number.isFinite(cols) || !Number.isFinite(rows)) {
56
- return { changed: false, cols: state.cols, rows: state.rows };
57
- }
58
- const changed = cols !== state.cols || rows !== state.rows || metrics.fontSizePx !== state.fontSizePx || metrics.cellW !== state.cellW || metrics.cellH !== state.cellH;
59
- Object.assign(state, metrics, { cols, rows });
60
- return { changed, cols, rows };
61
- }
62
- function clamp(value, min, max) {
63
- return Math.max(min, Math.min(max, value));
64
- }
65
-
66
- // src/selection/selection.ts
67
- function createSelectionState() {
68
- return {
69
- active: false,
70
- dragging: false,
71
- anchor: null,
72
- focus: null
73
- };
74
- }
75
- function clearSelection(state) {
76
- state.active = false;
77
- state.dragging = false;
78
- state.anchor = null;
79
- state.focus = null;
80
- }
81
- function startSelection(state, cell) {
82
- state.active = true;
83
- state.dragging = true;
84
- state.anchor = cell;
85
- state.focus = cell;
86
- }
87
- function updateSelection(state, cell) {
88
- if (!state.dragging)
89
- return;
90
- state.focus = cell;
91
- }
92
- function endSelection(state, cell) {
93
- if (!state.dragging)
94
- return false;
95
- state.dragging = false;
96
- state.focus = cell;
97
- if (state.anchor && state.focus && state.anchor.row === state.focus.row && state.anchor.col === state.focus.col) {
98
- clearSelection(state);
99
- return false;
100
- }
101
- return true;
102
- }
103
- function selectionForRow(state, row, cols) {
104
- if (!state.active || !state.anchor || !state.focus)
105
- return null;
106
- const a = state.anchor;
107
- const f = state.focus;
108
- const forward = f.row > a.row || f.row === a.row && f.col >= a.col;
109
- const start = forward ? a : f;
110
- const end = forward ? f : a;
111
- if (start.row === end.row && row === start.row) {
112
- const left = Math.min(start.col, end.col);
113
- const right = Math.max(start.col, end.col) + 1;
114
- return { start: clamp(left, 0, cols), end: clamp(right, 0, cols) };
115
- }
116
- if (row < start.row || row > end.row)
117
- return null;
118
- if (row === start.row) {
119
- return { start: clamp(start.col, 0, cols), end: cols };
120
- }
121
- if (row === end.row) {
122
- return { start: 0, end: clamp(end.col + 1, 0, cols) };
123
- }
124
- return { start: 0, end: cols };
125
- }
126
- function getSelectionText(state, rows, cols, getCellText) {
127
- if (!state.active || !state.anchor || !state.focus)
128
- return "";
129
- if (!rows || !cols)
130
- return "";
131
- const a = state.anchor;
132
- const f = state.focus;
133
- const forward = f.row > a.row || f.row === a.row && f.col >= a.col;
134
- const startRow = forward ? a.row : f.row;
135
- const endRow = forward ? f.row : a.row;
136
- const lines = [];
137
- for (let row = startRow;row <= endRow; row += 1) {
138
- const range = selectionForRow(state, row, cols);
139
- if (!range)
140
- continue;
141
- let line = "";
142
- for (let col = range.start;col < range.end; col += 1) {
143
- const idx = row * cols + col;
144
- line += getCellText(idx);
145
- }
146
- line = line.replace(/[ \t]+$/g, "");
147
- lines.push(line);
148
- }
149
- return lines.join(`
150
- `);
151
- }
152
- function normalizeSelectionCell(cell, rows, cols, wideFlags) {
153
- if (!cell)
154
- return cell;
155
- if (!rows || !cols)
156
- return cell;
157
- const row = clamp(cell.row, 0, rows - 1);
158
- const col = clamp(cell.col, 0, cols - 1);
159
- if (!wideFlags)
160
- return { row, col };
161
- const idx = row * cols + col;
162
- const flag = wideFlags[idx] ?? 0;
163
- if (flag === 2) {
164
- const left = col > 0 ? col - 1 : col;
165
- return { row, col: left };
166
- }
167
- if (flag === 3 && row > 0) {
168
- const prevRow = row - 1;
169
- for (let c = cols - 1;c >= 0; c -= 1) {
170
- const f = wideFlags[prevRow * cols + c] ?? 0;
171
- if (f !== 2 && f !== 3)
172
- return { row: prevRow, col: c };
173
- }
174
- }
175
- return { row, col };
176
- }
177
- function positionToCell(clientX, clientY, canvasRect, dpr, cellW, cellH, cols, rows) {
178
- const x = (clientX - canvasRect.left) * dpr;
179
- const y = (clientY - canvasRect.top) * dpr;
180
- const col = clamp(Math.floor(x / (cellW || 1)), 0, (cols || 1) - 1);
181
- const row = clamp(Math.floor(y / (cellH || 1)), 0, (rows || 1) - 1);
182
- return { row, col };
183
- }
184
- async function copyToClipboard(text) {
185
- if (!text)
186
- return false;
187
- try {
188
- await navigator.clipboard.writeText(text);
189
- return true;
190
- } catch {
191
- const temp = document.createElement("textarea");
192
- temp.value = text;
193
- temp.style.position = "fixed";
194
- temp.style.opacity = "0";
195
- document.body.appendChild(temp);
196
- temp.select();
197
- try {
198
- document.execCommand("copy");
199
- return true;
200
- } catch {
201
- return false;
202
- } finally {
203
- document.body.removeChild(temp);
204
- }
205
- }
206
- }
207
- async function pasteFromClipboard() {
208
- try {
209
- return await navigator.clipboard.readText();
210
- } catch {
211
- return null;
212
- }
213
- }
214
- export {
215
- updateSelection,
216
- startSelection,
217
- selectionForRow,
218
- positionToCell,
219
- pasteFromClipboard,
220
- normalizeSelectionCell,
221
- getSelectionText,
222
- endSelection,
223
- createSelectionState,
224
- copyToClipboard,
225
- clearSelection
226
- };