Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getInitialQuery to documentsearch interface #6256

Merged
merged 1 commit into from Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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