@kvndy/text-view 0.0.1 → 0.0.2
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 +141 -0
- package/dist/main.d.ts +1 -1
- package/dist/main.js +45 -15
- package/dist/text-view.d.ts +8 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,3 +5,144 @@ This is a simple attributed string and virtualized/windowing text view for **Pre
|
|
|
5
5
|
It works by appending overlay spans on top of a textarea (for editing) and a div (for glyph metrics). Due to this, the browser’s built-in search control will find text in triplicate. On scroll, reconciliation is bypassed and spans are directly inserted into the DOM, which get removed on resize or re-render.
|
|
6
6
|
|
|
7
7
|
The API and implementation is still under development. Until version 1.0.0, expect breaking semver changes. Consumers should pin to specific releases.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install --save-exact @kvndy/text-view@0.0.1
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`AttributedString` is a TypeScript type for a plain object containing the full `text` and its attribute `runs`. Each run applies foreground color, background color, or both to a range in that text. Offsets are UTF-16 code units, like textarea selection offsets, and range ends are exclusive.
|
|
16
|
+
|
|
17
|
+
`TextView` is the Preact component that renders and edits the text. Pass your `AttributedString` as `value` and the current caret or selection as `selection`. Use `onInput` to update the value and selection after an edit. Use `onSelectionChange` to track caret or selection movement.
|
|
18
|
+
|
|
19
|
+
The editor fills its parent, which must give it a height. It uses the browser's default monospace font with ligatures disabled. `fontSize` is in CSS pixels and defaults to 16. Text wraps by default. Set `softWrap={false}` to scroll long lines horizontally. TextView requires support for `field-sizing: content`.
|
|
20
|
+
|
|
21
|
+
Use `selection` to set a selection and `revealOffset` to reveal a text offset
|
|
22
|
+
after rendering. For commands, pass a stable `onController` callback.
|
|
23
|
+
TextView gives it a `TextViewController` after mount and calls it with `null`
|
|
24
|
+
on unmount or when the callback is replaced. Store the controller wherever
|
|
25
|
+
your app handles commands. Call `controller.focus()` to focus without
|
|
26
|
+
scrolling or changing selection, or `controller.revealOffset(offset)` to
|
|
27
|
+
reveal an offset again after a user scroll or resize.
|
|
28
|
+
|
|
29
|
+
`onInput` supplies a `coalescingKey` for apps that group edits in their undo
|
|
30
|
+
history. If your input handler already opens a group, use the key there.
|
|
31
|
+
Otherwise you can pass `runInputTransaction` to wrap the mutation. Call its
|
|
32
|
+
`apply` function once, synchronously, inside your transaction. Without this
|
|
33
|
+
prop, TextView calls `onInput` directly.
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import { useState } from "preact/hooks";
|
|
37
|
+
import { TextView, type AttributedString, type TextSelection } from "@kvndy/text-view";
|
|
38
|
+
|
|
39
|
+
export function Editor() {
|
|
40
|
+
const [text, setText] = useState("Hello world");
|
|
41
|
+
const [selection, setSelection] = useState<TextSelection>({
|
|
42
|
+
start: 0,
|
|
43
|
+
end: 0,
|
|
44
|
+
direction: "none",
|
|
45
|
+
});
|
|
46
|
+
const firstWord = text.match(/^\S+/)?.[0] ?? "";
|
|
47
|
+
const value: AttributedString = {
|
|
48
|
+
text,
|
|
49
|
+
runs: firstWord
|
|
50
|
+
? [{ range: { start: 0, end: firstWord.length }, attributes: { foreground: "royalblue" } }]
|
|
51
|
+
: [],
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<div style={{ height: "12rem" }}>
|
|
56
|
+
<TextView
|
|
57
|
+
ariaLabel="Example editor"
|
|
58
|
+
value={value}
|
|
59
|
+
selection={selection}
|
|
60
|
+
onInput={edit => {
|
|
61
|
+
setText(edit.text);
|
|
62
|
+
setSelection(edit.selection);
|
|
63
|
+
}}
|
|
64
|
+
onSelectionChange={setSelection}
|
|
65
|
+
/>
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Type definitions
|
|
72
|
+
|
|
73
|
+
`TextRange`, `TextAttributes`, `AttributeRun`, and `AttributedString` are
|
|
74
|
+
re-exported from `@kvndy/attributed-string`.
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
export interface TextRange {
|
|
78
|
+
readonly start: number;
|
|
79
|
+
readonly end: number;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface TextAttributes {
|
|
83
|
+
readonly foreground?: string;
|
|
84
|
+
readonly background?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface AttributeRun {
|
|
88
|
+
readonly range: TextRange;
|
|
89
|
+
readonly attributes: TextAttributes;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface AttributedString {
|
|
93
|
+
readonly text: string;
|
|
94
|
+
readonly runs: readonly AttributeRun[];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface TextSelection extends TextRange {
|
|
98
|
+
readonly direction: "forward" | "backward" | "none";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface TextInput {
|
|
102
|
+
readonly text: string;
|
|
103
|
+
readonly selection: TextSelection;
|
|
104
|
+
readonly inputType: string;
|
|
105
|
+
readonly isComposing: boolean;
|
|
106
|
+
readonly coalescingKey: symbol;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface TextViewController {
|
|
110
|
+
focus(): void;
|
|
111
|
+
revealOffset(offset: number): void;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface TextViewProps {
|
|
115
|
+
value: AttributedString;
|
|
116
|
+
selection: TextSelection;
|
|
117
|
+
ariaLabel?: string;
|
|
118
|
+
onController?: (controller: TextViewController | null) => void;
|
|
119
|
+
scrollTop?: number;
|
|
120
|
+
softWrap?: boolean;
|
|
121
|
+
fontSize?: number;
|
|
122
|
+
highlighted?: boolean;
|
|
123
|
+
disabled?: boolean;
|
|
124
|
+
revealOffset?: number | null;
|
|
125
|
+
onInput: (input: TextInput) => void;
|
|
126
|
+
runInputTransaction?: (input: TextInput, apply: () => void) => void;
|
|
127
|
+
onSelectionChange?: (selection: TextSelection) => void;
|
|
128
|
+
onScroll?: (scrollTop: number) => void;
|
|
129
|
+
onFocus?: () => void;
|
|
130
|
+
onBlur?: () => void;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export declare function TextView(props: TextViewProps): import("preact").JSX.Element;
|
|
134
|
+
|
|
135
|
+
export declare class EditCoalescing {
|
|
136
|
+
interrupt(): void;
|
|
137
|
+
input(inputType: string): symbol;
|
|
138
|
+
dispose(): void;
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Example
|
|
143
|
+
|
|
144
|
+
[Proof-of-concept live example](https://kevindoughty.gitlab.io/attributed-string/).
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
MIT
|
package/dist/main.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { TextView, type TextViewProps } from "./text-view.js";
|
|
1
|
+
export { TextView, type TextViewController, type TextViewProps } from "./text-view.js";
|
|
2
2
|
export { EditCoalescing } from "./edit-coalescing.js";
|
|
3
3
|
export type { AttributedString, TextRange, TextAttributes, AttributeRun } from "@kvndy/attributed-string";
|
|
4
4
|
export type { TextSelection, TextInput } from "./types.js";
|
package/dist/main.js
CHANGED
|
@@ -61,7 +61,7 @@ function ContentView({ lines, visibleRange, attributes, text, style, scrollHandl
|
|
|
61
61
|
};
|
|
62
62
|
return () => { scrollHandler.current = null; };
|
|
63
63
|
});
|
|
64
|
-
return jsx("div", {
|
|
64
|
+
return jsx("div", { "data-text-view-part": "content", ref: elementRef, style: { ...style, top: `${top}px` }, children: children });
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
/** Supplies identity keys to the consumer's UndoManager.group; owns no history. */
|
|
@@ -134,7 +134,7 @@ function readSelection(textarea) {
|
|
|
134
134
|
direction: textarea.selectionDirection ?? "none",
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
|
-
function TextView({ value: { text, runs }, selection, ariaLabel, scrollTop, softWrap = true, fontSize = 16, highlighted = true, disabled = false, revealOffset, onInput, onSelectionChange, onScroll, onFocus, onBlur, }) {
|
|
137
|
+
function TextView({ value: { text, runs }, selection, ariaLabel, onController, scrollTop, softWrap = true, fontSize = 16, highlighted = true, disabled = false, revealOffset, onInput, runInputTransaction, onSelectionChange, onScroll, onFocus, onBlur, }) {
|
|
138
138
|
const textareaRef = useRef(null);
|
|
139
139
|
const viewportRef = useRef(null);
|
|
140
140
|
const measureRef = useRef(null);
|
|
@@ -150,11 +150,11 @@ function TextView({ value: { text, runs }, selection, ariaLabel, scrollTop, soft
|
|
|
150
150
|
// final blank caret row measurable without changing the document.
|
|
151
151
|
return (text + "\n ").match(/[^\n]*\n|[^\n]+$/g);
|
|
152
152
|
}, [text]);
|
|
153
|
+
const refreshLayout = () => layout.refresh(text, softWrap, viewportRef.current, measureRef.current, glyphRef.current);
|
|
153
154
|
useLayoutEffect(() => {
|
|
154
155
|
const viewport = viewportRef.current;
|
|
155
156
|
const measure = measureRef.current;
|
|
156
|
-
const
|
|
157
|
-
const refresh = () => setView(layout.refresh(text, softWrap, viewport, measure, glyph));
|
|
157
|
+
const refresh = () => setView(refreshLayout());
|
|
158
158
|
refresh();
|
|
159
159
|
const observer = new ResizeObserver(refresh);
|
|
160
160
|
observer.observe(viewport);
|
|
@@ -173,19 +173,40 @@ function TextView({ value: { text, runs }, selection, ariaLabel, scrollTop, soft
|
|
|
173
173
|
if (scrollTop !== undefined)
|
|
174
174
|
viewportRef.current.scrollTop = scrollTop;
|
|
175
175
|
}, [scrollTop]);
|
|
176
|
-
|
|
177
|
-
if (revealOffset == null || !view.lines.length)
|
|
178
|
-
return;
|
|
176
|
+
const reveal = (offset) => {
|
|
179
177
|
const viewport = viewportRef.current;
|
|
180
|
-
|
|
178
|
+
if (!viewport || !measureRef.current || !glyphRef.current)
|
|
179
|
+
return;
|
|
180
|
+
const currentView = refreshLayout();
|
|
181
|
+
setView(currentView);
|
|
182
|
+
if (!currentView.lines.length)
|
|
183
|
+
return;
|
|
184
|
+
const top = findLineAtOffset(currentView.lines, offset).y + padding;
|
|
181
185
|
if (top < viewport.scrollTop || top + lineHeight > viewport.scrollTop + viewport.clientHeight) {
|
|
182
186
|
viewport.scrollTop = top - viewport.clientHeight / 2;
|
|
183
187
|
}
|
|
184
|
-
}
|
|
188
|
+
};
|
|
189
|
+
const revealHandler = useRef(reveal);
|
|
190
|
+
useLayoutEffect(() => { revealHandler.current = reveal; });
|
|
191
|
+
const [controller] = useState(() => ({
|
|
192
|
+
focus: () => textareaRef.current?.focus({ preventScroll: true }),
|
|
193
|
+
revealOffset: offset => revealHandler.current(offset),
|
|
194
|
+
}));
|
|
195
|
+
useLayoutEffect(() => {
|
|
196
|
+
if (!onController)
|
|
197
|
+
return;
|
|
198
|
+
onController(controller);
|
|
199
|
+
return () => onController(null);
|
|
200
|
+
}, [onController, controller]);
|
|
201
|
+
useLayoutEffect(() => {
|
|
202
|
+
if (revealOffset != null)
|
|
203
|
+
reveal(revealOffset);
|
|
204
|
+
}, [revealOffset, view, lineHeight]);
|
|
185
205
|
const reportSelection = () => {
|
|
186
|
-
|
|
206
|
+
const textarea = textareaRef.current;
|
|
207
|
+
if (disabled || !textarea)
|
|
187
208
|
return;
|
|
188
|
-
const next = readSelection(
|
|
209
|
+
const next = readSelection(textarea);
|
|
189
210
|
if (next.start === selection.start && next.end === selection.end &&
|
|
190
211
|
(next.start === next.end || next.direction === selection.direction))
|
|
191
212
|
return;
|
|
@@ -226,6 +247,8 @@ function TextView({ value: { text, runs }, selection, ariaLabel, scrollTop, soft
|
|
|
226
247
|
fontFamily: "monospace",
|
|
227
248
|
fontWeight: 400,
|
|
228
249
|
fontVariantLigatures: "none",
|
|
250
|
+
letterSpacing: "normal",
|
|
251
|
+
wordSpacing: "normal",
|
|
229
252
|
fontSize: `${fontSize}px`,
|
|
230
253
|
lineHeight: `${lineHeight}px`,
|
|
231
254
|
}, children: [jsx("span", { ref: glyphRef, "aria-hidden": "true", style: {
|
|
@@ -235,6 +258,9 @@ function TextView({ value: { text, runs }, selection, ariaLabel, scrollTop, soft
|
|
|
235
258
|
overflowX: softWrap ? "hidden" : "auto",
|
|
236
259
|
overflowY: "auto",
|
|
237
260
|
scrollbarGutter: "stable",
|
|
261
|
+
}, onClick: event => {
|
|
262
|
+
if (event.target === event.currentTarget)
|
|
263
|
+
controller.focus();
|
|
238
264
|
}, onScroll: event => {
|
|
239
265
|
const element = event.currentTarget;
|
|
240
266
|
coalescing.interrupt();
|
|
@@ -244,7 +270,7 @@ function TextView({ value: { text, runs }, selection, ariaLabel, scrollTop, soft
|
|
|
244
270
|
position: "relative",
|
|
245
271
|
width: softWrap ? "100%" : "max-content",
|
|
246
272
|
minWidth: "100%",
|
|
247
|
-
}, children: [jsx("textarea", {
|
|
273
|
+
}, children: [jsx("textarea", { ref: textareaRef, "aria-label": ariaLabel, value: text, wrap: softWrap ? "soft" : "off", spellcheck: false, disabled: disabled, style: {
|
|
248
274
|
...wrapping,
|
|
249
275
|
position: "relative",
|
|
250
276
|
zIndex: 1,
|
|
@@ -267,19 +293,23 @@ function TextView({ value: { text, runs }, selection, ariaLabel, scrollTop, soft
|
|
|
267
293
|
}, onInput: event => {
|
|
268
294
|
const textarea = event.currentTarget;
|
|
269
295
|
const { inputType = "", isComposing = false } = event;
|
|
270
|
-
|
|
296
|
+
const input = {
|
|
271
297
|
text: textarea.value,
|
|
272
298
|
selection: readSelection(textarea),
|
|
273
299
|
inputType,
|
|
274
300
|
isComposing,
|
|
275
301
|
coalescingKey: coalescing.input(inputType),
|
|
276
|
-
}
|
|
302
|
+
};
|
|
303
|
+
if (runInputTransaction)
|
|
304
|
+
runInputTransaction(input, () => onInput(input));
|
|
305
|
+
else
|
|
306
|
+
onInput(input);
|
|
277
307
|
}, onSelect: reportSelection, onFocus: () => { coalescing.interrupt(); onFocus?.(); }, onBlur: () => { coalescing.interrupt(); onBlur?.(); } }), jsxs("div", { class: "editor-overlay", "aria-hidden": "true", style: {
|
|
278
308
|
position: "absolute",
|
|
279
309
|
inset: `0 ${padding}px`,
|
|
280
310
|
overflow: "clip",
|
|
281
311
|
pointerEvents: "none",
|
|
282
|
-
}, children: [jsx("div", {
|
|
312
|
+
}, children: [jsx("div", { "data-text-view-part": "measure", ref: measureRef, style: { ...textLayer, color: "transparent" }, children: measureLines }), showOverlay && (jsx(ContentView, { lines: view.lines, visibleRange: visibleRange, text: text, attributes: runs, style: textLayer, scrollHandler: updateOverlay }))] })] }) })] }));
|
|
283
313
|
}
|
|
284
314
|
|
|
285
315
|
export { EditCoalescing, TextView };
|
package/dist/text-view.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export interface TextViewProps {
|
|
|
4
4
|
value: AttributedString;
|
|
5
5
|
selection: TextSelection;
|
|
6
6
|
ariaLabel?: string;
|
|
7
|
+
onController?: (controller: TextViewController | null) => void;
|
|
7
8
|
scrollTop?: number;
|
|
8
9
|
softWrap?: boolean;
|
|
9
10
|
fontSize?: number;
|
|
@@ -11,9 +12,15 @@ export interface TextViewProps {
|
|
|
11
12
|
disabled?: boolean;
|
|
12
13
|
revealOffset?: number | null;
|
|
13
14
|
onInput: (input: TextInput) => void;
|
|
15
|
+
/** Call apply once, synchronously, inside the app's edit transaction. */
|
|
16
|
+
runInputTransaction?: (input: TextInput, apply: () => void) => void;
|
|
14
17
|
onSelectionChange?: (selection: TextSelection) => void;
|
|
15
18
|
onScroll?: (scrollTop: number) => void;
|
|
16
19
|
onFocus?: () => void;
|
|
17
20
|
onBlur?: () => void;
|
|
18
21
|
}
|
|
19
|
-
export
|
|
22
|
+
export interface TextViewController {
|
|
23
|
+
focus(): void;
|
|
24
|
+
revealOffset(offset: number): void;
|
|
25
|
+
}
|
|
26
|
+
export declare function TextView({ value: { text, runs }, selection, ariaLabel, onController, scrollTop, softWrap, fontSize, highlighted, disabled, revealOffset, onInput, runInputTransaction, onSelectionChange, onScroll, onFocus, onBlur, }: TextViewProps): import("preact").JSX.Element;
|