@tooee/renderers 0.1.21 → 0.2.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/CodeView.d.ts +11 -11
- package/dist/CodeView.d.ts.map +1 -1
- package/dist/CodeView.js +2 -20
- package/dist/CodeView.js.map +1 -1
- package/dist/DocumentBindings.d.ts +16 -0
- package/dist/DocumentBindings.d.ts.map +1 -0
- package/dist/DocumentBindings.js +2 -0
- package/dist/DocumentBindings.js.map +1 -0
- package/dist/MarkdownView.d.ts +23 -28
- package/dist/MarkdownView.d.ts.map +1 -1
- package/dist/MarkdownView.js +4 -110
- package/dist/MarkdownView.js.map +1 -1
- package/dist/RowDocumentRenderable.d.ts +2 -0
- package/dist/RowDocumentRenderable.d.ts.map +1 -1
- package/dist/RowDocumentRenderable.js +11 -0
- package/dist/RowDocumentRenderable.js.map +1 -1
- package/dist/Table.d.ts +9 -10
- package/dist/Table.d.ts.map +1 -1
- package/dist/Table.js +3 -20
- package/dist/Table.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/markdown-blocks.d.ts +41 -0
- package/dist/markdown-blocks.d.ts.map +1 -0
- package/dist/markdown-blocks.js +246 -0
- package/dist/markdown-blocks.js.map +1 -0
- package/dist/source.d.ts +83 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +114 -0
- package/dist/source.js.map +1 -0
- package/package.json +15 -9
- package/src/CodeView.tsx +15 -41
- package/src/DocumentBindings.ts +16 -0
- package/src/MarkdownView.tsx +33 -150
- package/src/RowDocumentRenderable.ts +10 -0
- package/src/Table.tsx +13 -33
- package/src/index.ts +12 -2
- package/src/markdown-blocks.ts +296 -0
- package/src/source.ts +177 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source.js","sourceRoot":"","sources":["../src/source.ts"],"names":[],"mappings":"AAqDA,8EAA8E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C,CAAA;QACtC,IAAI,CAAC,IAAI,CAAC;YACR,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC;YAC9B,MAAM,EAAE,EAAE,OAAO,EAAE,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE;SACnD,CAAC,CAAA;IACJ,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC;AAED,4EAA4E;AAC5E,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,OAAO,EAAE,CAAC,GAAkB,EAAU,EAAE,CAAC,GAAG,CAAC,IAAI;IACjD,SAAS,EAAE,CAAC,GAAkB,EAAqB,EAAE,CAAC,GAAG,CAAC,MAAM;CACjE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tooee/renderers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Content renderers (markdown, code, text, table) for Tooee",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -30,29 +30,35 @@
|
|
|
30
30
|
"@tooee/source": "./src/index.ts",
|
|
31
31
|
"default": "./dist/index.js"
|
|
32
32
|
}
|
|
33
|
+
},
|
|
34
|
+
"./row-document": {
|
|
35
|
+
"import": {
|
|
36
|
+
"@tooee/source": "./src/row-document.ts",
|
|
37
|
+
"default": "./dist/row-document.js"
|
|
38
|
+
}
|
|
33
39
|
}
|
|
34
40
|
},
|
|
35
41
|
"scripts": {
|
|
36
42
|
"typecheck": "tsc --noEmit"
|
|
37
43
|
},
|
|
38
44
|
"dependencies": {
|
|
39
|
-
"@tooee/commands": "0.
|
|
40
|
-
"@tooee/fuzzy": "0.
|
|
41
|
-
"@tooee/marks": "0.
|
|
42
|
-
"@tooee/themes": "0.
|
|
45
|
+
"@tooee/commands": "0.2.0",
|
|
46
|
+
"@tooee/fuzzy": "0.2.0",
|
|
47
|
+
"@tooee/marks": "0.2.0",
|
|
48
|
+
"@tooee/themes": "0.2.0",
|
|
43
49
|
"beautiful-mermaid": "^1.1.3",
|
|
44
50
|
"marked": "^17.0.4"
|
|
45
51
|
},
|
|
46
52
|
"devDependencies": {
|
|
47
|
-
"@opentui/core": "^0.
|
|
48
|
-
"@opentui/react": "^0.
|
|
53
|
+
"@opentui/core": "^0.4.3",
|
|
54
|
+
"@opentui/react": "^0.4.3",
|
|
49
55
|
"@types/bun": "^1.3.10",
|
|
50
56
|
"@types/react": "^19.2.14",
|
|
51
57
|
"typescript": "^5.9.3"
|
|
52
58
|
},
|
|
53
59
|
"peerDependencies": {
|
|
54
|
-
"@opentui/core": "^0.
|
|
55
|
-
"@opentui/react": "^0.
|
|
60
|
+
"@opentui/core": "^0.4.3",
|
|
61
|
+
"@opentui/react": "^0.4.3",
|
|
56
62
|
"react": "^18.0.0 || ^19.0.0"
|
|
57
63
|
}
|
|
58
64
|
}
|
package/src/CodeView.tsx
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { type RefObject } from "react"
|
|
2
1
|
import { useTheme } from "@tooee/themes"
|
|
3
|
-
import type {
|
|
4
|
-
import
|
|
5
|
-
import { DEFAULT_SIGN_COLUMN_WIDTH, type RowDocumentRenderable } from "./RowDocumentRenderable.js"
|
|
2
|
+
import type { DocumentBindings } from "./DocumentBindings.js"
|
|
3
|
+
import { DEFAULT_SIGN_COLUMN_WIDTH } from "./RowDocumentRenderable.js"
|
|
6
4
|
import { useGutterPalette } from "./useGutterPalette.js"
|
|
7
5
|
import "./row-document.js"
|
|
8
6
|
|
|
@@ -10,56 +8,32 @@ interface CodeViewProps {
|
|
|
10
8
|
content: string
|
|
11
9
|
language?: string
|
|
12
10
|
showLineNumbers?: boolean
|
|
13
|
-
marks?: MarkState
|
|
14
|
-
docRef?: RefObject<RowDocumentRenderable | null>
|
|
15
11
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
12
|
+
* Binds the row document to a document controller: its ref, the decoration
|
|
13
|
+
* layers to paint, and the mouse handler. Rows are source lines. Omit it to
|
|
14
|
+
* render a static, non-interactive listing.
|
|
15
|
+
*
|
|
16
|
+
* The code view renders one `<code>` provider (no per-line children), so
|
|
17
|
+
* per-child mouse handlers are impossible; clicks bubble up to the
|
|
18
|
+
* row-document, where the controller resolves screen-Y to a row (gutter
|
|
19
|
+
* clicks included).
|
|
18
20
|
*/
|
|
19
|
-
|
|
20
|
-
/** Right-click on a source line — receives the line index and click coords. */
|
|
21
|
-
onRowContextMenu?: (index: number, x: number, y: number) => void
|
|
21
|
+
document?: DocumentBindings
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export function CodeView({
|
|
25
|
-
content,
|
|
26
|
-
language,
|
|
27
|
-
showLineNumbers = true,
|
|
28
|
-
marks,
|
|
29
|
-
docRef,
|
|
30
|
-
onRowClick,
|
|
31
|
-
onRowContextMenu,
|
|
32
|
-
}: CodeViewProps) {
|
|
24
|
+
export function CodeView({ content, language, showLineNumbers = true, document }: CodeViewProps) {
|
|
33
25
|
const { syntax } = useTheme()
|
|
34
26
|
const palette = useGutterPalette()
|
|
35
27
|
|
|
36
|
-
// A single handler on the row-document maps the click's screen-Y to a logical
|
|
37
|
-
// row. The code view renders one `<code>` provider (no per-line children), so
|
|
38
|
-
// per-child handlers are impossible; clicks bubble up to the row-document and
|
|
39
|
-
// are resolved via its virtual-row → row map (gutter clicks included).
|
|
40
|
-
const handleMouseDown =
|
|
41
|
-
onRowClick || onRowContextMenu
|
|
42
|
-
? (event: MouseEvent) => {
|
|
43
|
-
const row = docRef?.current?.getRowAtScreenY(event.y)
|
|
44
|
-
if (row == null) return
|
|
45
|
-
if (event.button === 0) {
|
|
46
|
-
onRowClick?.(row)
|
|
47
|
-
} else if (event.button === 2) {
|
|
48
|
-
event.preventDefault()
|
|
49
|
-
onRowContextMenu?.(row, event.x, event.y)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
: undefined
|
|
53
|
-
|
|
54
28
|
return (
|
|
55
29
|
<row-document
|
|
56
|
-
ref={
|
|
30
|
+
ref={document?.ref}
|
|
57
31
|
showLineNumbers={showLineNumbers}
|
|
58
32
|
palette={palette}
|
|
59
|
-
decorations={
|
|
33
|
+
decorations={document?.decorations}
|
|
60
34
|
signColumnWidth={DEFAULT_SIGN_COLUMN_WIDTH}
|
|
61
35
|
style={{ flexGrow: 1 }}
|
|
62
|
-
onMouseDown={
|
|
36
|
+
onMouseDown={document?.onMouseDown}
|
|
63
37
|
>
|
|
64
38
|
<code content={content} filetype={language} syntaxStyle={syntax} />
|
|
65
39
|
</row-document>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { RefObject } from "react"
|
|
2
|
+
import type { MouseEvent } from "@opentui/core"
|
|
3
|
+
import type { DecorationLayer } from "./DecorationLayer.js"
|
|
4
|
+
import type { RowDocumentRenderable } from "./RowDocumentRenderable.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The narrow projection a row renderer needs from a document controller:
|
|
8
|
+
* geometry access, the layers to paint, and the bound mouse handler. Renderers
|
|
9
|
+
* never see the controller's row type — this type lives here so a renderer can
|
|
10
|
+
* accept bindings without depending on `@tooee/shell`.
|
|
11
|
+
*/
|
|
12
|
+
export interface DocumentBindings {
|
|
13
|
+
ref: RefObject<RowDocumentRenderable | null>
|
|
14
|
+
decorations: readonly DecorationLayer[]
|
|
15
|
+
onMouseDown(event: MouseEvent): void
|
|
16
|
+
}
|
package/src/MarkdownView.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type Token, type Tokens } from "marked"
|
|
2
2
|
import { useMemo, type ReactNode, type RefObject } from "react"
|
|
3
3
|
import { useTheme, type ResolvedTheme } from "@tooee/themes"
|
|
4
4
|
import {
|
|
@@ -8,17 +8,17 @@ import {
|
|
|
8
8
|
parseColor,
|
|
9
9
|
} from "@opentui/core"
|
|
10
10
|
import type {
|
|
11
|
-
MouseEvent,
|
|
12
11
|
SyntaxStyle,
|
|
13
12
|
TextBufferRenderable,
|
|
14
13
|
TextTableContent,
|
|
15
14
|
TextTableCellContent,
|
|
16
15
|
TextChunk,
|
|
17
16
|
} from "@opentui/core"
|
|
18
|
-
import type {
|
|
19
|
-
import { DEFAULT_SIGN_COLUMN_WIDTH
|
|
17
|
+
import type { DocumentBindings } from "./DocumentBindings.js"
|
|
18
|
+
import { DEFAULT_SIGN_COLUMN_WIDTH } from "./RowDocumentRenderable.js"
|
|
20
19
|
import { useGutterPalette } from "./useGutterPalette.js"
|
|
21
20
|
import { CodeBlock, DEFAULT_CODE_BLOCK_RENDERERS, type CodeBlockRenderer } from "./code-blocks.js"
|
|
21
|
+
import { flattenMarkdown, type FlatBlock } from "./markdown-blocks.js"
|
|
22
22
|
import "./row-document.js"
|
|
23
23
|
import "./text-table.js"
|
|
24
24
|
|
|
@@ -28,9 +28,27 @@ import "./text-table.js"
|
|
|
28
28
|
|
|
29
29
|
interface MarkdownViewProps {
|
|
30
30
|
content: string
|
|
31
|
+
/**
|
|
32
|
+
* Pre-flattened blocks to render, normally the exact array a subview passed
|
|
33
|
+
* to its document controller. When supplied, `content` is *not* lexed again,
|
|
34
|
+
* so the rendered rows and the controller's navigation rows cannot drift.
|
|
35
|
+
* Omit it and `MarkdownView` flattens `content` itself for static callers.
|
|
36
|
+
*/
|
|
37
|
+
blocks?: readonly FlatBlock[]
|
|
31
38
|
showLineNumbers?: boolean
|
|
32
|
-
|
|
33
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Binds the row document to a document controller: its ref, the decoration
|
|
41
|
+
* layers to paint, and the mouse handler. Rows are flattened *block* indices —
|
|
42
|
+
* the same unit `j`/`k` move between and copy operates on. Omit it to render
|
|
43
|
+
* a static, non-interactive document.
|
|
44
|
+
*
|
|
45
|
+
* Blocks are per-child renderables, but their markup varies across renderer
|
|
46
|
+
* branches (box/text/null with differing margins), so the controller maps
|
|
47
|
+
* clicks by screen-Y rather than per-child handlers. Clicks bubble up from
|
|
48
|
+
* block children (including custom code blocks) unless a custom renderer
|
|
49
|
+
* stops propagation.
|
|
50
|
+
*/
|
|
51
|
+
document?: DocumentBindings
|
|
34
52
|
/**
|
|
35
53
|
* Registry of horizontally scrollable blocks, keyed by block index. Blocks
|
|
36
54
|
* that can overflow horizontally (mermaid diagrams and code blocks)
|
|
@@ -48,122 +66,6 @@ interface MarkdownViewProps {
|
|
|
48
66
|
* syntax-highlighted code block.
|
|
49
67
|
*/
|
|
50
68
|
codeBlockRenderers?: Record<string, CodeBlockRenderer>
|
|
51
|
-
/**
|
|
52
|
-
* Left-click on a block (additive; keyboard navigation is unaffected). The
|
|
53
|
-
* row index is the flattened *block* index, matching the view's `nav` model
|
|
54
|
-
* and the copy/selection unit — clicking selects the same block `j`/`k` move
|
|
55
|
-
* between. Clicks inside a custom-rendered code block bubble up here too,
|
|
56
|
-
* unless the custom renderer stops propagation on its own mouse handlers.
|
|
57
|
-
*/
|
|
58
|
-
onRowClick?: (index: number) => void
|
|
59
|
-
/** Right-click on a block — receives the block index and click coordinates. */
|
|
60
|
-
onRowContextMenu?: (index: number, x: number, y: number) => void
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* A flattened block — all blocks exist at the top level with an indent.
|
|
65
|
-
* Nested structures (lists containing code blocks, etc.) are flattened
|
|
66
|
-
* into sibling blocks with appropriate indentation.
|
|
67
|
-
*/
|
|
68
|
-
export interface FlatBlock {
|
|
69
|
-
token: Token
|
|
70
|
-
indent: number
|
|
71
|
-
bullet?: string // "- " or "1. " for list item lines
|
|
72
|
-
checked?: boolean // undefined = not a checkbox, true/false = checkbox state
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// ---------------------------------------------------------------------------
|
|
76
|
-
// Token flattening
|
|
77
|
-
// ---------------------------------------------------------------------------
|
|
78
|
-
|
|
79
|
-
export function flattenTokens(tokens: Token[]): FlatBlock[] {
|
|
80
|
-
const result: FlatBlock[] = []
|
|
81
|
-
flattenTokenList(tokens, 0, result)
|
|
82
|
-
return result
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function flattenTokenList(tokens: Token[], indent: number, result: FlatBlock[]): void {
|
|
86
|
-
for (const token of tokens) {
|
|
87
|
-
if (token.type === "space") continue
|
|
88
|
-
|
|
89
|
-
if (token.type === "list") {
|
|
90
|
-
const list = token as Tokens.List
|
|
91
|
-
for (let i = 0; i < list.items.length; i++) {
|
|
92
|
-
const item = list.items[i]
|
|
93
|
-
const bullet = list.ordered ? `${i + (list.start || 1)}. ` : "- "
|
|
94
|
-
flattenListItem(item, indent, bullet, result)
|
|
95
|
-
}
|
|
96
|
-
} else {
|
|
97
|
-
result.push({ token, indent })
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function flattenListItem(
|
|
103
|
-
item: Tokens.ListItem,
|
|
104
|
-
indent: number,
|
|
105
|
-
bullet: string,
|
|
106
|
-
result: FlatBlock[],
|
|
107
|
-
): void {
|
|
108
|
-
const checked = item.checked != null ? item.checked : undefined
|
|
109
|
-
const childTokens = item.tokens || []
|
|
110
|
-
let bulletUsed = false
|
|
111
|
-
|
|
112
|
-
for (const token of childTokens) {
|
|
113
|
-
if (token.type === "space" || token.type === "checkbox") continue
|
|
114
|
-
|
|
115
|
-
if (token.type === "text" || token.type === "paragraph") {
|
|
116
|
-
// Inline content — attach the bullet to the first one
|
|
117
|
-
result.push({
|
|
118
|
-
token,
|
|
119
|
-
indent,
|
|
120
|
-
bullet: bulletUsed ? undefined : bullet,
|
|
121
|
-
checked: bulletUsed ? undefined : checked,
|
|
122
|
-
})
|
|
123
|
-
bulletUsed = true
|
|
124
|
-
} else if (token.type === "list") {
|
|
125
|
-
// Emit bullet line if nothing preceded this nested list
|
|
126
|
-
if (!bulletUsed) {
|
|
127
|
-
result.push({
|
|
128
|
-
token: { type: "text", raw: "", text: "", tokens: [] } as unknown as Token,
|
|
129
|
-
indent,
|
|
130
|
-
bullet,
|
|
131
|
-
checked,
|
|
132
|
-
})
|
|
133
|
-
bulletUsed = true
|
|
134
|
-
}
|
|
135
|
-
// Nested list — increase indent to align with content after bullet
|
|
136
|
-
const list = token as Tokens.List
|
|
137
|
-
for (let i = 0; i < list.items.length; i++) {
|
|
138
|
-
const subItem = list.items[i]
|
|
139
|
-
const subBullet = list.ordered ? `${i + (list.start || 1)}. ` : "- "
|
|
140
|
-
flattenListItem(subItem, indent + bullet.length, subBullet, result)
|
|
141
|
-
}
|
|
142
|
-
} else {
|
|
143
|
-
// Block content (code, table, blockquote, hr, etc.)
|
|
144
|
-
if (!bulletUsed) {
|
|
145
|
-
result.push({
|
|
146
|
-
token: { type: "text", raw: "", text: "", tokens: [] } as unknown as Token,
|
|
147
|
-
indent,
|
|
148
|
-
bullet,
|
|
149
|
-
checked,
|
|
150
|
-
})
|
|
151
|
-
bulletUsed = true
|
|
152
|
-
}
|
|
153
|
-
// Emit the block indented to align with content after the bullet
|
|
154
|
-
result.push({ token, indent: indent + bullet.length })
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// List item had no content tokens — still emit the bullet
|
|
159
|
-
if (!bulletUsed) {
|
|
160
|
-
result.push({
|
|
161
|
-
token: { type: "text", raw: "", text: "", tokens: [] } as unknown as Token,
|
|
162
|
-
indent,
|
|
163
|
-
bullet,
|
|
164
|
-
checked,
|
|
165
|
-
})
|
|
166
|
-
}
|
|
167
69
|
}
|
|
168
70
|
|
|
169
71
|
// ---------------------------------------------------------------------------
|
|
@@ -172,18 +74,18 @@ function flattenListItem(
|
|
|
172
74
|
|
|
173
75
|
export function MarkdownView({
|
|
174
76
|
content,
|
|
77
|
+
blocks: providedBlocks,
|
|
175
78
|
showLineNumbers = true,
|
|
176
|
-
|
|
177
|
-
docRef,
|
|
79
|
+
document,
|
|
178
80
|
hScrollableBlocksRef,
|
|
179
81
|
codeBlockRenderers,
|
|
180
|
-
onRowClick,
|
|
181
|
-
onRowContextMenu,
|
|
182
82
|
}: MarkdownViewProps) {
|
|
183
83
|
const { theme, syntax } = useTheme()
|
|
184
84
|
const palette = useGutterPalette()
|
|
185
|
-
const
|
|
186
|
-
|
|
85
|
+
const blocks = useMemo(
|
|
86
|
+
() => providedBlocks ?? flattenMarkdown(content),
|
|
87
|
+
[providedBlocks, content],
|
|
88
|
+
)
|
|
187
89
|
|
|
188
90
|
// Merge user renderers over built-in defaults, normalizing keys to
|
|
189
91
|
// lowercase so registration matches fence types case-insensitively.
|
|
@@ -211,34 +113,15 @@ export function MarkdownView({
|
|
|
211
113
|
[blocks, theme, syntax, hScrollableBlocksRef, mergedCodeBlockRenderers],
|
|
212
114
|
)
|
|
213
115
|
|
|
214
|
-
// A single handler on the row-document maps the click's screen-Y to a block
|
|
215
|
-
// index. Blocks are per-child renderables, but their markup varies across
|
|
216
|
-
// renderer branches (box/text/null with differing margins), so mapping by
|
|
217
|
-
// screen-Y avoids wrapping every branch and keeps gutter/line-number geometry
|
|
218
|
-
// intact. Clicks bubble up from block children (including custom code blocks).
|
|
219
|
-
const handleMouseDown =
|
|
220
|
-
onRowClick || onRowContextMenu
|
|
221
|
-
? (event: MouseEvent) => {
|
|
222
|
-
const row = docRef?.current?.getRowAtScreenY(event.y)
|
|
223
|
-
if (row == null) return
|
|
224
|
-
if (event.button === 0) {
|
|
225
|
-
onRowClick?.(row)
|
|
226
|
-
} else if (event.button === 2) {
|
|
227
|
-
event.preventDefault()
|
|
228
|
-
onRowContextMenu?.(row, event.x, event.y)
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
: undefined
|
|
232
|
-
|
|
233
116
|
return (
|
|
234
117
|
<row-document
|
|
235
|
-
ref={
|
|
118
|
+
ref={document?.ref}
|
|
236
119
|
showLineNumbers={showLineNumbers}
|
|
237
120
|
palette={palette}
|
|
238
|
-
decorations={
|
|
121
|
+
decorations={document?.decorations}
|
|
239
122
|
signColumnWidth={DEFAULT_SIGN_COLUMN_WIDTH}
|
|
240
123
|
style={{ flexGrow: 1 }}
|
|
241
|
-
onMouseDown={
|
|
124
|
+
onMouseDown={document?.onMouseDown}
|
|
242
125
|
>
|
|
243
126
|
{blockElements}
|
|
244
127
|
</row-document>
|
|
@@ -117,6 +117,8 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
117
117
|
private _virtualRowToRow: number[] = []
|
|
118
118
|
private _virtualRowWraps: number[] = []
|
|
119
119
|
private _rowCount = 0
|
|
120
|
+
/** Virtual Y one past the last content row; the rest of the scroll area is empty. */
|
|
121
|
+
private _contentVirtualEnd = 0
|
|
120
122
|
private _lastGeometryHash = ""
|
|
121
123
|
|
|
122
124
|
constructor(ctx: RenderContext, options: RowDocumentOptions) {
|
|
@@ -218,6 +220,7 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
218
220
|
if (localY < 0 || localY >= this.viewport.height) return null
|
|
219
221
|
const virtualY = Math.floor(this.scrollTop) + localY
|
|
220
222
|
if (virtualY < 0 || virtualY >= this._virtualRowToRow.length) return null
|
|
223
|
+
if (virtualY >= this._contentVirtualEnd) return null
|
|
221
224
|
return this.getRowAtVirtualY(virtualY)
|
|
222
225
|
}
|
|
223
226
|
|
|
@@ -435,6 +438,13 @@ export class RowDocumentRenderable extends ScrollBoxRenderable {
|
|
|
435
438
|
virtualRowWraps: number[],
|
|
436
439
|
rowCount: number,
|
|
437
440
|
): void {
|
|
441
|
+
let contentVirtualEnd = 0
|
|
442
|
+
for (let row = 0; row < rowCount; row++) {
|
|
443
|
+
const end = rowVirtualStarts[row] + rowVirtualHeights[row]
|
|
444
|
+
if (end > contentVirtualEnd) contentVirtualEnd = end
|
|
445
|
+
}
|
|
446
|
+
this._contentVirtualEnd = contentVirtualEnd
|
|
447
|
+
|
|
438
448
|
// Change detection via hash
|
|
439
449
|
const hash = rowVirtualStarts.join(",")
|
|
440
450
|
if (hash !== this._lastGeometryHash) {
|
package/src/Table.tsx
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { useTerminalDimensions } from "@opentui/react"
|
|
2
2
|
import { useTheme } from "@tooee/themes"
|
|
3
|
-
import { useMemo, useRef
|
|
4
|
-
import type {
|
|
5
|
-
import type { MarkState } from "@tooee/marks"
|
|
3
|
+
import { useMemo, useRef } from "react"
|
|
4
|
+
import type { DocumentBindings } from "./DocumentBindings.js"
|
|
6
5
|
import type { ColumnDef, TableRow } from "./table-types.js"
|
|
7
6
|
import {
|
|
8
7
|
DEFAULT_SIGN_COLUMN_WIDTH,
|
|
@@ -25,14 +24,15 @@ export interface TableProps {
|
|
|
25
24
|
sampleSize?: number
|
|
26
25
|
/** Show line numbers in the gutter (default: true) */
|
|
27
26
|
showLineNumbers?: boolean
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Binds the data rows to a document controller: its ref, the decoration
|
|
29
|
+
* layers to paint, and the mouse handler. Rows are data-row indices; the
|
|
30
|
+
* fixed header sits outside the row document. Omit it to render a static,
|
|
31
|
+
* non-interactive table.
|
|
32
|
+
*/
|
|
33
|
+
document?: DocumentBindings
|
|
30
34
|
/** Column width mode: "content" sizes to content (default), "fill" expands to fill available width */
|
|
31
35
|
columnWidthMode?: "content" | "fill"
|
|
32
|
-
/** Left-click on a data row (additive; keyboard navigation is unaffected). */
|
|
33
|
-
onRowClick?: (index: number) => void
|
|
34
|
-
/** Right-click on a data row — receives the row index and click coordinates. */
|
|
35
|
-
onRowContextMenu?: (index: number, x: number, y: number) => void
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
const PADDING = 1
|
|
@@ -153,17 +153,14 @@ export function Table({
|
|
|
153
153
|
maxColumnWidth = DEFAULT_MAX_COL_WIDTH,
|
|
154
154
|
sampleSize = DEFAULT_SAMPLE_SIZE,
|
|
155
155
|
showLineNumbers = true,
|
|
156
|
-
|
|
157
|
-
docRef,
|
|
156
|
+
document,
|
|
158
157
|
columnWidthMode = "content",
|
|
159
|
-
onRowClick,
|
|
160
|
-
onRowContextMenu,
|
|
161
158
|
}: TableProps) {
|
|
162
159
|
const { theme } = useTheme()
|
|
163
160
|
const palette = useGutterPalette()
|
|
164
161
|
const { width: terminalWidth } = useTerminalDimensions()
|
|
165
162
|
const internalDocRef = useRef<RowDocumentRenderable | null>(null)
|
|
166
|
-
const rowDocumentRef =
|
|
163
|
+
const rowDocumentRef = document?.ref ?? internalDocRef
|
|
167
164
|
|
|
168
165
|
// Compute available content width: start with total space, subtract margins and gutter
|
|
169
166
|
const gutterWidth = useMemo(
|
|
@@ -245,23 +242,6 @@ export function Table({
|
|
|
245
242
|
[normalizedRows, colWidths, alignments, theme.text],
|
|
246
243
|
)
|
|
247
244
|
|
|
248
|
-
// Use the shared RowDocumentRenderable screen-Y mapper for table rows too.
|
|
249
|
-
// This keeps table, code, and markdown mouse handling on one code path and
|
|
250
|
-
// avoids per-row mouse closures in the virtualized row tree.
|
|
251
|
-
const handleMouseDown =
|
|
252
|
-
onRowClick || onRowContextMenu
|
|
253
|
-
? (event: MouseEvent) => {
|
|
254
|
-
const row = rowDocumentRef.current?.getRowAtScreenY(event.y)
|
|
255
|
-
if (row == null) return
|
|
256
|
-
if (event.button === 0) {
|
|
257
|
-
onRowClick?.(row)
|
|
258
|
-
} else if (event.button === 2) {
|
|
259
|
-
event.preventDefault()
|
|
260
|
-
onRowContextMenu?.(row, event.x, event.y)
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
: undefined
|
|
264
|
-
|
|
265
245
|
return (
|
|
266
246
|
<box
|
|
267
247
|
style={{
|
|
@@ -305,9 +285,9 @@ export function Table({
|
|
|
305
285
|
showLineNumbers={showLineNumbers}
|
|
306
286
|
signColumnWidth={DEFAULT_SIGN_COLUMN_WIDTH}
|
|
307
287
|
palette={palette}
|
|
308
|
-
decorations={
|
|
288
|
+
decorations={document?.decorations}
|
|
309
289
|
style={{ flexGrow: 1 }}
|
|
310
|
-
onMouseDown={
|
|
290
|
+
onMouseDown={document?.onMouseDown}
|
|
311
291
|
>
|
|
312
292
|
{rowElements}
|
|
313
293
|
</row-document>
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
export { MarkdownView
|
|
2
|
-
export
|
|
1
|
+
export { MarkdownView } from "./MarkdownView.js"
|
|
2
|
+
export { flattenMarkdown, flattenTokens, getFlatBlockText } from "./markdown-blocks.js"
|
|
3
|
+
export type { FlatBlock, FlattenMarkdownOptions } from "./markdown-blocks.js"
|
|
4
|
+
export { sourceLines, sourceLineAdapter } from "./source.js"
|
|
5
|
+
export type {
|
|
6
|
+
DocumentRowAnchor,
|
|
7
|
+
DocumentRowSource,
|
|
8
|
+
SourceLineRow,
|
|
9
|
+
SourcePoint,
|
|
10
|
+
SourceSpan,
|
|
11
|
+
} from "./source.js"
|
|
3
12
|
export {
|
|
4
13
|
CodeBlockChrome,
|
|
5
14
|
defaultCodeBlockRenderer,
|
|
@@ -25,4 +34,5 @@ export {
|
|
|
25
34
|
} from "./RowDocumentRenderable.js"
|
|
26
35
|
export type { RowDocumentOptions, RowDocumentPalette } from "./RowDocumentRenderable.js"
|
|
27
36
|
export type { DecorationLayer, RowDecoration } from "./DecorationLayer.js"
|
|
37
|
+
export type { DocumentBindings } from "./DocumentBindings.js"
|
|
28
38
|
export { useGutterPalette } from "./useGutterPalette.js"
|