@g9tm/lynxcontrols-code 0.0.0-stage → 2.10.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/README.md CHANGED
@@ -1,3 +1,125 @@
1
- # Temporary Holding Version
1
+ # @g9tm/lynxcontrols-code
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Two controls for apps that show code and logs (2.9.0, from G9 Proxy Manager):
4
+ - `G9CodeEditor`: an editor for JSON and similar text.
5
+ - `G9LogView`: a live log.
6
+
7
+ Both are always left to right, because code and logs are read that way in every locale. An optional package: an app
8
+ that does not import it never bundles it.
9
+
10
+ ```sh
11
+ npm install @g9tm/lynxcontrols-code
12
+ ```
13
+
14
+ | Import | What it holds |
15
+ | --- | --- |
16
+ | `@g9tm/lynxcontrols-code` | both controls and both cores |
17
+ | `@g9tm/lynxcontrols-code/editor` | `G9CodeEditor` and its core |
18
+ | `@g9tm/lynxcontrols-code/log` | `G9LogView` and its core |
19
+ | `@g9tm/lynxcontrols-code/editor-core` | the editor's plain-TS model (no JSX, no runtime import: Node loads it) |
20
+ | `@g9tm/lynxcontrols-code/log-core` | the log's plain-TS store and helpers |
21
+
22
+ ## G9CodeEditor
23
+
24
+ ```tsx
25
+ import { G9CodeEditor, type G9CodeMarker } from '@g9tm/lynxcontrols-code/editor';
26
+
27
+ <G9CodeEditor
28
+ id="Config.Json"
29
+ defaultValue={json}
30
+ onValueChange={setJson} // the whole text, at most every valueDelay ms (150)
31
+ markers={errors} // [{ line, message, severity }]
32
+ onCopy={(text) => clipboard.set(text)}
33
+ findLabels={{ placeholder: t('find'), count: '{0} / {1}' }}
34
+ />;
35
+ ```
36
+
37
+ It shows monospace text with line numbers, JSON colours and error marks:
38
+ - **Colours.** Keys, strings, numbers, `true` / `false`, `null`, punctuation and `//` comments each have a colour.
39
+ `language` takes `'json'` (the default), `'plain'`, or the app's own tokenizer (`(line) => G9Token[]`). The colours
40
+ come from the theme; `colors` overrides any of them.
41
+ - **Marks.** `markers` puts a dot in the gutter and tints the line. When a line has several marks, the most severe one
42
+ is shown. Its message appears on hover (mouse) or tap (touch), and screen readers read it.
43
+ - **Find.** Ctrl/⌘+F opens the find bar, using the selection as the query. Enter, F3, Ctrl/⌘+G, or the ↑ / ↓ buttons
44
+ go to the next or previous hit, and wrap around. The count reads `{0} / {1}`. Escape closes the bar.
45
+ - **Undo and redo.** Ctrl/⌘+Z undoes, and Ctrl/⌘+Y or Ctrl/⌘+Shift+Z redoes. A run of typing is one step, and
46
+ `handleRef` also has `undo` / `redo`.
47
+ - **Selection.** Shift with the arrows, Home / End and Page Up / Down extends the selection. Ctrl/⌘ with the arrows
48
+ moves by word. A double click selects a word, a drag selects a range, Shift+click extends, and Ctrl/⌘+A selects all.
49
+ - **Copy.** Ctrl/⌘+C and Ctrl/⌘+X hand the selection to `onCopy`. The app writes it to its clipboard; without
50
+ `onCopy` these keys do nothing.
51
+ - **Typing help.** Enter keeps the line's indent and adds one step after an opening bracket. Tab inserts `tabSize`
52
+ spaces.
53
+ - **Read only.** `readOnly` keeps moving, selecting, finding and copying, and drops every edit and the caret.
54
+
55
+ **Large text.** Only the lines on screen are drawn. A 1 MB text (30 000+ lines) opens, scrolls and types without lag.
56
+ An edit copies line pointers, never characters, and the tokenizer reads a line in one pass. A very long line, such as
57
+ minified JSON, is drawn only around the visible columns.
58
+
59
+ **Value.** Use `value` for a controlled editor or `defaultValue` for an uncontrolled one. A `value` the app changes
60
+ replaces the document and clears its history. `onChange` reports every edit cheaply, as `{ from, to, removed,
61
+ inserted }`.
62
+
63
+ **Typing.** Input arrives through a hidden `<textarea>` that keeps one sentinel character. A typed letter, an IME's
64
+ composed word, a paste and a Backspace on an empty line therefore all arrive as value changes, on every platform. Keys
65
+ such as the arrows and Ctrl+Z come from three places:
66
+ - the textarea's own keydown;
67
+ - `G9KeyCapture` (Lynxtron);
68
+ - `forwardG9Keys` (Lynx for Web), which forwards every key of a field marked `data-g9keys="all"`.
69
+
70
+ The same key arriving from two of these counts once.
71
+
72
+ **Handle.** `handleRef` offers `getValue`, `getSelectedText`, `setSelection`, `revealLine(n)`, `focus`, `undo`,
73
+ `redo` and `openFind(query)`.
74
+
75
+ The core at `/editor-core` has:
76
+ - the document: `splitLines`, `applyChange` (with its inverse), `textInRange`, `replaceRange`, `clampPos`;
77
+ - carets: `moveCaret`, `wordAt`, `newlineText`, `indentOf`;
78
+ - find: `findAll`, `nextMatchIndex`;
79
+ - history: `G9EditHistory`;
80
+ - colours: `tokenizeJsonLine`, `tokenizePlainLine`;
81
+ - positions: `detectEol`, `comparePos`, `orderedRange`, `caretAt`, `isCaret`.
82
+
83
+ ## G9LogView
84
+
85
+ ```tsx
86
+ import { G9LogStore, G9LogView } from '@g9tm/lynxcontrols-code/log';
87
+
88
+ const store = new G9LogStore({ capacity: 50_000 }); // module scope or a ref: it outlives renders
89
+ proxy.onLog((l) => store.append({ time: l.at, level: l.level, category: l.source, message: l.text }));
90
+
91
+ <G9LogView store={store} onCopy={(text) => clipboard.set(text)} labels={{ search: t('search'), jump: t('latest') }} />;
92
+ ```
93
+
94
+ - **Rows.** Each row shows the time (`HH:MM:SS.mmm`, local or `utc`, optionally with the date), the level in its
95
+ colour (trace, debug, info, warn, error, fatal; `levelColors`), the category (`showCategory`) and the message.
96
+ - **Following.** The view follows the end while the reader is there. When the reader scrolls up, following pauses and
97
+ a chip shows the count of lines that arrived since. The chip, Ctrl/⌘+End or `handleRef.jumpToLatest()` go back to
98
+ the end. `follow={false}` never follows.
99
+ - **Filter.** Level chips and a search box filter the rows. The search is case-insensitive, covers category and
100
+ message, and highlights its hits. Each filter can be controlled or uncontrolled: `levels` / `defaultLevels` /
101
+ `onLevelsChange`, and `query` / `defaultQuery` / `onQueryChange`. `toolbar={false}` hides both for an app with its
102
+ own.
103
+ - **Selection.** A tap selects a row. Ctrl/⌘ adds or removes a row, Shift selects a range, Ctrl/⌘+A selects every
104
+ shown row, and Escape clears. Ctrl/⌘+C hands the selected rows to `onCopy`, one line each (`time LEVEL [category]
105
+ message`). A double tap calls `onActivate`.
106
+ - **Throughput.** `G9LogStore` keeps the newest `capacity` lines, and an append costs only the new lines. The view
107
+ gathers appends and shows them every `batchMs` (100 ms), so thousands of lines a minute cost about ten renders a
108
+ second. Rows are drawn by `G9VirtualList`.
109
+ - **Handle.** `handleRef` offers `jumpToLatest`, `paused`, `getRows` (what the filter shows), `getSelected`,
110
+ `clearSelection` and `copySelection`.
111
+
112
+ The core at `/log-core` has:
113
+ - the store: `G9LogStore`, with `append`, `clear`, `subscribe`, `entries`, `at`, `size` and `capacity`;
114
+ - filtering: `filterLog`, `logFilterTest`, `logHighlights`, `logRuns`;
115
+ - text: `formatLogTime`, `formatLogEntry`, `formatLogEntries`;
116
+ - the rest: `nextLogSelection`, `logLevelRank`, `g9LogLevels`.
117
+
118
+ ## Tests
119
+
120
+ `npx rstest run packages/code`:
121
+ - The models have pure tests, including a 1 MB one-line JSON tokenized in linear time and 300 000 appends into a
122
+ capped store.
123
+ - The controls are tested in the Lynx testing environment: typing through the sentinel, keys from both sources, the
124
+ drawn window of a 1 MB text, marks, find, copy, read-only, batching, the cap, the filters, selection, follow, pause
125
+ and the jump.
@@ -0,0 +1,17 @@
1
+ # G9CodeEditor
2
+
3
+ `import { G9CodeEditor } from '@g9tm/lynxcontrols-code/editor';`
4
+
5
+ Editable monospace text with line numbers, colours, marks, find, undo / redo; only the visible lines are drawn.
6
+
7
+ Props:
8
+ - `value?`: string
9
+ - `onChange?`: (change: G9TextChange) => void
10
+ - `onValueChange?`: (value: string) => void
11
+ - `onCopy?`: (text: string) => void
12
+ - `fontSize?`: number = 13
13
+ - `lineHeight?`: number = 20
14
+ - `tabSize?`: number = 2
15
+ - `defaultValue?`: string
16
+
17
+ Example: `<G9CodeEditor />`
@@ -0,0 +1,17 @@
1
+ # G9LogView
2
+
3
+ `import { G9LogView } from '@g9tm/lynxcontrols-code/log';`
4
+
5
+ A live, virtualised log: follows the end, pauses on scroll-up, filters by level and search, copies selected rows.
6
+
7
+ Props:
8
+ - `store`: object
9
+ - `onActivate?`: (entry: G9LogEntry) => void
10
+ - `onCopy?`: (text: string) => void
11
+ - `onLevelsChange?`: (levels: G9LogLevel[]) => void
12
+ - `onQueryChange?`: (query: string) => void
13
+ - `onSelectionChange?`: (entries: G9LogEntry[]) => void
14
+ - `levelColors?`: object
15
+ - `rowHeight?`: number = 22
16
+
17
+ Example: `<G9LogView store={store} />`