Skip to content

Commit

Permalink
add documentsearch commands to the notebook example
Browse files Browse the repository at this point in the history
  • Loading branch information
aschlaep committed Apr 25, 2019
1 parent 90d2025 commit e7cd790
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 6 deletions.
4 changes: 4 additions & 0 deletions examples/notebook/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ body {
.jp-NotebookPanel {
border-bottom: 1px solid #e0e0e0;
}

.notebookCommandPalette {
min-width: 225px;
}
1 change: 1 addition & 0 deletions examples/notebook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@jupyterlab/completer": "^1.0.0-alpha.6",
"@jupyterlab/docmanager": "^1.0.0-alpha.6",
"@jupyterlab/docregistry": "^1.0.0-alpha.6",
"@jupyterlab/documentsearch": "^1.0.0-alpha.7",
"@jupyterlab/mathjax2": "^1.0.0-alpha.6",
"@jupyterlab/notebook": "^1.0.0-alpha.7",
"@jupyterlab/rendermime": "^1.0.0-alpha.6",
Expand Down
70 changes: 69 additions & 1 deletion examples/notebook/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import { CommandRegistry } from '@phosphor/commands';
import { CompletionHandler } from '@jupyterlab/completer';
import { NotebookPanel, NotebookActions } from '@jupyterlab/notebook';
import {
SearchInstance,
NotebookSearchProvider
} from '@jupyterlab/documentsearch';
import { CommandPalette } from '@phosphor/widgets';

/**
Expand All @@ -14,6 +18,9 @@ const cmdIds = {
select: 'completer:select',
invokeNotebook: 'completer:invoke-notebook',
selectNotebook: 'completer:select-notebook',
startSearch: 'documentsearch:start-search',
findNext: 'documentsearch:find-next',
findPrevious: 'documentsearch:find-previous',
save: 'notebook:save',
interrupt: 'notebook:interrupt-kernel',
restart: 'notebook:restart-kernel',
Expand Down Expand Up @@ -67,6 +74,49 @@ export const SetupCommands = (
label: 'Save',
execute: () => nbWidget.context.save()
});

let searchInstance: SearchInstance;
commands.addCommand(cmdIds.startSearch, {
label: 'Find...',
execute: () => {
if (searchInstance) {
searchInstance.focusInput();
return;
}
const provider = new NotebookSearchProvider();
searchInstance = new SearchInstance(nbWidget, provider);
searchInstance.disposed.connect(() => {
searchInstance = undefined;
// find next and previous are now not enabled
commands.notifyCommandChanged();
});
// find next and previous are now enabled
commands.notifyCommandChanged();
searchInstance.focusInput();
}
});
commands.addCommand(cmdIds.findNext, {
label: 'Find Next',
isEnabled: () => !!searchInstance,
execute: async () => {
if (!searchInstance) {
return;
}
await searchInstance.provider.highlightNext();
searchInstance.updateIndices();
}
});
commands.addCommand(cmdIds.findPrevious, {
label: 'Find Previous',
isEnabled: () => !!searchInstance,
execute: async () => {
if (!searchInstance) {
return;
}
await searchInstance.provider.highlightPrevious();
searchInstance.updateIndices();
}
});
commands.addCommand(cmdIds.interrupt, {
label: 'Interrupt',
execute: async () => {
Expand Down Expand Up @@ -143,7 +193,10 @@ export const SetupCommands = (
cmdIds.restart,
cmdIds.editMode,
cmdIds.commandMode,
cmdIds.switchKernel
cmdIds.switchKernel,
cmdIds.startSearch,
cmdIds.findNext,
cmdIds.findPrevious
].forEach(command => palette.addItem({ command, category }));

category = 'Notebook Cell Operations';
Expand Down Expand Up @@ -180,6 +233,21 @@ export const SetupCommands = (
keys: ['Accel S'],
command: cmdIds.save
},
{
selector: '.jp-Notebook',
keys: ['Accel F'],
command: cmdIds.startSearch
},
{
selector: '.jp-Notebook',
keys: ['Accel G'],
command: cmdIds.findNext
},
{
selector: '.jp-Notebook',
keys: ['Accel Shift G'],
command: cmdIds.findPrevious
},
{
selector: '.jp-Notebook.jp-mod-commandMode:focus',
keys: ['I', 'I'],
Expand Down
1 change: 1 addition & 0 deletions examples/notebook/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function createApp(manager: ServiceManager.IManager): void {
let notebookPath = PageConfig.getOption('notebookPath');
let nbWidget = docManager.open(notebookPath) as NotebookPanel;
let palette = new CommandPalette({ commands });
palette.addClass('notebookCommandPalette');

const editor =
nbWidget.content.activeCell && nbWidget.content.activeCell.editor;
Expand Down
13 changes: 8 additions & 5 deletions examples/notebook/test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {
"tags": []
},
Expand Down Expand Up @@ -137,7 +137,10 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": true,
"jupyter": {
"outputs_hidden": true
}
},
"outputs": [],
"source": []
Expand All @@ -146,7 +149,7 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -160,9 +163,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
2 changes: 2 additions & 0 deletions packages/documentsearch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ import '../style/index.css';
export * from './interfaces';
export * from './searchinstance';
export * from './searchproviderregistry';
export * from './providers/codemirrorsearchprovider';
export * from './providers/notebooksearchprovider';

0 comments on commit e7cd790

Please sign in to comment.