@tooee/renderers 0.2.4 → 0.3.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.
Files changed (116) hide show
  1. package/dist/code-blocks.d.ts +6 -6
  2. package/dist/code-blocks.d.ts.map +1 -1
  3. package/dist/code-blocks.js +52 -42
  4. package/dist/code-blocks.js.map +1 -1
  5. package/dist/{CodeView.d.ts → code-view.d.ts} +3 -3
  6. package/dist/code-view.d.ts.map +1 -0
  7. package/dist/{CodeView.js → code-view.js} +5 -5
  8. package/dist/code-view.js.map +1 -0
  9. package/dist/{CommandPalette.d.ts → command-palette.d.ts} +2 -2
  10. package/dist/command-palette.d.ts.map +1 -0
  11. package/dist/{CommandPalette.js → command-palette.js} +27 -24
  12. package/dist/command-palette.js.map +1 -0
  13. package/dist/{ContextMenu.d.ts → context-menu.d.ts} +2 -2
  14. package/dist/context-menu.d.ts.map +1 -0
  15. package/dist/{ContextMenu.js → context-menu.js} +24 -14
  16. package/dist/context-menu.js.map +1 -0
  17. package/dist/{DecorationLayer.d.ts → decoration-layer.d.ts} +2 -2
  18. package/dist/decoration-layer.d.ts.map +1 -0
  19. package/dist/decoration-layer.js +2 -0
  20. package/dist/decoration-layer.js.map +1 -0
  21. package/dist/{DocumentBindings.d.ts → document-bindings.d.ts} +4 -4
  22. package/dist/document-bindings.d.ts.map +1 -0
  23. package/dist/document-bindings.js +2 -0
  24. package/dist/document-bindings.js.map +1 -0
  25. package/dist/index.d.ts +16 -15
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +9 -9
  28. package/dist/index.js.map +1 -1
  29. package/dist/markdown-blocks.d.ts +9 -13
  30. package/dist/markdown-blocks.d.ts.map +1 -1
  31. package/dist/markdown-blocks.js +98 -79
  32. package/dist/markdown-blocks.js.map +1 -1
  33. package/dist/{MarkdownView.d.ts → markdown-view.d.ts} +13 -13
  34. package/dist/markdown-view.d.ts.map +1 -0
  35. package/dist/markdown-view.js +455 -0
  36. package/dist/markdown-view.js.map +1 -0
  37. package/dist/mermaid.d.ts +5 -5
  38. package/dist/mermaid.d.ts.map +1 -1
  39. package/dist/mermaid.js +33 -25
  40. package/dist/mermaid.js.map +1 -1
  41. package/dist/parsers.d.ts +5 -5
  42. package/dist/parsers.d.ts.map +1 -1
  43. package/dist/parsers.js +64 -44
  44. package/dist/parsers.js.map +1 -1
  45. package/dist/{RowDocumentRenderable.d.ts → row-document-renderable.d.ts} +15 -11
  46. package/dist/row-document-renderable.d.ts.map +1 -0
  47. package/dist/{RowDocumentRenderable.js → row-document-renderable.js} +110 -62
  48. package/dist/row-document-renderable.js.map +1 -0
  49. package/dist/row-document.d.ts +1 -1
  50. package/dist/row-document.d.ts.map +1 -1
  51. package/dist/row-document.js +1 -1
  52. package/dist/row-document.js.map +1 -1
  53. package/dist/source.d.ts +6 -6
  54. package/dist/source.d.ts.map +1 -1
  55. package/dist/source.js +35 -26
  56. package/dist/source.js.map +1 -1
  57. package/dist/table-types.d.ts.map +1 -1
  58. package/dist/{Table.d.ts → table.d.ts} +6 -6
  59. package/dist/table.d.ts.map +1 -0
  60. package/dist/{Table.js → table.js} +65 -35
  61. package/dist/table.js.map +1 -0
  62. package/dist/text-table.d.ts.map +1 -1
  63. package/dist/text-table.js.map +1 -1
  64. package/dist/use-gutter-palette.d.ts +3 -0
  65. package/dist/use-gutter-palette.d.ts.map +1 -0
  66. package/dist/{useGutterPalette.js → use-gutter-palette.js} +4 -4
  67. package/dist/use-gutter-palette.js.map +1 -0
  68. package/package.json +5 -5
  69. package/src/code-blocks.tsx +122 -106
  70. package/src/{CodeView.tsx → code-view.tsx} +19 -14
  71. package/src/command-palette.tsx +163 -0
  72. package/src/context-menu.tsx +157 -0
  73. package/src/decoration-layer.ts +11 -0
  74. package/src/document-bindings.ts +16 -0
  75. package/src/index.ts +24 -23
  76. package/src/markdown-blocks.ts +178 -138
  77. package/src/markdown-view.tsx +853 -0
  78. package/src/mermaid.ts +94 -71
  79. package/src/parsers.ts +141 -103
  80. package/src/row-document-renderable.ts +672 -0
  81. package/src/row-document.ts +4 -4
  82. package/src/source.ts +92 -72
  83. package/src/table-types.ts +4 -4
  84. package/src/table.tsx +346 -0
  85. package/src/text-table.ts +4 -4
  86. package/src/use-gutter-palette.ts +15 -0
  87. package/dist/CodeView.d.ts.map +0 -1
  88. package/dist/CodeView.js.map +0 -1
  89. package/dist/CommandPalette.d.ts.map +0 -1
  90. package/dist/CommandPalette.js.map +0 -1
  91. package/dist/ContextMenu.d.ts.map +0 -1
  92. package/dist/ContextMenu.js.map +0 -1
  93. package/dist/DecorationLayer.d.ts.map +0 -1
  94. package/dist/DecorationLayer.js +0 -2
  95. package/dist/DecorationLayer.js.map +0 -1
  96. package/dist/DocumentBindings.d.ts.map +0 -1
  97. package/dist/DocumentBindings.js +0 -2
  98. package/dist/DocumentBindings.js.map +0 -1
  99. package/dist/MarkdownView.d.ts.map +0 -1
  100. package/dist/MarkdownView.js +0 -360
  101. package/dist/MarkdownView.js.map +0 -1
  102. package/dist/RowDocumentRenderable.d.ts.map +0 -1
  103. package/dist/RowDocumentRenderable.js.map +0 -1
  104. package/dist/Table.d.ts.map +0 -1
  105. package/dist/Table.js.map +0 -1
  106. package/dist/useGutterPalette.d.ts +0 -3
  107. package/dist/useGutterPalette.d.ts.map +0 -1
  108. package/dist/useGutterPalette.js.map +0 -1
  109. package/src/CommandPalette.tsx +0 -147
  110. package/src/ContextMenu.tsx +0 -132
  111. package/src/DecorationLayer.ts +0 -11
  112. package/src/DocumentBindings.ts +0 -16
  113. package/src/MarkdownView.tsx +0 -738
  114. package/src/RowDocumentRenderable.ts +0 -593
  115. package/src/Table.tsx +0 -300
  116. package/src/useGutterPalette.ts +0 -15
@@ -0,0 +1,672 @@
1
+ import { ScrollBoxRenderable, RGBA } from "@opentui/core";
2
+ import type {
3
+ RenderContext,
4
+ LineInfoProvider,
5
+ ScrollBoxOptions,
6
+ OptimizedBuffer,
7
+ } from "@opentui/core";
8
+ import type { DecorationLayer, RowDecoration } from "./decoration-layer.js";
9
+
10
+ // ---------------------------------------------------------------------------
11
+ // Types
12
+ // ---------------------------------------------------------------------------
13
+
14
+ export const DEFAULT_SIGN_COLUMN_WIDTH = 3;
15
+
16
+ export interface RowDocumentPalette {
17
+ gutterFg?: string;
18
+ gutterBg?: string;
19
+ }
20
+
21
+ export interface RowDocumentOptions extends ScrollBoxOptions {
22
+ mode?: "auto" | "multi" | "provider";
23
+
24
+ // Gutter
25
+ showGutter?: boolean;
26
+ showLineNumbers?: boolean;
27
+ lineNumberStart?: number;
28
+ signColumnWidth?: number;
29
+ gutterPaddingRight?: number;
30
+
31
+ // Multi-child: skip N leading children that aren't logical rows
32
+ rowChildOffset?: number;
33
+
34
+ // Colors
35
+ palette?: RowDocumentPalette;
36
+ decorations?: readonly DecorationLayer[];
37
+ }
38
+
39
+ // ---------------------------------------------------------------------------
40
+ // Provider detection
41
+ // ---------------------------------------------------------------------------
42
+
43
+ const isRowContentProvider = function isRowContentProvider(x: unknown): x is LineInfoProvider {
44
+ return (
45
+ x !== null &&
46
+ x !== undefined &&
47
+ typeof x === "object" &&
48
+ "lineInfo" in x &&
49
+ "lineCount" in x &&
50
+ "virtualLineCount" in x
51
+ );
52
+ };
53
+
54
+ // ---------------------------------------------------------------------------
55
+ // Color cache — avoids re-parsing hex strings every frame
56
+ // ---------------------------------------------------------------------------
57
+
58
+ const colorCache = new Map<string, RGBA>();
59
+
60
+ const cachedColor = function cachedColor(hex: string): RGBA {
61
+ let c = colorCache.get(hex);
62
+ if (!c) {
63
+ c = RGBA.fromHex(hex);
64
+ colorCache.set(hex, c);
65
+ }
66
+ return c;
67
+ };
68
+
69
+ const normalizePalette = function normalizePalette(
70
+ palette: RowDocumentPalette = {},
71
+ ): Required<RowDocumentPalette> {
72
+ return {
73
+ gutterBg: palette.gutterBg ?? "#0d1117",
74
+ gutterFg: palette.gutterFg ?? "#6e7681",
75
+ };
76
+ };
77
+
78
+ const normalizeDecorationLayers = function normalizeDecorationLayers(
79
+ layers: readonly DecorationLayer[] | undefined,
80
+ ): readonly DecorationLayer[] {
81
+ if (!layers || layers.length === 0) {
82
+ return [];
83
+ }
84
+ return [...layers].toSorted((a, b) => a.priority - b.priority);
85
+ };
86
+
87
+ export const computeRowDocumentGutterWidth = function computeRowDocumentGutterWidth(opts: {
88
+ showLineNumbers: boolean;
89
+ rowCount: number;
90
+ lineNumberStart?: number;
91
+ signColumnWidth?: number;
92
+ gutterPaddingRight?: number;
93
+ }): number {
94
+ let width = 0;
95
+ if (opts.showLineNumbers) {
96
+ const maxLineNum = (opts.lineNumberStart ?? 1) + opts.rowCount - 1;
97
+ width += Math.max(String(maxLineNum).length, 1);
98
+ }
99
+ width += opts.signColumnWidth ?? 0;
100
+ width += opts.gutterPaddingRight ?? 1;
101
+ return width;
102
+ };
103
+
104
+ // ---------------------------------------------------------------------------
105
+ // RowDocumentRenderable
106
+ // ---------------------------------------------------------------------------
107
+
108
+ export class RowDocumentRenderable extends ScrollBoxRenderable {
109
+ // -- Options --
110
+ private readonly _mode: "auto" | "multi" | "provider";
111
+ private _showGutter: boolean;
112
+ private _showLineNumbers: boolean;
113
+ private readonly _lineNumberStart: number;
114
+ private readonly _signColumnWidth: number;
115
+ private readonly _gutterPaddingRight: number;
116
+ private readonly _rowChildOffset: number;
117
+ private _palette: Required<RowDocumentPalette>;
118
+ private _layers: readonly DecorationLayer[] = [];
119
+ private _layerGutterBgs = new Map<number, string>();
120
+ private _layerSigns = new Map<number, NonNullable<RowDecoration["sign"]>>();
121
+
122
+ // -- Geometry arrays --
123
+ private _rowVirtualStarts: number[] = [];
124
+ private _rowVirtualHeights: number[] = [];
125
+ private _virtualRowToRow: number[] = [];
126
+ private _virtualRowWraps: number[] = [];
127
+ private _rowCount = 0;
128
+ /** Virtual Y one past the last content row; the rest of the scroll area is empty. */
129
+ private _contentVirtualEnd = 0;
130
+ private _lastGeometryHash = "";
131
+
132
+ constructor(ctx: RenderContext, options: RowDocumentOptions) {
133
+ super(ctx, options);
134
+
135
+ this._mode = options.mode ?? "auto";
136
+ this._showGutter = options.showGutter ?? true;
137
+ this._showLineNumbers = options.showLineNumbers ?? true;
138
+ this._lineNumberStart = options.lineNumberStart ?? 1;
139
+ this._signColumnWidth = options.signColumnWidth ?? 0;
140
+ this._gutterPaddingRight = options.gutterPaddingRight ?? 1;
141
+ this._rowChildOffset = options.rowChildOffset ?? 0;
142
+
143
+ this._palette = normalizePalette(options.palette);
144
+ this._layers = normalizeDecorationLayers(options.decorations);
145
+ }
146
+
147
+ // -----------------------------------------------------------------------
148
+ // Decorations
149
+ // -----------------------------------------------------------------------
150
+
151
+ get decorations(): readonly DecorationLayer[] {
152
+ return this._layers;
153
+ }
154
+
155
+ set decorations(layers: readonly DecorationLayer[] | undefined) {
156
+ this._layers = normalizeDecorationLayers(layers);
157
+ this.requestRender();
158
+ }
159
+
160
+ get palette(): Required<RowDocumentPalette> {
161
+ return this._palette;
162
+ }
163
+
164
+ set palette(palette: RowDocumentPalette | undefined) {
165
+ this._palette = normalizePalette(palette);
166
+ this.requestRender();
167
+ }
168
+
169
+ // -----------------------------------------------------------------------
170
+ // Gutter config setters
171
+ // -----------------------------------------------------------------------
172
+
173
+ get showGutter(): boolean {
174
+ return this._showGutter;
175
+ }
176
+
177
+ set showGutter(value: boolean) {
178
+ if (this._showGutter !== value) {
179
+ this._showGutter = value;
180
+ this._applyGutterPadding();
181
+ this.requestRender();
182
+ }
183
+ }
184
+
185
+ get showLineNumbers(): boolean {
186
+ return this._showLineNumbers;
187
+ }
188
+
189
+ set showLineNumbers(value: boolean) {
190
+ if (this._showLineNumbers !== value) {
191
+ this._showLineNumbers = value;
192
+ this._applyGutterPadding();
193
+ this.requestRender();
194
+ }
195
+ }
196
+
197
+ // -----------------------------------------------------------------------
198
+ // Geometry — public API
199
+ // -----------------------------------------------------------------------
200
+
201
+ get rowCount(): number {
202
+ return this._rowCount;
203
+ }
204
+
205
+ get virtualRowCount(): number {
206
+ return this._virtualRowToRow.length;
207
+ }
208
+
209
+ getRowMetrics(row: number): { row: number; virtualTop: number; virtualHeight: number } | null {
210
+ if (row < 0 || row >= this._rowCount) {
211
+ return null;
212
+ }
213
+ return {
214
+ row,
215
+ virtualHeight: this._rowVirtualHeights[row],
216
+ virtualTop: this._rowVirtualStarts[row],
217
+ };
218
+ }
219
+
220
+ getRowAtVirtualY(y: number): number {
221
+ const clamped = Math.max(0, Math.min(y, this._virtualRowToRow.length - 1));
222
+ const row = this._virtualRowToRow[clamped];
223
+ if (row >= 0) {
224
+ return row;
225
+ }
226
+ // Gap row — search backward for nearest valid row
227
+ for (let i = clamped - 1; i >= 0; i -= 1) {
228
+ const r = this._virtualRowToRow[i];
229
+ if (r >= 0) {
230
+ return r;
231
+ }
232
+ }
233
+ return 0;
234
+ }
235
+
236
+ /**
237
+ * Map an absolute terminal Y coordinate (as delivered on a `MouseEvent`) to a
238
+ * logical row index. Returns `null` when the Y is above/below the scroll
239
+ * viewport or past the last rendered content row, so callers can ignore clicks
240
+ * that land on empty space. Clicks that land in an inter-row margin gap resolve
241
+ * to the nearest row above (via `getRowAtVirtualY`).
242
+ *
243
+ * Works for both multi-child mode (row = child index, e.g. a markdown block)
244
+ * and provider mode (row = source line, e.g. a code line): geometry is
245
+ * recomputed every frame in `renderSelf`, so `_virtualRowToRow` is current by
246
+ * the time a click is processed.
247
+ */
248
+ getRowAtScreenY(screenY: number): number | null {
249
+ const localY = screenY - this.viewport.y;
250
+ if (localY < 0 || localY >= this.viewport.height) {
251
+ return null;
252
+ }
253
+ const virtualY = Math.floor(this.scrollTop) + localY;
254
+ if (virtualY < 0 || virtualY >= this._virtualRowToRow.length) {
255
+ return null;
256
+ }
257
+ if (virtualY >= this._contentVirtualEnd) {
258
+ return null;
259
+ }
260
+ return this.getRowAtVirtualY(virtualY);
261
+ }
262
+
263
+ getVisibleRange(): {
264
+ virtualTop: number;
265
+ virtualBottom: number;
266
+ firstRow: number;
267
+ lastRow: number;
268
+ } {
269
+ const top = Math.floor(this.scrollTop);
270
+ const bottom = top + this.viewport.height;
271
+ return {
272
+ firstRow: this.getRowAtVirtualY(top),
273
+ lastRow: this.getRowAtVirtualY(bottom - 1),
274
+ virtualBottom: bottom,
275
+ virtualTop: top,
276
+ };
277
+ }
278
+
279
+ // -----------------------------------------------------------------------
280
+ // scrollToRow
281
+ // -----------------------------------------------------------------------
282
+
283
+ scrollToRow(row: number, align: "nearest" | "start" | "center" | "end" = "nearest"): void {
284
+ const metrics = this.getRowMetrics(row);
285
+ if (!metrics) {
286
+ return;
287
+ }
288
+
289
+ const vpHeight = this.viewport.height;
290
+ const { virtualTop, virtualHeight } = metrics;
291
+
292
+ let target = this.scrollTop;
293
+
294
+ switch (align) {
295
+ case "start": {
296
+ target = virtualTop;
297
+ break;
298
+ }
299
+ case "center": {
300
+ target = virtualTop - (vpHeight - virtualHeight) / 2;
301
+ break;
302
+ }
303
+ case "end": {
304
+ target = virtualTop + virtualHeight - vpHeight;
305
+ break;
306
+ }
307
+ case "nearest": {
308
+ if (virtualTop < this.scrollTop) {
309
+ target = virtualTop;
310
+ } else if (virtualTop + virtualHeight > this.scrollTop + vpHeight) {
311
+ target = virtualTop + virtualHeight - vpHeight;
312
+ }
313
+ break;
314
+ }
315
+ default: {
316
+ break;
317
+ }
318
+ }
319
+
320
+ // When scrolling to the first row, include header content (pre-offset children)
321
+ if (row === 0 && this._rowChildOffset > 0) {
322
+ target = Math.min(target, 0);
323
+ }
324
+
325
+ // When the next row is the last logical row (e.g. bottom border),
326
+ // extend the scroll to include it
327
+ if (row + 1 === this._rowCount - 1) {
328
+ const next = this.getRowMetrics(row + 1);
329
+ if (next) {
330
+ const bottomEdge = next.virtualTop + next.virtualHeight;
331
+ if (bottomEdge > target + vpHeight) {
332
+ target = bottomEdge - vpHeight;
333
+ }
334
+ }
335
+ }
336
+
337
+ this.scrollTop = Math.max(0, Math.round(target));
338
+ }
339
+
340
+ // -----------------------------------------------------------------------
341
+ // Rendering
342
+ // -----------------------------------------------------------------------
343
+
344
+ protected renderSelf(buffer: OptimizedBuffer): void {
345
+ // Let ScrollBox do its own rendering first
346
+ super.renderSelf(buffer);
347
+
348
+ // Recompute geometry every frame (cheap — just iterating children)
349
+ this._computeGeometry();
350
+
351
+ // Apply gutter padding to content area
352
+ this._applyGutterPadding();
353
+
354
+ // Paint decorations and gutter on top
355
+ this._paintDecorations(buffer);
356
+ if (this._showGutter) {
357
+ this._paintGutter(buffer);
358
+ }
359
+ }
360
+
361
+ // -----------------------------------------------------------------------
362
+ // Geometry computation
363
+ // -----------------------------------------------------------------------
364
+
365
+ private _detectMode(): "multi" | "provider" {
366
+ if (this._mode === "multi") {
367
+ return "multi";
368
+ }
369
+ if (this._mode === "provider") {
370
+ return "provider";
371
+ }
372
+
373
+ // Auto-detect: single child with LineInfoProvider → provider mode
374
+ const children = this.getChildren();
375
+ if (children.length === 1 && isRowContentProvider(children[0])) {
376
+ return "provider";
377
+ }
378
+ return "multi";
379
+ }
380
+
381
+ private _computeGeometry(): void {
382
+ const mode = this._detectMode();
383
+ if (mode === "provider") {
384
+ this._computeFromProvider();
385
+ } else {
386
+ this._computeFromChildren();
387
+ }
388
+ }
389
+
390
+ private _computeFromChildren(): void {
391
+ const children = this.getChildren();
392
+ const offset = this._rowChildOffset;
393
+ const contentY = this.content.y;
394
+ const totalHeight = this.scrollHeight;
395
+
396
+ if (totalHeight === 0 || children.length <= offset) {
397
+ this._finishGeometry([], [], [], [], 0);
398
+ return;
399
+ }
400
+
401
+ const rowCount = children.length - offset;
402
+ const rowVirtualStarts: number[] = [];
403
+ const rowVirtualHeights: number[] = [];
404
+ const virtualRowToRow = Array.from({ length: totalHeight }, () => -1);
405
+ const virtualRowWraps = Array.from({ length: totalHeight }, () => 0);
406
+
407
+ for (let i = offset; i < children.length; i += 1) {
408
+ const row = i - offset;
409
+ const child = children[i];
410
+ const childStart = child.y - contentY;
411
+ const h = Math.max(1, child.height);
412
+
413
+ rowVirtualStarts[row] = childStart;
414
+ rowVirtualHeights[row] = h;
415
+
416
+ for (let r = 0; r < h; r += 1) {
417
+ const vRow = childStart + r;
418
+ if (vRow >= 0 && vRow < totalHeight) {
419
+ virtualRowToRow[vRow] = row;
420
+ virtualRowWraps[vRow] = r === 0 ? 0 : 1;
421
+ }
422
+ }
423
+ }
424
+
425
+ this._finishGeometry(
426
+ rowVirtualStarts,
427
+ rowVirtualHeights,
428
+ virtualRowToRow,
429
+ virtualRowWraps,
430
+ rowCount,
431
+ );
432
+ }
433
+
434
+ private _computeFromProvider(): void {
435
+ const children = this.getChildren();
436
+ if (children.length === 0) {
437
+ return;
438
+ }
439
+
440
+ const [provider] = children;
441
+ if (!isRowContentProvider(provider)) {
442
+ return;
443
+ }
444
+
445
+ const info = provider.lineInfo;
446
+ const { lineSources, lineWraps } = info;
447
+
448
+ const rowVirtualStarts: number[] = [];
449
+ const rowVirtualHeights: number[] = [];
450
+ const virtualRowToRow: number[] = [];
451
+ const virtualRowWraps: number[] = [];
452
+
453
+ let currentRow = -1;
454
+ let rowCount = 0;
455
+
456
+ for (let v = 0; v < lineSources.length; v += 1) {
457
+ const row = lineSources[v];
458
+ virtualRowToRow[v] = row;
459
+ virtualRowWraps[v] = lineWraps[v];
460
+
461
+ if (row !== currentRow) {
462
+ // New logical row
463
+ if (currentRow >= 0) {
464
+ rowVirtualHeights[currentRow] = v - rowVirtualStarts[currentRow];
465
+ }
466
+ rowVirtualStarts[row] = v;
467
+ currentRow = row;
468
+ rowCount = Math.max(rowCount, row + 1);
469
+ }
470
+ }
471
+
472
+ // Finalize last row
473
+ if (currentRow >= 0) {
474
+ rowVirtualHeights[currentRow] = lineSources.length - rowVirtualStarts[currentRow];
475
+ }
476
+
477
+ this._finishGeometry(
478
+ rowVirtualStarts,
479
+ rowVirtualHeights,
480
+ virtualRowToRow,
481
+ virtualRowWraps,
482
+ rowCount,
483
+ );
484
+ }
485
+
486
+ private _finishGeometry(
487
+ rowVirtualStarts: number[],
488
+ rowVirtualHeights: number[],
489
+ virtualRowToRow: number[],
490
+ virtualRowWraps: number[],
491
+ rowCount: number,
492
+ ): void {
493
+ let contentVirtualEnd = 0;
494
+ for (let row = 0; row < rowCount; row += 1) {
495
+ const end = rowVirtualStarts[row] + rowVirtualHeights[row];
496
+ if (end > contentVirtualEnd) {
497
+ contentVirtualEnd = end;
498
+ }
499
+ }
500
+ this._contentVirtualEnd = contentVirtualEnd;
501
+
502
+ // Change detection via hash
503
+ const hash = rowVirtualStarts.join(",");
504
+ if (hash !== this._lastGeometryHash) {
505
+ this._rowVirtualStarts = rowVirtualStarts;
506
+ this._rowVirtualHeights = rowVirtualHeights;
507
+ this._virtualRowToRow = virtualRowToRow;
508
+ this._virtualRowWraps = virtualRowWraps;
509
+ this._rowCount = rowCount;
510
+ this._lastGeometryHash = hash;
511
+ this.emit("row-geometry-change");
512
+ }
513
+ }
514
+
515
+ // -----------------------------------------------------------------------
516
+ // Gutter width calculation and padding
517
+ // -----------------------------------------------------------------------
518
+
519
+ private _computeGutterWidth(): number {
520
+ if (!this._showGutter) {
521
+ return 0;
522
+ }
523
+
524
+ return computeRowDocumentGutterWidth({
525
+ gutterPaddingRight: this._gutterPaddingRight,
526
+ lineNumberStart: this._lineNumberStart,
527
+ rowCount: this._rowCount,
528
+ showLineNumbers: this._showLineNumbers,
529
+ signColumnWidth: this._signColumnWidth,
530
+ });
531
+ }
532
+
533
+ private _applyGutterPadding(): void {
534
+ const gutterWidth = this._computeGutterWidth();
535
+ this.content.paddingLeft = gutterWidth;
536
+ }
537
+
538
+ // -----------------------------------------------------------------------
539
+ // Decoration painting (row backgrounds)
540
+ // -----------------------------------------------------------------------
541
+
542
+ private _paintDecorations(buffer: OptimizedBuffer): void {
543
+ this._layerGutterBgs = new Map();
544
+ this._layerSigns = new Map();
545
+
546
+ if (this._layers.length === 0) {
547
+ return;
548
+ }
549
+
550
+ const vpX = this.viewport.x;
551
+ const vpY = this.viewport.y;
552
+ const vpHeight = this.viewport.height;
553
+ const vpWidth = this.viewport.width;
554
+ const top = Math.floor(this.scrollTop);
555
+
556
+ const { firstRow, lastRow } = this.getVisibleRange();
557
+ const rowBgs = new Map<number, string>();
558
+ const rowGutterBgs = new Map<number, string>();
559
+ const rowSigns = new Map<number, NonNullable<RowDecoration["sign"]>>();
560
+
561
+ for (const layer of this._layers) {
562
+ for (const deco of layer.forVisibleRows(firstRow, lastRow)) {
563
+ if (deco.background !== undefined && deco.background !== "") {
564
+ rowBgs.set(deco.row, deco.background);
565
+ }
566
+ if (deco.gutterBackground !== undefined && deco.gutterBackground !== "") {
567
+ rowGutterBgs.set(deco.row, deco.gutterBackground);
568
+ }
569
+ if (deco.sign) {
570
+ rowSigns.set(deco.row, deco.sign);
571
+ }
572
+ }
573
+ }
574
+
575
+ for (let screenY = 0; screenY < vpHeight; screenY += 1) {
576
+ const vRow = top + screenY;
577
+ if (vRow >= this._virtualRowToRow.length) {
578
+ break;
579
+ }
580
+
581
+ const row = this._virtualRowToRow[vRow];
582
+ if (row < 0) {
583
+ continue;
584
+ }
585
+ const bg = rowBgs.get(row);
586
+ if (bg === undefined || bg === "") {
587
+ continue;
588
+ }
589
+ buffer.fillRect(vpX, vpY + screenY, vpWidth, 1, cachedColor(bg));
590
+ }
591
+
592
+ this._layerGutterBgs = rowGutterBgs;
593
+ this._layerSigns = rowSigns;
594
+ }
595
+
596
+ // -----------------------------------------------------------------------
597
+ // Gutter painting
598
+ // -----------------------------------------------------------------------
599
+
600
+ private _paintGutter(buffer: OptimizedBuffer): void {
601
+ const gutterWidth = this._computeGutterWidth();
602
+ if (gutterWidth === 0) {
603
+ return;
604
+ }
605
+
606
+ const vpX = this.viewport.x;
607
+ const vpY = this.viewport.y;
608
+ const vpHeight = this.viewport.height;
609
+ const top = Math.floor(this.scrollTop);
610
+
611
+ const gutterBg = cachedColor(this._palette.gutterBg);
612
+ const gutterFg = cachedColor(this._palette.gutterFg);
613
+
614
+ // Fill gutter background
615
+ buffer.fillRect(vpX, vpY, gutterWidth, vpHeight, gutterBg);
616
+
617
+ const lineNumWidth = this._showLineNumbers
618
+ ? Math.max(String(this._lineNumberStart + this._rowCount - 1).length, 1)
619
+ : 0;
620
+
621
+ for (let screenY = 0; screenY < vpHeight; screenY += 1) {
622
+ const vRow = top + screenY;
623
+ if (vRow >= this._virtualRowToRow.length) {
624
+ break;
625
+ }
626
+
627
+ const row = this._virtualRowToRow[vRow];
628
+ if (row < 0) {
629
+ continue;
630
+ }
631
+ const isFirstLine = this._virtualRowWraps[vRow] === 0;
632
+
633
+ if (!isFirstLine) {
634
+ continue;
635
+ }
636
+
637
+ const drawX = vpX;
638
+ let col = 0;
639
+
640
+ const rowGutterHex = this._layerGutterBgs.get(row);
641
+ const effectiveGutterBg =
642
+ rowGutterHex !== undefined && rowGutterHex !== "" ? cachedColor(rowGutterHex) : gutterBg;
643
+ if (rowGutterHex !== undefined && rowGutterHex !== "") {
644
+ buffer.fillRect(drawX, vpY + screenY, gutterWidth, 1, effectiveGutterBg);
645
+ }
646
+
647
+ // Line number
648
+ if (this._showLineNumbers) {
649
+ const lineNum = String(this._lineNumberStart + row);
650
+ const padded = lineNum.padStart(lineNumWidth, " ");
651
+ buffer.drawText(padded, drawX + col, vpY + screenY, gutterFg, effectiveGutterBg);
652
+ col += lineNumWidth;
653
+ }
654
+
655
+ // Sign column
656
+ if (this._signColumnWidth > 0) {
657
+ const sign = this._layerSigns.get(row);
658
+
659
+ if (sign) {
660
+ const signFg = sign.fg !== undefined && sign.fg !== "" ? cachedColor(sign.fg) : gutterFg;
661
+ buffer.drawText(
662
+ sign.text.slice(0, this._signColumnWidth),
663
+ drawX + col,
664
+ vpY + screenY,
665
+ signFg,
666
+ effectiveGutterBg,
667
+ );
668
+ }
669
+ }
670
+ }
671
+ }
672
+ }
@@ -1,10 +1,10 @@
1
- import { extend } from "@opentui/react"
2
- import { RowDocumentRenderable } from "./RowDocumentRenderable.js"
1
+ import { extend } from "@opentui/react";
2
+ import { RowDocumentRenderable } from "./row-document-renderable.js";
3
3
 
4
- extend({ "row-document": RowDocumentRenderable })
4
+ extend({ "row-document": RowDocumentRenderable });
5
5
 
6
6
  declare module "@opentui/react" {
7
7
  interface OpenTUIComponents {
8
- "row-document": typeof RowDocumentRenderable
8
+ "row-document": typeof RowDocumentRenderable;
9
9
  }
10
10
  }