forked from jupyterlab/jupyterlab
/
index.ts
165 lines (146 loc) · 4.66 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
// 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
} from '@jupyterlab/documentsearch';
import { IMainMenu } from '@jupyterlab/mainmenu';
const SEARCHABLE_CLASS = 'jp-mod-searchable';
/**
* Initialization data for the document-search extension.
*/
const extension: JupyterFrontEndPlugin<ISearchProviderRegistry> = {
id: '@jupyterlab/documentsearch:plugin',
provides: ISearchProviderRegistry,
requires: [ICommandPalette, ILabShell],
optional: [IMainMenu],
autoStart: true,
activate: (
app: JupyterFrontEnd,
palette: ICommandPalette,
labShell: ILabShell,
mainMenu: IMainMenu | null
) => {
// Create registry, retrieve all default providers
const registry: SearchProviderRegistry = new SearchProviderRegistry();
// TODO: Should register the default providers, with an application-specific
// enabler.
const activeSearches = new Map<string, SearchInstance>();
labShell.activeChanged.connect((_, args) => {
const newWidget = args.newValue;
if (
newWidget &&
!newWidget.hasClass(SEARCHABLE_CLASS) &&
registry.getProviderForWidget(newWidget) !== undefined
) {
newWidget.addClass(SEARCHABLE_CLASS);
}
});
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.
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;