@tooee/renderers 0.12.0 → 0.13.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.
- package/dist/chooser/choose-filter.d.ts.map +1 -1
- package/dist/chooser/choose-filter.js.map +1 -1
- package/dist/chooser/choose-highlighted-text.d.ts.map +1 -1
- package/dist/chooser/choose-highlighted-text.js.map +1 -1
- package/dist/chooser/choose-list.d.ts.map +1 -1
- package/dist/chooser/choose-list.js.map +1 -1
- package/dist/chooser/choose-store.d.ts +3 -3
- package/dist/chooser/choose-store.d.ts.map +1 -1
- package/dist/chooser/choose-store.js.map +1 -1
- package/dist/chooser/chooser.d.ts.map +1 -1
- package/dist/chooser/chooser.js.map +1 -1
- package/dist/chooser/source.d.ts.map +1 -1
- package/dist/chooser/source.js.map +1 -1
- package/dist/chooser/use-choose.d.ts.map +1 -1
- package/dist/chooser/use-choose.js.map +1 -1
- package/dist/code-blocks.d.ts.map +1 -1
- package/dist/code-blocks.js.map +1 -1
- package/dist/command-palette.d.ts.map +1 -1
- package/dist/command-palette.js.map +1 -1
- package/dist/context-menu.d.ts.map +1 -1
- package/dist/context-menu.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/markdown/blocks/blockquote.d.ts.map +1 -1
- package/dist/markdown/blocks/blockquote.js.map +1 -1
- package/dist/markdown/blocks/flat-block.d.ts.map +1 -1
- package/dist/markdown/blocks/flat-block.js.map +1 -1
- package/dist/markdown/blocks/heading.d.ts.map +1 -1
- package/dist/markdown/blocks/heading.js.map +1 -1
- package/dist/markdown/blocks/list-line.d.ts.map +1 -1
- package/dist/markdown/blocks/list-line.js.map +1 -1
- package/dist/markdown/blocks/paragraph.d.ts.map +1 -1
- package/dist/markdown/blocks/paragraph.js.map +1 -1
- package/dist/markdown/blocks/table.d.ts.map +1 -1
- package/dist/markdown/blocks/table.js.map +1 -1
- package/dist/markdown/links.d.ts.map +1 -1
- package/dist/markdown/links.js.map +1 -1
- package/dist/markdown/plain-text.d.ts.map +1 -1
- package/dist/markdown/plain-text.js.map +1 -1
- package/dist/markdown/table-links.d.ts.map +1 -1
- package/dist/markdown/table-links.js.map +1 -1
- package/dist/markdown/tokens.d.ts.map +1 -1
- package/dist/markdown/tokens.js.map +1 -1
- package/dist/markdown/visit-inline.d.ts.map +1 -1
- package/dist/markdown/visit-inline.js.map +1 -1
- package/dist/markdown-blocks.d.ts.map +1 -1
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/markdown-images.d.ts.map +1 -1
- package/dist/markdown-images.js.map +1 -1
- package/dist/markdown-view.d.ts.map +1 -1
- package/dist/markdown-view.js.map +1 -1
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +8 -4
- package/dist/mermaid.js.map +1 -1
- package/dist/parsers.d.ts.map +1 -1
- package/dist/parsers.js.map +1 -1
- package/dist/row-document-renderable.d.ts.map +1 -1
- package/dist/row-document-renderable.js.map +1 -1
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js.map +1 -1
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js.map +1 -1
- package/dist/text-table-internals.d.ts.map +1 -1
- package/dist/text-table-internals.js.map +1 -1
- package/package.json +6 -6
- package/src/chooser/choose-filter.tsx +1 -0
- package/src/chooser/choose-highlighted-text.tsx +2 -0
- package/src/chooser/choose-list.tsx +6 -0
- package/src/chooser/choose-store.ts +20 -3
- package/src/chooser/chooser.tsx +2 -0
- package/src/chooser/source.ts +5 -0
- package/src/chooser/use-choose.ts +44 -0
- package/src/code-blocks.tsx +14 -0
- package/src/command-palette.tsx +4 -0
- package/src/context-menu.tsx +13 -0
- package/src/index.ts +42 -0
- package/src/markdown/blocks/blockquote.tsx +2 -0
- package/src/markdown/blocks/flat-block.tsx +13 -0
- package/src/markdown/blocks/heading.tsx +1 -0
- package/src/markdown/blocks/list-line.tsx +3 -0
- package/src/markdown/blocks/paragraph.tsx +1 -0
- package/src/markdown/blocks/table.tsx +7 -0
- package/src/markdown/links.ts +26 -0
- package/src/markdown/plain-text.ts +5 -0
- package/src/markdown/table-links.ts +12 -0
- package/src/markdown/tokens.ts +2 -0
- package/src/markdown/visit-inline.ts +20 -0
- package/src/markdown-blocks.ts +31 -0
- package/src/markdown-images.ts +21 -0
- package/src/markdown-view.tsx +4 -0
- package/src/mermaid.ts +21 -4
- package/src/parsers.ts +39 -0
- package/src/row-document-renderable.ts +53 -0
- package/src/source.ts +18 -0
- package/src/table.tsx +32 -0
- package/src/text-table-internals.ts +12 -0
package/src/source.ts
CHANGED
|
@@ -56,16 +56,19 @@ export interface DocumentRowAnchor<T> {
|
|
|
56
56
|
// ---------------------------------------------------------------------------
|
|
57
57
|
|
|
58
58
|
const LINE_FEED_CODE = 10;
|
|
59
|
+
|
|
59
60
|
const CARRIAGE_RETURN_CODE = 13;
|
|
60
61
|
|
|
61
62
|
/** Offsets of the first character of every physical line. */
|
|
62
63
|
const buildLineStarts = function buildLineStarts(text: string): number[] {
|
|
63
64
|
const starts = [0];
|
|
65
|
+
|
|
64
66
|
for (let i = 0; i < text.length; i += 1) {
|
|
65
67
|
if (text.codePointAt(i) === LINE_FEED_CODE) {
|
|
66
68
|
starts.push(i + 1);
|
|
67
69
|
}
|
|
68
70
|
}
|
|
71
|
+
|
|
69
72
|
return starts;
|
|
70
73
|
};
|
|
71
74
|
|
|
@@ -90,19 +93,23 @@ export class SourceIndex {
|
|
|
90
93
|
const starts = this.lineStarts;
|
|
91
94
|
let lo = 0;
|
|
92
95
|
let hi = starts.length - 1;
|
|
96
|
+
|
|
93
97
|
while (lo < hi) {
|
|
94
98
|
const mid = Math.floor((lo + hi + 1) / 2);
|
|
99
|
+
|
|
95
100
|
if (starts[mid] <= offset) {
|
|
96
101
|
lo = mid;
|
|
97
102
|
} else {
|
|
98
103
|
hi = mid - 1;
|
|
99
104
|
}
|
|
100
105
|
}
|
|
106
|
+
|
|
101
107
|
return lo;
|
|
102
108
|
}
|
|
103
109
|
|
|
104
110
|
point(offset: number): SourcePoint {
|
|
105
111
|
const line = this.lineAt(offset);
|
|
112
|
+
|
|
106
113
|
return { column: offset - this.lineStarts[line], line, offset };
|
|
107
114
|
}
|
|
108
115
|
|
|
@@ -111,12 +118,15 @@ export class SourceIndex {
|
|
|
111
118
|
const starts = this.lineStarts;
|
|
112
119
|
const nextStart = line + 1 < starts.length ? starts[line + 1] : this.text.length;
|
|
113
120
|
let end = nextStart;
|
|
121
|
+
|
|
114
122
|
if (end > starts[line] && this.text.codePointAt(end - 1) === LINE_FEED_CODE) {
|
|
115
123
|
end -= 1;
|
|
124
|
+
|
|
116
125
|
if (end > starts[line] && this.text.codePointAt(end - 1) === CARRIAGE_RETURN_CODE) {
|
|
117
126
|
end -= 1;
|
|
118
127
|
}
|
|
119
128
|
}
|
|
129
|
+
|
|
120
130
|
return end;
|
|
121
131
|
}
|
|
122
132
|
|
|
@@ -128,14 +138,17 @@ export class SourceIndex {
|
|
|
128
138
|
*/
|
|
129
139
|
span(rawStart: number, rawEnd: number, trimTrailingNewlines = true): SourceSpan {
|
|
130
140
|
let end = rawEnd;
|
|
141
|
+
|
|
131
142
|
if (trimTrailingNewlines) {
|
|
132
143
|
while (end > rawStart && this.text.codePointAt(end - 1) === 10) {
|
|
133
144
|
end -= 1;
|
|
145
|
+
|
|
134
146
|
if (end > rawStart && this.text.codePointAt(end - 1) === 13) {
|
|
135
147
|
end -= 1;
|
|
136
148
|
}
|
|
137
149
|
}
|
|
138
150
|
}
|
|
151
|
+
|
|
139
152
|
if (end < rawStart) {
|
|
140
153
|
end = rawStart;
|
|
141
154
|
}
|
|
@@ -143,6 +156,7 @@ export class SourceIndex {
|
|
|
143
156
|
const start = this.point(rawStart);
|
|
144
157
|
const endPoint = this.point(end);
|
|
145
158
|
const lastLine = end > rawStart ? this.lineAt(end - 1) : start.line;
|
|
159
|
+
|
|
146
160
|
const span: SourceSpan = {
|
|
147
161
|
end: endPoint,
|
|
148
162
|
lastLine,
|
|
@@ -150,9 +164,11 @@ export class SourceIndex {
|
|
|
150
164
|
start,
|
|
151
165
|
text: this.text.slice(rawStart, end),
|
|
152
166
|
};
|
|
167
|
+
|
|
153
168
|
if (this.sourceId !== undefined) {
|
|
154
169
|
span.sourceId = this.sourceId;
|
|
155
170
|
}
|
|
171
|
+
|
|
156
172
|
return span;
|
|
157
173
|
}
|
|
158
174
|
}
|
|
@@ -179,6 +195,7 @@ export const sourceLines = function sourceLines(
|
|
|
179
195
|
): SourceLineRow[] {
|
|
180
196
|
const index = new SourceIndex(source, options?.sourceId);
|
|
181
197
|
const rows: SourceLineRow[] = [];
|
|
198
|
+
|
|
182
199
|
for (let line = 0; line < index.lineStarts.length; line += 1) {
|
|
183
200
|
const start = index.lineStarts[line];
|
|
184
201
|
const end = index.lineContentEnd(line);
|
|
@@ -187,6 +204,7 @@ export const sourceLines = function sourceLines(
|
|
|
187
204
|
text: source.slice(start, end),
|
|
188
205
|
});
|
|
189
206
|
}
|
|
207
|
+
|
|
190
208
|
return rows;
|
|
191
209
|
};
|
|
192
210
|
|
package/src/table.tsx
CHANGED
|
@@ -38,9 +38,13 @@ export interface TableProps {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
const PADDING = 1;
|
|
41
|
+
|
|
41
42
|
const TABLE_MARGIN = 1;
|
|
43
|
+
|
|
42
44
|
const DEFAULT_MIN_COL_WIDTH = 4;
|
|
45
|
+
|
|
43
46
|
const DEFAULT_MAX_COL_WIDTH = 80;
|
|
47
|
+
|
|
44
48
|
const DEFAULT_SAMPLE_SIZE = 100;
|
|
45
49
|
|
|
46
50
|
const isNumeric = function isNumeric(value: string): boolean {
|
|
@@ -58,12 +62,15 @@ const sampleRows = function sampleRows(rows: string[][], sampleSize: number): st
|
|
|
58
62
|
if (rows.length <= sampleSize) {
|
|
59
63
|
return rows;
|
|
60
64
|
}
|
|
65
|
+
|
|
61
66
|
// Sample evenly distributed rows for representative widths
|
|
62
67
|
const step = rows.length / sampleSize;
|
|
63
68
|
const sampled: string[][] = [];
|
|
69
|
+
|
|
64
70
|
for (let i = 0; i < sampleSize; i += 1) {
|
|
65
71
|
sampled.push(rows[Math.floor(i * step)]);
|
|
66
72
|
}
|
|
73
|
+
|
|
67
74
|
return sampled;
|
|
68
75
|
};
|
|
69
76
|
|
|
@@ -85,15 +92,18 @@ const computeColumnWidths = function computeColumnWidths(
|
|
|
85
92
|
const headerLen = Bun.stringWidth(header);
|
|
86
93
|
let maxRowLen = 0;
|
|
87
94
|
const sampledRowCount = sampledRows.length;
|
|
95
|
+
|
|
88
96
|
for (let rowIndex = 0; rowIndex < sampledRowCount; rowIndex += 1) {
|
|
89
97
|
if (rowIndex in sampledRows) {
|
|
90
98
|
const row = sampledRows[rowIndex];
|
|
91
99
|
maxRowLen = Math.max(maxRowLen, Bun.stringWidth(row[col] ?? ""));
|
|
92
100
|
}
|
|
93
101
|
}
|
|
102
|
+
|
|
94
103
|
const contentWidth = Math.max(headerLen, maxRowLen);
|
|
95
104
|
// Apply min/max constraints before adding padding
|
|
96
105
|
const constrainedWidth = Math.min(maxColumnWidth, Math.max(minColumnWidth, contentWidth));
|
|
106
|
+
|
|
97
107
|
return constrainedWidth + PADDING * 2;
|
|
98
108
|
});
|
|
99
109
|
|
|
@@ -106,8 +116,10 @@ const computeColumnWidths = function computeColumnWidths(
|
|
|
106
116
|
const extra = maxWidth - totalNatural;
|
|
107
117
|
const perCol = Math.floor(extra / colCount);
|
|
108
118
|
const remainder = extra - perCol * colCount;
|
|
119
|
+
|
|
109
120
|
return naturalWidths.map((w, i) => w + perCol + (i < remainder ? 1 : 0));
|
|
110
121
|
}
|
|
122
|
+
|
|
111
123
|
return naturalWidths;
|
|
112
124
|
}
|
|
113
125
|
|
|
@@ -130,6 +142,7 @@ const computeColumnWidths = function computeColumnWidths(
|
|
|
130
142
|
if (longTotal === 0 || remaining <= 0) {
|
|
131
143
|
// All compact or no space left -- distribute evenly
|
|
132
144
|
const total = naturalWidths.reduce((a, b) => a + b, 0);
|
|
145
|
+
|
|
133
146
|
return naturalWidths.map((w) =>
|
|
134
147
|
Math.max(minColWidthWithPadding, Math.floor((w / total) * available))
|
|
135
148
|
);
|
|
@@ -139,6 +152,7 @@ const computeColumnWidths = function computeColumnWidths(
|
|
|
139
152
|
if (compact[i]) {
|
|
140
153
|
return w;
|
|
141
154
|
}
|
|
155
|
+
|
|
142
156
|
return Math.max(minColWidthWithPadding, Math.floor((w / longTotal) * remaining));
|
|
143
157
|
});
|
|
144
158
|
};
|
|
@@ -148,36 +162,46 @@ export const formatTableCell = function formatTableCell(value: TableRow[string])
|
|
|
148
162
|
if (value === null || value === undefined) {
|
|
149
163
|
return "";
|
|
150
164
|
}
|
|
165
|
+
|
|
151
166
|
if (value instanceof Date) {
|
|
152
167
|
return value.toISOString();
|
|
153
168
|
}
|
|
169
|
+
|
|
154
170
|
const valueTag = Object.prototype.toString.call(value);
|
|
171
|
+
|
|
155
172
|
switch (valueTag) {
|
|
156
173
|
case "[object String]": {
|
|
157
174
|
return String.prototype.toString.call(value);
|
|
158
175
|
}
|
|
176
|
+
|
|
159
177
|
case "[object Number]": {
|
|
160
178
|
return Number.prototype.toString.call(value);
|
|
161
179
|
}
|
|
180
|
+
|
|
162
181
|
case "[object BigInt]": {
|
|
163
182
|
return BigInt.prototype.toString.call(value);
|
|
164
183
|
}
|
|
184
|
+
|
|
165
185
|
case "[object Boolean]": {
|
|
166
186
|
return Boolean.prototype.toString.call(value);
|
|
167
187
|
}
|
|
188
|
+
|
|
168
189
|
case "[object Symbol]": {
|
|
169
190
|
return Symbol.prototype.toString.call(value);
|
|
170
191
|
}
|
|
192
|
+
|
|
171
193
|
case "[object Function]":
|
|
172
194
|
case "[object AsyncFunction]":
|
|
173
195
|
case "[object GeneratorFunction]":
|
|
174
196
|
case "[object AsyncGeneratorFunction]": {
|
|
175
197
|
return Function.prototype.toString.call(value);
|
|
176
198
|
}
|
|
199
|
+
|
|
177
200
|
default: {
|
|
178
201
|
break;
|
|
179
202
|
}
|
|
180
203
|
}
|
|
204
|
+
|
|
181
205
|
try {
|
|
182
206
|
return JSON.stringify(value);
|
|
183
207
|
} catch {
|
|
@@ -212,12 +236,14 @@ export const Table = function Table({
|
|
|
212
236
|
}),
|
|
213
237
|
[showLineNumbers, rows.length]
|
|
214
238
|
);
|
|
239
|
+
|
|
215
240
|
const effectiveMaxWidth = Math.max(
|
|
216
241
|
0,
|
|
217
242
|
(maxWidth ?? terminalWidth) - TABLE_MARGIN * 2 - gutterWidth
|
|
218
243
|
);
|
|
219
244
|
|
|
220
245
|
const headers = useMemo(() => columns.map((column) => column.header ?? column.key), [columns]);
|
|
246
|
+
|
|
221
247
|
const normalizedRows = useMemo(
|
|
222
248
|
() => rows.map((row) => columns.map((column) => formatTableCell(row[column.key]))),
|
|
223
249
|
[columns, rows]
|
|
@@ -249,11 +275,14 @@ export const Table = function Table({
|
|
|
249
275
|
if (column.align === "right") {
|
|
250
276
|
return true;
|
|
251
277
|
}
|
|
278
|
+
|
|
252
279
|
if (column.align === "left") {
|
|
253
280
|
return false;
|
|
254
281
|
}
|
|
282
|
+
|
|
255
283
|
const sampleValues = normalizedRows.slice(0, 10).map((row) => row[colIdx] ?? "");
|
|
256
284
|
const numericCount = sampleValues.filter(isNumeric).length;
|
|
285
|
+
|
|
257
286
|
return numericCount > sampleValues.length / 2;
|
|
258
287
|
}),
|
|
259
288
|
[columns, normalizedRows]
|
|
@@ -266,10 +295,12 @@ export const Table = function Table({
|
|
|
266
295
|
{row.map((cell, j): ReactNode => {
|
|
267
296
|
const contentWidth = colWidths[j] - PADDING * 2;
|
|
268
297
|
const cellWidth = Bun.stringWidth(cell);
|
|
298
|
+
|
|
269
299
|
const displayCell =
|
|
270
300
|
alignments[j] && cellWidth <= contentWidth
|
|
271
301
|
? " ".repeat(contentWidth - cellWidth) + cell
|
|
272
302
|
: cell;
|
|
303
|
+
|
|
273
304
|
return (
|
|
274
305
|
<text
|
|
275
306
|
key={j}
|
|
@@ -344,4 +375,5 @@ export const Table = function Table({
|
|
|
344
375
|
|
|
345
376
|
// Exported for testing and MarkdownView
|
|
346
377
|
export { computeColumnWidths, isNumeric, sampleRows };
|
|
378
|
+
|
|
347
379
|
export type { ColumnWidthOptions };
|
|
@@ -33,7 +33,9 @@ export const hasTextTableInternals = function hasTextTableInternals(
|
|
|
33
33
|
) {
|
|
34
34
|
return false;
|
|
35
35
|
}
|
|
36
|
+
|
|
36
37
|
const { _layout: layout } = table;
|
|
38
|
+
|
|
37
39
|
return (
|
|
38
40
|
Array.isArray(table._cells) &&
|
|
39
41
|
Number.isInteger(table.cellPaddingX) &&
|
|
@@ -68,10 +70,12 @@ const spanAt = function spanAt(
|
|
|
68
70
|
): Span | null {
|
|
69
71
|
for (const [index, size] of sizes.entries()) {
|
|
70
72
|
const inside = local - (offsets[index] ?? 0) - 1;
|
|
73
|
+
|
|
71
74
|
if (inside >= 0 && inside < size) {
|
|
72
75
|
return { index, inside };
|
|
73
76
|
}
|
|
74
77
|
}
|
|
78
|
+
|
|
75
79
|
return null;
|
|
76
80
|
};
|
|
77
81
|
|
|
@@ -98,27 +102,35 @@ export const textTableCellAt = function textTableCellAt(
|
|
|
98
102
|
if (!hasTextTableInternals(table)) {
|
|
99
103
|
return null;
|
|
100
104
|
}
|
|
105
|
+
|
|
101
106
|
const { columnOffsets, columnWidths, rowHeights, rowOffsets } = table._layout;
|
|
102
107
|
const row = spanAt(rowOffsets, rowHeights, y - table.y);
|
|
103
108
|
const column = spanAt(columnOffsets, columnWidths, x - table.x);
|
|
109
|
+
|
|
104
110
|
if (row === null || column === null) {
|
|
105
111
|
return null;
|
|
106
112
|
}
|
|
113
|
+
|
|
107
114
|
const view = table._cells[row.index]?.[column.index]?.textBufferView;
|
|
115
|
+
|
|
108
116
|
if (!(view instanceof TextBufferView)) {
|
|
109
117
|
return null;
|
|
110
118
|
}
|
|
119
|
+
|
|
111
120
|
const cellX = column.inside - table.cellPaddingX;
|
|
112
121
|
const visualLine = row.inside - table.cellPaddingY;
|
|
113
122
|
const { lineSources, lineStartCols, lineWidthCols } = view.lineInfo;
|
|
114
123
|
const line = lineSources[visualLine];
|
|
115
124
|
const start = lineStartCols[visualLine];
|
|
116
125
|
const width = lineWidthCols[visualLine];
|
|
126
|
+
|
|
117
127
|
if (line === undefined || start === undefined || width === undefined) {
|
|
118
128
|
return null;
|
|
119
129
|
}
|
|
130
|
+
|
|
120
131
|
if (cellX < 0 || cellX >= width) {
|
|
121
132
|
return null;
|
|
122
133
|
}
|
|
134
|
+
|
|
123
135
|
return { column: column.index, line, offset: start + cellX, row: row.index };
|
|
124
136
|
};
|