| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
import { |
| 5 |
IRouter, |
| 6 |
JupyterFrontEnd, |
| 7 |
JupyterFrontEndPlugin, |
| 8 |
} from '@jupyterlab/application'; |
| 9 |
|
| 10 |
import { PageConfig, URLExt } from '@jupyterlab/coreutils'; |
| 11 |
|
| 12 |
import { ITerminalTracker } from '@jupyterlab/terminal'; |
| 13 |
|
| 14 |
import { |
| 15 |
INotebookPathOpener, |
| 16 |
defaultNotebookPathOpener, |
| 17 |
} from '@jupyter-notebook/application'; |
| 18 |
|
| 19 |
import { find } from '@lumino/algorithm'; |
| 20 |
|
| 21 |
/** |
| 22 |
* A plugin to open terminals in a new tab |
| 23 |
*/ |
| 24 |
const opener: JupyterFrontEndPlugin<void> = { |
| 25 |
id: '@jupyter-notebook/terminal-extension:opener', |
| 26 |
description: 'A plugin to open terminals in a new tab.', |
| 27 |
requires: [IRouter, ITerminalTracker], |
| 28 |
autoStart: true, |
| 29 |
activate: ( |
| 30 |
app: JupyterFrontEnd, |
| 31 |
router: IRouter, |
| 32 |
tracker: ITerminalTracker |
| 33 |
) => { |
| 34 |
const { commands } = app; |
| 35 |
const terminalPattern = new RegExp('/terminals/(.*)'); |
| 36 |
|
| 37 |
const command = 'router:terminal'; |
| 38 |
commands.addCommand(command, { |
| 39 |
execute: (args: any) => { |
| 40 |
const parsed = args as IRouter.ILocation; |
| 41 |
const matches = parsed.path.match(terminalPattern); |
| 42 |
if (!matches) { |
| 43 |
return; |
| 44 |
} |
| 45 |
const [, name] = matches; |
| 46 |
if (!name) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
tracker.widgetAdded.connect((send, terminal) => { |
| 51 |
terminal.content.setOption('closeOnExit', false); |
| 52 |
}); |
| 53 |
commands.execute('terminal:open', { name }); |
| 54 |
}, |
| 55 |
}); |
| 56 |
|
| 57 |
router.register({ command, pattern: terminalPattern }); |
| 58 |
}, |
| 59 |
}; |
| 60 |
|
| 61 |
/** |
| 62 |
* Open terminals in a new tab. |
| 63 |
*/ |
| 64 |
const redirect: JupyterFrontEndPlugin<void> = { |
| 65 |
id: '@jupyter-notebook/terminal-extension:redirect', |
| 66 |
description: 'Open terminals in a new tab.', |
| 67 |
requires: [ITerminalTracker], |
| 68 |
optional: [INotebookPathOpener], |
| 69 |
autoStart: true, |
| 70 |
activate: ( |
| 71 |
app: JupyterFrontEnd, |
| 72 |
tracker: ITerminalTracker, |
| 73 |
notebookPathOpener: INotebookPathOpener | null |
| 74 |
) => { |
| 75 |
const baseUrl = PageConfig.getBaseUrl(); |
| 76 |
const opener = notebookPathOpener ?? defaultNotebookPathOpener; |
| 77 |
|
| 78 |
tracker.widgetAdded.connect((send, terminal) => { |
| 79 |
const widget = find( |
| 80 |
app.shell.widgets('main'), |
| 81 |
(w) => w.id === terminal.id |
| 82 |
); |
| 83 |
if (widget) { |
| 84 |
// bail if the terminal is already added to the main area |
| 85 |
return; |
| 86 |
} |
| 87 |
const name = terminal.content.session.name; |
| 88 |
opener.open({ |
| 89 |
prefix: URLExt.join(baseUrl, 'terminals'), |
| 90 |
path: name, |
| 91 |
target: '_blank', |
| 92 |
}); |
| 93 |
|
| 94 |
// dispose the widget since it is not used on this page |
| 95 |
terminal.dispose(); |
| 96 |
}); |
| 97 |
}, |
| 98 |
}; |
| 99 |
|
| 100 |
/** |
| 101 |
* Export the plugins as default. |
| 102 |
*/ |
| 103 |
const plugins: JupyterFrontEndPlugin<any>[] = [opener, redirect]; |
| 104 |
|
| 105 |
export default plugins; |