|
| 1 | +// Most of the code in this file was inspired by the following PRs in JupyterLab upstream: |
| 2 | +// 1. https://github.com/jupyterlab/jupyterlab/pull/16602 |
| 3 | +// 2. https://github.com/jupyterlab/jupyterlab/pull/17775 |
| 4 | + |
| 5 | +// SPDX-License-Identifier: BSD-3-Clause |
| 6 | + |
| 7 | +// JupyterLab uses a shared copyright model that enables all contributors to maintain |
| 8 | +// the copyright on their contributions. All code is licensed under the terms of the |
| 9 | +// revised BSD license. |
| 10 | + |
| 11 | +// Copyright (c) 2015-2025 Project Jupyter Contributors |
| 12 | +// All rights reserved. |
| 13 | + |
| 14 | +// Redistribution and use in source and binary forms, with or without |
| 15 | +// modification, are permitted provided that the following conditions are met: |
| 16 | + |
| 17 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 18 | +// list of conditions and the following disclaimer. |
| 19 | + |
| 20 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 21 | +// this list of conditions and the following disclaimer in the documentation |
| 22 | +// and/or other materials provided with the distribution. |
| 23 | + |
| 24 | +// 3. Neither the name of the copyright holder nor the names of its |
| 25 | +// contributors may be used to endorse or promote products derived from |
| 26 | +// this software without specific prior written permission. |
| 27 | + |
| 28 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 29 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 30 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 31 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 32 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 33 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 34 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 35 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 36 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | + |
| 39 | +import { JupyterFrontEnd, JupyterFrontEndPlugin } from '@jupyterlab/application'; |
| 40 | +import { IEditorServices } from '@jupyterlab/codeeditor'; |
| 41 | +import { ToolbarButton } from '@jupyterlab/ui-components'; |
| 42 | +import { Widget, PanelLayout } from '@lumino/widgets'; |
| 43 | +import { Notebook, NotebookPanel } from '@jupyterlab/notebook'; |
| 44 | +import { EverywhereIcons } from './icons'; |
| 45 | + |
| 46 | +const INPUT_PROMPT_CLASS = 'jp-InputPrompt'; |
| 47 | +const INPUT_AREA_PROMPT_INDICATOR_CLASS = 'jp-InputArea-prompt-indicator'; |
| 48 | +const INPUT_AREA_PROMPT_INDICATOR_EMPTY_CLASS = 'jp-InputArea-prompt-indicator-empty'; |
| 49 | +const INPUT_AREA_PROMPT_RUN_CLASS = 'jp-InputArea-prompt-run'; |
| 50 | + |
| 51 | +export interface IInputPromptIndicator extends Widget { |
| 52 | + executionCount: string | null; |
| 53 | +} |
| 54 | + |
| 55 | +export interface IInputPrompt extends IInputPromptIndicator { |
| 56 | + runButton?: ToolbarButton; |
| 57 | +} |
| 58 | + |
| 59 | +export class InputPromptIndicator extends Widget implements IInputPromptIndicator { |
| 60 | + private _executionCount: string | null = null; |
| 61 | + |
| 62 | + constructor() { |
| 63 | + super(); |
| 64 | + this.addClass(INPUT_AREA_PROMPT_INDICATOR_CLASS); |
| 65 | + } |
| 66 | + |
| 67 | + get executionCount(): string | null { |
| 68 | + return this._executionCount; |
| 69 | + } |
| 70 | + |
| 71 | + set executionCount(value: string | null) { |
| 72 | + this._executionCount = value; |
| 73 | + if (value) { |
| 74 | + this.node.textContent = `[${value}]:`; |
| 75 | + this.removeClass(INPUT_AREA_PROMPT_INDICATOR_EMPTY_CLASS); |
| 76 | + } else { |
| 77 | + this.node.textContent = '[ ]:'; |
| 78 | + this.addClass(INPUT_AREA_PROMPT_INDICATOR_EMPTY_CLASS); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +export class JEInputPrompt extends Widget implements IInputPrompt { |
| 84 | + private _customExecutionCount: string | null = null; |
| 85 | + private _promptIndicator: InputPromptIndicator; |
| 86 | + private _runButton: ToolbarButton; |
| 87 | + |
| 88 | + constructor(private _app: JupyterFrontEnd) { |
| 89 | + super(); |
| 90 | + this.addClass(INPUT_PROMPT_CLASS); |
| 91 | + |
| 92 | + const layout = (this.layout = new PanelLayout()); |
| 93 | + this._promptIndicator = new InputPromptIndicator(); |
| 94 | + layout.addWidget(this._promptIndicator); |
| 95 | + this._runButton = new ToolbarButton({ |
| 96 | + icon: EverywhereIcons.runCell, |
| 97 | + onClick: () => { |
| 98 | + this._app.commands.execute('notebook:run-cell'); |
| 99 | + }, |
| 100 | + tooltip: 'Run this cell' |
| 101 | + }); |
| 102 | + this._runButton.addClass(INPUT_AREA_PROMPT_RUN_CLASS); |
| 103 | + this._runButton.addClass('je-cell-run-button'); |
| 104 | + layout.addWidget(this._runButton); |
| 105 | + } |
| 106 | + |
| 107 | + get executionCount(): string | null { |
| 108 | + return this._customExecutionCount; |
| 109 | + } |
| 110 | + |
| 111 | + set executionCount(value: string | null) { |
| 112 | + this._customExecutionCount = value; |
| 113 | + this._promptIndicator.executionCount = value; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +export namespace JENotebookContentFactory { |
| 118 | + export interface IOptions extends Notebook.ContentFactory.IOptions { |
| 119 | + app: JupyterFrontEnd; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +export class JENotebookContentFactory extends Notebook.ContentFactory { |
| 124 | + private _app: JupyterFrontEnd; |
| 125 | + |
| 126 | + constructor(options: JENotebookContentFactory.IOptions) { |
| 127 | + super(options); |
| 128 | + this._app = options.app; |
| 129 | + } |
| 130 | + |
| 131 | + createInputPrompt(): JEInputPrompt { |
| 132 | + return new JEInputPrompt(this._app); |
| 133 | + } |
| 134 | + |
| 135 | + createNotebook(options: Notebook.IOptions): Notebook { |
| 136 | + return new Notebook(options); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Plugin that provides the custom notebook factory with run buttons |
| 142 | + */ |
| 143 | +export const notebookFactoryPlugin: JupyterFrontEndPlugin<NotebookPanel.IContentFactory> = { |
| 144 | + id: 'jupytereverywhere:notebook-factory', |
| 145 | + description: 'Provides notebook cell factory with input prompts', |
| 146 | + provides: NotebookPanel.IContentFactory, |
| 147 | + requires: [IEditorServices], |
| 148 | + autoStart: true, |
| 149 | + activate: (app: JupyterFrontEnd, editorServices: IEditorServices) => { |
| 150 | + const editorFactory = editorServices.factoryService.newInlineEditor; |
| 151 | + |
| 152 | + const factory = new JENotebookContentFactory({ |
| 153 | + editorFactory, |
| 154 | + app |
| 155 | + }); |
| 156 | + |
| 157 | + return factory; |
| 158 | + } |
| 159 | +}; |
0 commit comments