Skip to content

Commit

Permalink
Merge pull request #7725 from mlucool/find-replace
Browse files Browse the repository at this point in the history
Add command to open find with replace
  • Loading branch information
Steven Silvester committed Feb 15, 2020
2 parents 4538bf5 + 29e7c86 commit 2763b5b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
78 changes: 50 additions & 28 deletions packages/documentsearch-extension/src/index.ts
Expand Up @@ -89,42 +89,64 @@ const extension: JupyterFrontEndPlugin<ISearchProviderRegistry> = {
const activeSearches = new Map<string, SearchInstance>();

const startCommand: string = 'documentsearch:start';
const startReplaceCommand: string = 'documentsearch:startWithReplace';
const nextCommand: string = 'documentsearch:highlightNext';
const prevCommand: string = 'documentsearch:highlightPrevious';

const currentWidgetHasSearchProvider = () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return false;
}
return registry.getProviderForWidget(currentWidget) !== undefined;
};
const getCurrentWidgetSearchInstance = () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return;
}
const widgetId = currentWidget.id;
let searchInstance = activeSearches.get(widgetId);
if (!searchInstance) {
const searchProvider = registry.getProviderForWidget(currentWidget);
if (!searchProvider) {
return;
}
searchInstance = new SearchInstance(currentWidget, searchProvider);

activeSearches.set(widgetId, searchInstance);
// find next and previous are now enabled
app.commands.notifyCommandChanged();

searchInstance.disposed.connect(() => {
activeSearches.delete(widgetId);
// find next and previous are now not enabled
app.commands.notifyCommandChanged();
});
}
return searchInstance;
};

app.commands.addCommand(startCommand, {
label: 'Find…',
isEnabled: () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return false;
}
return registry.getProviderForWidget(currentWidget) !== undefined;
},
isEnabled: currentWidgetHasSearchProvider,
execute: () => {
const currentWidget = app.shell.currentWidget;
if (!currentWidget) {
return;
const searchInstance = getCurrentWidgetSearchInstance();
if (searchInstance) {
searchInstance.focusInput();
}
const widgetId = currentWidget.id;
let searchInstance = activeSearches.get(widgetId);
if (!searchInstance) {
const searchProvider = registry.getProviderForWidget(currentWidget);
if (!searchProvider) {
return;
}
searchInstance = new SearchInstance(currentWidget, searchProvider);

activeSearches.set(widgetId, searchInstance);
// find next and previous are now enabled
app.commands.notifyCommandChanged();
}
});

searchInstance.disposed.connect(() => {
activeSearches.delete(widgetId);
// find next and previous are now not enabled
app.commands.notifyCommandChanged();
});
app.commands.addCommand(startReplaceCommand, {
label: 'Find and Replace…',
isEnabled: currentWidgetHasSearchProvider,
execute: () => {
const searchInstance = getCurrentWidgetSearchInstance();
if (searchInstance) {
searchInstance.showReplace();
searchInstance.focusInput();
}
searchInstance.focusInput();
}
});

Expand Down
6 changes: 6 additions & 0 deletions packages/documentsearch/src/searchinstance.ts
Expand Up @@ -77,6 +77,12 @@ export class SearchInstance implements IDisposable {
this._displayState.forceFocus = false;
}

/**
* If there is a replace box, show it.
*/
showReplace(): void {
this._displayState.replaceEntryShown = true;
}
/**
* Updates the match index and total display in the search widget.
*/
Expand Down

0 comments on commit 2763b5b

Please sign in to comment.