Skip to content

Commit

Permalink
Merge pull request #6256 from aschlaep/initial-search-text
Browse files Browse the repository at this point in the history
Add getInitialQuery to documentsearch interface
  • Loading branch information
blink1073 committed Apr 27, 2019
2 parents e0a1d0c + f5668f5 commit 392dba9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/csvviewer-extension/src/searchprovider.ts
Expand Up @@ -17,6 +17,17 @@ export class CSVSearchProvider implements ISearchProvider {
);
}

/**
* Get an initial query value if applicable so that it can be entered
* into the search box as an initial query
*
* @returns Initial value used to populate the search box.
*/
getInitialQuery(searchTarget: IDocumentWidget<CSVViewer>): any {
// CSV Viewer does not support selection
return null;
}

/**
* Initialize the search using the provided options. Should update the UI
* to highlight all matches and "select" whatever the first match should be.
Expand Down
9 changes: 9 additions & 0 deletions packages/documentsearch/src/interfaces.ts
Expand Up @@ -106,6 +106,15 @@ export interface ISearchProviderConstructor {
}

export interface ISearchProvider {
/**
* Get an initial query value if applicable so that it can be entered
* into the search box as an initial query
*
* @param searchTarget The widget to be searched
*
* @returns Initial value used to populate the search box.
*/
getInitialQuery(searchTarget: Widget): any;
/**
* Initialize the search using the provided options. Should update the UI
* to highlight all matches and "select" whatever the first match should be.
Expand Down
19 changes: 17 additions & 2 deletions packages/documentsearch/src/providers/codemirrorsearchprovider.ts
Expand Up @@ -44,6 +44,21 @@ import { FileEditor } from '@jupyterlab/fileeditor';
type MatchMap = { [key: number]: { [key: number]: ISearchMatch } };

export class CodeMirrorSearchProvider implements ISearchProvider {
/**
* Get an initial query value if applicable so that it can be entered
* into the search box as an initial query
*
* @returns Initial value used to populate the search box.
*/
getInitialQuery(searchTarget: Widget): any {
const target = searchTarget as MainAreaWidget;
const content = target.content as FileEditor;
const cm = content.editor as CodeMirrorEditor;
const selection = cm.doc.getSelection();
// if there are newlines, just return empty string
return selection.search(/\r?\n|\r/g) === -1 ? selection : '';
}

/**
* Initialize the search using the provided options. Should update the UI
* to highlight all matches and "select" whatever the first match should be.
Expand All @@ -63,8 +78,8 @@ export class CodeMirrorSearchProvider implements ISearchProvider {

// Extract the codemirror object from the editor widget. Each of these casts
// is justified by the canSearchOn call above.
let target = searchTarget as MainAreaWidget;
let content = target.content as FileEditor;
const target = searchTarget as MainAreaWidget;
const content = target.content as FileEditor;
this._cm = content.editor as CodeMirrorEditor;
return this._startQuery(query);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/documentsearch/src/providers/notebooksearchprovider.ts
Expand Up @@ -17,6 +17,19 @@ interface ICellSearchPair {
}

export class NotebookSearchProvider implements ISearchProvider {
/**
* Get an initial query value if applicable so that it can be entered
* into the search box as an initial query
*
* @returns Initial value used to populate the search box.
*/
getInitialQuery(searchTarget: NotebookPanel): any {
const activeCell = searchTarget.content.activeCell;
const selection = (activeCell.editor as CodeMirrorEditor).doc.getSelection();
// if there are newlines, just return empty string
return selection.search(/\r?\n|\r/g) === -1 ? selection : '';
}

/**
* Initialize the search using the provided options. Should update the UI
* to highlight all matches and "select" whatever the first match should be.
Expand Down
3 changes: 3 additions & 0 deletions packages/documentsearch/src/searchinstance.ts
Expand Up @@ -17,6 +17,9 @@ export class SearchInstance implements IDisposable {
this._widget = widget;
this._activeProvider = searchProvider;

const initialQuery = this._activeProvider.getInitialQuery(this._widget);
this._displayState.searchText = initialQuery || '';

this._searchWidget = createSearchOverlay({
widgetChanged: this._displayUpdateSignal,
overlayState: this._displayState,
Expand Down
6 changes: 6 additions & 0 deletions packages/documentsearch/src/searchoverlay.tsx
Expand Up @@ -243,6 +243,12 @@ class SearchOverlay extends React.Component<
this.state = props.overlayState;
}

componentDidMount() {
if (this.state.searchText) {
this._executeSearch(true, this.state.searchText);
}
}

private _onSearchChange(event: React.ChangeEvent) {
const searchText = (event.target as HTMLInputElement).value;
this.setState({ searchText: searchText });
Expand Down

0 comments on commit 392dba9

Please sign in to comment.