forked from jupyterlab/jupyterlab
/
index.ts
202 lines (181 loc) · 6.01 KB
/
index.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
JupyterFrontEnd,
JupyterFrontEndPlugin,
ILabShell
} from '@jupyterlab/application';
import { ICommandPalette } from '@jupyterlab/apputils';
import {
ISearchProviderRegistry,
SearchInstance,
SearchProviderRegistry,
CodeMirrorSearchProvider,
NotebookSearchProvider
} from '@jupyterlab/documentsearch';
import { IMainMenu } from '@jupyterlab/mainmenu';
import { Widget } from '@phosphor/widgets';
const SEARCHABLE_CLASS = 'jp-mod-searchable';
const labShellWidgetListener: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab/documentsearch:labShellWidgetListener',
requires: [ILabShell, ISearchProviderRegistry],
autoStart: true,
activate: (
app: JupyterFrontEnd,
labShell: ILabShell,
registry: ISearchProviderRegistry
) => {
// If a given widget is searchable, apply the searchable class.
// If it's not searchable, remove the class.
const transformWidgetSearchability = (widget: Widget) => {
if (!widget) {
return;
}
const providerForWidget = registry.getProviderForWidget(widget);
if (providerForWidget) {
widget.addClass(SEARCHABLE_CLASS);
}
if (!providerForWidget) {
widget.removeClass(SEARCHABLE_CLASS);
}
};
// Update searchability of the active widget when the registry
// changes, in case a provider for the current widget was added
// or removed
registry.changed.connect(() =>
transformWidgetSearchability(labShell.activeWidget)
);
// Apply the searchable class only to the active widget if it is actually
// searchable. Remove the searchable class from a widget when it's
// no longer active.
labShell.activeChanged.connect((_, args) => {
const oldWidget = args.oldValue;
if (oldWidget) {
oldWidget.removeClass(SEARCHABLE_CLASS);
}
transformWidgetSearchability(args.newValue);
});
}
};
/**
* Initialization data for the document-search extension.
*/
const extension: JupyterFrontEndPlugin<ISearchProviderRegistry> = {
id: '@jupyterlab/documentsearch:plugin',
provides: ISearchProviderRegistry,
optional: [ICommandPalette, IMainMenu],
autoStart: true,
activate: (
app: JupyterFrontEnd,
palette: ICommandPalette,
mainMenu: IMainMenu | null
) => {
// Create registry, retrieve all default providers
const registry: SearchProviderRegistry = new SearchProviderRegistry();
// Register default implementations of the Notebook and CodeMirror search providers
registry.register('jp-notebookSearchProvider', NotebookSearchProvider);
registry.register('jp-codeMirrorSearchProvider', CodeMirrorSearchProvider);
const activeSearches = new Map<string, SearchInstance>();
const startCommand: string = 'documentsearch:start';
const nextCommand: string = 'documentsearch:highlightNext';
const prevCommand: string = 'documentsearch:highlightPrevious';
app.commands.addCommand(startCommand, {
label: 'Find…',
isEnabled: () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return;
}
return registry.getProviderForWidget(currentWidget) !== undefined;
},
execute: () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return;
}
const widgetId = currentWidget.id;
let searchInstance = activeSearches.get(widgetId);
if (!searchInstance) {
const searchProvider = registry.getProviderForWidget(currentWidget);
if (!searchProvider) {
return;
}
searchInstance = new SearchInstance(currentWidget, searchProvider);
activeSearches.set(widgetId, searchInstance);
// find next and previous are now enabled
app.commands.notifyCommandChanged();
searchInstance.disposed.connect(() => {
activeSearches.delete(widgetId);
// find next and previous are now not enabled
app.commands.notifyCommandChanged();
});
}
searchInstance.focusInput();
}
});
app.commands.addCommand(nextCommand, {
label: 'Find Next',
isEnabled: () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return;
}
return activeSearches.has(currentWidget.id);
},
execute: async () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return;
}
const instance = activeSearches.get(currentWidget.id);
if (!instance) {
return;
}
await instance.provider.highlightNext();
instance.updateIndices();
}
});
app.commands.addCommand(prevCommand, {
label: 'Find Previous',
isEnabled: () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return;
}
return activeSearches.has(currentWidget.id);
},
execute: async () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return;
}
const instance = activeSearches.get(currentWidget.id);
if (!instance) {
return;
}
await instance.provider.highlightPrevious();
instance.updateIndices();
}
});
// Add the command to the palette.
if (palette) {
palette.addItem({ command: startCommand, category: 'Main Area' });
palette.addItem({ command: nextCommand, category: 'Main Area' });
palette.addItem({ command: prevCommand, category: 'Main Area' });
}
// Add main menu notebook menu.
if (mainMenu) {
mainMenu.editMenu.addGroup(
[
{ command: startCommand },
{ command: nextCommand },
{ command: prevCommand }
],
10
);
}
// Provide the registry to the system.
return registry;
}
};
export default [extension, labShellWidgetListener];