index.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import '../style/index.css';
  4. import { SearchProviderRegistry } from './searchproviderregistry';
  5. import { SearchInstance } from './searchinstance';
  6. import { JupyterLab, JupyterLabPlugin } from '@jupyterlab/application';
  7. import { ICommandPalette } from '@jupyterlab/apputils';
  8. import { ISignal } from '@phosphor/signaling';
  9. import { Widget } from '@phosphor/widgets';
  10. export interface ISearchMatch {
  11. /**
  12. * Text of the exact match itself
  13. */
  14. readonly text: string;
  15. /**
  16. * Fragment containing match
  17. */
  18. readonly fragment: string;
  19. /**
  20. * Line number of match
  21. */
  22. line: number;
  23. /**
  24. * Column location of match
  25. */
  26. column: number;
  27. /**
  28. * Index among the other matches
  29. */
  30. index: number;
  31. }
  32. /**
  33. * This interface is meant to enforce that SearchProviders implement the static
  34. * canSearchOn function.
  35. */
  36. export interface ISearchProviderConstructor {
  37. new (): ISearchProvider;
  38. /**
  39. * Report whether or not this provider has the ability to search on the given object
  40. */
  41. canSearchOn(domain: any): boolean;
  42. }
  43. export interface ISearchProvider {
  44. /**
  45. * Initialize the search using the provided options. Should update the UI
  46. * to highlight all matches and "select" whatever the first match should be.
  47. *
  48. * @param query A RegExp to be use to perform the search
  49. * @param searchTarget The widget to be searched
  50. *
  51. * @returns A promise that resolves with a list of all matches
  52. */
  53. startSearch(query: RegExp, searchTarget: any): Promise<ISearchMatch[]>;
  54. /**
  55. * Resets UI state, removes all matches.
  56. *
  57. * @returns A promise that resolves when all state has been cleaned up.
  58. */
  59. endSearch(): Promise<void>;
  60. /**
  61. * Move the current match indicator to the next match.
  62. *
  63. * @returns A promise that resolves once the action has completed.
  64. */
  65. highlightNext(): Promise<ISearchMatch | undefined>;
  66. /**
  67. * Move the current match indicator to the previous match.
  68. *
  69. * @returns A promise that resolves once the action has completed.
  70. */
  71. highlightPrevious(): Promise<ISearchMatch | undefined>;
  72. /**
  73. * The same list of matches provided by the startSearch promise resoluton
  74. */
  75. readonly matches: ISearchMatch[];
  76. /**
  77. * Signal indicating that something in the search has changed, so the UI should update
  78. */
  79. readonly changed: ISignal<ISearchProvider, void>;
  80. /**
  81. * The current index of the selected match.
  82. */
  83. readonly currentMatchIndex: number;
  84. }
  85. export interface IDisplayState {
  86. /**
  87. * The index of the currently selected match
  88. */
  89. currentIndex: number;
  90. /**
  91. * The total number of matches found in the document
  92. */
  93. totalMatches: number;
  94. /**
  95. * Should the search be case sensitive?
  96. */
  97. caseSensitive: boolean;
  98. /**
  99. * Should the search string be treated as a RegExp?
  100. */
  101. useRegex: boolean;
  102. /**
  103. * The text in the entry
  104. */
  105. inputText: string;
  106. /**
  107. * The query constructed from the text and the case/regex flags
  108. */
  109. query: RegExp;
  110. /**
  111. * An error message (used for bad regex syntax)
  112. */
  113. errorMessage: string;
  114. /**
  115. * Should the focus forced into the input on the next render?
  116. */
  117. forceFocus: boolean;
  118. }
  119. /**
  120. * Initialization data for the document-search extension.
  121. */
  122. const extension: JupyterLabPlugin<void> = {
  123. id: '@jupyterlab/documentsearch:plugin',
  124. autoStart: true,
  125. requires: [ICommandPalette],
  126. activate: (app: JupyterLab, palette: ICommandPalette) => {
  127. // Create registry, retrieve all default providers
  128. const registry: SearchProviderRegistry = new SearchProviderRegistry();
  129. const activeSearches: Private.ActiveSearchMap = {};
  130. const startCommand: string = 'documentsearch:start';
  131. const nextCommand: string = 'documentsearch:highlightNext';
  132. const prevCommand: string = 'documentsearch:highlightPrevious';
  133. app.commands.addCommand(startCommand, {
  134. label: 'Search the open document',
  135. execute: () => {
  136. let currentWidget = app.shell.currentWidget;
  137. if (!currentWidget) {
  138. return;
  139. }
  140. Private.onStartCommand(currentWidget, registry, activeSearches);
  141. }
  142. });
  143. app.commands.addCommand(nextCommand, {
  144. label: 'Next match in open document',
  145. execute: () => {
  146. let currentWidget = app.shell.currentWidget;
  147. if (!currentWidget) {
  148. return;
  149. }
  150. Private.openBoxOrExecute(
  151. currentWidget,
  152. registry,
  153. activeSearches,
  154. Private.onNextCommand
  155. );
  156. }
  157. });
  158. app.commands.addCommand(prevCommand, {
  159. label: 'Previous match in open document',
  160. execute: () => {
  161. let currentWidget = app.shell.currentWidget;
  162. if (!currentWidget) {
  163. return;
  164. }
  165. Private.openBoxOrExecute(
  166. currentWidget,
  167. registry,
  168. activeSearches,
  169. Private.onPrevCommand
  170. );
  171. }
  172. });
  173. // Add the command to the palette.
  174. palette.addItem({ command: startCommand, category: 'Main Area' });
  175. }
  176. };
  177. namespace Private {
  178. export type ActiveSearchMap = {
  179. [key: string]: SearchInstance;
  180. };
  181. export function openBoxOrExecute(
  182. currentWidget: Widget,
  183. registry: SearchProviderRegistry,
  184. activeSearches: ActiveSearchMap,
  185. command: (instance: SearchInstance) => void
  186. ): void {
  187. const instance = activeSearches[currentWidget.id];
  188. if (instance) {
  189. command(instance);
  190. } else {
  191. onStartCommand(currentWidget, registry, activeSearches);
  192. }
  193. }
  194. export function onStartCommand(
  195. currentWidget: Widget,
  196. registry: SearchProviderRegistry,
  197. activeSearches: ActiveSearchMap
  198. ): void {
  199. const widgetId = currentWidget.id;
  200. if (activeSearches[widgetId]) {
  201. activeSearches[widgetId].focusInput();
  202. return;
  203. }
  204. const searchProvider = registry.getProviderForWidget(currentWidget);
  205. if (!searchProvider) {
  206. // TODO: Is there a way to pass the invocation of ctrl+f through to the browser?
  207. return;
  208. }
  209. const searchInstance = new SearchInstance(currentWidget, searchProvider);
  210. activeSearches[widgetId] = searchInstance;
  211. searchInstance.searchWidget.disposed.connect(() => {
  212. delete activeSearches[widgetId];
  213. });
  214. Widget.attach(searchInstance.searchWidget, currentWidget.node);
  215. // Focusing after attach even though we're focusing on componentDidMount
  216. // because the notebook steals focus when switching to command mode on blur.
  217. // This is a bit of a kludge to be addressed later.
  218. searchInstance.focusInput();
  219. }
  220. export async function onNextCommand(instance: SearchInstance) {
  221. await instance.provider.highlightNext();
  222. instance.updateIndices();
  223. }
  224. export async function onPrevCommand(instance: SearchInstance) {
  225. await instance.provider.highlightPrevious();
  226. instance.updateIndices();
  227. }
  228. }
  229. export default extension;