@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,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, type MarkdownLinkHandler } 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"
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 "./CodeView.js"
21
- export { Table, computeColumnWidths, isNumeric, sampleRows } from "./Table.js"
22
- export type { TableProps, ColumnWidthOptions } from "./Table.js"
23
- export type { ColumnDef, TableRow } from "./table-types.js"
24
- export { CommandPalette } from "./CommandPalette.js"
25
- export type { CommandPaletteEntry } from "./CommandPalette.js"
26
- export { ContextMenu } from "./ContextMenu.js"
27
- export type { ContextMenuEntry } from "./ContextMenu.js"
28
- export { parseCSV, parseTSV, parseJSON, parseAuto, detectFormat } from "./parsers.js"
29
- export type { Format, ParsedTable } from "./parsers.js"
30
- export { RowDocumentRenderable } from "./RowDocumentRenderable.js"
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 "./RowDocumentRenderable.js"
35
- export type { RowDocumentOptions, RowDocumentPalette } from "./RowDocumentRenderable.js"
36
- export type { DecorationLayer, RowDecoration } from "./DecorationLayer.js"
37
- export type { DocumentBindings } from "./DocumentBindings.js"
38
- export { useGutterPalette } from "./useGutterPalette.js"
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";