Parcourir la source

Merge pull request #6282 from aschlaep/remove-searchprovider-deregister

Remove deregisterProvider from SearchProviderRegistry interface
Steven Silvester il y a 6 ans
Parent
commit
a6466065df

+ 2 - 2
packages/csvviewer-extension/src/index.ts

@@ -139,7 +139,7 @@ function activateCsv(
 
   addMenuEntries(mainMenu, tracker);
   if (searchregistry) {
-    searchregistry.registerProvider('csv', CSVSearchProvider);
+    searchregistry.register('csv', CSVSearchProvider);
   }
 }
 
@@ -209,7 +209,7 @@ function activateTsv(
 
   addMenuEntries(mainMenu, tracker);
   if (searchregistry) {
-    searchregistry.registerProvider('tsv', CSVSearchProvider);
+    searchregistry.register('tsv', CSVSearchProvider);
   }
 }
 

+ 2 - 1
packages/documentsearch-extension/package.json

@@ -32,7 +32,8 @@
     "@jupyterlab/application": "^1.0.0-alpha.6",
     "@jupyterlab/apputils": "^1.0.0-alpha.6",
     "@jupyterlab/documentsearch": "^1.0.0-alpha.7",
-    "@jupyterlab/mainmenu": "^1.0.0-alpha.6"
+    "@jupyterlab/mainmenu": "^1.0.0-alpha.6",
+    "@phosphor/widgets": "^1.6.0"
   },
   "devDependencies": {
     "rimraf": "~2.6.2",

+ 27 - 4
packages/documentsearch-extension/src/index.ts

@@ -16,6 +16,7 @@ import {
 } from '@jupyterlab/documentsearch';
 
 import { IMainMenu } from '@jupyterlab/mainmenu';
+import { Widget } from '@phosphor/widgets';
 
 const SEARCHABLE_CLASS = 'jp-mod-searchable';
 
@@ -28,15 +29,37 @@ const labShellWidgetListener: JupyterFrontEndPlugin<void> = {
     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;
-      const newWidget = args.newValue;
-      if (newWidget && registry.getProviderForWidget(newWidget) !== undefined) {
-        newWidget.addClass(SEARCHABLE_CLASS);
-      }
       if (oldWidget) {
         oldWidget.removeClass(SEARCHABLE_CLASS);
       }
+      transformWidgetSearchability(args.newValue);
     });
   }
 };

+ 26 - 20
packages/documentsearch/src/searchproviderregistry.ts

@@ -6,6 +6,8 @@ import { NotebookSearchProvider } from './providers/notebooksearchprovider';
 
 import { Token } from '@phosphor/coreutils';
 import { Widget } from '@phosphor/widgets';
+import { IDisposable, DisposableDelegate } from '@phosphor/disposable';
+import { ISignal, Signal } from '@phosphor/signaling';
 
 /* tslint:disable */
 /**
@@ -21,16 +23,9 @@ export interface ISearchProviderRegistry {
    * Add a provider to the registry.
    *
    * @param key - The provider key.
+   * @returns A disposable delegate that, when disposed, deregisters the given search provider
    */
-  registerProvider(key: string, provider: ISearchProviderConstructor): void;
-
-  /**
-   * Remove provider from registry.
-   *
-   * @param key - The provider key.
-   * @returns true if removed, false if key did not exist in map.
-   */
-  deregisterProvider(key: string): boolean;
+  register(key: string, provider: ISearchProviderConstructor): IDisposable;
 
   /**
    * Returns a matching provider for the widget.
@@ -39,6 +34,12 @@ export interface ISearchProviderRegistry {
    * @returns the search provider, or undefined if none exists.
    */
   getProviderForWidget(widget: any): ISearchProvider | undefined;
+
+  /**
+   * Signal that emits when a new search provider has been registered
+   * or removed.
+   */
+  changed: ISignal<ISearchProviderRegistry, void>;
 }
 
 export class SearchProviderRegistry implements ISearchProviderRegistry {
@@ -57,19 +58,15 @@ export class SearchProviderRegistry implements ISearchProviderRegistry {
    * Add a provider to the registry.
    *
    * @param key - The provider key.
+   * @returns A disposable delegate that, when disposed, deregisters the given search provider
    */
-  registerProvider(key: string, provider: ISearchProviderConstructor): void {
+  register(key: string, provider: ISearchProviderConstructor): IDisposable {
     this._customProviders.set(key, provider);
-  }
-
-  /**
-   * Remove provider from registry.
-   *
-   * @param key - The provider key.
-   * @returns true if removed, false if key did not exist in map.
-   */
-  deregisterProvider(key: string): boolean {
-    return this._customProviders.delete(key);
+    this._changed.emit();
+    return new DisposableDelegate(() => {
+      this._customProviders.delete(key);
+      this._changed.emit();
+    });
   }
 
   /**
@@ -85,6 +82,14 @@ export class SearchProviderRegistry implements ISearchProviderRegistry {
     );
   }
 
+  /**
+   * Signal that emits when a new search provider has been registered
+   * or removed.
+   */
+  get changed(): ISignal<this, void> {
+    return this._changed;
+  }
+
   private _registerDefaultProviders(
     key: string,
     provider: ISearchProviderConstructor
@@ -106,6 +111,7 @@ export class SearchProviderRegistry implements ISearchProviderRegistry {
     return undefined;
   }
 
+  private _changed = new Signal<this, void>(this);
   private _defaultProviders: Private.ProviderMap = new Map<
     string,
     ISearchProviderConstructor