@tooee/renderers 0.7.3 → 0.9.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 (171) hide show
  1. package/dist/chooser/choose-filter.d.ts +13 -0
  2. package/dist/chooser/choose-filter.d.ts.map +1 -0
  3. package/dist/chooser/choose-filter.js +22 -0
  4. package/dist/chooser/choose-filter.js.map +1 -0
  5. package/dist/chooser/choose-highlighted-text.d.ts +9 -0
  6. package/dist/chooser/choose-highlighted-text.d.ts.map +1 -0
  7. package/dist/chooser/choose-highlighted-text.js +32 -0
  8. package/dist/chooser/choose-highlighted-text.js.map +1 -0
  9. package/dist/chooser/choose-list.d.ts +29 -0
  10. package/dist/chooser/choose-list.d.ts.map +1 -0
  11. package/dist/chooser/choose-list.js +66 -0
  12. package/dist/chooser/choose-list.js.map +1 -0
  13. package/dist/chooser/choose-store.d.ts +58 -0
  14. package/dist/chooser/choose-store.d.ts.map +1 -0
  15. package/dist/chooser/choose-store.js +102 -0
  16. package/dist/chooser/choose-store.js.map +1 -0
  17. package/dist/chooser/chooser.d.ts +18 -0
  18. package/dist/chooser/chooser.d.ts.map +1 -0
  19. package/dist/chooser/chooser.js +25 -0
  20. package/dist/chooser/chooser.js.map +1 -0
  21. package/dist/chooser/fuzzy.d.ts +9 -0
  22. package/dist/chooser/fuzzy.d.ts.map +1 -0
  23. package/dist/chooser/fuzzy.js +5 -0
  24. package/dist/chooser/fuzzy.js.map +1 -0
  25. package/dist/chooser/source.d.ts +5 -0
  26. package/dist/chooser/source.d.ts.map +1 -0
  27. package/dist/chooser/source.js +26 -0
  28. package/dist/chooser/source.js.map +1 -0
  29. package/dist/chooser/types.d.ts +22 -0
  30. package/dist/chooser/types.d.ts.map +1 -0
  31. package/dist/chooser/types.js +2 -0
  32. package/dist/chooser/types.js.map +1 -0
  33. package/dist/chooser/use-choose.d.ts +84 -0
  34. package/dist/chooser/use-choose.d.ts.map +1 -0
  35. package/dist/chooser/use-choose.js +382 -0
  36. package/dist/chooser/use-choose.js.map +1 -0
  37. package/dist/code-blocks.d.ts +2 -1
  38. package/dist/code-blocks.d.ts.map +1 -1
  39. package/dist/code-blocks.js +20 -18
  40. package/dist/code-blocks.js.map +1 -1
  41. package/dist/code-view.d.ts +2 -1
  42. package/dist/code-view.d.ts.map +1 -1
  43. package/dist/code-view.js.map +1 -1
  44. package/dist/command-palette.d.ts +2 -1
  45. package/dist/command-palette.d.ts.map +1 -1
  46. package/dist/command-palette.js +15 -76
  47. package/dist/command-palette.js.map +1 -1
  48. package/dist/context-menu.d.ts +2 -1
  49. package/dist/context-menu.d.ts.map +1 -1
  50. package/dist/context-menu.js +50 -15
  51. package/dist/context-menu.js.map +1 -1
  52. package/dist/index.d.ts +19 -3
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +9 -1
  55. package/dist/index.js.map +1 -1
  56. package/dist/markdown/blocks/blockquote.d.ts +11 -0
  57. package/dist/markdown/blocks/blockquote.d.ts.map +1 -0
  58. package/dist/markdown/blocks/blockquote.js +26 -0
  59. package/dist/markdown/blocks/blockquote.js.map +1 -0
  60. package/dist/markdown/blocks/flat-block.d.ts +20 -0
  61. package/dist/markdown/blocks/flat-block.d.ts.map +1 -0
  62. package/dist/markdown/blocks/flat-block.js +52 -0
  63. package/dist/markdown/blocks/flat-block.js.map +1 -0
  64. package/dist/markdown/blocks/heading.d.ts +11 -0
  65. package/dist/markdown/blocks/heading.d.ts.map +1 -0
  66. package/dist/markdown/blocks/heading.js +23 -0
  67. package/dist/markdown/blocks/heading.js.map +1 -0
  68. package/dist/markdown/blocks/horizontal-rule.d.ts +7 -0
  69. package/dist/markdown/blocks/horizontal-rule.d.ts.map +1 -0
  70. package/dist/markdown/blocks/horizontal-rule.js +5 -0
  71. package/dist/markdown/blocks/horizontal-rule.js.map +1 -0
  72. package/dist/markdown/blocks/list-line.d.ts +10 -0
  73. package/dist/markdown/blocks/list-line.d.ts.map +1 -0
  74. package/dist/markdown/blocks/list-line.js +21 -0
  75. package/dist/markdown/blocks/list-line.js.map +1 -0
  76. package/dist/markdown/blocks/paragraph.d.ts +12 -0
  77. package/dist/markdown/blocks/paragraph.d.ts.map +1 -0
  78. package/dist/markdown/blocks/paragraph.js +27 -0
  79. package/dist/markdown/blocks/paragraph.js.map +1 -0
  80. package/dist/markdown/blocks/table.d.ts +7 -0
  81. package/dist/markdown/blocks/table.d.ts.map +1 -0
  82. package/dist/markdown/blocks/table.js +30 -0
  83. package/dist/markdown/blocks/table.js.map +1 -0
  84. package/dist/markdown/chunks.d.ts +5 -0
  85. package/dist/markdown/chunks.d.ts.map +1 -0
  86. package/dist/markdown/chunks.js +29 -0
  87. package/dist/markdown/chunks.js.map +1 -0
  88. package/dist/markdown/inline.d.ts +8 -0
  89. package/dist/markdown/inline.d.ts.map +1 -0
  90. package/dist/markdown/inline.js +27 -0
  91. package/dist/markdown/inline.js.map +1 -0
  92. package/dist/markdown/links.d.ts +11 -0
  93. package/dist/markdown/links.d.ts.map +1 -0
  94. package/dist/markdown/links.js +107 -0
  95. package/dist/markdown/links.js.map +1 -0
  96. package/dist/markdown/plain-text.d.ts +4 -0
  97. package/dist/markdown/plain-text.d.ts.map +1 -0
  98. package/dist/markdown/plain-text.js +21 -0
  99. package/dist/markdown/plain-text.js.map +1 -0
  100. package/dist/markdown/tokens.d.ts +32 -0
  101. package/dist/markdown/tokens.d.ts.map +1 -0
  102. package/dist/markdown/tokens.js +25 -0
  103. package/dist/markdown/tokens.js.map +1 -0
  104. package/dist/markdown/visit-inline.d.ts +17 -0
  105. package/dist/markdown/visit-inline.d.ts.map +1 -0
  106. package/dist/markdown/visit-inline.js +61 -0
  107. package/dist/markdown/visit-inline.js.map +1 -0
  108. package/dist/markdown-blocks.d.ts.map +1 -1
  109. package/dist/markdown-blocks.js +6 -4
  110. package/dist/markdown-blocks.js.map +1 -1
  111. package/dist/markdown-images.d.ts.map +1 -1
  112. package/dist/markdown-images.js +14 -5
  113. package/dist/markdown-images.js.map +1 -1
  114. package/dist/markdown-view.d.ts +10 -32
  115. package/dist/markdown-view.d.ts.map +1 -1
  116. package/dist/markdown-view.js +9 -468
  117. package/dist/markdown-view.js.map +1 -1
  118. package/dist/mermaid.d.ts +5 -4
  119. package/dist/mermaid.d.ts.map +1 -1
  120. package/dist/mermaid.js +5 -5
  121. package/dist/mermaid.js.map +1 -1
  122. package/dist/parsers.d.ts +6 -13
  123. package/dist/parsers.d.ts.map +1 -1
  124. package/dist/parsers.js +66 -46
  125. package/dist/parsers.js.map +1 -1
  126. package/dist/row-document-renderable.d.ts +7 -6
  127. package/dist/row-document-renderable.d.ts.map +1 -1
  128. package/dist/row-document-renderable.js +2 -7
  129. package/dist/row-document-renderable.js.map +1 -1
  130. package/dist/table-types.d.ts +4 -0
  131. package/dist/table-types.d.ts.map +1 -1
  132. package/dist/table.d.ts +4 -1
  133. package/dist/table.d.ts.map +1 -1
  134. package/dist/table.js +30 -23
  135. package/dist/table.js.map +1 -1
  136. package/package.json +9 -5
  137. package/src/chooser/choose-filter.tsx +66 -0
  138. package/src/chooser/choose-highlighted-text.tsx +49 -0
  139. package/src/chooser/choose-list.tsx +159 -0
  140. package/src/chooser/choose-store.ts +147 -0
  141. package/src/chooser/chooser.tsx +80 -0
  142. package/src/chooser/fuzzy.ts +13 -0
  143. package/src/chooser/source.ts +36 -0
  144. package/src/chooser/types.ts +28 -0
  145. package/src/chooser/use-choose.ts +540 -0
  146. package/src/code-blocks.tsx +28 -26
  147. package/src/code-view.tsx +2 -1
  148. package/src/command-palette.tsx +49 -137
  149. package/src/context-menu.tsx +54 -15
  150. package/src/index.ts +46 -3
  151. package/src/markdown/blocks/blockquote.tsx +53 -0
  152. package/src/markdown/blocks/flat-block.tsx +119 -0
  153. package/src/markdown/blocks/heading.tsx +53 -0
  154. package/src/markdown/blocks/horizontal-rule.tsx +16 -0
  155. package/src/markdown/blocks/list-line.tsx +52 -0
  156. package/src/markdown/blocks/paragraph.tsx +90 -0
  157. package/src/markdown/blocks/table.tsx +55 -0
  158. package/src/markdown/chunks.ts +42 -0
  159. package/src/markdown/inline.tsx +45 -0
  160. package/src/markdown/links.ts +133 -0
  161. package/src/markdown/plain-text.ts +22 -0
  162. package/src/markdown/tokens.ts +53 -0
  163. package/src/markdown/visit-inline.ts +82 -0
  164. package/src/markdown-blocks.ts +6 -4
  165. package/src/markdown-images.ts +15 -6
  166. package/src/markdown-view.tsx +20 -845
  167. package/src/mermaid.ts +11 -9
  168. package/src/parsers.ts +86 -62
  169. package/src/row-document-renderable.ts +13 -15
  170. package/src/table-types.ts +5 -0
  171. package/src/table.tsx +36 -30
@@ -1,7 +1,9 @@
1
- import { useState, useMemo, useCallback } from "react";
2
- import { useCommand } from "@tooee/commands";
3
- import { useTheme, CloseButton } from "@tooee/themes";
4
- import { fuzzyMatch } from "@tooee/fuzzy";
1
+ import { useMemo } from "react";
2
+ import type { ReactNode } from "react";
3
+ import { useTheme } from "@tooee/themes";
4
+ import { Chooser } from "./chooser/chooser.js";
5
+ import { ChooseHighlightedText } from "./chooser/choose-highlighted-text.js";
6
+ import type { ChooseItem } from "./chooser/types.js";
5
7
 
6
8
  export interface CommandPaletteEntry {
7
9
  id: string;
@@ -21,143 +23,53 @@ export const CommandPalette = function CommandPalette({
21
23
  commands,
22
24
  onSelect,
23
25
  onClose,
24
- }: CommandPaletteProps): React.ReactNode {
26
+ }: CommandPaletteProps): ReactNode {
25
27
  const { theme } = useTheme();
26
- const [filter, setFilter] = useState("");
27
- const [activeIndex, setActiveIndex] = useState(0);
28
-
29
- const filtered = useMemo(() => {
30
- if (!filter) {
31
- return commands;
32
- }
33
- const results: { entry: CommandPaletteEntry; score: number }[] = [];
34
- for (const entry of commands) {
35
- const score = fuzzyMatch(filter, entry.title);
36
- if (score !== null) {
37
- results.push({ entry, score });
38
- }
39
- }
40
- results.sort((a, b) => b.score - a.score);
41
- return results.map((r) => r.entry);
42
- }, [commands, filter]);
43
-
44
- const handleSelect = useCallback(() => {
45
- const item = filtered[activeIndex];
46
- if (item !== undefined) {
47
- onSelect(item.id);
28
+ const entriesById = useMemo(
29
+ () => new Map(commands.map((entry) => [entry.id, entry])),
30
+ [commands],
31
+ );
32
+ const items = useMemo<ChooseItem[]>(
33
+ () => commands.map((entry) => ({ text: entry.title, value: entry.id })),
34
+ [commands],
35
+ );
36
+ const handleSelect = (item: ChooseItem): void => {
37
+ if (item.value !== undefined) {
38
+ onSelect(item.value);
48
39
  }
49
- }, [filtered, activeIndex, onSelect]);
50
-
51
- const moveUp = useCallback(() => {
52
- setActiveIndex((i) => Math.max(0, i - 1));
53
- }, []);
54
-
55
- const moveDown = useCallback(() => {
56
- setActiveIndex((i) => Math.min(filtered.length - 1, i + 1));
57
- }, [filtered.length]);
58
-
59
- useCommand({
60
- handler: onClose,
61
- hidden: true,
62
- hotkey: "Escape",
63
- id: "command-palette:close",
64
- modes: ["insert", "cursor"],
65
- title: "Close command palette",
66
- });
67
- useCommand({
68
- handler: handleSelect,
69
- hidden: true,
70
- hotkey: "Enter",
71
- id: "command-palette:select",
72
- modes: ["insert", "cursor"],
73
- title: "Run selected command",
74
- });
75
- useCommand({
76
- handler: moveUp,
77
- hidden: true,
78
- hotkey: "up",
79
- id: "command-palette:move-up",
80
- modes: ["insert", "cursor"],
81
- title: "Move up",
82
- });
83
- useCommand({
84
- handler: moveDown,
85
- hidden: true,
86
- hotkey: "down",
87
- id: "command-palette:move-down",
88
- modes: ["insert", "cursor"],
89
- title: "Move down",
90
- });
40
+ };
91
41
 
92
42
  return (
93
- <box
94
- position="absolute"
95
- left="20%"
96
- right="20%"
97
- top={2}
98
- maxHeight="60%"
99
- flexDirection="column"
100
- backgroundColor={theme.backgroundPanel}
101
- border
102
- borderColor={theme.border}
103
- >
104
- {/* Filter row */}
105
- <box flexDirection="row" paddingLeft={1} paddingRight={1} height={1}>
106
- <text content=":" fg={theme.accent} />
107
- <input
108
- focused
109
- placeholder="Filter commands..."
110
- onSubmit={handleSelect}
111
- onInput={(value: string) => {
112
- setFilter(value);
113
- setActiveIndex(0);
114
- }}
115
- backgroundColor="transparent"
116
- focusedBackgroundColor="transparent"
117
- textColor={theme.text}
118
- placeholderColor={theme.textMuted}
119
- cursorColor={theme.accent}
120
- style={{ flexGrow: 1 }}
121
- />
122
- <text content={` ${filtered.length}`} fg={theme.textMuted} />
123
- <CloseButton onClose={onClose} />
124
- </box>
125
-
126
- {/* Separator */}
127
- <box height={1} width="100%" backgroundColor={theme.border} />
128
-
129
- {/* Command list */}
130
- <scrollbox focused={false} style={{ flexGrow: 1 }}>
131
- {filtered.map(
132
- (entry, i): React.ReactNode => (
133
- <box
134
- key={entry.id}
135
- flexDirection="row"
136
- paddingLeft={1}
137
- paddingRight={1}
138
- height={1}
139
- backgroundColor={i === activeIndex ? theme.backgroundElement : undefined}
140
- onMouseDown={(event) => {
141
- // Left-click runs the entry — same code path as Enter.
142
- if (event.button !== 0) {
143
- return;
144
- }
145
- event.preventDefault();
146
- event.stopPropagation();
147
- onSelect(entry.id);
148
- }}
149
- >
150
- <text content={entry.title} fg={theme.text} style={{ flexGrow: 1 }} />
151
- {(entry.hotkey?.length ?? 0) > 0 && (
152
- <text content={entry.hotkey} fg={theme.textMuted} />
153
- )}
154
- {(entry.category?.length ?? 0) > 0 && (
155
- <text content={` ${entry.category}`} fg={theme.textMuted} />
43
+ <Chooser
44
+ items={items}
45
+ commandScope="command-palette"
46
+ prompt={<text content=":" fg={theme.accent} />}
47
+ placeholder="Filter commands..."
48
+ onCancel={onClose}
49
+ onSelect={handleSelect}
50
+ renderItem={({ item, positions }): ReactNode => {
51
+ const entry = item.value === undefined ? undefined : entriesById.get(item.value);
52
+ return (
53
+ <>
54
+ <text fg={theme.text} style={{ flexGrow: 1 }}>
55
+ {(entry?.icon?.length ?? 0) > 0 && (
56
+ <span fg={theme.textMuted}>{`${entry?.icon} `}</span>
156
57
  )}
157
- </box>
158
- ),
159
- )}
160
- </scrollbox>
161
- </box>
58
+ <ChooseHighlightedText
59
+ text={item.text}
60
+ positions={positions}
61
+ highlightColor={theme.warning}
62
+ />
63
+ </text>
64
+ {(entry?.hotkey?.length ?? 0) > 0 && (
65
+ <text content={entry?.hotkey} fg={theme.textMuted} />
66
+ )}
67
+ {(entry?.category?.length ?? 0) > 0 && (
68
+ <text content={` ${entry?.category}`} fg={theme.textMuted} />
69
+ )}
70
+ </>
71
+ );
72
+ }}
73
+ />
162
74
  );
163
75
  };
@@ -1,6 +1,8 @@
1
1
  import { useState, useCallback } from "react";
2
- import { useKeyboard, useTerminalDimensions } from "@opentui/react";
2
+ import { useTerminalDimensions } from "@opentui/react";
3
+ import { useCommand } from "@tooee/commands";
3
4
  import { useTheme } from "@tooee/themes";
5
+ import type { ReactNode } from "react";
4
6
 
5
7
  export interface ContextMenuEntry {
6
8
  id: string;
@@ -35,7 +37,7 @@ export const ContextMenu = function ContextMenu({
35
37
  y,
36
38
  onSelect,
37
39
  onClose,
38
- }: ContextMenuProps): React.ReactNode {
40
+ }: ContextMenuProps): ReactNode {
39
41
  const { theme } = useTheme();
40
42
  const { width: termWidth, height: termHeight } = useTerminalDimensions();
41
43
  const [activeIndex, setActiveIndex] = useState(0);
@@ -50,18 +52,55 @@ export const ContextMenu = function ContextMenu({
50
52
  [entries, onSelect],
51
53
  );
52
54
 
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).
55
+ const moveUp = useCallback(() => {
56
+ setActiveIndex((index) => Math.max(0, index - 1));
57
+ }, []);
58
+ const moveDown = useCallback(() => {
59
+ setActiveIndex((index) => Math.min(entries.length - 1, index + 1));
60
+ }, [entries.length]);
61
+ const selectActive = useCallback(() => {
62
+ select(activeIndex);
63
+ }, [activeIndex, select]);
64
+
65
+ useCommand({
66
+ handler: moveUp,
67
+ hidden: true,
68
+ hotkey: "up",
69
+ id: "context-menu:move-up",
70
+ modes: ["insert", "cursor"],
71
+ title: "Move up",
72
+ });
73
+ useCommand({
74
+ handler: moveUp,
75
+ hidden: true,
76
+ hotkey: "k",
77
+ id: "context-menu:move-up-vim",
78
+ modes: ["insert", "cursor"],
79
+ title: "Move up",
80
+ });
81
+ useCommand({
82
+ handler: moveDown,
83
+ hidden: true,
84
+ hotkey: "down",
85
+ id: "context-menu:move-down",
86
+ modes: ["insert", "cursor"],
87
+ title: "Move down",
88
+ });
89
+ useCommand({
90
+ handler: moveDown,
91
+ hidden: true,
92
+ hotkey: "j",
93
+ id: "context-menu:move-down-vim",
94
+ modes: ["insert", "cursor"],
95
+ title: "Move down",
96
+ });
97
+ useCommand({
98
+ handler: selectActive,
99
+ hidden: true,
100
+ hotkey: "Enter",
101
+ id: "context-menu:select",
102
+ modes: ["insert", "cursor"],
103
+ title: "Select action",
65
104
  });
66
105
 
67
106
  // Size the panel from its contents (terminal cell width, not code units,
@@ -122,7 +161,7 @@ export const ContextMenu = function ContextMenu({
122
161
  </box>
123
162
  ) : (
124
163
  entries.map(
125
- (entry, i): React.ReactNode => (
164
+ (entry, i): ReactNode => (
126
165
  <box
127
166
  key={entry.id}
128
167
  flexDirection="row"
package/src/index.ts CHANGED
@@ -25,21 +25,64 @@ export {
25
25
  } from "./code-blocks.js";
26
26
  export type { CodeBlockRenderer, CodeBlockRendererProps, CodeBlockHScroll } from "./code-blocks.js";
27
27
  export { CodeView } from "./code-view.js";
28
- export { Table, computeColumnWidths, isNumeric, sampleRows } from "./table.js";
28
+ export { Table, computeColumnWidths, formatTableCell, isNumeric, sampleRows } from "./table.js";
29
29
  export type { TableProps, ColumnWidthOptions } from "./table.js";
30
30
  export type { ColumnDef, TableRow } from "./table-types.js";
31
31
  export { CommandPalette } from "./command-palette.js";
32
32
  export type { CommandPaletteEntry } from "./command-palette.js";
33
+ export { Chooser } from "./chooser/chooser.js";
34
+ export type { ChooserProps } from "./chooser/chooser.js";
35
+ export { ChooseFilter } from "./chooser/choose-filter.js";
36
+ export type { ChooseFilterProps } from "./chooser/choose-filter.js";
37
+ export { ChooseHighlightedText } from "./chooser/choose-highlighted-text.js";
38
+ export type { ChooseHighlightedTextProps } from "./chooser/choose-highlighted-text.js";
39
+ export { ChooseList } from "./chooser/choose-list.js";
40
+ export type { ChooseItemRenderContext, ChooseListProps } from "./chooser/choose-list.js";
41
+ export {
42
+ createChooseStore,
43
+ selectActiveIndex,
44
+ selectError,
45
+ selectFilterQuery,
46
+ selectItems,
47
+ selectLoading,
48
+ selectMatches,
49
+ selectReloadRevision,
50
+ selectSelectedOriginalIndices,
51
+ } from "./chooser/choose-store.js";
52
+ export type { ChooseStore, ChooseStoreContext, ChooseStoreEvents } from "./chooser/choose-store.js";
53
+ export { fuzzyFilter } from "./chooser/fuzzy.js";
54
+ export type { FuzzyMatch } from "./chooser/fuzzy.js";
55
+ export { loadChooseSource } from "./chooser/source.js";
56
+ export { useChoose } from "./chooser/use-choose.js";
57
+ export type {
58
+ ChooseCommandGroup,
59
+ ChooseController,
60
+ ChooseState,
61
+ ChooseViewModel,
62
+ UseChooseOptions,
63
+ UseChooseResult,
64
+ } from "./chooser/use-choose.js";
65
+ export type {
66
+ ChooseContentProvider,
67
+ ChooseItem,
68
+ ChooseOptions,
69
+ ChooseResult,
70
+ ChooseSource,
71
+ } from "./chooser/types.js";
33
72
  export { ContextMenu } from "./context-menu.js";
34
73
  export type { ContextMenuEntry } from "./context-menu.js";
35
74
  export { parseCSV, parseTSV, parseJSON, parseAuto, detectFormat } from "./parsers.js";
36
- export type { Format, ParsedTable } from "./parsers.js";
75
+ export type { Format, ParsedTable, TableData } from "./parsers.js";
37
76
  export { RowDocumentRenderable } from "./row-document-renderable.js";
38
77
  export {
39
78
  DEFAULT_SIGN_COLUMN_WIDTH,
40
79
  computeRowDocumentGutterWidth,
41
80
  } from "./row-document-renderable.js";
42
- export type { RowDocumentOptions, RowDocumentPalette } from "./row-document-renderable.js";
81
+ export type {
82
+ RowDocumentOptions,
83
+ RowDocumentPalette,
84
+ VisibleRowRange,
85
+ } from "./row-document-renderable.js";
43
86
  export type { DecorationLayer, RowDecoration } from "./decoration-layer.js";
44
87
  export type { DocumentBindings } from "./document-bindings.js";
45
88
  export { useGutterPalette } from "./use-gutter-palette.js";
@@ -0,0 +1,53 @@
1
+ import type { Token, Tokens } from "marked";
2
+ import type { ReactNode } from "react";
3
+ import type { ResolvedTheme } from "@tooee/themes";
4
+ import { InlineTokens } from "../inline.js";
5
+ import { linkMouseHandler } from "../links.js";
6
+ import type { MarkdownLinkHandler } from "../links.js";
7
+ import { hasMarkedText } from "../tokens.js";
8
+
9
+ export const BlockquoteRenderer = function BlockquoteRenderer({
10
+ token,
11
+ theme,
12
+ indent,
13
+ onLinkActivate,
14
+ }: {
15
+ token: Tokens.Blockquote;
16
+ theme: ResolvedTheme;
17
+ indent: number;
18
+ onLinkActivate?: MarkdownLinkHandler;
19
+ }): ReactNode {
20
+ const inlineTokens: Token[] = [];
21
+ for (const child of token.tokens) {
22
+ if ("tokens" in child && Array.isArray(child.tokens)) {
23
+ if (inlineTokens.length > 0) {
24
+ inlineTokens.push({ raw: "\n", type: "br" });
25
+ }
26
+ inlineTokens.push(...child.tokens);
27
+ } else if (hasMarkedText(child)) {
28
+ inlineTokens.push(child);
29
+ }
30
+ }
31
+
32
+ return (
33
+ <box
34
+ border={["left"]}
35
+ borderColor={theme.markdownBlockQuote}
36
+ borderStyle="single"
37
+ style={{
38
+ marginBottom: 1,
39
+ marginLeft: 1 + indent,
40
+ marginRight: 1,
41
+ marginTop: 0,
42
+ paddingLeft: 1,
43
+ }}
44
+ >
45
+ <text
46
+ style={{ fg: theme.textMuted }}
47
+ onMouseDown={linkMouseHandler(inlineTokens, onLinkActivate)}
48
+ >
49
+ <InlineTokens tokens={inlineTokens} theme={theme} />
50
+ </text>
51
+ </box>
52
+ );
53
+ };
@@ -0,0 +1,119 @@
1
+ import type { ReactNode, RefObject } from "react";
2
+ import type { SyntaxStyle, TextBufferRenderable } from "@opentui/core";
3
+ import type { ResolvedTheme } from "@tooee/themes";
4
+ import { CodeBlock } from "../../code-blocks.js";
5
+ import type { CodeBlockRenderer } from "../../code-blocks.js";
6
+ import type { FlatBlock } from "../../markdown-blocks.js";
7
+ import { BlockquoteRenderer } from "./blockquote.js";
8
+ import { HeadingRenderer } from "./heading.js";
9
+ import { HorizontalRule } from "./horizontal-rule.js";
10
+ import { ListLineRenderer } from "./list-line.js";
11
+ import { ParagraphRenderer } from "./paragraph.js";
12
+ import { MarkdownTableRenderer } from "./table.js";
13
+ import type { MarkdownLinkHandler } from "../links.js";
14
+ import { hasMarkedText, narrowToken } from "../tokens.js";
15
+
16
+ type CodeBlockRendererRegistry = Record<string, CodeBlockRenderer>;
17
+
18
+ export const FlatBlockRenderer = function FlatBlockRenderer({
19
+ block,
20
+ blockIndex,
21
+ theme,
22
+ syntax,
23
+ contentWidth,
24
+ hScrollableBlocksRef,
25
+ codeBlockRenderers,
26
+ onLinkActivate,
27
+ imageBasePath,
28
+ }: {
29
+ block: FlatBlock;
30
+ blockIndex: number;
31
+ theme: ResolvedTheme;
32
+ syntax: SyntaxStyle;
33
+ contentWidth: number;
34
+ hScrollableBlocksRef?: RefObject<Map<number, TextBufferRenderable>>;
35
+ codeBlockRenderers?: CodeBlockRendererRegistry;
36
+ onLinkActivate?: MarkdownLinkHandler;
37
+ imageBasePath?: string;
38
+ }): ReactNode {
39
+ const { token, indent, bullet } = block;
40
+
41
+ if (bullet !== undefined) {
42
+ return <ListLineRenderer block={block} theme={theme} onLinkActivate={onLinkActivate} />;
43
+ }
44
+
45
+ const heading = narrowToken(token, "heading");
46
+ if (heading !== null) {
47
+ return (
48
+ <HeadingRenderer
49
+ token={heading}
50
+ theme={theme}
51
+ indent={indent}
52
+ onLinkActivate={onLinkActivate}
53
+ />
54
+ );
55
+ }
56
+ const paragraph = narrowToken(token, "paragraph");
57
+ if (paragraph !== null) {
58
+ return (
59
+ <ParagraphRenderer
60
+ token={paragraph}
61
+ theme={theme}
62
+ indent={indent}
63
+ onLinkActivate={onLinkActivate}
64
+ imageBasePath={imageBasePath}
65
+ />
66
+ );
67
+ }
68
+ const code = narrowToken(token, "code");
69
+ if (code !== null) {
70
+ return (
71
+ <CodeBlock
72
+ token={code}
73
+ blockIndex={blockIndex}
74
+ theme={theme}
75
+ syntax={syntax}
76
+ indent={indent}
77
+ contentWidth={contentWidth}
78
+ hScrollableBlocksRef={hScrollableBlocksRef}
79
+ renderers={codeBlockRenderers}
80
+ />
81
+ );
82
+ }
83
+ const blockquote = narrowToken(token, "blockquote");
84
+ if (blockquote !== null) {
85
+ return (
86
+ <BlockquoteRenderer
87
+ token={blockquote}
88
+ theme={theme}
89
+ indent={indent}
90
+ onLinkActivate={onLinkActivate}
91
+ />
92
+ );
93
+ }
94
+ const table = narrowToken(token, "table");
95
+ if (table !== null) {
96
+ return <MarkdownTableRenderer token={table} indent={indent} />;
97
+ }
98
+ if (token.type === "hr") {
99
+ return <HorizontalRule theme={theme} indent={indent} />;
100
+ }
101
+ if (token.type === "space" || token.type === "html") {
102
+ return null;
103
+ }
104
+ if (!hasMarkedText(token)) {
105
+ return null;
106
+ }
107
+ return (
108
+ <text
109
+ content={token.text}
110
+ style={{
111
+ fg: theme.markdownText,
112
+ marginBottom: 1,
113
+ marginLeft: 1 + indent,
114
+ marginRight: 1,
115
+ marginTop: 0,
116
+ }}
117
+ />
118
+ );
119
+ };
@@ -0,0 +1,53 @@
1
+ import type { Tokens } from "marked";
2
+ import type { ReactNode } from "react";
3
+ import type { ResolvedTheme } from "@tooee/themes";
4
+ import { InlineTokens } from "../inline.js";
5
+ import { linkMouseHandler } from "../links.js";
6
+ import type { MarkdownLinkHandler } from "../links.js";
7
+
8
+ export const HeadingRenderer = function HeadingRenderer({
9
+ token,
10
+ theme,
11
+ indent,
12
+ onLinkActivate,
13
+ }: {
14
+ token: Tokens.Heading;
15
+ theme: ResolvedTheme;
16
+ indent: number;
17
+ onLinkActivate?: MarkdownLinkHandler;
18
+ }): ReactNode {
19
+ const headingColors = new Map<number, string>([
20
+ [1, theme.markdownHeading],
21
+ [2, theme.secondary],
22
+ [3, theme.accent],
23
+ [4, theme.text],
24
+ [5, theme.textMuted],
25
+ [6, theme.textMuted],
26
+ ]);
27
+ const prefixes = new Map<number, string>([
28
+ [1, "# "],
29
+ [2, "## "],
30
+ [3, "### "],
31
+ [4, "#### "],
32
+ [5, "##### "],
33
+ [6, "###### "],
34
+ ]);
35
+
36
+ return (
37
+ <box style={{ marginBottom: 1, marginLeft: indent, marginTop: 1 }}>
38
+ <text
39
+ style={{ fg: headingColors.get(token.depth) ?? theme.text }}
40
+ onMouseDown={linkMouseHandler(
41
+ token.tokens,
42
+ onLinkActivate,
43
+ Bun.stringWidth(prefixes.get(token.depth) ?? ""),
44
+ )}
45
+ >
46
+ <span fg={theme.textMuted}>{prefixes.get(token.depth)}</span>
47
+ <strong>
48
+ <InlineTokens tokens={token.tokens} theme={theme} />
49
+ </strong>
50
+ </text>
51
+ </box>
52
+ );
53
+ };
@@ -0,0 +1,16 @@
1
+ import type { ReactNode } from "react";
2
+ import type { ResolvedTheme } from "@tooee/themes";
3
+
4
+ export const HorizontalRule = function HorizontalRule({
5
+ theme,
6
+ indent,
7
+ }: {
8
+ theme: ResolvedTheme;
9
+ indent: number;
10
+ }): ReactNode {
11
+ return (
12
+ <box style={{ marginBottom: 1, marginLeft: 1 + indent, marginRight: 1, marginTop: 0 }}>
13
+ <text style={{ fg: theme.markdownHorizontalRule }} content={"─".repeat(40)} />
14
+ </box>
15
+ );
16
+ };
@@ -0,0 +1,52 @@
1
+ import type { Token } from "marked";
2
+ import type { ReactNode } from "react";
3
+ import type { ResolvedTheme } from "@tooee/themes";
4
+ import { checkboxMarker } from "../../markdown-blocks.js";
5
+ import type { FlatBlock } from "../../markdown-blocks.js";
6
+ import { InlineTokens } from "../inline.js";
7
+ import { linkMouseHandler } from "../links.js";
8
+ import type { MarkdownLinkHandler } from "../links.js";
9
+ import { hasMarkedText } from "../tokens.js";
10
+
11
+ export const ListLineRenderer = function ListLineRenderer({
12
+ block,
13
+ theme,
14
+ onLinkActivate,
15
+ }: {
16
+ block: FlatBlock;
17
+ theme: ResolvedTheme;
18
+ onLinkActivate?: MarkdownLinkHandler;
19
+ }): ReactNode {
20
+ const { token, indent, bullet, checked } = block;
21
+ const checkboxPrefix = checkboxMarker(checked);
22
+ const inlineTokens: Token[] =
23
+ "tokens" in token && Array.isArray(token.tokens) ? token.tokens : [];
24
+ const tokenText = hasMarkedText(token) ? token.text : "";
25
+ const hasContent = inlineTokens.length > 0 || tokenText.length > 0;
26
+
27
+ let content: ReactNode = null;
28
+ if (inlineTokens.length > 0) {
29
+ content = <InlineTokens tokens={inlineTokens} theme={theme} />;
30
+ } else if (tokenText.length > 0) {
31
+ content = tokenText;
32
+ }
33
+
34
+ return (
35
+ <box style={{ marginLeft: 1 + indent, marginRight: 1 }}>
36
+ <text
37
+ style={{ fg: theme.markdownText }}
38
+ onMouseDown={linkMouseHandler(
39
+ inlineTokens,
40
+ onLinkActivate,
41
+ Bun.stringWidth(`${bullet}${checkboxPrefix}`),
42
+ )}
43
+ >
44
+ <span fg={theme.markdownListItem}>{bullet}</span>
45
+ {checkboxPrefix !== "" && (
46
+ <span fg={checked === true ? theme.accent : theme.textMuted}>{checkboxPrefix}</span>
47
+ )}
48
+ {hasContent && content}
49
+ </text>
50
+ </box>
51
+ );
52
+ };