jupyter-js-notebook
Advanced tools
Comparing version 0.13.4 to 0.14.0
@@ -8,6 +8,2 @@ import { IDisposable } from 'phosphor-disposable'; | ||
/** | ||
* The interactivity modes for a cell. | ||
*/ | ||
export declare type CellMode = 'command' | 'edit'; | ||
/** | ||
* The scrolled setting of a cell. | ||
@@ -37,10 +33,2 @@ */ | ||
/** | ||
* Whether the cell is the active cell in the notebook. | ||
*/ | ||
active: boolean; | ||
/** | ||
* The mode of the cell. | ||
*/ | ||
mode: CellMode; | ||
/** | ||
* The input area of the cell. | ||
@@ -62,6 +50,2 @@ * | ||
readOnly: boolean; | ||
/** | ||
* Whether the cell is selected for applying commands. | ||
*/ | ||
selected: boolean; | ||
} | ||
@@ -124,17 +108,2 @@ /** | ||
/** | ||
* Whether the cell is the active cell in the notebook. | ||
*/ | ||
active: boolean; | ||
/** | ||
* The mode of the cell. | ||
* | ||
* #### Notes | ||
* This is a delegate to the focused state of the input's editor. | ||
*/ | ||
mode: CellMode; | ||
/** | ||
* Whether the cell is selected for applying commands. | ||
*/ | ||
selected: boolean; | ||
/** | ||
* Get the input area model. | ||
@@ -179,6 +148,2 @@ */ | ||
type: CellType; | ||
/** | ||
* Handle changes to the editor model. | ||
*/ | ||
private onEditorChanged(editor, args); | ||
private _input; | ||
@@ -185,0 +150,0 @@ } |
@@ -21,3 +21,2 @@ // Copyright (c) Jupyter Development Team. | ||
this._input = input; | ||
input.textEditor.stateChanged.connect(this.onEditorChanged, this); | ||
} | ||
@@ -34,49 +33,2 @@ Object.defineProperty(BaseCellModel.prototype, "stateChanged", { | ||
}); | ||
Object.defineProperty(BaseCellModel.prototype, "active", { | ||
/** | ||
* Whether the cell is the active cell in the notebook. | ||
*/ | ||
get: function () { | ||
return CellModelPrivate.activeProperty.get(this); | ||
}, | ||
set: function (value) { | ||
CellModelPrivate.activeProperty.set(this, value); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(BaseCellModel.prototype, "mode", { | ||
/** | ||
* The mode of the cell. | ||
* | ||
* #### Notes | ||
* This is a delegate to the focused state of the input's editor. | ||
*/ | ||
get: function () { | ||
if (this.input.textEditor.focused) { | ||
return 'edit'; | ||
} | ||
else { | ||
return 'command'; | ||
} | ||
}, | ||
set: function (value) { | ||
this.input.textEditor.focused = value === 'edit'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(BaseCellModel.prototype, "selected", { | ||
/** | ||
* Whether the cell is selected for applying commands. | ||
*/ | ||
get: function () { | ||
return CellModelPrivate.selectedProperty.get(this); | ||
}, | ||
set: function (value) { | ||
CellModelPrivate.selectedProperty.set(this, value); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(BaseCellModel.prototype, "input", { | ||
@@ -174,24 +126,2 @@ /** | ||
}; | ||
/** | ||
* Handle changes to the editor model. | ||
*/ | ||
BaseCellModel.prototype.onEditorChanged = function (editor, args) { | ||
// Handle changes to the focused state of the editor. | ||
if (args.name === 'focused') { | ||
if (args.newValue) { | ||
this.stateChanged.emit({ | ||
name: 'mode', | ||
newValue: 'edit', | ||
oldValue: 'command' | ||
}); | ||
} | ||
else { | ||
this.stateChanged.emit({ | ||
name: 'mode', | ||
newValue: 'command', | ||
oldValue: 'edit' | ||
}); | ||
} | ||
} | ||
}; | ||
return BaseCellModel; | ||
@@ -210,3 +140,3 @@ }()); | ||
_super.call(this, input); | ||
this.type = "code"; | ||
this.type = 'code'; | ||
this._output = null; | ||
@@ -296,3 +226,3 @@ this._output = output; | ||
_super.apply(this, arguments); | ||
this.type = "markdown"; | ||
this.type = 'markdown'; | ||
} | ||
@@ -322,3 +252,3 @@ Object.defineProperty(MarkdownCellModel.prototype, "rendered", { | ||
_super.apply(this, arguments); | ||
this.type = "raw"; | ||
this.type = 'raw'; | ||
} | ||
@@ -345,3 +275,3 @@ Object.defineProperty(RawCellModel.prototype, "format", { | ||
function isMarkdownCellModel(m) { | ||
return (m.type === "markdown"); | ||
return (m.type === 'markdown'); | ||
} | ||
@@ -353,3 +283,3 @@ exports.isMarkdownCellModel = isMarkdownCellModel; | ||
function isCodeCellModel(m) { | ||
return (m.type === "code"); | ||
return (m.type === 'code'); | ||
} | ||
@@ -361,3 +291,3 @@ exports.isCodeCellModel = isCodeCellModel; | ||
function isRawCellModel(m) { | ||
return (m.type === "raw"); | ||
return (m.type === 'raw'); | ||
} | ||
@@ -375,16 +305,2 @@ exports.isRawCellModel = isRawCellModel; | ||
/** | ||
* A property descriptor for the selected state of the cell. | ||
*/ | ||
CellModelPrivate.selectedProperty = new phosphor_properties_1.Property({ | ||
name: 'selected', | ||
notify: CellModelPrivate.stateChangedSignal, | ||
}); | ||
/** | ||
* A property descriptor for the active state of the cell. | ||
*/ | ||
CellModelPrivate.activeProperty = new phosphor_properties_1.Property({ | ||
name: 'active', | ||
notify: CellModelPrivate.stateChangedSignal, | ||
}); | ||
/** | ||
* A property descriptor for the name of a cell. | ||
@@ -391,0 +307,0 @@ */ |
@@ -37,14 +37,2 @@ import { Message } from 'phosphor-messaging'; | ||
dispose(): void; | ||
/** | ||
* Handle `update_request` messages. | ||
*/ | ||
protected onUpdateRequest(message: Message): void; | ||
/** | ||
* Handle changes to the model state. | ||
*/ | ||
protected onModelChanged(sender: ICellModel, args: IChangedArgs<any>): void; | ||
/** | ||
* Handle `after-attach` messages to the cell. | ||
*/ | ||
protected onAfterAttach(msg: Message): void; | ||
private _input; | ||
@@ -51,0 +39,0 @@ private _model; |
@@ -21,10 +21,2 @@ // Copyright (c) Jupyter Development Team. | ||
/** | ||
* The class name added to active widgets. | ||
*/ | ||
var ACTIVE_CLASS = 'jp-mod-active'; | ||
/** | ||
* The class name added to selected widgets. | ||
*/ | ||
var SELECTED_CLASS = 'jp-mod-selected'; | ||
/** | ||
* The class name added to code cells. | ||
@@ -50,10 +42,2 @@ */ | ||
/** | ||
* The class name added to a cell in edit mode. | ||
*/ | ||
var EDIT_CLASS = 'jp-mod-editMode'; | ||
/** | ||
* The class name added to a cell in command mode. | ||
*/ | ||
var COMMAND_CLASS = 'jp-mod-commandMode'; | ||
/** | ||
* The text applied to an empty markdown cell. | ||
@@ -75,4 +59,2 @@ */ | ||
this.addClass(CELL_CLASS); | ||
// Make the cell focusable by setting the tabIndex. | ||
this.node.tabIndex = -1; | ||
this._model = model; | ||
@@ -83,3 +65,2 @@ var constructor = this.constructor; | ||
this.layout.addChild(this._input); | ||
model.stateChanged.connect(this.onModelChanged, this); | ||
} | ||
@@ -130,40 +111,2 @@ /** | ||
}; | ||
/** | ||
* Handle `update_request` messages. | ||
*/ | ||
BaseCellWidget.prototype.onUpdateRequest = function (message) { | ||
_super.prototype.onUpdateRequest.call(this, message); | ||
if (this.model.active) { | ||
this.addClass(ACTIVE_CLASS); | ||
} | ||
else { | ||
this.removeClass(ACTIVE_CLASS); | ||
} | ||
if (this.model.selected) { | ||
this.addClass(SELECTED_CLASS); | ||
} | ||
else { | ||
this.removeClass(SELECTED_CLASS); | ||
} | ||
if (this.model.mode === 'edit') { | ||
this.addClass(EDIT_CLASS); | ||
this.removeClass(COMMAND_CLASS); | ||
} | ||
else { | ||
this.addClass(COMMAND_CLASS); | ||
this.removeClass(EDIT_CLASS); | ||
} | ||
}; | ||
/** | ||
* Handle changes to the model state. | ||
*/ | ||
BaseCellWidget.prototype.onModelChanged = function (sender, args) { | ||
this.update(); | ||
}; | ||
/** | ||
* Handle `after-attach` messages to the cell. | ||
*/ | ||
BaseCellWidget.prototype.onAfterAttach = function (msg) { | ||
this.update(); | ||
}; | ||
return BaseCellWidget; | ||
@@ -234,3 +177,3 @@ }(phosphor_widget_1.Widget)); | ||
this.layout.addChild(this._rendered); | ||
this.update(); | ||
this.model.stateChanged.connect(this.onModelChanged, this); | ||
} | ||
@@ -281,3 +224,2 @@ Object.defineProperty(MarkdownCellWidget.prototype, "rendered", { | ||
MarkdownCellWidget.prototype.onModelChanged = function (sender, args) { | ||
_super.prototype.onModelChanged.call(this, sender, args); | ||
switch (args.name) { | ||
@@ -284,0 +226,0 @@ case 'rendered': |
@@ -28,6 +28,2 @@ import { IDisposable } from 'phosphor-disposable'; | ||
/** | ||
* Whether the editor is focused. | ||
*/ | ||
focused: boolean; | ||
/** | ||
* Whether the text editor has a fixed maximum height. | ||
@@ -122,6 +118,2 @@ * | ||
/** | ||
* Whether the editor is focused for editing. | ||
*/ | ||
focused: boolean; | ||
/** | ||
* The tabSize number for the editor model. | ||
@@ -128,0 +120,0 @@ */ |
@@ -93,15 +93,2 @@ // Copyright (c) Jupyter Development Team. | ||
}); | ||
Object.defineProperty(EditorModel.prototype, "focused", { | ||
/** | ||
* Whether the editor is focused for editing. | ||
*/ | ||
get: function () { | ||
return EditorModelPrivate.focusedProperty.get(this); | ||
}, | ||
set: function (value) { | ||
EditorModelPrivate.focusedProperty.set(this, value); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(EditorModel.prototype, "tabSize", { | ||
@@ -209,10 +196,2 @@ /** | ||
/** | ||
* The property descriptor for the editor focused property. | ||
*/ | ||
EditorModelPrivate.focusedProperty = new phosphor_properties_1.Property({ | ||
name: 'focused', | ||
value: false, | ||
notify: EditorModelPrivate.stateChangedSignal | ||
}); | ||
/** | ||
* The property descriptor for the editor text. | ||
@@ -219,0 +198,0 @@ */ |
@@ -15,2 +15,6 @@ import 'codemirror/mode/meta'; | ||
model: IEditorModel; | ||
/** | ||
* Focus the editor. | ||
*/ | ||
focus(): void; | ||
} | ||
@@ -33,2 +37,6 @@ /** | ||
/** | ||
* Focus the editor. | ||
*/ | ||
focus(): void; | ||
/** | ||
* Dispose of the resources held by the widget. | ||
@@ -35,0 +43,0 @@ */ |
@@ -15,2 +15,6 @@ // Copyright (c) Jupyter Development Team. | ||
/** | ||
* The class name added to Editor widget instances. | ||
*/ | ||
var EDITOR_CLASS = 'jp-Editor'; | ||
/** | ||
* The class name added to CodeMirrorWidget instances. | ||
@@ -41,2 +45,3 @@ */ | ||
this._needsUpdate = false; | ||
this.addClass(EDITOR_CLASS); | ||
this.addClass(CODEMIRROR_CLASS); | ||
@@ -57,8 +62,2 @@ this._editor = CodeMirror(this.node); | ||
}); | ||
this._editor.on('focus', function () { | ||
_this._model.focused = true; | ||
}); | ||
this._editor.on('blur', function () { | ||
_this._model.focused = false; | ||
}); | ||
model.stateChanged.connect(this.onModelStateChanged, this); | ||
@@ -80,2 +79,8 @@ } | ||
/** | ||
* Focus the editor. | ||
*/ | ||
CodeMirrorWidget.prototype.focus = function () { | ||
this._editor.focus(); | ||
}; | ||
/** | ||
* Dispose of the resources held by the widget. | ||
@@ -235,6 +240,2 @@ */ | ||
break; | ||
case 'focused': | ||
if (args.newValue) { | ||
this._editor.focus(); | ||
} | ||
} | ||
@@ -241,0 +242,0 @@ }; |
@@ -20,10 +20,5 @@ // Copyright (c) Jupyter Development Team. | ||
pythonConf.singleOperators = new RegExp('^[\\+\\-\\*/%&|\\^~<>!\\?]'); | ||
if (pythonConf.version === 3) { | ||
pythonConf.identifiers = new RegExp('^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*'); | ||
} | ||
else if (pythonConf.version === 2) { | ||
pythonConf.identifiers = new RegExp('^[_A-Za-z][_A-Za-z0-9]*'); | ||
} | ||
pythonConf.identifiers = new RegExp('^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*'); | ||
return CodeMirror.getMode(config, pythonConf); | ||
}); | ||
CodeMirror.defineMIME('text/x-ipython', 'ipython'); |
export * from './model'; | ||
export * from './nbformat'; | ||
export * from './manager'; | ||
export * from './serialize'; | ||
export * from './selector'; | ||
export * from './trust'; | ||
export * from './widget'; |
@@ -9,4 +9,6 @@ // Copyright (c) Jupyter Development Team. | ||
__export(require('./nbformat')); | ||
__export(require('./manager')); | ||
__export(require('./serialize')); | ||
__export(require('./selector')); | ||
__export(require('./trust')); | ||
__export(require('./widget')); |
@@ -1,2 +0,2 @@ | ||
import { INotebookSession, IContentsManager } from 'jupyter-js-services'; | ||
import { INotebookSession } from 'jupyter-js-services'; | ||
import { IDisposable } from 'phosphor-disposable'; | ||
@@ -12,2 +12,8 @@ import { IObservableList } from 'phosphor-observablelist'; | ||
/** | ||
* The interactivity modes for the notebook. | ||
*/ | ||
export declare type NotebookMode = 'command' | 'edit'; | ||
import './codemirror-ipython'; | ||
import './codemirror-ipythongfm'; | ||
/** | ||
* The definition of a model object for a notebook widget. | ||
@@ -28,2 +34,6 @@ */ | ||
/** | ||
* The interactivity mode of the notebook. | ||
*/ | ||
mode: NotebookMode; | ||
/** | ||
* Whether the notebook has unsaved changes. | ||
@@ -52,2 +62,14 @@ */ | ||
/** | ||
* Select a cell. | ||
*/ | ||
select(cell: ICellModel): void; | ||
/** | ||
* Deselect a cell. | ||
*/ | ||
deselect(cell: ICellModel): void; | ||
/** | ||
* Weheter a cell is selected. | ||
*/ | ||
isSelected(cell: ICellModel): boolean; | ||
/** | ||
* A factory for creating a new code cell. | ||
@@ -88,6 +110,2 @@ * | ||
/** | ||
* Save the notebook state. | ||
*/ | ||
save(): Promise<void>; | ||
/** | ||
* The metadata associated with the notebook. | ||
@@ -116,3 +134,3 @@ */ | ||
*/ | ||
constructor(manager: IContentsManager); | ||
constructor(); | ||
/** | ||
@@ -154,2 +172,6 @@ * A signal emitted when the state of the model changes. | ||
/** | ||
* The mode of the notebook. | ||
*/ | ||
mode: NotebookMode; | ||
/** | ||
* Whether the notebook has unsaved changes. | ||
@@ -170,2 +192,14 @@ */ | ||
/** | ||
* Select a cell. | ||
*/ | ||
select(cell: ICellModel): void; | ||
/** | ||
* Deselect a cell. | ||
*/ | ||
deselect(cell: ICellModel): void; | ||
/** | ||
* Weheter a cell is selected. | ||
*/ | ||
isSelected(cell: ICellModel): boolean; | ||
/** | ||
* Create a code cell model. | ||
@@ -187,9 +221,2 @@ */ | ||
/** | ||
* Save the notebook and clear the dirty state of the model. | ||
* | ||
* #### Notes | ||
* Also updates the metadata if there is a current session. | ||
*/ | ||
save(): Promise<void>; | ||
/** | ||
* Execute the given cell. | ||
@@ -203,15 +230,6 @@ */ | ||
/** | ||
* Save the notebook contents to disk. | ||
*/ | ||
private _save(); | ||
/** | ||
* Handle a change in the cells list. | ||
*/ | ||
private _onCellsChanged(list, change); | ||
/** | ||
* Handle a change to a cell state. | ||
*/ | ||
private _onCellStateChanged(cell, change); | ||
private _cells; | ||
private _manager; | ||
} |
@@ -1,2 +0,6 @@ | ||
"use strict"; | ||
// Copyright (c) Jupyter Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||
'use strict'; | ||
var CodeMirror = require('codemirror'); | ||
var jupyter_js_utils_1 = require('jupyter-js-utils'); | ||
var phosphor_observablelist_1 = require('phosphor-observablelist'); | ||
@@ -9,3 +13,4 @@ var phosphor_properties_1 = require('phosphor-properties'); | ||
var cells_1 = require('../cells'); | ||
var serialize_1 = require('./serialize'); | ||
require('./codemirror-ipython'); | ||
require('./codemirror-ipythongfm'); | ||
/** | ||
@@ -18,6 +23,4 @@ * An implementation of a notebook Model. | ||
*/ | ||
function NotebookModel(manager) { | ||
function NotebookModel() { | ||
this._cells = null; | ||
this._manager = null; | ||
this._manager = manager; | ||
this._cells = new phosphor_observablelist_1.ObservableList(); | ||
@@ -138,7 +141,2 @@ this._cells.changed.connect(this._onCellsChanged, this); | ||
NotebookModelPrivate.activeCellIndexProperty.set(this, value); | ||
var cells = this.cells; | ||
for (var i = 0; i < cells.length; i++) { | ||
var cell = cells.get(i); | ||
cell.active = value === i; | ||
} | ||
}, | ||
@@ -148,2 +146,15 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(NotebookModel.prototype, "mode", { | ||
/** | ||
* The mode of the notebook. | ||
*/ | ||
get: function () { | ||
return NotebookModelPrivate.modeProperty.get(this); | ||
}, | ||
set: function (value) { | ||
NotebookModelPrivate.modeProperty.set(this, value); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(NotebookModel.prototype, "dirty", { | ||
@@ -193,2 +204,20 @@ /** | ||
/** | ||
* Select a cell. | ||
*/ | ||
NotebookModel.prototype.select = function (cell) { | ||
NotebookModelPrivate.selectedProperty.set(cell, true); | ||
}; | ||
/** | ||
* Deselect a cell. | ||
*/ | ||
NotebookModel.prototype.deselect = function (cell) { | ||
NotebookModelPrivate.selectedProperty.set(cell, false); | ||
}; | ||
/** | ||
* Weheter a cell is selected. | ||
*/ | ||
NotebookModel.prototype.isSelected = function (cell) { | ||
return NotebookModelPrivate.selectedProperty.get(cell); | ||
}; | ||
/** | ||
* Create a code cell model. | ||
@@ -282,2 +311,3 @@ */ | ||
} | ||
this.mode = 'command'; | ||
if (cells_1.isMarkdownCellModel(cell)) { | ||
@@ -293,32 +323,4 @@ cell.trusted = true; | ||
} | ||
if (this.activeCellIndex === this.cells.length - 1) { | ||
var cell_1 = this.createCodeCell(); | ||
this.cells.add(cell_1); | ||
cell_1.mode = 'edit'; // This already sets the new index. | ||
} | ||
else { | ||
this.activeCellIndex += 1; | ||
} | ||
}; | ||
/** | ||
* Save the notebook and clear the dirty state of the model. | ||
* | ||
* #### Notes | ||
* Also updates the metadata if there is a current session. | ||
*/ | ||
NotebookModel.prototype.save = function () { | ||
var _this = this; | ||
if (!this.session || this.session.isDisposed) { | ||
return this._save(); | ||
} | ||
return this.session.kernel.getKernelSpec().then(function (spec) { | ||
_this.metadata.kernelspec.display_name = spec.display_name; | ||
_this.metadata.kernelspec.name = _this.session.kernel.name; | ||
return _this.session.kernel.kernelInfo(); | ||
}).then(function (info) { | ||
_this.metadata.language_info = info.language_info; | ||
return _this._save(); | ||
}); | ||
}; | ||
/** | ||
* Execute the given cell. | ||
@@ -330,2 +332,3 @@ */ | ||
} | ||
this.dirty = true; | ||
var text = cell.input.textEditor.text.trim(); | ||
@@ -372,12 +375,2 @@ if (!text) { | ||
/** | ||
* Save the notebook contents to disk. | ||
*/ | ||
NotebookModel.prototype._save = function () { | ||
var _this = this; | ||
var content = serialize_1.serialize(this); | ||
var name = this.session.notebookPath; | ||
return this._manager.save(name, { type: 'notebook', content: content }) | ||
.then(function () { _this.dirty = false; }); | ||
}; | ||
/** | ||
* Handle a change in the cells list. | ||
@@ -389,3 +382,2 @@ */ | ||
var cell = change.newValue; | ||
cell.stateChanged.connect(this._onCellStateChanged, this); | ||
this.activeCellIndex = change.newIndex; | ||
@@ -400,21 +392,9 @@ break; | ||
for (var _i = 0, oldValues_1 = oldValues; _i < oldValues_1.length; _i++) { | ||
var cell_2 = oldValues_1[_i]; | ||
cell_2.dispose(); | ||
var cell_1 = oldValues_1[_i]; | ||
cell_1.dispose(); | ||
} | ||
break; | ||
} | ||
this.dirty = true; | ||
}; | ||
/** | ||
* Handle a change to a cell state. | ||
*/ | ||
NotebookModel.prototype._onCellStateChanged = function (cell, change) { | ||
if (change.name === 'mode') { | ||
var cells = this.cells; | ||
for (var i = 0; i < cells.length; i++) { | ||
if (cells.get(i) === cell) { | ||
this.activeCellIndex = i; | ||
} | ||
} | ||
} | ||
}; | ||
return NotebookModel; | ||
@@ -452,2 +432,3 @@ }()); | ||
name: 'session', | ||
changed: sessionChanged, | ||
notify: NotebookModelPrivate.stateChangedSignal, | ||
@@ -470,4 +451,11 @@ }); | ||
/** | ||
* A property descriptor for the dirty state | ||
* A property descriptor for the notebook mode. | ||
*/ | ||
NotebookModelPrivate.modeProperty = new phosphor_properties_1.Property({ | ||
name: 'mode', | ||
notify: NotebookModelPrivate.stateChangedSignal, | ||
}); | ||
/** | ||
* A property descriptor for the dirty state of the notebook. | ||
*/ | ||
NotebookModelPrivate.dirtyProperty = new phosphor_properties_1.Property({ | ||
@@ -477,2 +465,71 @@ name: 'dirty', | ||
}); | ||
/** | ||
* An attached property for the selected state of a cell. | ||
*/ | ||
NotebookModelPrivate.selectedProperty = new phosphor_properties_1.Property({ | ||
name: 'selected' | ||
}); | ||
/* | ||
* Handle a change to the model session. | ||
*/ | ||
function sessionChanged(model, session) { | ||
model.session.kernelChanged.connect(function () { kernelChanged(model); }); | ||
// Update the kernel data now. | ||
kernelChanged(model); | ||
} | ||
/** | ||
* Handle a change to the model kernel. | ||
*/ | ||
function kernelChanged(model) { | ||
var session = model.session; | ||
var kernel = session.kernel; | ||
var metadata = jupyter_js_utils_1.copy(model.metadata || {}); | ||
kernel.getKernelSpec().then(function (spec) { | ||
metadata.kernelspec.display_name = spec.display_name; | ||
metadata.kernelspec.name = session.kernel.name; | ||
return session.kernel.kernelInfo(); | ||
}).then(function (info) { | ||
metadata.language_info = info.language_info; | ||
// Use the codemirror mode if given since some kernels rely on it. | ||
var mode = metadata.language_info.codemirror_mode; | ||
var mime = ''; | ||
if (mode) { | ||
if (typeof mode === 'string') { | ||
if (CodeMirror.modes.hasOwnProperty(mode)) { | ||
mode = CodeMirror.modes[mode]; | ||
} | ||
else { | ||
mode = CodeMirror.findModeByName(mode); | ||
} | ||
} | ||
else if (mode.mime) { | ||
} | ||
else if (mode.name) { | ||
if (CodeMirror.modes.hasOwnProperty(mode.name)) { | ||
mode = CodeMirror.modes[mode.name]; | ||
} | ||
else { | ||
mode = CodeMirror.findModeByName(mode); | ||
} | ||
} | ||
if (mode) | ||
mime = mode.mime; | ||
} | ||
else { | ||
mime = info.language_info.mimetype; | ||
} | ||
if (mime) { | ||
model.defaultMimetype = mime; | ||
var cells = model.cells; | ||
for (var i = 0; i < cells.length; i++) { | ||
var cell = cells.get(i); | ||
if (cells_1.isCodeCellModel(cell)) { | ||
cell.input.textEditor.mimetype = mime; | ||
} | ||
} | ||
} | ||
// Trigger a change to the notebook metadata. | ||
model.metadata = metadata; | ||
}); | ||
} | ||
})(NotebookModelPrivate || (NotebookModelPrivate = {})); |
@@ -1,2 +0,3 @@ | ||
import { INotebookContent } from './nbformat'; | ||
import { ICellModel } from '../cells'; | ||
import { INotebookContent, ICell } from './nbformat'; | ||
import { INotebookModel } from './model'; | ||
@@ -11,1 +12,9 @@ /** | ||
export declare function deserialize(data: INotebookContent, model: INotebookModel): void; | ||
/** | ||
* Serialize a cell model. | ||
*/ | ||
export declare function serializeCell(cell: ICellModel): ICell; | ||
/** | ||
* Deserialize cell data. | ||
*/ | ||
export declare function deserializeCell(data: ICell, model: ICellModel): void; |
@@ -81,2 +81,3 @@ // Copyright (c) Jupyter Development Team. | ||
} | ||
exports.serializeCell = serializeCell; | ||
/** | ||
@@ -108,1 +109,2 @@ * Deserialize cell data. | ||
} | ||
exports.deserializeCell = deserializeCell; |
@@ -1,2 +0,1 @@ | ||
import { INotebookSession, IKernel } from 'jupyter-js-services'; | ||
import { Message } from 'phosphor-messaging'; | ||
@@ -6,24 +5,23 @@ import { IChangedArgs } from 'phosphor-properties'; | ||
import { Widget } from 'phosphor-widget'; | ||
import { ICellModel } from '../cells'; | ||
import { ICellModel, BaseCellWidget } from '../cells'; | ||
import { NotebookManager } from './manager'; | ||
import { INotebookModel } from './model'; | ||
import './codemirror-ipython'; | ||
import './codemirror-ipythongfm'; | ||
/** | ||
* A widget for a notebook. | ||
* A widget which contains a toolbar and a notebook. | ||
*/ | ||
export declare class NotebookWidget extends Widget { | ||
export declare class NotebookPane extends Widget { | ||
/** | ||
* Create a new cell widget given a cell model. | ||
* Create a new toolbar for the pane. | ||
*/ | ||
static createCell(cell: ICellModel): Widget; | ||
static createToolbar(manager: NotebookManager): NotebookToolbar; | ||
/** | ||
* Create a new toolbar for the notebook. | ||
* Create a new notebook for the pane. | ||
*/ | ||
static createToolbar(model: INotebookModel): NotebookToolbar; | ||
static createNotebook(model: INotebookModel): NotebookWidget; | ||
/** | ||
* Construct a notebook widget. | ||
* Construct a notebook pane widget. | ||
*/ | ||
constructor(model: INotebookModel); | ||
constructor(manager: NotebookManager); | ||
/** | ||
* Get the model for the widget. | ||
* Get the toolbar used by the widget. | ||
* | ||
@@ -33,5 +31,5 @@ * #### Notes | ||
*/ | ||
model: INotebookModel; | ||
toolbar: NotebookToolbar; | ||
/** | ||
* Get the toolbar used by the widget. | ||
* Get the notebook used by the widget. | ||
* | ||
@@ -41,64 +39,84 @@ * #### Notes | ||
*/ | ||
toolbar: NotebookToolbar; | ||
notebook: NotebookWidget; | ||
private _toolbar; | ||
private _notebook; | ||
} | ||
/** | ||
* A widget holding the notebook cells. | ||
*/ | ||
export declare class NotebookWidget extends Widget { | ||
/** | ||
* Dispose of the resources held by the widget. | ||
* Create a new cell widget given a cell model. | ||
*/ | ||
dispose(): void; | ||
static createCell(cell: ICellModel): BaseCellWidget; | ||
/** | ||
* Insert a new code cell above the current cell. | ||
* Construct a notebook widget. | ||
*/ | ||
insertAbove(): void; | ||
constructor(model: INotebookModel); | ||
/** | ||
* Insert a new code cell below the current cell. | ||
* Get the model for the widget. | ||
* | ||
* #### Notes | ||
* This is a read-only property. | ||
*/ | ||
insertBelow(): void; | ||
model: INotebookModel; | ||
/** | ||
* Delete selected cell(s), putting them on the undelete stack. | ||
* Dispose of the resources held by the widget. | ||
*/ | ||
delete(): void; | ||
dispose(): void; | ||
/** | ||
* Undelete the cell(s) at the top of the undelete stack. | ||
* Handle the DOM events for the widget. | ||
* | ||
* @param event - The DOM event sent to the widget. | ||
* | ||
* #### Notes | ||
* This method implements the DOM `EventListener` interface and is | ||
* called in response to events on the dock panel's node. It should | ||
* not be called directly by user code. | ||
*/ | ||
undelete(): void; | ||
handleEvent(event: Event): void; | ||
/** | ||
* Copy the selected cell(s) to the clipboard. | ||
* Handle `after_attach` messages for the widget. | ||
*/ | ||
copy(): void; | ||
protected onAfterAttach(msg: Message): void; | ||
/** | ||
* Cut the selected cell(s). | ||
* Handle `before_detach` messages for the widget. | ||
*/ | ||
cut(): void; | ||
protected onBeforeDetach(msg: Message): void; | ||
/** | ||
* Paste cell(s) from the clipboard. | ||
* Find the cell index containing the target html element. | ||
* | ||
* #### Notes | ||
* Returns -1 if the cell is not found. | ||
*/ | ||
paste(): void; | ||
protected findCell(node: HTMLElement): number; | ||
/** | ||
* Merge selected cells. | ||
* Handle `update-request` messages sent to the widget. | ||
*/ | ||
merge(): void; | ||
protected onUpdateRequest(msg: Message): void; | ||
/** | ||
* Change the selected cell type(s). | ||
* Handle changes to the notebook model. | ||
*/ | ||
changeCellType(value: string): void; | ||
protected onModelChanged(model: INotebookModel, args: IChangedArgs<any>): void; | ||
/** | ||
* Run the selected cell(s). | ||
* Handle a change cells event. | ||
*/ | ||
run(): void; | ||
protected onCellsChanged(sender: IObservableList<ICellModel>, args: IListChangedArgs<ICellModel>): void; | ||
/** | ||
* Interrupt the kernel. | ||
* Handle `click` events for the widget. | ||
*/ | ||
interrupt(): Promise<void>; | ||
private _evtClick(event); | ||
/** | ||
* Restart the kernel. | ||
* Handle `dblclick` events for the widget. | ||
*/ | ||
restart(): Promise<void>; | ||
private _evtDblClick(event); | ||
/** | ||
* Handle a change cells event. This function must be on this class | ||
* because it uses the static [createCell] method. | ||
* Handle `blur` events for the widget. | ||
*/ | ||
protected onCellsChanged(sender: IObservableList<ICellModel>, args: IListChangedArgs<ICellModel>): void; | ||
private _undeleteStack; | ||
private _evtBlur(event); | ||
/** | ||
* Handle `focus` events for the widget. | ||
*/ | ||
private _evtFocus(event); | ||
private _model; | ||
private _toolbar; | ||
private _notebook; | ||
} | ||
@@ -116,3 +134,3 @@ /** | ||
*/ | ||
constructor(model: INotebookModel); | ||
constructor(manager: NotebookManager); | ||
/** | ||
@@ -137,41 +155,2 @@ * Get the model used by the widget. | ||
kernelIndicatorNode: HTMLElement; | ||
/** | ||
* Clone a cell model. | ||
*/ | ||
cloneCell(cell: ICellModel): ICellModel; | ||
/** | ||
* Insert a new code cell above the current cell. | ||
*/ | ||
insertAbove(): void; | ||
/** | ||
* Insert a node code cell below the current cell. | ||
*/ | ||
insertBelow(): void; | ||
/** | ||
* Copy the selected cell(s) to the clipboard. | ||
*/ | ||
copy(): void; | ||
/** | ||
* Cut the selected cell(s). | ||
*/ | ||
cut(): void; | ||
/** | ||
* Paste cell(s) from the clipboard. | ||
*/ | ||
paste(): void; | ||
/** | ||
* Change the selected cell type(s). | ||
*/ | ||
changeCellType(value: string): void; | ||
/** | ||
* Run the selected cell(s). | ||
*/ | ||
run(): void; | ||
/** | ||
* Interrupt the kernel. | ||
*/ | ||
interrupt(): Promise<void>; | ||
/** | ||
* Restart the kernel. | ||
*/ | ||
restart(): Promise<void>; | ||
@@ -200,3 +179,3 @@ /** | ||
*/ | ||
protected onModelChanged(sender: INotebookModel, args: IChangedArgs<any>): void; | ||
protected onModelChanged(model: INotebookModel, args: IChangedArgs<any>): void; | ||
/** | ||
@@ -207,12 +186,7 @@ * Handle a change to the session. | ||
/** | ||
* Handle a change to the kernel. | ||
*/ | ||
protected handleKernel(session: INotebookSession, kernel: IKernel): void; | ||
/** | ||
* Handle `click` events for the widget. | ||
*/ | ||
private _evtClick(event); | ||
private _manager; | ||
private _model; | ||
private _copied; | ||
private _cut; | ||
} |
@@ -15,4 +15,2 @@ // Copyright (c) Jupyter Development Team. | ||
var cells_1 = require('../cells'); | ||
require('./codemirror-ipython'); | ||
require('./codemirror-ipythongfm'); | ||
/** | ||
@@ -23,9 +21,9 @@ * The class name added to notebook widgets. | ||
/** | ||
* The class name added to notebook container widget. | ||
* The class name added to notebook container widgets. | ||
*/ | ||
var NB_CONTAINER = 'jp-Notebook-Container'; | ||
var NB_CONTAINER = 'jp-Notebook-container'; | ||
/** | ||
* The class name added to notebook cell widget. | ||
* The class name added to notebook panes. | ||
*/ | ||
var NB_CELLS = 'jp-Notebook-Cells'; | ||
var NB_PANE = 'jp-Notebook-pane'; | ||
/** | ||
@@ -36,3 +34,3 @@ * The class name added to notebook widget cells. | ||
/** | ||
* The class name added to the notebook toolbar. | ||
* The class name added to notebook toolbars. | ||
*/ | ||
@@ -101,71 +99,61 @@ var NB_TOOLBAR = 'jp-NBToolbar'; | ||
/** | ||
* The maximum size of the delete stack. | ||
* The class name added to a notebook in edit mode. | ||
*/ | ||
var DELETE_STACK_SIZE = 10; | ||
var EDIT_CLASS = 'jp-mod-editMode'; | ||
/** | ||
* A widget for a notebook. | ||
* The class name added to a notebook in command mode. | ||
*/ | ||
var NotebookWidget = (function (_super) { | ||
__extends(NotebookWidget, _super); | ||
var COMMAND_CLASS = 'jp-mod-commandMode'; | ||
/** | ||
* The class name added to notebook editor instances. | ||
*/ | ||
var NB_EDITOR_CLASS = 'jp-Notebook-editor'; | ||
/** | ||
* The class name added to the active cell. | ||
*/ | ||
var ACTIVE_CLASS = 'jp-mod-active'; | ||
/** | ||
* The class name added to selected cells. | ||
*/ | ||
var SELECTED_CLASS = 'jp-mod-selected'; | ||
/** | ||
* A widget which contains a toolbar and a notebook. | ||
*/ | ||
var NotebookPane = (function (_super) { | ||
__extends(NotebookPane, _super); | ||
/** | ||
* Construct a notebook widget. | ||
* Construct a notebook pane widget. | ||
*/ | ||
function NotebookWidget(model) { | ||
function NotebookPane(manager) { | ||
_super.call(this); | ||
this._undeleteStack = []; | ||
this._model = null; | ||
this._toolbar = null; | ||
this._notebook = null; | ||
this.addClass(NB_CLASS); | ||
this._model = model; | ||
this.addClass(NB_PANE); | ||
var constructor = this.constructor; | ||
this._toolbar = constructor.createToolbar(manager); | ||
this.layout = new phosphor_panel_1.PanelLayout(); | ||
var layout = this.layout; | ||
this._toolbar = constructor.createToolbar(model); | ||
layout.addChild(this._toolbar); | ||
layout.insertChild(0, this._toolbar); | ||
var container = new phosphor_widget_1.Widget(); | ||
container.addClass(NB_CONTAINER); | ||
container.layout = new phosphor_panel_1.PanelLayout(); | ||
this._notebook = new NotebookCells(model); | ||
this._notebook = constructor.createNotebook(manager.model); | ||
container.layout.addChild(this._notebook); | ||
layout.addChild(container); | ||
var cellsLayout = this._notebook.layout; | ||
var factory = constructor.createCell; | ||
for (var i = 0; i < model.cells.length; i++) { | ||
cellsLayout.addChild(factory(model.cells.get(i))); | ||
} | ||
model.cells.changed.connect(this.onCellsChanged, this); | ||
} | ||
/** | ||
* Create a new cell widget given a cell model. | ||
* Create a new toolbar for the pane. | ||
*/ | ||
NotebookWidget.createCell = function (cell) { | ||
var widget; | ||
switch (cell.type) { | ||
case 'code': | ||
widget = new cells_1.CodeCellWidget(cell); | ||
break; | ||
case 'markdown': | ||
widget = new cells_1.MarkdownCellWidget(cell); | ||
break; | ||
case 'raw': | ||
widget = new cells_1.RawCellWidget(cell); | ||
break; | ||
default: | ||
// If there are any issues, just return a blank placeholder | ||
// widget so the lists stay in sync. | ||
widget = new phosphor_widget_1.Widget(); | ||
} | ||
widget.addClass(NB_CELL_CLASS); | ||
return widget; | ||
NotebookPane.createToolbar = function (manager) { | ||
return new NotebookToolbar(manager); | ||
}; | ||
/** | ||
* Create a new toolbar for the notebook. | ||
* Create a new notebook for the pane. | ||
*/ | ||
NotebookWidget.createToolbar = function (model) { | ||
return new NotebookToolbar(model); | ||
NotebookPane.createNotebook = function (model) { | ||
return new NotebookWidget(model); | ||
}; | ||
Object.defineProperty(NotebookWidget.prototype, "model", { | ||
Object.defineProperty(NotebookPane.prototype, "toolbar", { | ||
/** | ||
* Get the model for the widget. | ||
* Get the toolbar used by the widget. | ||
* | ||
@@ -176,3 +164,3 @@ * #### Notes | ||
get: function () { | ||
return this._model; | ||
return this._toolbar; | ||
}, | ||
@@ -182,5 +170,5 @@ enumerable: true, | ||
}); | ||
Object.defineProperty(NotebookWidget.prototype, "toolbar", { | ||
Object.defineProperty(NotebookPane.prototype, "notebook", { | ||
/** | ||
* Get the toolbar used by the widget. | ||
* Get the notebook used by the widget. | ||
* | ||
@@ -191,3 +179,3 @@ * #### Notes | ||
get: function () { | ||
return this._toolbar; | ||
return this._notebook; | ||
}, | ||
@@ -197,202 +185,52 @@ enumerable: true, | ||
}); | ||
return NotebookPane; | ||
}(phosphor_widget_1.Widget)); | ||
exports.NotebookPane = NotebookPane; | ||
/** | ||
* A widget holding the notebook cells. | ||
*/ | ||
var NotebookWidget = (function (_super) { | ||
__extends(NotebookWidget, _super); | ||
/** | ||
* Dispose of the resources held by the widget. | ||
* Construct a notebook widget. | ||
*/ | ||
NotebookWidget.prototype.dispose = function () { | ||
this._model.dispose(); | ||
function NotebookWidget(model) { | ||
_super.call(this); | ||
this._model = null; | ||
_super.prototype.dispose.call(this); | ||
}; | ||
/** | ||
* Insert a new code cell above the current cell. | ||
*/ | ||
NotebookWidget.prototype.insertAbove = function () { | ||
this._toolbar.insertAbove(); | ||
}; | ||
/** | ||
* Insert a new code cell below the current cell. | ||
*/ | ||
NotebookWidget.prototype.insertBelow = function () { | ||
this._toolbar.insertBelow(); | ||
}; | ||
/** | ||
* Delete selected cell(s), putting them on the undelete stack. | ||
*/ | ||
NotebookWidget.prototype.delete = function () { | ||
var undelete = []; | ||
var model = this.model; | ||
this.node.tabIndex = -1; // Allow the widget to take focus. | ||
this.addClass(NB_CLASS); | ||
this._model = model; | ||
this.layout = new phosphor_panel_1.PanelLayout(); | ||
var constructor = this.constructor; | ||
var cellsLayout = this.layout; | ||
var factory = constructor.createCell; | ||
for (var i = 0; i < model.cells.length; i++) { | ||
var cell = model.cells.get(i); | ||
if (cell.selected || cell.active) { | ||
undelete.push(this._toolbar.cloneCell(cell)); | ||
model.cells.remove(cell); | ||
} | ||
cellsLayout.addChild(factory(model.cells.get(i))); | ||
} | ||
if (undelete.length) { | ||
this._undeleteStack.push(undelete); | ||
} | ||
if (this._undeleteStack.length > DELETE_STACK_SIZE) { | ||
this._undeleteStack.shift(); | ||
} | ||
}; | ||
model.cells.changed.connect(this.onCellsChanged, this); | ||
model.stateChanged.connect(this.onModelChanged, this); | ||
} | ||
/** | ||
* Undelete the cell(s) at the top of the undelete stack. | ||
* Create a new cell widget given a cell model. | ||
*/ | ||
NotebookWidget.prototype.undelete = function () { | ||
var model = this.model; | ||
var index = model.activeCellIndex + 1; | ||
if (this._undeleteStack.length === 0) { | ||
return; | ||
} | ||
var undelete = this._undeleteStack.pop(); | ||
// Insert the undeleted cells in reverse order. | ||
for (var _i = 0, _a = undelete.reverse(); _i < _a.length; _i++) { | ||
var cell = _a[_i]; | ||
model.cells.insert(index, cell); | ||
} | ||
}; | ||
/** | ||
* Copy the selected cell(s) to the clipboard. | ||
*/ | ||
NotebookWidget.prototype.copy = function () { | ||
this._toolbar.copy(); | ||
}; | ||
/** | ||
* Cut the selected cell(s). | ||
*/ | ||
NotebookWidget.prototype.cut = function () { | ||
this._toolbar.cut(); | ||
}; | ||
/** | ||
* Paste cell(s) from the clipboard. | ||
*/ | ||
NotebookWidget.prototype.paste = function () { | ||
this._toolbar.paste(); | ||
}; | ||
/** | ||
* Merge selected cells. | ||
*/ | ||
NotebookWidget.prototype.merge = function () { | ||
var toMerge = []; | ||
var toDelete = []; | ||
var activeCell; | ||
var model = this.model; | ||
for (var i = 0; i < model.cells.length; i++) { | ||
var cell = model.cells.get(i); | ||
if (cell.selected || cell.active) { | ||
toMerge.push(cell.input.textEditor.text); | ||
} | ||
if (cell.active) { | ||
activeCell = cell; | ||
} | ||
else { | ||
toDelete.push(cell); | ||
} | ||
} | ||
// Make sure there are cells to merge. | ||
if (toMerge.length < 2 || !activeCell) { | ||
return; | ||
} | ||
// For rendered markdown cells, unrender before setting the text. | ||
if (activeCell.rendered) { | ||
activeCell.rendered = false; | ||
} | ||
// For all cells types, set the merged text. | ||
activeCell.input.textEditor.text = toMerge.join('\n\n'); | ||
// Remove the other cells and add them to the delete stack. | ||
var copies = []; | ||
for (var _i = 0, toDelete_1 = toDelete; _i < toDelete_1.length; _i++) { | ||
var cell = toDelete_1[_i]; | ||
copies.push(this._toolbar.cloneCell(cell)); | ||
model.cells.remove(cell); | ||
} | ||
this._undeleteStack.push(toDelete); | ||
// Make sure the previous cell is still active. | ||
model.activeCellIndex = model.cells.indexOf(activeCell); | ||
}; | ||
/** | ||
* Change the selected cell type(s). | ||
*/ | ||
NotebookWidget.prototype.changeCellType = function (value) { | ||
this._toolbar.changeCellType(value); | ||
}; | ||
/** | ||
* Run the selected cell(s). | ||
*/ | ||
NotebookWidget.prototype.run = function () { | ||
this._toolbar.run(); | ||
}; | ||
/** | ||
* Interrupt the kernel. | ||
*/ | ||
NotebookWidget.prototype.interrupt = function () { | ||
return this._toolbar.interrupt(); | ||
}; | ||
/** | ||
* Restart the kernel. | ||
*/ | ||
NotebookWidget.prototype.restart = function () { | ||
return this._toolbar.restart(); | ||
}; | ||
/** | ||
* Handle a change cells event. This function must be on this class | ||
* because it uses the static [createCell] method. | ||
*/ | ||
NotebookWidget.prototype.onCellsChanged = function (sender, args) { | ||
var layout = this._notebook.layout; | ||
var constructor = this.constructor; | ||
var factory = constructor.createCell; | ||
NotebookWidget.createCell = function (cell) { | ||
var widget; | ||
switch (args.type) { | ||
case phosphor_observablelist_1.ListChangeType.Add: | ||
layout.insertChild(args.newIndex, factory(args.newValue)); | ||
switch (cell.type) { | ||
case 'code': | ||
widget = new cells_1.CodeCellWidget(cell); | ||
break; | ||
case phosphor_observablelist_1.ListChangeType.Move: | ||
layout.insertChild(args.newIndex, layout.childAt(args.oldIndex)); | ||
case 'markdown': | ||
widget = new cells_1.MarkdownCellWidget(cell); | ||
break; | ||
case phosphor_observablelist_1.ListChangeType.Remove: | ||
widget = layout.childAt(args.oldIndex); | ||
layout.removeChild(widget); | ||
widget.dispose(); | ||
case 'raw': | ||
widget = new cells_1.RawCellWidget(cell); | ||
break; | ||
case phosphor_observablelist_1.ListChangeType.Replace: | ||
var oldValues = args.oldValue; | ||
for (var i = args.oldIndex; i < oldValues.length; i++) { | ||
widget = layout.childAt(args.oldIndex); | ||
layout.removeChild(widget); | ||
widget.dispose(); | ||
} | ||
var newValues = args.newValue; | ||
for (var i = newValues.length; i < 0; i--) { | ||
layout.insertChild(args.newIndex, factory(newValues[i])); | ||
} | ||
break; | ||
case phosphor_observablelist_1.ListChangeType.Set: | ||
widget = layout.childAt(args.newIndex); | ||
layout.removeChild(widget); | ||
widget.dispose(); | ||
layout.insertChild(args.newIndex, factory(args.newValue)); | ||
break; | ||
default: | ||
// If there are any issues, just return a blank placeholder | ||
// widget so the lists stay in sync. | ||
widget = new cells_1.BaseCellWidget(cell); | ||
} | ||
return widget; | ||
}; | ||
return NotebookWidget; | ||
}(phosphor_widget_1.Widget)); | ||
exports.NotebookWidget = NotebookWidget; | ||
/** | ||
* A widget holding the notebook cells. | ||
*/ | ||
var NotebookCells = (function (_super) { | ||
__extends(NotebookCells, _super); | ||
/** | ||
* Construct a notebook cells widget. | ||
*/ | ||
function NotebookCells(model) { | ||
_super.call(this); | ||
this._model = null; | ||
this.addClass(NB_CELLS); | ||
this._model = model; | ||
this.layout = new phosphor_panel_1.PanelLayout(); | ||
model.stateChanged.connect(this.onModelChanged, this); | ||
} | ||
Object.defineProperty(NotebookCells.prototype, "model", { | ||
Object.defineProperty(NotebookWidget.prototype, "model", { | ||
/** | ||
@@ -413,3 +251,3 @@ * Get the model for the widget. | ||
*/ | ||
NotebookCells.prototype.dispose = function () { | ||
NotebookWidget.prototype.dispose = function () { | ||
// Do nothing if already disposed. | ||
@@ -433,3 +271,3 @@ if (this.isDisposed) { | ||
*/ | ||
NotebookCells.prototype.handleEvent = function (event) { | ||
NotebookWidget.prototype.handleEvent = function (event) { | ||
switch (event.type) { | ||
@@ -442,2 +280,8 @@ case 'click': | ||
break; | ||
case 'blur': | ||
this._evtBlur(event); | ||
break; | ||
case 'focus': | ||
this._evtFocus(event); | ||
break; | ||
} | ||
@@ -448,5 +292,7 @@ }; | ||
*/ | ||
NotebookCells.prototype.onAfterAttach = function (msg) { | ||
NotebookWidget.prototype.onAfterAttach = function (msg) { | ||
this.node.addEventListener('click', this); | ||
this.node.addEventListener('dblclick', this); | ||
this.node.addEventListener('focus', this, true); | ||
this.node.addEventListener('blur', this, true); | ||
}; | ||
@@ -456,5 +302,7 @@ /** | ||
*/ | ||
NotebookCells.prototype.onBeforeDetach = function (msg) { | ||
NotebookWidget.prototype.onBeforeDetach = function (msg) { | ||
this.node.removeEventListener('click', this); | ||
this.node.removeEventListener('dblclick', this); | ||
this.node.removeEventListener('focus', this, true); | ||
this.node.removeEventListener('blur', this, true); | ||
}; | ||
@@ -467,3 +315,3 @@ /** | ||
*/ | ||
NotebookCells.prototype.findCell = function (node) { | ||
NotebookWidget.prototype.findCell = function (node) { | ||
// Trace up the DOM hierarchy to find the root cell node. | ||
@@ -486,18 +334,110 @@ // Then find the corresponding child and select it. | ||
/** | ||
* Handle changes to the model. | ||
* Handle `update-request` messages sent to the widget. | ||
*/ | ||
NotebookCells.prototype.onModelChanged = function (model, args) { | ||
if (args.name === 'activeCellIndex') { | ||
var layout = this.layout; | ||
var child = layout.childAt(args.newValue); | ||
if (!child) { | ||
return; | ||
NotebookWidget.prototype.onUpdateRequest = function (msg) { | ||
// Set the appropriate classes on the cells. | ||
var model = this.model; | ||
var layout = this.layout; | ||
var widget = layout.childAt(model.activeCellIndex); | ||
if (model.mode === 'edit') { | ||
this.addClass(EDIT_CLASS); | ||
this.removeClass(COMMAND_CLASS); | ||
if (widget) { | ||
widget.input.editor.focus(); | ||
} | ||
Private.scrollIfNeeded(this.parent.node, child.node); | ||
} | ||
else { | ||
this.addClass(COMMAND_CLASS); | ||
this.removeClass(EDIT_CLASS); | ||
this.node.focus(); | ||
} | ||
if (widget) { | ||
widget.addClass(ACTIVE_CLASS); | ||
} | ||
for (var i = 0; i < layout.childCount(); i++) { | ||
var cell = model.cells.get(i); | ||
widget = layout.childAt(i); | ||
if (i !== model.activeCellIndex) { | ||
widget.removeClass(ACTIVE_CLASS); | ||
} | ||
if (i === model.activeCellIndex || model.isSelected(cell)) { | ||
widget.addClass(SELECTED_CLASS); | ||
} | ||
else { | ||
widget.removeClass(SELECTED_CLASS); | ||
} | ||
} | ||
}; | ||
/** | ||
* Handle changes to the notebook model. | ||
*/ | ||
NotebookWidget.prototype.onModelChanged = function (model, args) { | ||
switch (args.name) { | ||
case 'mode': | ||
this.update(); | ||
break; | ||
case 'activeCellIndex': | ||
var layout = this.layout; | ||
var child = layout.childAt(args.newValue); | ||
if (!child) { | ||
return; | ||
} | ||
Private.scrollIfNeeded(this.parent.node, child.node); | ||
this.update(); | ||
} | ||
}; | ||
/** | ||
* Handle a change cells event. | ||
*/ | ||
NotebookWidget.prototype.onCellsChanged = function (sender, args) { | ||
var layout = this.layout; | ||
var constructor = this.constructor; | ||
var factory = constructor.createCell; | ||
var widget; | ||
switch (args.type) { | ||
case phosphor_observablelist_1.ListChangeType.Add: | ||
widget = factory(args.newValue); | ||
widget.addClass(NB_CELL_CLASS); | ||
widget.input.editor.addClass(NB_EDITOR_CLASS); | ||
layout.insertChild(args.newIndex, widget); | ||
break; | ||
case phosphor_observablelist_1.ListChangeType.Move: | ||
layout.insertChild(args.newIndex, layout.childAt(args.oldIndex)); | ||
break; | ||
case phosphor_observablelist_1.ListChangeType.Remove: | ||
widget = layout.childAt(args.oldIndex); | ||
layout.removeChild(widget); | ||
widget.dispose(); | ||
break; | ||
case phosphor_observablelist_1.ListChangeType.Replace: | ||
var oldValues = args.oldValue; | ||
for (var i = args.oldIndex; i < oldValues.length; i++) { | ||
widget = layout.childAt(args.oldIndex); | ||
layout.removeChild(widget); | ||
widget.dispose(); | ||
} | ||
var newValues = args.newValue; | ||
for (var i = newValues.length; i < 0; i--) { | ||
widget = factory(newValues[i]); | ||
widget.addClass(NB_CELL_CLASS); | ||
widget.input.editor.addClass(NB_EDITOR_CLASS); | ||
layout.insertChild(args.newIndex, widget); | ||
} | ||
break; | ||
case phosphor_observablelist_1.ListChangeType.Set: | ||
widget = layout.childAt(args.newIndex); | ||
layout.removeChild(widget); | ||
widget.dispose(); | ||
widget = factory(args.newValue); | ||
layout.insertChild(args.newIndex, widget); | ||
widget.addClass(NB_CELL_CLASS); | ||
widget.input.editor.addClass(NB_EDITOR_CLASS); | ||
break; | ||
} | ||
this.update(); | ||
}; | ||
/** | ||
* Handle `click` events for the widget. | ||
*/ | ||
NotebookCells.prototype._evtClick = function (event) { | ||
NotebookWidget.prototype._evtClick = function (event) { | ||
var model = this.model; | ||
@@ -515,3 +455,3 @@ if (!model.readOnly) { | ||
*/ | ||
NotebookCells.prototype._evtDblClick = function (event) { | ||
NotebookWidget.prototype._evtDblClick = function (event) { | ||
var model = this._model; | ||
@@ -528,7 +468,38 @@ if (model.readOnly) { | ||
cell.rendered = false; | ||
cell.mode = 'edit'; | ||
model.mode = 'edit'; | ||
} | ||
}; | ||
return NotebookCells; | ||
/** | ||
* Handle `blur` events for the widget. | ||
*/ | ||
NotebookWidget.prototype._evtBlur = function (event) { | ||
var node = event.target; | ||
// Trace up the DOM hierarchy looking for an editor node. | ||
while (node && node !== this.node) { | ||
if (node.classList.contains(NB_EDITOR_CLASS)) { | ||
this.model.mode = 'command'; | ||
break; | ||
} | ||
node = node.parentElement; | ||
} | ||
}; | ||
/** | ||
* Handle `focus` events for the widget. | ||
*/ | ||
NotebookWidget.prototype._evtFocus = function (event) { | ||
var node = event.target; | ||
// Trace up the DOM hierarchy to looking for an editor node. | ||
// Then find the corresponding child and activate it. | ||
while (node && node !== this.node) { | ||
if (node.classList.contains(NB_EDITOR_CLASS)) { | ||
this.model.mode = 'edit'; | ||
this.model.activeCellIndex = this.findCell(node); | ||
break; | ||
} | ||
node = node.parentElement; | ||
} | ||
}; | ||
return NotebookWidget; | ||
}(phosphor_widget_1.Widget)); | ||
exports.NotebookWidget = NotebookWidget; | ||
/** | ||
@@ -542,10 +513,10 @@ * A class which provides a notebook toolbar widget. | ||
*/ | ||
function NotebookToolbar(model) { | ||
function NotebookToolbar(manager) { | ||
var _this = this; | ||
_super.call(this); | ||
this._manager = null; | ||
this._model = null; | ||
this._copied = []; | ||
this._cut = []; | ||
this.addClass(NB_TOOLBAR); | ||
this._model = model; | ||
this._manager = manager; | ||
var model = this._model = manager.model; | ||
if (model.metadata && model.metadata.kernelspec) { | ||
@@ -562,3 +533,3 @@ this.kernelNameNode.textContent = model.metadata.kernelspec.display_name; | ||
this.cellTypeNode.addEventListener('change', function (event) { | ||
_this.changeCellType(_this.cellTypeNode.value); | ||
manager.changeCellType(_this.cellTypeNode.value); | ||
}); | ||
@@ -655,138 +626,5 @@ if (model.session) { | ||
}); | ||
/** | ||
* Clone a cell model. | ||
/* | ||
* Restart the kernel with a confirmation dialog. | ||
*/ | ||
NotebookToolbar.prototype.cloneCell = function (cell) { | ||
switch (cell.type) { | ||
case 'code': | ||
return this.model.createCodeCell(cell); | ||
case 'markdown': | ||
return this.model.createMarkdownCell(cell); | ||
default: | ||
return this.model.createRawCell(cell); | ||
} | ||
}; | ||
/** | ||
* Insert a new code cell above the current cell. | ||
*/ | ||
NotebookToolbar.prototype.insertAbove = function () { | ||
var cell = this.model.createCodeCell(); | ||
this.model.cells.insert(this.model.activeCellIndex, cell); | ||
}; | ||
/** | ||
* Insert a node code cell below the current cell. | ||
*/ | ||
NotebookToolbar.prototype.insertBelow = function () { | ||
var cell = this.model.createCodeCell(); | ||
this.model.cells.insert(this.model.activeCellIndex + 1, cell); | ||
}; | ||
/** | ||
* Copy the selected cell(s) to the clipboard. | ||
*/ | ||
NotebookToolbar.prototype.copy = function () { | ||
this._copied = []; | ||
this._cut = []; | ||
var model = this.model; | ||
for (var i = 0; i < model.cells.length; i++) { | ||
var cell = model.cells.get(i); | ||
if (cell.selected || cell.active) { | ||
this._copied.push(this.cloneCell(cell)); | ||
} | ||
} | ||
}; | ||
/** | ||
* Cut the selected cell(s). | ||
*/ | ||
NotebookToolbar.prototype.cut = function () { | ||
this._copied = []; | ||
this._cut = []; | ||
var model = this.model; | ||
for (var i = 0; i < model.cells.length; i++) { | ||
var cell = model.cells.get(i); | ||
if (cell.selected || cell.active) { | ||
this._cut.push(this.cloneCell(cell)); | ||
model.cells.remove(cell); | ||
} | ||
} | ||
}; | ||
/** | ||
* Paste cell(s) from the clipboard. | ||
*/ | ||
NotebookToolbar.prototype.paste = function () { | ||
var model = this.model; | ||
var cut = this._cut; | ||
var copied = this._copied; | ||
var index = model.activeCellIndex + 1; | ||
if (copied.length > 0) { | ||
// Insert copies of the original cells in reverse order. | ||
for (var _i = 0, _a = copied.reverse(); _i < _a.length; _i++) { | ||
var cell = _a[_i]; | ||
model.cells.insert(index, cell); | ||
} | ||
} | ||
else { | ||
// Insert the cut cell(s) in reverse order. | ||
for (var _b = 0, _c = cut.reverse(); _b < _c.length; _b++) { | ||
var cell = _c[_b]; | ||
model.cells.insert(index, cell); | ||
} | ||
} | ||
this._copied = []; | ||
this._cut = []; | ||
}; | ||
/** | ||
* Change the selected cell type(s). | ||
*/ | ||
NotebookToolbar.prototype.changeCellType = function (value) { | ||
var model = this.model; | ||
for (var i = 0; i < model.cells.length; i++) { | ||
var cell = model.cells.get(i); | ||
if (!cell.selected && !cell.active) { | ||
continue; | ||
} | ||
var newCell = void 0; | ||
switch (value) { | ||
case 'code': | ||
newCell = model.createCodeCell(cell); | ||
break; | ||
case 'markdown': | ||
newCell = model.createMarkdownCell(cell); | ||
newCell.rendered = false; | ||
break; | ||
default: | ||
newCell = model.createRawCell(cell); | ||
break; | ||
} | ||
model.cells.remove(cell); | ||
model.cells.insert(i, newCell); | ||
} | ||
}; | ||
/** | ||
* Run the selected cell(s). | ||
*/ | ||
NotebookToolbar.prototype.run = function () { | ||
var model = this.model; | ||
var cells = model.cells; | ||
var selected = []; | ||
for (var i = 0; i < cells.length; i++) { | ||
var cell = cells.get(i); | ||
if (cell.selected || cell.active) { | ||
selected.push(cell); | ||
} | ||
} | ||
for (var _i = 0, selected_1 = selected; _i < selected_1.length; _i++) { | ||
var cell = selected_1[_i]; | ||
model.activeCellIndex = cells.indexOf(cell); | ||
model.runActiveCell(); | ||
} | ||
}; | ||
/** | ||
* Interrupt the kernel. | ||
*/ | ||
NotebookToolbar.prototype.interrupt = function () { | ||
return this.model.session.kernel.interrupt(); | ||
}; | ||
/** | ||
* Restart the kernel. | ||
*/ | ||
NotebookToolbar.prototype.restart = function () { | ||
@@ -800,3 +638,3 @@ var _this = this; | ||
if (result.text === 'OK') { | ||
return _this.model.session.kernel.restart(); | ||
return _this._manager.restart(); | ||
} | ||
@@ -858,7 +696,7 @@ }); | ||
*/ | ||
NotebookToolbar.prototype.onModelChanged = function (sender, args) { | ||
NotebookToolbar.prototype.onModelChanged = function (model, args) { | ||
switch (args.name) { | ||
case 'metadata': | ||
if (this.model.metadata && this.model.metadata.kernelspec) { | ||
var name_2 = this.model.metadata.kernelspec.display_name; | ||
if (model.metadata && model.metadata.kernelspec) { | ||
var name_2 = model.metadata.kernelspec.display_name; | ||
this.kernelNameNode.textContent = name_2; | ||
@@ -868,3 +706,3 @@ } | ||
case 'activeCellIndex': | ||
var cell = this.model.cells.get(this.model.activeCellIndex); | ||
var cell = model.cells.get(model.activeCellIndex); | ||
this.cellTypeNode.value = cell.type; | ||
@@ -897,15 +735,4 @@ break; | ||
} | ||
session.kernelChanged.connect(this.handleKernel, this); | ||
this.handleKernel(session, session.kernel); | ||
}; | ||
/** | ||
* Handle a change to the kernel. | ||
*/ | ||
NotebookToolbar.prototype.handleKernel = function (session, kernel) { | ||
var _this = this; | ||
kernel.getKernelSpec().then(function (spec) { | ||
_this.kernelNameNode.textContent = spec.display_name; | ||
}); | ||
}; | ||
/** | ||
* Handle `click` events for the widget. | ||
@@ -927,24 +754,24 @@ */ | ||
} | ||
var index; | ||
var manager = this._manager; | ||
switch (selected) { | ||
case TOOLBAR_SAVE: | ||
this.model.save(); | ||
manager.save(); | ||
break; | ||
case TOOLBAR_INSERT: | ||
this.insertBelow(); | ||
manager.insertBelow(); | ||
break; | ||
case TOOLBAR_CUT: | ||
this.cut(); | ||
manager.cut(); | ||
break; | ||
case TOOLBAR_COPY: | ||
this.copy(); | ||
manager.copy(); | ||
break; | ||
case TOOLBAR_PASTE: | ||
this.paste(); | ||
manager.paste(); | ||
break; | ||
case TOOLBAR_RUN: | ||
this.run(); | ||
manager.runAndAdvance(); | ||
break; | ||
case TOOLBAR_INTERRUPT: | ||
this.interrupt(); | ||
manager.interrupt(); | ||
break; | ||
@@ -951,0 +778,0 @@ case TOOLBAR_RESTART: |
@@ -147,3 +147,3 @@ // Copyright (c) Jupyter Development Team. | ||
switch (output.output_type) { | ||
case "execute_result": | ||
case 'execute_result': | ||
bundle = output.data; | ||
@@ -157,7 +157,7 @@ widget.addClass(EXECUTE_CLASS); | ||
break; | ||
case "display_data": | ||
case 'display_data': | ||
bundle = output.data; | ||
widget.addClass(DISPLAY_CLASS); | ||
break; | ||
case "stream": | ||
case 'stream': | ||
bundle = { 'jupyter/console-text': output.text }; | ||
@@ -171,3 +171,3 @@ if (output.name == 'stdout') { | ||
break; | ||
case "error": | ||
case 'error': | ||
var out = output; | ||
@@ -209,3 +209,3 @@ var traceback = out.traceback.join('\n'); | ||
// Don't display if we don't know how to sanitize it. | ||
console.log("Ignoring untrusted " + key + " output."); | ||
console.log('Ignoring untrusted ' + key + ' output.'); | ||
delete bundle[key]; | ||
@@ -212,0 +212,0 @@ continue; |
{ | ||
"name": "jupyter-js-notebook", | ||
"version": "0.13.4", | ||
"version": "0.14.0", | ||
"description": "Notebook widget for Jupyter", | ||
@@ -10,4 +10,5 @@ "main": "lib/index.js", | ||
"diff-match-patch": "^1.0.0", | ||
"jupyter-js-services": "^0.6.1", | ||
"jupyter-js-services": "^0.6.6", | ||
"jupyter-js-ui": "^0.2.0", | ||
"jupyter-js-utils": "^0.3.3", | ||
"marked": "^0.3.5", | ||
@@ -14,0 +15,0 @@ "phosphor-disposable": "^1.0.5", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
190915
55
6078
15
+ Addedjupyter-js-utils@^0.3.3
Updatedjupyter-js-services@^0.6.6