@myrmidon/cadmus-text-segment 0.0.0-stage → 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
CHANGED
|
@@ -1,3 +1,411 @@
|
|
|
1
|
-
#
|
|
1
|
+
# CadmusTextSegment
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
📦 `@myrmidon/cadmus-text-segment`
|
|
4
|
+
|
|
5
|
+
- [CadmusTextSegment](#cadmustextsegment)
|
|
6
|
+
- [Concepts](#concepts)
|
|
7
|
+
- [Segments and Blocks](#segments-and-blocks)
|
|
8
|
+
- [Selection](#selection)
|
|
9
|
+
- [Segmenters and Coordinates](#segmenters-and-coordinates)
|
|
10
|
+
- [Requirements](#requirements)
|
|
11
|
+
- [Quick Start](#quick-start)
|
|
12
|
+
- [Components](#components)
|
|
13
|
+
- [SegmentedTextView](#segmentedtextview)
|
|
14
|
+
- [TextSegmentBlockView](#textsegmentblockview)
|
|
15
|
+
- [TextSegmentView](#textsegmentview)
|
|
16
|
+
- [Settings](#settings)
|
|
17
|
+
- [Tips](#tips)
|
|
18
|
+
- [User Interaction](#user-interaction)
|
|
19
|
+
- [Services and Utilities](#services-and-utilities)
|
|
20
|
+
- [SegmentedTextStore](#segmentedtextstore)
|
|
21
|
+
- [TextMeasureService](#textmeasureservice)
|
|
22
|
+
- [Pure Functions](#pure-functions)
|
|
23
|
+
- [Writing a Segmenter](#writing-a-segmenter)
|
|
24
|
+
- [SpacedTextSegmenter](#spacedtextsegmenter)
|
|
25
|
+
- [Theming](#theming)
|
|
26
|
+
- [Performance](#performance)
|
|
27
|
+
- [History](#history)
|
|
28
|
+
- [0.0.1](#001)
|
|
29
|
+
|
|
30
|
+
A viewer for **segmented text**, letting users select arbitrary portions of it like they would select text in a plain text editor.
|
|
31
|
+
|
|
32
|
+
A segmented text is a text split into _segments_, each being an arbitrarily defined portion of it: a single character, a word, a syllable, a grapheme cluster, or any other sequence of characters. Segments are grouped into _blocks_ (e.g. lines or paragraphs).
|
|
33
|
+
|
|
34
|
+
This library has **no segmentation logic at all**: segmentation is up to your code, typically via a service implementing the `TextSegmenter` interface. The components are agnostic: they just display the segments they are bound to, and let users select them according to the rules attached to each segment.
|
|
35
|
+
|
|
36
|
+
This library provides:
|
|
37
|
+
|
|
38
|
+
- 🖼️ `SegmentedTextView`: the full viewer, with a scrollable text, zoom, find, and (when a segmenter is available) a coordinates box synchronized with the selection.
|
|
39
|
+
- 🧱 `TextSegmentBlockView` and `TextSegmentView`: the dumb building blocks of the viewer, rendering a block and a segment.
|
|
40
|
+
- 🧠 `SegmentedTextStore`: the DOM-independent selection, navigation and search logic.
|
|
41
|
+
- ✂️ `SpacedTextSegmenter`: a sample segmenter (one segment per character, one block per line) with token-based coordinates, useful as a reference implementation and for testing.
|
|
42
|
+
|
|
43
|
+
## Concepts
|
|
44
|
+
|
|
45
|
+
### Segments and Blocks
|
|
46
|
+
|
|
47
|
+
A **segment** (`TextSegment<T>`) has:
|
|
48
|
+
|
|
49
|
+
- `id`: an arbitrary numeric ID, unique in the whole text.
|
|
50
|
+
- `index`: an ordinal defined by the segmentation logic (e.g. the offset of a character in the source text). This is informative only: there can be holes in its sequence (e.g. when skipping CR+LF). The viewer orders segments by their position in blocks.
|
|
51
|
+
- `value`: the text value. This is what the finder searches.
|
|
52
|
+
- `label`: the text displayed, falling back to `value` (e.g. `_` for a space, or `␉` for a tab).
|
|
53
|
+
- `tip`: an optional precomputed tip.
|
|
54
|
+
- `payload`: an optional data object of type `T`, the same for all the segments of a text, carrying any metadata your logic needs (and usable for tips).
|
|
55
|
+
- `selectionMode`: how the segment takes part in a selection (see below).
|
|
56
|
+
- `color`, `textColor`, `borderColor`: optional colors overriding the defaults from settings. Your logic can use them to visually represent metadata.
|
|
57
|
+
- `cssClass`: optional CSS class(es) for the segment's host element.
|
|
58
|
+
|
|
59
|
+
A **block** (`TextSegmentBlock<T>`) is just an `index` (0-N) and an array of `segments`. A block can be empty (e.g. an empty line).
|
|
60
|
+
|
|
61
|
+
Segments are **pure data**: they never carry view state like selection or search results, which is kept by the viewer. So you should treat them as immutable: to change a segment, replace it (and its block) with a new object.
|
|
62
|
+
|
|
63
|
+
### Selection
|
|
64
|
+
|
|
65
|
+
The selection is **linear**, like in a text editor: it always spans all the segments between a start and an end segment, across blocks. Every segment has a `selectionMode`:
|
|
66
|
+
|
|
67
|
+
| mode | start/end here | included when wrapped | crossable | example |
|
|
68
|
+
| --------------------- | -------------- | --------------------- | --------- | -------------------------------------- |
|
|
69
|
+
| `allowed` (default) | yes | yes | yes | a letter |
|
|
70
|
+
| `wrapped` | no | yes | yes | a space between words |
|
|
71
|
+
| `attached` | no (\*) | yes | yes | a combining diacritic after its letter |
|
|
72
|
+
| `blocked` | no | no | **no** | a boundary no selection may cross |
|
|
73
|
+
|
|
74
|
+
(\*) An `attached` segment is glued to the segment preceding it: when that segment ends a selection, all its attached segments are included too; and a selection starting at an attached segment starts at its base instead. Use it to avoid splitting grapheme clusters like `e` + `U+0301`.
|
|
75
|
+
|
|
76
|
+
When dragging or extending a selection towards or beyond a `blocked` segment, the selection stops before it.
|
|
77
|
+
|
|
78
|
+
### Segmenters and Coordinates
|
|
79
|
+
|
|
80
|
+
A `TextSegmenter<T>` encapsulates the whole segmentation logic:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
interface TextSegmenter<T = unknown> {
|
|
84
|
+
// get blocks of segments from the specified text
|
|
85
|
+
getBlocks(text?: string): TextSegmentBlock<T>[];
|
|
86
|
+
// get the coordinates of the specified (selected) segments
|
|
87
|
+
getSegmentCoords(
|
|
88
|
+
blocks: readonly TextSegmentBlock<T>[],
|
|
89
|
+
segments: readonly TextSegment<T>[],
|
|
90
|
+
): string;
|
|
91
|
+
// get the segments included by the coordinates, or null if invalid
|
|
92
|
+
getCoordsSegments(
|
|
93
|
+
blocks: readonly TextSegmentBlock<T>[],
|
|
94
|
+
coords: string,
|
|
95
|
+
): TextSegment<T>[] | null;
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
_Coordinates_ are a textual representation of a selection in a syntax defined by the segmenter (e.g. `1.2@2x2`). The viewer uses them only through the segmenter, and only if one is available: it shows the coordinates of the current selection, and lets users select by typing coordinates. Both coordinate methods get the blocks of the whole text, because coordinates are usually relative to the text structure (blocks, tokens, etc.), which cannot be derived from the selected segments alone.
|
|
100
|
+
|
|
101
|
+
## Requirements
|
|
102
|
+
|
|
103
|
+
- Angular Material with an M3 theme: the default colors are `--mat-sys-*` tokens.
|
|
104
|
+
- the Material Icons font (for the toolbar icons).
|
|
105
|
+
- the CDK overlay styles (included by Angular Material's core styles), for the tip popup.
|
|
106
|
+
|
|
107
|
+
## Quick Start
|
|
108
|
+
|
|
109
|
+
▶️ (1) install the package:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
npm i @myrmidon/cadmus-text-segment
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
▶️ (2) get the blocks from your segmentation logic, and bind them to the viewer. Here we use the sample `SpacedTextSegmenter`:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { Component, computed, signal } from "@angular/core";
|
|
119
|
+
import {
|
|
120
|
+
SegmentedTextView,
|
|
121
|
+
SpacedTextSegmenter,
|
|
122
|
+
TextSegmentSelection,
|
|
123
|
+
} from "@myrmidon/cadmus-text-segment";
|
|
124
|
+
|
|
125
|
+
@Component({
|
|
126
|
+
selector: "app-text",
|
|
127
|
+
imports: [SegmentedTextView],
|
|
128
|
+
template: `
|
|
129
|
+
<div style="height: 400px">
|
|
130
|
+
<cadmus-segmented-text-view
|
|
131
|
+
[blocks]="blocks()"
|
|
132
|
+
[segmenter]="segmenter"
|
|
133
|
+
[settings]="{ showIdBox: true }"
|
|
134
|
+
[(selectedIds)]="selectedIds"
|
|
135
|
+
(selectionChange)="onSelectionChange($event)"
|
|
136
|
+
/>
|
|
137
|
+
</div>
|
|
138
|
+
`,
|
|
139
|
+
})
|
|
140
|
+
export class TextComponent {
|
|
141
|
+
public readonly segmenter = new SpacedTextSegmenter();
|
|
142
|
+
public readonly text = signal("alpha beta\ngamma");
|
|
143
|
+
public readonly blocks = computed(() =>
|
|
144
|
+
this.segmenter.getBlocks(this.text()),
|
|
145
|
+
);
|
|
146
|
+
public readonly selectedIds = signal<number[]>([]);
|
|
147
|
+
|
|
148
|
+
public onSelectionChange(selection: TextSegmentSelection): void {
|
|
149
|
+
console.log(selection.coords, selection.segments);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
The viewer takes **all the space of its container**: give the container a height (or place it in a flex layout).
|
|
155
|
+
|
|
156
|
+
▶️ (3) optionally, instead of binding the `segmenter` input, provide a segmenter for all the viewers in an injector via the `TEXT_SEGMENTER` token:
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import { TEXT_SEGMENTER, SpacedTextSegmenter } from "@myrmidon/cadmus-text-segment";
|
|
160
|
+
|
|
161
|
+
// providers:
|
|
162
|
+
{ provide: TEXT_SEGMENTER, useValue: new SpacedTextSegmenter() },
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The `segmenter` input, when set, takes precedence over the injected one.
|
|
166
|
+
|
|
167
|
+
## Components
|
|
168
|
+
|
|
169
|
+
### SegmentedTextView
|
|
170
|
+
|
|
171
|
+
- 🔑 `SegmentedTextView<T>`
|
|
172
|
+
- 🚩 `cadmus-segmented-text-view`
|
|
173
|
+
- ▶️ input:
|
|
174
|
+
- `blocks` (`readonly TextSegmentBlock<T>[]`): the blocks to display.
|
|
175
|
+
- `settings` (`Partial<SegmentedTextSettings>`): display settings; any missing setting gets its default from `DEFAULT_SEGMENTED_TEXT_SETTINGS`.
|
|
176
|
+
- `segmenter` (`TextSegmenter<T>`): the segmenter for coordinates. If not set, the one provided by `TEXT_SEGMENTER` is used, if any. Without a segmenter, the coordinates box is hidden.
|
|
177
|
+
- `showZoom` (`boolean`, default `true`): show the zoom control.
|
|
178
|
+
- `showFind` (`boolean`, default `true`): show the find control.
|
|
179
|
+
- `showCoords` (`boolean`, default `true`): show the coordinates box (when a segmenter is available).
|
|
180
|
+
- `ariaLabel` (`string`): the accessible label of the text.
|
|
181
|
+
- 🔁 model:
|
|
182
|
+
- `selectedIds` (`number[]`): the IDs of the selected segments, in text order. Setting it selects the specified segments (bypassing selection modes), sorted in text order, ignoring unknown IDs. It does not emit `selectionChange`.
|
|
183
|
+
- 🔥 output:
|
|
184
|
+
- `selectionChange` (`TextSegmentSelection<T>`): emitted whenever the user makes a new selection, with the selected `ids` and `segments` in text order, and their `coords` when a segmenter is available. When dragging, it is emitted once at the end of the drag.
|
|
185
|
+
- `segmentClick` (`TextSegmentClick<T>`): emitted when the user clicks a segment (pressing and releasing the primary pointer button on the same segment), with the clicked `segment` and the pointer `event`. This is emitted for any segment, whatever its selection mode, after any selection change caused by the click: so it is the way to know that users clicked a segment which cannot be selected, e.g. a `blocked` segment representing an existing annotation which you want to let users pick.
|
|
186
|
+
- ⚙️ public methods: `scrollToSegment(id)`, `clearSelection()`, `goToMatch(dir)`.
|
|
187
|
+
|
|
188
|
+
When `blocks` change, the selection is kept for the segments whose IDs are still present.
|
|
189
|
+
|
|
190
|
+
The bottom toolbar has:
|
|
191
|
+
|
|
192
|
+
- a **zoom** slider (50%-300%) with a reset button. You can also zoom with CTRL+wheel on the text.
|
|
193
|
+
- a **find** box: matches are highlighted with the result color until the search is cleared. Enter and SHIFT+Enter (or the arrow buttons) go to the next or previous match, selecting it. The 🔗 toggle switches between finding text within each single segment (default), or across segments (concatenating the values of the segments of each block, so that e.g. `ta` matches across the two segments `t` and `a`). Matches never cross block boundaries. `Aa` toggles case sensitivity.
|
|
194
|
+
- the **coordinates** box: it shows the coordinates of the selection, and selects the segments corresponding to the coordinates typed in it (after a short debounce, `SEGMENTED_TEXT_COORDS_DEBOUNCE`). Invalid coordinates are flagged.
|
|
195
|
+
|
|
196
|
+
### TextSegmentBlockView
|
|
197
|
+
|
|
198
|
+
- 🔑 `TextSegmentBlockView`
|
|
199
|
+
- 🚩 `cadmus-text-segment-block-view`
|
|
200
|
+
- ▶️ input:
|
|
201
|
+
- `block` (`TextSegmentBlock`, required).
|
|
202
|
+
- `settings` (`SegmentedTextSettings`): full (resolved) settings.
|
|
203
|
+
- `selectedIds` (`ReadonlySet<number>`): the selected segment IDs.
|
|
204
|
+
- `hitIds` (`ReadonlySet<number>`): the IDs of segments in search results.
|
|
205
|
+
- `caretId` (`number | null`): the ID of the segment with the caret.
|
|
206
|
+
|
|
207
|
+
A row of segment views, reflowing to the next line like characters in a paragraph, optionally preceded by the block number (index + 1).
|
|
208
|
+
|
|
209
|
+
By default (`wrapMode` = `segment`) the row can wrap after any segment. With `wrapMode` = `word`, the segments are grouped into runs, each ending with a whitespace segment (a segment whose value is entirely whitespace; see `getTextSegmentRuns`), and the row wraps only between runs, so that e.g. the characters of a word stay together like in normal text. A run wider than the whole line still wraps internally.
|
|
210
|
+
|
|
211
|
+
### TextSegmentView
|
|
212
|
+
|
|
213
|
+
- 🔑 `TextSegmentView`
|
|
214
|
+
- 🚩 `cadmus-text-segment-view`
|
|
215
|
+
- ▶️ input:
|
|
216
|
+
- `segment` (`TextSegment`, required).
|
|
217
|
+
- `settings` (`SegmentedTextSettings`): full (resolved) settings.
|
|
218
|
+
- `selected`, `hit`, `caret` (`boolean`).
|
|
219
|
+
|
|
220
|
+
A single SVG with, stacked from top to bottom: the **text box** (a rounded rectangle whose height is set by settings, and whose width fits the label), and optionally the **ID box**, the **index box**, and the **tip box** (when the tip position is `below`).
|
|
221
|
+
|
|
222
|
+
The host element carries the segment ID in its `data-seg-id` attribute, and the classes `selected`, `hit`, `caret`, `mode-<selectionMode>` (for non-allowed modes), plus the segment's `cssClass`. Both dumb components have no outputs: the viewer handles all the pointer events by delegation, using `data-seg-id`.
|
|
223
|
+
|
|
224
|
+
## Settings
|
|
225
|
+
|
|
226
|
+
`SegmentedTextSettings` (all the properties are optional when passed to the viewer; defaults are in `DEFAULT_SEGMENTED_TEXT_SETTINGS`, and `resolveSegmentedTextSettings(partial)` merges them):
|
|
227
|
+
|
|
228
|
+
| setting | default | description |
|
|
229
|
+
| ------------------------- | ---------------------------------------- | ------------------------------------------------------ |
|
|
230
|
+
| `segmentOffset` | `2` | horizontal gap (px) between segments |
|
|
231
|
+
| `lineOffset` | `6` | vertical gap (px) between wrapped lines of a block |
|
|
232
|
+
| `blockOffset` | `12` | vertical gap (px) between blocks |
|
|
233
|
+
| `showBlockNumbers` | `true` | show block numbers |
|
|
234
|
+
| `wrapMode` | `segment` | wrap after any `segment`, or after spaces (`word`) |
|
|
235
|
+
| `showIdBox` | `false` | show ID boxes |
|
|
236
|
+
| `showIndexBox` | `false` | show index boxes |
|
|
237
|
+
| `showTipBox` | `false` | show tips (below or as popup) |
|
|
238
|
+
| `color` | `var(--mat-sys-surface-container-low)` | text box fill |
|
|
239
|
+
| `textColor` | `var(--mat-sys-on-surface)` | text color |
|
|
240
|
+
| `borderColor` | `var(--mat-sys-outline-variant)` | text box border |
|
|
241
|
+
| `selectionColor` | `var(--mat-sys-primary)` | fill of selected segments |
|
|
242
|
+
| `selectionTextColor` | `var(--mat-sys-on-primary)` | text of selected segments |
|
|
243
|
+
| `resultColor` | `var(--mat-sys-tertiary-container)` | fill of search results |
|
|
244
|
+
| `resultTextColor` | `var(--mat-sys-on-tertiary-container)` | text of search results |
|
|
245
|
+
| `caretColor` | `var(--mat-sys-primary)` | outline of the segment with the caret |
|
|
246
|
+
| `fontFace`, `fontSize` | Roboto, `18px` | text font |
|
|
247
|
+
| `textRectHeight` | `28` | text box height (px) |
|
|
248
|
+
| `textRectMinWidth` | `10` | text box minimum width (px), to show e.g. spaces |
|
|
249
|
+
| `textRectPadding` | `3` | horizontal padding (px) around the text |
|
|
250
|
+
| `textRectRadius` | `4` | corner radius (px) of boxes |
|
|
251
|
+
| `textRectStrokeWidth` | `1` | text box border width (px) |
|
|
252
|
+
| `textRectStrokeDasharray` | - | text box border dash array, e.g. `4 2` |
|
|
253
|
+
| `infoFontFace` | Roboto Mono | ID and index boxes font family |
|
|
254
|
+
| `infoFontSize` | `10px` | ID and index boxes font size |
|
|
255
|
+
| `infoRectHeight` | `14` | ID and index boxes height (px) |
|
|
256
|
+
| `tipFontFace` | Roboto | tip font family |
|
|
257
|
+
| `tipFontSize` | `10px` | tip font size |
|
|
258
|
+
| `tipRectHeight` | `14` | tip box height (px) when below |
|
|
259
|
+
| `tipTemplate` | - | tip template (see below) |
|
|
260
|
+
| `tipFn` | - | tip function (see below) |
|
|
261
|
+
| `tipComponent` | - | tip component (see below) |
|
|
262
|
+
| `tipMaxLength` | `30` | max textual tip length, truncated with an ellipsis |
|
|
263
|
+
| `tipPosition` | `popup` | `below` the segment or as a `popup` on hover |
|
|
264
|
+
|
|
265
|
+
The selection color replaces the segment's own color while selected (and the result color replaces it while matching a search), without altering the segment data.
|
|
266
|
+
|
|
267
|
+
### Tips
|
|
268
|
+
|
|
269
|
+
The tip of a segment comes from the first available among:
|
|
270
|
+
|
|
271
|
+
1. `tipComponent`: a component rendering the tip, for a fully custom visualization. It gets the segment bound to its `segment` input (see `TextSegmentTipComponent`). When below, it is rendered in an SVG `foreignObject` of the segment's width and `tipRectHeight` height.
|
|
272
|
+
2. `tipFn`: a function returning the tip text from a segment.
|
|
273
|
+
3. `tipTemplate`: a template whose placeholders in `{{...}}` are dot-separated paths into the segment's payload, or `$id`, `$index`, `$value`, `$label` for the segment's own properties. Unresolved placeholders are removed, and the result is trimmed. For instance, with payload `{ "uni": { "category": "Ll" } }`, the template `cat.='{{uni.category}}' {{notdefined}}` gives `cat.='Ll'`.
|
|
274
|
+
4. the segment's own `tip`.
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
settings = {
|
|
278
|
+
showTipBox: true,
|
|
279
|
+
tipPosition: "popup",
|
|
280
|
+
tipTemplate: "{{code}} {{category}}",
|
|
281
|
+
};
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
@Component({
|
|
286
|
+
selector: "app-unicode-tip",
|
|
287
|
+
template: `{{ segment()?.payload?.code }}`,
|
|
288
|
+
})
|
|
289
|
+
export class UnicodeTipComponent {
|
|
290
|
+
public readonly segment = input<TextSegment<UnicodePayload>>();
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
settings = { showTipBox: true, tipComponent: UnicodeTipComponent };
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## User Interaction
|
|
297
|
+
|
|
298
|
+
With the pointer:
|
|
299
|
+
|
|
300
|
+
- **click** a segment to select it. Any click also emits `segmentClick`, even on segments which cannot be selected.
|
|
301
|
+
- **drag** to select all the segments from the start to the end of the drag; dragging near the top or bottom edge scrolls the text.
|
|
302
|
+
- **SHIFT+click** to extend the selection from the last clicked segment.
|
|
303
|
+
|
|
304
|
+
With the keyboard, when the text has focus (click it or reach it with TAB):
|
|
305
|
+
|
|
306
|
+
| keys | action |
|
|
307
|
+
| -------------------------- | ------------------------------------------------------- |
|
|
308
|
+
| ← → | move the caret to the previous/next selectable segment |
|
|
309
|
+
| SHIFT+← → | extend or collapse the selection |
|
|
310
|
+
| ↑ ↓ (+SHIFT) | move (or extend) to the same place in prev/next block |
|
|
311
|
+
| HOME/END (+SHIFT) | move (or extend) to the start/end of the block |
|
|
312
|
+
| CTRL+HOME/END (+SHIFT) | move (or extend) to the start/end of the text |
|
|
313
|
+
| CTRL+A | select all (up to the first blocked segment) |
|
|
314
|
+
| ESC | clear the selection |
|
|
315
|
+
|
|
316
|
+
These shortcuts are handled only while the text has focus, so they do not conflict with the browser's or the page's ones. ESC is not consumed when there is nothing to clear, so it can still close e.g. a containing dialog.
|
|
317
|
+
|
|
318
|
+
Every new selection replaces the previous one. The segment with the caret is outlined.
|
|
319
|
+
|
|
320
|
+
## Services and Utilities
|
|
321
|
+
|
|
322
|
+
### SegmentedTextStore
|
|
323
|
+
|
|
324
|
+
`SegmentedTextStore<T>` contains all the selection, navigation and search logic, independently from the DOM. Each `SegmentedTextView` provides its own instance (exposed by its `store` property), so you can have many viewers in a page. Its state is entirely signal-based:
|
|
325
|
+
|
|
326
|
+
- `blocks`, `segments` (flat list), `positions` (by ID).
|
|
327
|
+
- `selectedIds`, `selectedSet`, `selectedSegments`, `caretId`.
|
|
328
|
+
- `find` (writable find options), `matches`, `hitSet`, `currentMatch`.
|
|
329
|
+
|
|
330
|
+
Its actions return `true` when they changed the selection: `setBlocks`, `startAt(id, extend)`, `extendTo(id)`, `moveCaret(dir, extend)`, `moveBlock(dir, extend)`, `moveToBlockEdge(edge, extend)`, `moveToTextEdge(edge, extend)`, `selectAll()`, `clear()`, `setSelectedIds(ids)`, and `goToMatch(dir)`.
|
|
331
|
+
|
|
332
|
+
As it has no dependencies, you can also use it outside of the viewer, e.g. to build a different UI.
|
|
333
|
+
|
|
334
|
+
### TextMeasureService
|
|
335
|
+
|
|
336
|
+
Measures the width of text in a given font (via an offscreen canvas, falling back to an estimate where no canvas is available), to size the SVG boxes. Measures are cached; when web fonts finish loading, the cache is cleared and its `version` signal is incremented, so that segments are re-measured.
|
|
337
|
+
|
|
338
|
+
### Pure Functions
|
|
339
|
+
|
|
340
|
+
- `resolveSegmentRange(segments, anchor, focus)`: the core selection rules (see [Selection](#selection)).
|
|
341
|
+
- `findTextSegments(blocks, options)`: the finder.
|
|
342
|
+
- `fillTipTemplate(template, segment)`, `resolveSegmentTip(segment, settings)`: tips.
|
|
343
|
+
|
|
344
|
+
## Writing a Segmenter
|
|
345
|
+
|
|
346
|
+
To write your own segmenter, implement `TextSegmenter<T>`:
|
|
347
|
+
|
|
348
|
+
1. `getBlocks`: split the text into blocks and segments. Assign unique IDs, set `selectionMode` for segments which must not start/end a selection (`wrapped`, `attached`) or must not be crossed (`blocked`), set labels for invisible values, and attach any metadata as `payload` (and colors, if you want to visualize it).
|
|
349
|
+
2. `getSegmentCoords`: map the selected segments to coordinates in your syntax, using the blocks for context. Return an empty string when there are no coordinates for them.
|
|
350
|
+
3. `getCoordsSegments`: parse coordinates and return the corresponding segments in text order, or `null` if they are invalid.
|
|
351
|
+
|
|
352
|
+
As the blocks of a text do not change while it is displayed, a segmenter can cache any index it builds from them (e.g. in a `WeakMap` keyed by the blocks array, as `SpacedTextSegmenter` does).
|
|
353
|
+
|
|
354
|
+
You can also post-process the segments returned by a segmenter, e.g. to set `selectionMode`, colors or CSS classes according to other data, as long as you create new segment objects rather than mutating them.
|
|
355
|
+
|
|
356
|
+
## SpacedTextSegmenter
|
|
357
|
+
|
|
358
|
+
A sample segmenter where:
|
|
359
|
+
|
|
360
|
+
- each character (code point) is a segment, including spaces. CR is ignored, and LF ends a block (and is included as the last segment of its block only if `includeLf` is true). Ignored characters still count for IDs and indexes.
|
|
361
|
+
- segment IDs are character ordinals (1-N); indexes are UTF-16 offsets in the source text, so that `text.substring(index, index + value.length) === value`.
|
|
362
|
+
- ASCII control characters get labels from the Unicode Control Pictures block (e.g. `␉` for TAB, `␊` for LF); space gets the `spaceLabel` (default `_`).
|
|
363
|
+
- whitespace characters are `wrapped`; Unicode combining characters are `wrapped` too, or `attached` if `combiningMode` is `attached`.
|
|
364
|
+
- an optional `payloadFactory(char, id, index)` builds the payload of each segment.
|
|
365
|
+
|
|
366
|
+
Its coordinates are token-based, a token being any sequence of non-whitespace characters, and optionally character-based within tokens. A point is `Y.X@AxR`:
|
|
367
|
+
|
|
368
|
+
- `Y`: block ordinal (1-N).
|
|
369
|
+
- `X`: token ordinal in the block (1-N).
|
|
370
|
+
- optionally, if not selecting whole tokens: `A` is the ordinal of the first character in the token (1-N), and `R` the count of characters (1-N; omitted when 1, so `1.2@3` is equal to `1.2@3x1`).
|
|
371
|
+
|
|
372
|
+
A selection within a single token has a single point. A selection spanning several tokens has a start and an end point, separated by `-`, both inclusive: the start point picks from its `A` to the end of its token, the end point picks from the start of its token to `A+R-1`. Whitespace between tokens is included, while whitespace at the edges of a selection is ignored.
|
|
373
|
+
|
|
374
|
+
For instance, with this text:
|
|
375
|
+
|
|
376
|
+
```txt
|
|
377
|
+
alpha beta
|
|
378
|
+
gamma
|
|
379
|
+
delta epsilon waw
|
|
380
|
+
zeta
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
- `2.1` = `gamma`.
|
|
384
|
+
- `1.2@2x2` = `et` (of `beta`).
|
|
385
|
+
- `3.1@4x2-3.2@1` = `ta e` (`ta` of `delta`, `e` of `epsilon`).
|
|
386
|
+
- `1.2@4-4.1@1x2` = `a gamma delta epsilon waw ze` (from the 4th character of `beta` to the first two of `zeta`).
|
|
387
|
+
|
|
388
|
+
## Theming
|
|
389
|
+
|
|
390
|
+
The components use only Angular Material M3 system tokens (`--mat-sys-*`), so they follow your theme, including dark mode. All the colors in settings are CSS colors, so you can use either tokens (`var(--mat-sys-...)`) or literal colors there. The caret outline color is also exposed as the CSS variable `--cadmus-tseg-caret-color` on the viewer's host.
|
|
391
|
+
|
|
392
|
+
## Performance
|
|
393
|
+
|
|
394
|
+
The viewer is designed for fluid updates with large texts:
|
|
395
|
+
|
|
396
|
+
- all the components are `OnPush` and signal-based, and zoneless-ready.
|
|
397
|
+
- segments are tracked by ID, and their selection, search and caret states are plain boolean inputs, so changing the selection re-renders only the segments whose state changed.
|
|
398
|
+
- pointer events are handled by a single set of listeners on the viewer (event delegation), not per segment.
|
|
399
|
+
- text widths are measured once per label and font, and cached.
|
|
400
|
+
- the tip popup is a single shared overlay.
|
|
401
|
+
- blocks out of the viewport skip layout and paint (`content-visibility: auto`).
|
|
402
|
+
- zoom uses CSS `zoom`, so it does not re-render segments.
|
|
403
|
+
|
|
404
|
+
## History
|
|
405
|
+
|
|
406
|
+
### 0.0.1
|
|
407
|
+
|
|
408
|
+
- 2026-11-19:
|
|
409
|
+
- added `segmentClick` output to `SegmentedTextView`, emitted for any clicked segment, including `blocked` ones.
|
|
410
|
+
- added `wrapMode` setting to wrap lines by `word` rather than by `segment`.
|
|
411
|
+
- 2026-11-13: initial release.
|