jupyterlab-emrys
Version:
A computational environment for Jupyter. Powered by Emrys
95 lines (94 loc) • 3.21 kB
JavaScript
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
;
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var CodeMirror = require('codemirror');
require('codemirror/mode/meta');
var codemirror_1 = require('../codemirror');
var widget_1 = require('../codemirror/widget');
var docregistry_1 = require('../docregistry');
var plugin_1 = require('./plugin');
/**
* The class name added to a dirty widget.
*/
var DIRTY_CLASS = 'jp-mod-dirty';
/**
* The class name added to a jupyter code mirror widget.
*/
var EDITOR_CLASS = 'jp-EditorWidget';
/**
* A document widget for codemirrors.
*/
var EditorWidget = (function (_super) {
__extends(EditorWidget, _super);
/**
* Construct a new editor widget.
*/
function EditorWidget(context) {
var _this = this;
_super.call(this);
plugin_1.tracker.addWidget(this);
this.addClass(EDITOR_CLASS);
var editor = this.editor;
var model = context.model;
editor.setOption('lineNumbers', true);
var doc = editor.getDoc();
doc.setValue(model.toString());
this.title.text = context.path.split('/').pop();
codemirror_1.loadModeByFileName(editor, context.path);
model.stateChanged.connect(function (m, args) {
if (args.name === 'dirty') {
if (args.newValue) {
_this.title.className += " " + DIRTY_CLASS;
}
else {
_this.title.className = _this.title.className.replace(DIRTY_CLASS, '');
}
}
});
context.pathChanged.connect(function (c, path) {
codemirror_1.loadModeByFileName(editor, path);
_this.title.text = path.split('/').pop();
});
model.contentChanged.connect(function () {
var old = doc.getValue();
var text = model.toString();
if (old !== text) {
doc.setValue(text);
}
});
CodeMirror.on(doc, 'change', function (instance, change) {
if (change.origin !== 'setValue') {
model.fromString(instance.getValue());
}
});
}
return EditorWidget;
}(widget_1.CodeMirrorWidget));
exports.EditorWidget = EditorWidget;
/**
* A widget factory for editors.
*/
var EditorWidgetFactory = (function (_super) {
__extends(EditorWidgetFactory, _super);
function EditorWidgetFactory() {
_super.apply(this, arguments);
}
/**
* Create a new widget given a context.
*/
EditorWidgetFactory.prototype.createNew = function (context, kernel) {
if (kernel) {
context.changeKernel(kernel);
}
var widget = new EditorWidget(context);
this.widgetCreated.emit(widget);
return widget;
};
return EditorWidgetFactory;
}(docregistry_1.ABCWidgetFactory));
exports.EditorWidgetFactory = EditorWidgetFactory;