@tooee/renderers 0.2.4 → 0.3.1
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/code-blocks.d.ts +6 -6
- package/dist/code-blocks.d.ts.map +1 -1
- package/dist/code-blocks.js +52 -42
- package/dist/code-blocks.js.map +1 -1
- package/dist/{CodeView.d.ts → code-view.d.ts} +3 -3
- package/dist/code-view.d.ts.map +1 -0
- package/dist/{CodeView.js → code-view.js} +5 -5
- package/dist/code-view.js.map +1 -0
- package/dist/{CommandPalette.d.ts → command-palette.d.ts} +2 -2
- package/dist/command-palette.d.ts.map +1 -0
- package/dist/{CommandPalette.js → command-palette.js} +27 -24
- package/dist/command-palette.js.map +1 -0
- package/dist/{ContextMenu.d.ts → context-menu.d.ts} +2 -2
- package/dist/context-menu.d.ts.map +1 -0
- package/dist/{ContextMenu.js → context-menu.js} +24 -14
- package/dist/context-menu.js.map +1 -0
- package/dist/{DecorationLayer.d.ts → decoration-layer.d.ts} +2 -2
- package/dist/decoration-layer.d.ts.map +1 -0
- package/dist/decoration-layer.js +2 -0
- package/dist/decoration-layer.js.map +1 -0
- package/dist/{DocumentBindings.d.ts → document-bindings.d.ts} +4 -4
- package/dist/document-bindings.d.ts.map +1 -0
- package/dist/document-bindings.js +2 -0
- package/dist/document-bindings.js.map +1 -0
- package/dist/index.d.ts +16 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -9
- package/dist/index.js.map +1 -1
- package/dist/markdown-blocks.d.ts +9 -13
- package/dist/markdown-blocks.d.ts.map +1 -1
- package/dist/markdown-blocks.js +98 -79
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/{MarkdownView.d.ts → markdown-view.d.ts} +13 -13
- package/dist/markdown-view.d.ts.map +1 -0
- package/dist/markdown-view.js +455 -0
- package/dist/markdown-view.js.map +1 -0
- package/dist/mermaid.d.ts +6 -6
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +56 -26
- package/dist/mermaid.js.map +1 -1
- package/dist/parsers.d.ts +5 -5
- package/dist/parsers.d.ts.map +1 -1
- package/dist/parsers.js +64 -44
- package/dist/parsers.js.map +1 -1
- package/dist/{RowDocumentRenderable.d.ts → row-document-renderable.d.ts} +15 -11
- package/dist/row-document-renderable.d.ts.map +1 -0
- package/dist/{RowDocumentRenderable.js → row-document-renderable.js} +110 -62
- package/dist/row-document-renderable.js.map +1 -0
- package/dist/row-document.d.ts +1 -1
- package/dist/row-document.d.ts.map +1 -1
- package/dist/row-document.js +1 -1
- package/dist/row-document.js.map +1 -1
- package/dist/source.d.ts +6 -6
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +35 -26
- package/dist/source.js.map +1 -1
- package/dist/table-types.d.ts.map +1 -1
- package/dist/{Table.d.ts → table.d.ts} +6 -6
- package/dist/table.d.ts.map +1 -0
- package/dist/{Table.js → table.js} +65 -35
- package/dist/table.js.map +1 -0
- package/dist/text-table.d.ts.map +1 -1
- package/dist/text-table.js.map +1 -1
- package/dist/use-gutter-palette.d.ts +3 -0
- package/dist/use-gutter-palette.d.ts.map +1 -0
- package/dist/{useGutterPalette.js → use-gutter-palette.js} +4 -4
- package/dist/use-gutter-palette.js.map +1 -0
- package/package.json +5 -5
- package/src/code-blocks.tsx +122 -106
- package/src/{CodeView.tsx → code-view.tsx} +19 -14
- package/src/command-palette.tsx +163 -0
- package/src/context-menu.tsx +157 -0
- package/src/decoration-layer.ts +11 -0
- package/src/document-bindings.ts +16 -0
- package/src/index.ts +24 -23
- package/src/markdown-blocks.ts +178 -138
- package/src/markdown-view.tsx +853 -0
- package/src/mermaid.ts +122 -70
- package/src/parsers.ts +141 -103
- package/src/row-document-renderable.ts +672 -0
- package/src/row-document.ts +4 -4
- package/src/source.ts +92 -72
- package/src/table-types.ts +4 -4
- package/src/table.tsx +346 -0
- package/src/text-table.ts +4 -4
- package/src/use-gutter-palette.ts +15 -0
- package/dist/CodeView.d.ts.map +0 -1
- package/dist/CodeView.js.map +0 -1
- package/dist/CommandPalette.d.ts.map +0 -1
- package/dist/CommandPalette.js.map +0 -1
- package/dist/ContextMenu.d.ts.map +0 -1
- package/dist/ContextMenu.js.map +0 -1
- package/dist/DecorationLayer.d.ts.map +0 -1
- package/dist/DecorationLayer.js +0 -2
- package/dist/DecorationLayer.js.map +0 -1
- package/dist/DocumentBindings.d.ts.map +0 -1
- package/dist/DocumentBindings.js +0 -2
- package/dist/DocumentBindings.js.map +0 -1
- package/dist/MarkdownView.d.ts.map +0 -1
- package/dist/MarkdownView.js +0 -360
- package/dist/MarkdownView.js.map +0 -1
- package/dist/RowDocumentRenderable.d.ts.map +0 -1
- package/dist/RowDocumentRenderable.js.map +0 -1
- package/dist/Table.d.ts.map +0 -1
- package/dist/Table.js.map +0 -1
- package/dist/useGutterPalette.d.ts +0 -3
- package/dist/useGutterPalette.d.ts.map +0 -1
- package/dist/useGutterPalette.js.map +0 -1
- package/src/CommandPalette.tsx +0 -147
- package/src/ContextMenu.tsx +0 -132
- package/src/DecorationLayer.ts +0 -11
- package/src/DocumentBindings.ts +0 -16
- package/src/MarkdownView.tsx +0 -738
- package/src/RowDocumentRenderable.ts +0 -593
- package/src/Table.tsx +0 -300
- package/src/useGutterPalette.ts +0 -15
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { useState, useCallback } from "react";
|
|
2
|
+
import { useKeyboard, useTerminalDimensions } from "@opentui/react";
|
|
3
|
+
import { useTheme } from "@tooee/themes";
|
|
4
|
+
|
|
5
|
+
export interface ContextMenuEntry {
|
|
6
|
+
id: string;
|
|
7
|
+
title: string;
|
|
8
|
+
hotkey?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ContextMenuProps {
|
|
12
|
+
entries: ContextMenuEntry[];
|
|
13
|
+
/** Anchor coordinates (screen space) — usually the click position. */
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
onSelect: (id: string) => void;
|
|
17
|
+
onClose: () => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const MIN_WIDTH = 16;
|
|
21
|
+
const HORIZONTAL_PADDING = 2;
|
|
22
|
+
const BORDER = 2;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A positioned, keyboard-navigable mini action menu shown on right-click.
|
|
26
|
+
*
|
|
27
|
+
* Renders a full-screen transparent backdrop (click-outside dismisses) and a
|
|
28
|
+
* bordered panel anchored near the cursor, clamped to stay on screen. Mouse is
|
|
29
|
+
* additive: j/k + arrows move, Enter invokes, Escape closes (handled by the
|
|
30
|
+
* overlay layer), click selects.
|
|
31
|
+
*/
|
|
32
|
+
export const ContextMenu = function ContextMenu({
|
|
33
|
+
entries,
|
|
34
|
+
x,
|
|
35
|
+
y,
|
|
36
|
+
onSelect,
|
|
37
|
+
onClose,
|
|
38
|
+
}: ContextMenuProps): React.ReactNode {
|
|
39
|
+
const { theme } = useTheme();
|
|
40
|
+
const { width: termWidth, height: termHeight } = useTerminalDimensions();
|
|
41
|
+
const [activeIndex, setActiveIndex] = useState(0);
|
|
42
|
+
|
|
43
|
+
const select = useCallback(
|
|
44
|
+
(index: number) => {
|
|
45
|
+
const entry = entries[index];
|
|
46
|
+
if (entry !== undefined) {
|
|
47
|
+
onSelect(entry.id);
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
[entries, onSelect],
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
useKeyboard((key) => {
|
|
54
|
+
if (key.name === "up" || key.raw === "k") {
|
|
55
|
+
key.preventDefault();
|
|
56
|
+
setActiveIndex((i) => Math.max(0, i - 1));
|
|
57
|
+
} else if (key.name === "down" || key.raw === "j") {
|
|
58
|
+
key.preventDefault();
|
|
59
|
+
setActiveIndex((i) => Math.min(entries.length - 1, i + 1));
|
|
60
|
+
} else if (key.name === "return") {
|
|
61
|
+
key.preventDefault();
|
|
62
|
+
select(activeIndex);
|
|
63
|
+
}
|
|
64
|
+
// Escape is handled by the overlay layer (dismissOnEscape).
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Size the panel from its contents (terminal cell width, not code units,
|
|
68
|
+
// so wide glyphs like CJK and emoji are counted correctly).
|
|
69
|
+
let longest = 0;
|
|
70
|
+
const entryCount = entries.length;
|
|
71
|
+
for (let index = 0; index < entryCount; index += 1) {
|
|
72
|
+
if (index in entries) {
|
|
73
|
+
const entry = entries[index];
|
|
74
|
+
const width =
|
|
75
|
+
Bun.stringWidth(entry.title) +
|
|
76
|
+
(entry.hotkey !== undefined && entry.hotkey !== "" ? Bun.stringWidth(entry.hotkey) + 2 : 0);
|
|
77
|
+
longest = Math.max(longest, width);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const innerWidth = Math.max(MIN_WIDTH, longest);
|
|
81
|
+
const panelWidth = innerWidth + HORIZONTAL_PADDING + BORDER;
|
|
82
|
+
const panelHeight = Math.max(1, entries.length) + BORDER;
|
|
83
|
+
|
|
84
|
+
// Clamp on-screen: flip left/up when the anchor is near the right/bottom edge.
|
|
85
|
+
let left = x;
|
|
86
|
+
if (left + panelWidth > termWidth) {
|
|
87
|
+
left = Math.max(0, termWidth - panelWidth);
|
|
88
|
+
}
|
|
89
|
+
let top = y;
|
|
90
|
+
if (top + panelHeight > termHeight) {
|
|
91
|
+
top = Math.max(0, termHeight - panelHeight);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<box position="absolute" left={0} top={0} width={termWidth} height={termHeight}>
|
|
96
|
+
{/* Backdrop: click outside the menu dismisses it. */}
|
|
97
|
+
<box
|
|
98
|
+
position="absolute"
|
|
99
|
+
left={0}
|
|
100
|
+
top={0}
|
|
101
|
+
width={termWidth}
|
|
102
|
+
height={termHeight}
|
|
103
|
+
backgroundColor="transparent"
|
|
104
|
+
onMouseDown={(event) => {
|
|
105
|
+
event.preventDefault();
|
|
106
|
+
onClose();
|
|
107
|
+
}}
|
|
108
|
+
/>
|
|
109
|
+
<box
|
|
110
|
+
position="absolute"
|
|
111
|
+
left={left}
|
|
112
|
+
top={top}
|
|
113
|
+
width={panelWidth}
|
|
114
|
+
flexDirection="column"
|
|
115
|
+
backgroundColor={theme.backgroundPanel}
|
|
116
|
+
border
|
|
117
|
+
borderColor={theme.borderActive}
|
|
118
|
+
>
|
|
119
|
+
{entries.length === 0 ? (
|
|
120
|
+
<box height={1} paddingLeft={1} paddingRight={1}>
|
|
121
|
+
<text content="No actions" fg={theme.textMuted} />
|
|
122
|
+
</box>
|
|
123
|
+
) : (
|
|
124
|
+
entries.map(
|
|
125
|
+
(entry, i): React.ReactNode => (
|
|
126
|
+
<box
|
|
127
|
+
key={entry.id}
|
|
128
|
+
flexDirection="row"
|
|
129
|
+
height={1}
|
|
130
|
+
paddingLeft={1}
|
|
131
|
+
paddingRight={1}
|
|
132
|
+
backgroundColor={i === activeIndex ? theme.backgroundElement : undefined}
|
|
133
|
+
onMouseDown={(event) => {
|
|
134
|
+
if (event.button !== 0) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
event.preventDefault();
|
|
138
|
+
event.stopPropagation();
|
|
139
|
+
onSelect(entry.id);
|
|
140
|
+
}}
|
|
141
|
+
>
|
|
142
|
+
<text
|
|
143
|
+
content={entry.title}
|
|
144
|
+
fg={i === activeIndex ? theme.primary : theme.text}
|
|
145
|
+
style={{ flexGrow: 1 }}
|
|
146
|
+
/>
|
|
147
|
+
{(entry.hotkey?.length ?? 0) > 0 && (
|
|
148
|
+
<text content={` ${entry.hotkey}`} fg={theme.textMuted} />
|
|
149
|
+
)}
|
|
150
|
+
</box>
|
|
151
|
+
),
|
|
152
|
+
)
|
|
153
|
+
)}
|
|
154
|
+
</box>
|
|
155
|
+
</box>
|
|
156
|
+
);
|
|
157
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface RowDecoration {
|
|
2
|
+
row: number;
|
|
3
|
+
background?: string;
|
|
4
|
+
gutterBackground?: string;
|
|
5
|
+
sign?: { text: string; fg?: string };
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface DecorationLayer {
|
|
9
|
+
readonly priority: number;
|
|
10
|
+
forVisibleRows: (from: number, to: number) => Iterable<RowDecoration>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { RefObject } from "react";
|
|
2
|
+
import type { MouseEvent } from "@opentui/core";
|
|
3
|
+
import type { DecorationLayer } from "./decoration-layer.js";
|
|
4
|
+
import type { RowDocumentRenderable } from "./row-document-renderable.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/index.ts
CHANGED
|
@@ -1,38 +1,39 @@
|
|
|
1
|
-
export { MarkdownView
|
|
2
|
-
export {
|
|
3
|
-
export
|
|
4
|
-
export {
|
|
1
|
+
export { MarkdownView } from "./markdown-view.js";
|
|
2
|
+
export type { MarkdownLinkHandler } from "./markdown-view.js";
|
|
3
|
+
export { flattenMarkdown, getFlatBlockText } from "./markdown-blocks.js";
|
|
4
|
+
export type { FlatBlock, FlattenMarkdownOptions } from "./markdown-blocks.js";
|
|
5
|
+
export { sourceLines, sourceLineAdapter } from "./source.js";
|
|
5
6
|
export type {
|
|
6
7
|
DocumentRowAnchor,
|
|
7
8
|
DocumentRowSource,
|
|
8
9
|
SourceLineRow,
|
|
9
10
|
SourcePoint,
|
|
10
11
|
SourceSpan,
|
|
11
|
-
} from "./source.js"
|
|
12
|
+
} from "./source.js";
|
|
12
13
|
export {
|
|
13
14
|
CodeBlockChrome,
|
|
14
15
|
defaultCodeBlockRenderer,
|
|
15
16
|
mermaidCodeBlockRenderer,
|
|
16
17
|
DEFAULT_CODE_BLOCK_RENDERERS,
|
|
17
18
|
getFenceType,
|
|
18
|
-
} from "./code-blocks.js"
|
|
19
|
-
export type { CodeBlockRenderer, CodeBlockRendererProps, CodeBlockHScroll } from "./code-blocks.js"
|
|
20
|
-
export { CodeView } from "./
|
|
21
|
-
export { Table, computeColumnWidths, isNumeric, sampleRows } from "./
|
|
22
|
-
export type { TableProps, ColumnWidthOptions } from "./
|
|
23
|
-
export type { ColumnDef, TableRow } from "./table-types.js"
|
|
24
|
-
export { CommandPalette } from "./
|
|
25
|
-
export type { CommandPaletteEntry } from "./
|
|
26
|
-
export { ContextMenu } from "./
|
|
27
|
-
export type { ContextMenuEntry } from "./
|
|
28
|
-
export { parseCSV, parseTSV, parseJSON, parseAuto, detectFormat } from "./parsers.js"
|
|
29
|
-
export type { Format, ParsedTable } from "./parsers.js"
|
|
30
|
-
export { RowDocumentRenderable } from "./
|
|
19
|
+
} from "./code-blocks.js";
|
|
20
|
+
export type { CodeBlockRenderer, CodeBlockRendererProps, CodeBlockHScroll } from "./code-blocks.js";
|
|
21
|
+
export { CodeView } from "./code-view.js";
|
|
22
|
+
export { Table, computeColumnWidths, isNumeric, sampleRows } from "./table.js";
|
|
23
|
+
export type { TableProps, ColumnWidthOptions } from "./table.js";
|
|
24
|
+
export type { ColumnDef, TableRow } from "./table-types.js";
|
|
25
|
+
export { CommandPalette } from "./command-palette.js";
|
|
26
|
+
export type { CommandPaletteEntry } from "./command-palette.js";
|
|
27
|
+
export { ContextMenu } from "./context-menu.js";
|
|
28
|
+
export type { ContextMenuEntry } from "./context-menu.js";
|
|
29
|
+
export { parseCSV, parseTSV, parseJSON, parseAuto, detectFormat } from "./parsers.js";
|
|
30
|
+
export type { Format, ParsedTable } from "./parsers.js";
|
|
31
|
+
export { RowDocumentRenderable } from "./row-document-renderable.js";
|
|
31
32
|
export {
|
|
32
33
|
DEFAULT_SIGN_COLUMN_WIDTH,
|
|
33
34
|
computeRowDocumentGutterWidth,
|
|
34
|
-
} from "./
|
|
35
|
-
export type { RowDocumentOptions, RowDocumentPalette } from "./
|
|
36
|
-
export type { DecorationLayer, RowDecoration } from "./
|
|
37
|
-
export type { DocumentBindings } from "./
|
|
38
|
-
export { useGutterPalette } from "./
|
|
35
|
+
} from "./row-document-renderable.js";
|
|
36
|
+
export type { RowDocumentOptions, RowDocumentPalette } from "./row-document-renderable.js";
|
|
37
|
+
export type { DecorationLayer, RowDecoration } from "./decoration-layer.js";
|
|
38
|
+
export type { DocumentBindings } from "./document-bindings.js";
|
|
39
|
+
export { useGutterPalette } from "./use-gutter-palette.js";
|