forked from jupyterlab/jupyterlab
/
searchinstance.ts
211 lines (182 loc) · 5.79 KB
/
searchinstance.ts
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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IDisplayState, ISearchProvider } from './interfaces';
import { createSearchOverlay } from './searchoverlay';
import { MainAreaWidget } from '@jupyterlab/apputils';
import { IDisposable } from '@phosphor/disposable';
import { ISignal, Signal } from '@phosphor/signaling';
import { Widget } from '@phosphor/widgets';
/**
* Represents a search on a single widget.
*/
export class SearchInstance implements IDisposable {
constructor(widget: Widget, searchProvider: ISearchProvider) {
this._widget = widget;
this._activeProvider = searchProvider;
const initialQuery = this._activeProvider.getInitialQuery(this._widget);
this._displayState.searchText = initialQuery || '';
this._searchWidget = createSearchOverlay({
widgetChanged: this._displayUpdateSignal,
overlayState: this._displayState,
onCaseSensitiveToggled: this._onCaseSensitiveToggled.bind(this),
onRegexToggled: this._onRegexToggled.bind(this),
onHightlightNext: this._highlightNext.bind(this),
onHighlightPrevious: this._highlightPrevious.bind(this),
onStartQuery: this._startQuery.bind(this),
onReplaceCurrent: this._replaceCurrent.bind(this),
onReplaceAll: this._replaceAll.bind(this),
onEndSearch: this.dispose.bind(this),
isReadOnly: this._activeProvider.isReadOnly
});
this._widget.disposed.connect(() => {
this.dispose();
});
this._searchWidget.disposed.connect(() => {
this.dispose();
});
// TODO: this does not update if the toolbar changes height.
if (this._widget instanceof MainAreaWidget) {
// Offset the position of the search widget to not cover the toolbar.
this._searchWidget.node.style.top = `${
this._widget.toolbar.node.clientHeight
}px`;
}
this._displaySearchWidget();
}
/**
* The search widget.
*/
get searchWidget() {
return this._searchWidget;
}
/**
* The search provider.
*/
get provider() {
return this._activeProvider;
}
/**
* Focus the search widget input.
*/
focusInput(): void {
this._displayState.forceFocus = true;
// Trigger a rerender without resetting the forceFocus.
this._displayUpdateSignal.emit(this._displayState);
}
/**
* Updates the match index and total display in the search widget.
*/
updateIndices(): void {
this._displayState.totalMatches = this._activeProvider.matches.length;
this._displayState.currentIndex = this._activeProvider.currentMatchIndex;
this._updateDisplay();
}
private _updateDisplay() {
// Reset the focus attribute to make sure we don't steal focus.
this._displayState.forceFocus = false;
// Trigger a rerender
this._displayUpdateSignal.emit(this._displayState);
}
private async _startQuery(query: RegExp) {
// save the last query (or set it to the current query if this is the first)
if (this._activeProvider && this._displayState.query) {
await this._activeProvider.endQuery();
}
this._displayState.query = query;
await this._activeProvider.startQuery(query, this._widget);
this.updateIndices();
// this signal should get injected when the widget is
// created and hooked up to react!
this._activeProvider.changed.connect(this.updateIndices, this);
}
private async _replaceCurrent(newText: string) {
if (this._activeProvider && this._displayState.query && !!newText) {
await this._activeProvider.replaceCurrentMatch(newText);
this.updateIndices();
}
}
private async _replaceAll(newText: string) {
if (this._activeProvider && this._displayState.query && !!newText) {
await this._activeProvider.replaceAllMatches(newText);
this.updateIndices();
}
}
/**
* Dispose of the resources held by the search instance.
*/
dispose() {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
// If a query hasn't been executed yet, no need to call endSearch
if (this._displayState.query) {
void this._activeProvider.endSearch();
}
this._searchWidget.dispose();
this._disposed.emit(undefined);
Signal.clearData(this);
}
/**
* Test if the object has been disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* A signal emitted when the object is disposed.
*/
get disposed(): ISignal<this, void> {
return this._disposed;
}
/**
* Display search widget.
*/
_displaySearchWidget() {
if (!this._searchWidget.isAttached) {
Widget.attach(this._searchWidget, this._widget.node);
}
}
private async _highlightNext() {
if (!this._displayState.query) {
return;
}
await this._activeProvider.highlightNext();
this.updateIndices();
}
private async _highlightPrevious() {
if (!this._displayState.query) {
return;
}
await this._activeProvider.highlightPrevious();
this.updateIndices();
}
private _onCaseSensitiveToggled() {
this._displayState.caseSensitive = !this._displayState.caseSensitive;
this._updateDisplay();
}
private _onRegexToggled() {
this._displayState.useRegex = !this._displayState.useRegex;
this._updateDisplay();
}
private _widget: Widget;
private _displayState: IDisplayState = {
currentIndex: 0,
totalMatches: 0,
caseSensitive: false,
useRegex: false,
searchText: '',
query: null,
errorMessage: '',
searchInputFocused: true,
replaceInputFocused: false,
forceFocus: true,
replaceText: '',
replaceEntryShown: false
};
private _displayUpdateSignal = new Signal<this, IDisplayState>(this);
private _activeProvider: ISearchProvider;
private _searchWidget: Widget;
private _isDisposed = false;
private _disposed = new Signal<this, void>(this);
}