searchproviderregistry.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ISearchProvider, ISearchProviderConstructor } from './interfaces';
  4. import { CodeMirrorSearchProvider } from './providers/codemirrorsearchprovider';
  5. import { NotebookSearchProvider } from './providers/notebooksearchprovider';
  6. import { Token } from '@phosphor/coreutils';
  7. import { Widget } from '@phosphor/widgets';
  8. import { IDisposable, DisposableDelegate } from '@phosphor/disposable';
  9. import { ISignal, Signal } from '@phosphor/signaling';
  10. /* tslint:disable */
  11. /**
  12. * The search provider registry token.
  13. */
  14. export const ISearchProviderRegistry = new Token<ISearchProviderRegistry>(
  15. '@jupyterlab/documentsearch:ISearchProviderRegistry'
  16. );
  17. /* tslint:enable */
  18. export interface ISearchProviderRegistry {
  19. /**
  20. * Add a provider to the registry.
  21. *
  22. * @param key - The provider key.
  23. */
  24. register(key: string, provider: ISearchProviderConstructor): IDisposable;
  25. /**
  26. * Returns a matching provider for the widget.
  27. *
  28. * @param widget - The widget to search over.
  29. * @returns the search provider, or undefined if none exists.
  30. */
  31. getProviderForWidget(widget: any): ISearchProvider | undefined;
  32. /**
  33. * Signal that emits when a new search provider has been registered
  34. * or removed.
  35. */
  36. changed: ISignal<ISearchProviderRegistry, void>;
  37. }
  38. export class SearchProviderRegistry implements ISearchProviderRegistry {
  39. constructor() {
  40. this._registerDefaultProviders(
  41. 'jl-defaultNotebookSearchProvider',
  42. NotebookSearchProvider
  43. );
  44. this._registerDefaultProviders(
  45. 'jl-defaultCodeMirrorSearchProvider',
  46. CodeMirrorSearchProvider
  47. );
  48. }
  49. /**
  50. * Add a provider to the registry.
  51. *
  52. * @param key - The provider key.
  53. */
  54. register(key: string, provider: ISearchProviderConstructor): IDisposable {
  55. this._customProviders.set(key, provider);
  56. this._changed.emit();
  57. return new DisposableDelegate(() => {
  58. this._customProviders.delete(key);
  59. this._changed.emit();
  60. });
  61. }
  62. /**
  63. * Returns a matching provider for the widget.
  64. *
  65. * @param widget - The widget to search over.
  66. * @returns the search provider, or undefined if none exists.
  67. */
  68. getProviderForWidget(widget: Widget): ISearchProvider | undefined {
  69. return (
  70. this._findMatchingProvider(this._customProviders, widget) ||
  71. this._findMatchingProvider(this._defaultProviders, widget)
  72. );
  73. }
  74. get changed(): ISignal<this, void> {
  75. return this._changed;
  76. }
  77. private _registerDefaultProviders(
  78. key: string,
  79. provider: ISearchProviderConstructor
  80. ): void {
  81. this._defaultProviders.set(key, provider);
  82. }
  83. private _findMatchingProvider(
  84. providerMap: Private.ProviderMap,
  85. widget: Widget
  86. ): ISearchProvider | undefined {
  87. // iterate through all providers and ask each one if it can search on the
  88. // widget.
  89. for (let P of providerMap.values()) {
  90. if (P.canSearchOn(widget)) {
  91. return new P();
  92. }
  93. }
  94. return undefined;
  95. }
  96. private _changed = new Signal<this, void>(this);
  97. private _defaultProviders: Private.ProviderMap = new Map<
  98. string,
  99. ISearchProviderConstructor
  100. >();
  101. private _customProviders: Private.ProviderMap = new Map<
  102. string,
  103. ISearchProviderConstructor
  104. >();
  105. }
  106. namespace Private {
  107. export type ProviderMap = Map<string, ISearchProviderConstructor>;
  108. }