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 description: 'A plugin to add document search functionalities',
22 activate: (
23 app: JupyterFrontEnd,
24 notebookShell: INotebookShell,
25 registry: ISearchProviderRegistry
26 ) => {
27 // If a given widget is searchable, apply the searchable class.
28 // If it's not searchable, remove the class.
29 const transformWidgetSearchability = (widget: Widget | null) => {
30 if (!widget) {
31 return;
32 }
33 if (registry.hasProvider(widget)) {
34 widget.addClass(SEARCHABLE_CLASS);
35 } else {
36 widget.removeClass(SEARCHABLE_CLASS);
37 }
38 };
39
40 // Update searchability of the active widget when the registry
41 // changes, in case a provider for the current widget was added
42 // or removed
43 registry.changed.connect(() =>
44 transformWidgetSearchability(notebookShell.currentWidget)
45 );
46
47 // Apply the searchable class only to the active widget if it is actually
48 // searchable. Remove the searchable class from a widget when it's
49 // no longer active.
50 notebookShell.currentChanged.connect((_, args) => {
51 if (notebookShell.currentWidget) {
52 transformWidgetSearchability(notebookShell.currentWidget);
53 }
54 });
55 },
56 };
57
58 /**
59 * Export the plugins as default.
60 */
61 const plugins: JupyterFrontEndPlugin<any>[] = [notebookShellWidgetListener];
62
63 export default plugins;