thebe-core
Version:
Typescript based core functionality for Thebe
106 lines • 3.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rendermime_1 = require("./rendermime");
const outputarea_1 = require("@jupyterlab/outputarea");
const options_1 = require("./options");
const widgets_1 = require("@lumino/widgets");
const messaging_1 = require("@lumino/messaging");
class PassiveCellRenderer {
constructor(id, rendermime, mathjax) {
this.id = id;
this.rendermime = rendermime !== null && rendermime !== void 0 ? rendermime : (0, rendermime_1.makeRenderMimeRegistry)(mathjax !== null && mathjax !== void 0 ? mathjax : (0, options_1.makeMathjaxOptions)());
this.model = new outputarea_1.OutputAreaModel({ trusted: true });
this.area = new outputarea_1.OutputArea({
model: this.model,
rendermime: this.rendermime,
});
}
/**
* Serialize the model state to JSON
*/
get outputs() {
return this.model.toJSON();
}
get isAttachedToDOM() {
return this.area.isAttached;
}
attachToDOM(el, strict = false) {
if (!this.area || !el) {
console.error(`thebe:renderer:attachToDOM - could not attach to DOM - area: ${this.area}, el: ${el}`);
return;
}
if (this.area.isAttached) {
// TODO should we detach and reattach?
console.debug(`thebe:renderer:attachToDOM - already attached`);
if (strict)
return;
}
else {
// if the target element has contents, preserve it but wrap it in our output area
console.debug(`thebe:renderer:attachToDOM ${this.id} - appending existing contents`);
if (el.innerHTML) {
this.area.model.add({
output_type: 'display_data',
data: {
'text/html': el.innerHTML,
},
});
}
}
el.textContent = '';
const div = document.createElement('div');
div.style.position = 'relative';
div.className = 'thebe-output';
el.append(div);
messaging_1.MessageLoop.sendMessage(this.area, widgets_1.Widget.Msg.BeforeAttach);
div.appendChild(this.area.node);
messaging_1.MessageLoop.sendMessage(this.area, widgets_1.Widget.Msg.AfterAttach);
}
setOutputText(text) {
if (!this.area)
return;
this.area.model.clear(true);
this.area.model.add({
output_type: 'stream',
name: 'stdout',
text,
});
}
/**
* Clears the output area model
*
* @returns
*/
clear() {
if (!this.area)
return;
this.area.model.clear();
}
/**
* Will trigger the output to render an error with text taken from the optional argument
*
* @param error
* @returns
*/
clearOnError(error) {
if (!this.area)
return;
this.area.model.clear();
this.area.model.add({
output_type: 'stream',
name: 'stderr',
text: `Failed to execute. ${error !== null && error !== void 0 ? error : ''} Please refresh the page.`,
});
}
/**
* Render output data directly from json
*
* @param outputs - serialised jupyter outputs
* @returns
*/
render(outputs) {
this.model.fromJSON(outputs);
}
}
exports.default = PassiveCellRenderer;
//# sourceMappingURL=passive.js.map