-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathservice.ts
More file actions
247 lines (200 loc) · 6.03 KB
/
Copy pathservice.ts
File metadata and controls
247 lines (200 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { isServer, type ReactiveController } from 'lit';
import {
escapeRegex,
iterNodes,
nanoid,
scrollIntoView,
wrap,
} from '../common/util.js';
import type IgcHighlightComponent from './highlight.js';
type Match = { node: Node; indices: [start: number, end: number] };
/* jsonAPIPlainObject */
/* marshalByValue */
/**
* Options for controlling navigation behavior when moving the active highlight.
*/
export interface HighlightNavigation {
/** If true, prevents the component from scrolling the new active match into view. */
preventScroll?: boolean;
}
function* matchText(
nodes: IterableIterator<Node>,
regexp: RegExp
): Generator<Match> {
for (const node of nodes) {
if (node.textContent) {
for (const match of node.textContent.matchAll(regexp)) {
const [start, end] = match.indices![0]!;
yield { node, indices: [start, end] } satisfies Match;
}
}
}
}
class HighlightService implements ReactiveController {
//#region Private properties
private readonly _host: IgcHighlightComponent;
private readonly _id: string;
private readonly _activeId: string;
private readonly _styles: string;
private readonly _styleSheet?: CSSStyleSheet;
private _highlight!: Highlight;
private _activeHighlight!: Highlight;
private _ranges: Range[] = [];
private _current = 0;
//#endregion
//#region Public properties
/**
* The total number of matches found in the component's content.
*/
public get size(): number {
return this._ranges.length;
}
/**
* The index of the currently active match. Returns 0 if there are no matches.
*/
public get current(): number {
return this._current;
}
//#endregion
constructor(host: IgcHighlightComponent) {
this._host = host;
this._host.addController(this);
this._id = `igc-highlight-${nanoid()}`;
this._activeId = `${this._id}-active`;
this._styles = `
::highlight(${this._id}) {
background-color: var(--background, var(--ig-secondary-700));
color: var(--foreground, var(--ig-secondary-700-contrast));
}
::highlight(${this._activeId}) {
background-color: var(--background-active, var(--ig-primary-500));
color: var(--foreground-active, var(--ig-primary-500-contrast));
}`;
if (!isServer) {
this._styleSheet = new CSSStyleSheet();
this._styleSheet.replaceSync(this._styles);
}
}
//#region Controller life-cycle
/** @internal */
public hostConnected(): void {
this._createHighlightEntries();
this.find(this._host.searchText);
}
/** @internal */
public hostDisconnected(): void {
this._removeHighlightEntries();
this._removeStyleSheet();
}
//#endregion
//#region Private methods
private _removeStyleSheet(): void {
const root = this._host.renderRoot as ShadowRoot;
if (this._styleSheet) {
root.adoptedStyleSheets = root.adoptedStyleSheets.filter(
(sheet) => sheet !== this._styleSheet
);
}
}
private _createHighlightEntries(): void {
this._highlight = new Highlight();
this._highlight.priority = 0;
this._activeHighlight = new Highlight();
this._activeHighlight.priority = 1;
CSS.highlights.set(this._id, this._highlight);
CSS.highlights.set(this._activeId, this._activeHighlight);
}
private _removeHighlightEntries(): void {
CSS.highlights.delete(this._id);
CSS.highlights.delete(this._activeId);
this._ranges.length = 0;
}
private _createRegex(value: string): RegExp {
return new RegExp(
escapeRegex(value),
this._host.caseSensitive ? 'dg' : 'dgi'
);
}
private _updateActiveHighlight(): void {
if (this.size) {
this._activeHighlight.clear();
this._activeHighlight.add(this._ranges[this._current]);
}
}
private _goTo(index: number, options?: HighlightNavigation): void {
if (!this.size) {
return;
}
this._current = wrap(0, this.size - 1, index);
const range = this._ranges[this._current];
this._activeHighlight.clear();
this._activeHighlight.add(range);
if (!options?.preventScroll) {
scrollIntoView(range.commonAncestorContainer.parentElement, {
block: 'center',
inline: 'center',
});
}
}
//#endregion
//#region Public methods
/**
* Attaches the service's stylesheet to the render root.
* Necessary for the component to apply highlight styles to its content.
*/
public attachStylesheet(): void {
const adoptedSheets = (this._host.renderRoot as ShadowRoot)
.adoptedStyleSheets;
if (this._styleSheet && !adoptedSheets.includes(this._styleSheet)) {
adoptedSheets.push(this._styleSheet);
}
}
/**
* Finds matches for the given search text in the component's content and creates highlight ranges for them.
*/
public find(value: string): void {
if (!value?.trim()) {
return;
}
const iterator = matchText(
iterNodes(this._host, { show: 'SHOW_TEXT' }),
this._createRegex(value)
);
for (const {
node,
indices: [start, end],
} of iterator) {
const range = new Range();
range.setStart(node, start);
range.setEnd(node, end);
this._ranges.push(range);
this._highlight.add(range);
}
this._updateActiveHighlight();
}
/** Moves the active state to the next match. */
public next(options?: HighlightNavigation): void {
this._goTo(this._current + 1, options);
}
/** Moves the active state to the previous match. */
public previous(options?: HighlightNavigation): void {
this._goTo(this._current - 1, options);
}
/** Moves the active state to the given index. */
public setActive(index: number, options?: HighlightNavigation): void {
this._goTo(index, options);
}
/** Clears highlight collections. */
public clear(): void {
this._activeHighlight.clear();
this._highlight.clear();
this._ranges.length = 0;
this._current = 0;
}
//#endregion
}
export function createHighlightController(
host: IgcHighlightComponent
): HighlightService {
return new HighlightService(host);
}