Debian Codegraph @

packages/documentsearch-extension/src/index.ts

raw
1 import {
2 JupyterFrontEnd,
3 JupyterFrontEndPlugin,
4 } from '@jupyterlab/application';
5
6 import { ISearchProviderRegistry } from '@jupyterlab/documentsearch';
7
8 import { Widget } from '@lumino/widgets';
9
10 import { INotebookShell } from '@jupyter-notebook/application';
11
12 const SEARCHABLE_CLASS = 'jp-mod-searchable';
13
14 /**
15 * A plugin to add document search functionalities.
16 */
17 const notebookShellWidgetListener: JupyterFrontEndPlugin<void> = {
18 id: '@jupyter-notebook/documentsearch-extension:notebookShellWidgetListener',
19 requires: [INotebookShell, ISearchProviderRegistry],
20 autoStart: true,
21 activate: (
22 app: JupyterFrontEnd,
23 notebookShell: INotebookShell,
24 registry: ISearchProviderRegistry
25 ) => {
26 // If a given widget is searchable, apply the searchable class.
27 // If it's not searchable, remove the class.
28 const transformWidgetSearchability = (widget: Widget | null) => {
29 if (!widget) {
30 return;
31 }
32 if (registry.hasProvider(widget)) {
33 widget.addClass(SEARCHABLE_CLASS);
34 } else {
35 widget.removeClass(SEARCHABLE_CLASS);
36 }
37 };
38
39 // Update searchability of the active widget when the registry
40 // changes, in case a provider for the current widget was added
41 // or removed
42 registry.changed.connect(() =>
43 transformWidgetSearchability(notebookShell.currentWidget)
44 );
45
46 // Apply the searchable class only to the active widget if it is actually
47 // searchable. Remove the searchable class from a widget when it's
48 // no longer active.
49 notebookShell.currentChanged.connect((_, args) => {
50 if (notebookShell.currentWidget) {
51 transformWidgetSearchability(notebookShell.currentWidget);
52 }
53 });
54 },
55 };
56
57 /**
58 * Export the plugins as default.
59 */
60 const plugins: JupyterFrontEndPlugin<any>[] = [notebookShellWidgetListener];
61
62 export default plugins;