UNPKG

sophon-notebook-notebook

Version:
240 lines 8.53 kB
// Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. import * as React from 'react'; import { NotebookActions } from './actions'; import { showDialog, Dialog, Toolbar, ToolbarButtonComponent, UseSignal, addToolbarButtonClass, ReactWidget, ToolbarButton } from 'sophon-notebook-apputils'; import { HTMLSelect } from 'sophon-notebook-ui-components'; /** * The class name added to toolbar save button. */ const TOOLBAR_SAVE_CLASS = 'jp-SaveIcon'; /** * The class name added to toolbar insert button. */ const TOOLBAR_INSERT_CLASS = 'jp-AddIcon'; /** * The class name added to toolbar cut button. */ const TOOLBAR_CUT_CLASS = 'jp-CutIcon'; /** * The class name added to toolbar copy button. */ const TOOLBAR_COPY_CLASS = 'jp-CopyIcon'; /** * The class name added to toolbar paste button. */ const TOOLBAR_PASTE_CLASS = 'jp-PasteIcon'; /** * The class name added to toolbar run button. */ const TOOLBAR_RUN_CLASS = 'jp-RunIcon'; /** * The class name added to toolbar cell type dropdown wrapper. */ const TOOLBAR_CELLTYPE_CLASS = 'jp-Notebook-toolbarCellType'; /** * The class name added to toolbar cell type dropdown. */ const TOOLBAR_CELLTYPE_DROPDOWN_CLASS = 'jp-Notebook-toolbarCellTypeDropdown'; /** * A namespace for the default toolbar items. */ export var ToolbarItems; (function (ToolbarItems) { /** * Create save button toolbar item. */ function createSaveButton(panel) { function onClick() { if (panel.context.model.readOnly) { return showDialog({ title: 'Cannot Save', body: 'Document is read-only', buttons: [Dialog.okButton()] }); } void panel.context.save().then(() => { if (!panel.isDisposed) { return panel.context.createCheckpoint(); } }); } return addToolbarButtonClass(ReactWidget.create(React.createElement(UseSignal, { signal: panel.context.fileChanged }, () => (React.createElement(ToolbarButtonComponent, { iconClassName: TOOLBAR_SAVE_CLASS, onClick: onClick, tooltip: "Save the notebook contents and create checkpoint", enabled: !!(panel && panel.context && panel.context.contentsModel && panel.context.contentsModel.writable) }))))); } ToolbarItems.createSaveButton = createSaveButton; /** * Create an insert toolbar item. */ function createInsertButton(panel) { return new ToolbarButton({ iconClassName: TOOLBAR_INSERT_CLASS, onClick: () => { NotebookActions.insertBelow(panel.content); }, tooltip: 'Insert a cell below' }); } ToolbarItems.createInsertButton = createInsertButton; /** * Create a cut toolbar item. */ function createCutButton(panel) { return new ToolbarButton({ iconClassName: TOOLBAR_CUT_CLASS, onClick: () => { NotebookActions.cut(panel.content); }, tooltip: 'Cut the selected cells' }); } ToolbarItems.createCutButton = createCutButton; /** * Create a copy toolbar item. */ function createCopyButton(panel) { return new ToolbarButton({ iconClassName: TOOLBAR_COPY_CLASS, onClick: () => { NotebookActions.copy(panel.content); }, tooltip: 'Copy the selected cells' }); } ToolbarItems.createCopyButton = createCopyButton; /** * Create a paste toolbar item. */ function createPasteButton(panel) { return new ToolbarButton({ iconClassName: TOOLBAR_PASTE_CLASS, onClick: () => { NotebookActions.paste(panel.content); }, tooltip: 'Paste cells from the clipboard' }); } ToolbarItems.createPasteButton = createPasteButton; /** * Create a run toolbar item. */ function createRunButton(panel) { return new ToolbarButton({ iconClassName: TOOLBAR_RUN_CLASS, onClick: () => { void NotebookActions.runAndAdvance(panel.content, panel.session); }, tooltip: 'Run the selected cells and advance' }); } ToolbarItems.createRunButton = createRunButton; /** * Create a cell type switcher item. * * #### Notes * It will display the type of the current active cell. * If more than one cell is selected but are of different types, * it will display `'-'`. * When the user changes the cell type, it will change the * cell types of the selected cells. * It can handle a change to the context. */ function createCellTypeItem(panel) { return new CellTypeSwitcher(panel.content); } ToolbarItems.createCellTypeItem = createCellTypeItem; /** * Get the default toolbar items for panel */ function getDefaultItems(panel) { return [ { name: 'save', widget: createSaveButton(panel) }, { name: 'insert', widget: createInsertButton(panel) }, { name: 'cut', widget: createCutButton(panel) }, { name: 'copy', widget: createCopyButton(panel) }, { name: 'paste', widget: createPasteButton(panel) }, { name: 'run', widget: createRunButton(panel) }, { name: 'interrupt', widget: Toolbar.createInterruptButton(panel.session) }, { name: 'restart', widget: Toolbar.createRestartButton(panel.session) }, { name: 'cellType', widget: createCellTypeItem(panel) }, { name: 'spacer', widget: Toolbar.createSpacerItem() }, { name: 'kernelName', widget: Toolbar.createKernelNameItem(panel.session) }, { name: 'kernelStatus', widget: Toolbar.createKernelStatusItem(panel.session) } ]; } ToolbarItems.getDefaultItems = getDefaultItems; })(ToolbarItems || (ToolbarItems = {})); /** * A toolbar widget that switches cell types. */ export class CellTypeSwitcher extends ReactWidget { /** * Construct a new cell type switcher. */ constructor(widget) { super(); /** * Handle `change` events for the HTMLSelect component. */ this.handleChange = (event) => { if (event.target.value !== '-') { NotebookActions.changeCellType(this._notebook, event.target .value); this._notebook.activate(); } }; /** * Handle `keydown` events for the HTMLSelect component. */ this.handleKeyDown = (event) => { if (event.keyCode === 13) { this._notebook.activate(); } }; this._notebook = null; this.addClass(TOOLBAR_CELLTYPE_CLASS); this._notebook = widget; if (widget.model) { this.update(); } widget.activeCellChanged.connect(this.update, this); // Follow a change in the selection. widget.selectionChanged.connect(this.update, this); } render() { let value = '-'; if (this._notebook.activeCell) { value = this._notebook.activeCell.model.type; } for (let widget of this._notebook.widgets) { if (this._notebook.isSelectedOrActive(widget)) { if (widget.model.type !== value) { value = '-'; break; } } } return (React.createElement(HTMLSelect, { className: TOOLBAR_CELLTYPE_DROPDOWN_CLASS, onChange: this.handleChange, onKeyDown: this.handleKeyDown, value: value, iconProps: { icon: React.createElement("span", { className: "jp-MaterialIcon jp-DownCaretIcon bp3-icon" }) }, "aria-label": "Cell type", minimal: true }, React.createElement("option", { value: "-" }, "-"), React.createElement("option", { value: "code" }, "Code"), React.createElement("option", { value: "markdown" }, "Markdown"), React.createElement("option", { value: "raw" }, "Raw"))); } } //# sourceMappingURL=default-toolbar.js.map