livebook/assets/js/hooks/cell_editor.js
Jonatan Kłosko b3b79afed4
Refactor JS hooks (#1055)
* Restructure hook files

* Simplify app.js

* Refactor hooks

* Implement password toggle with JS commands
2022-03-16 11:33:53 +01:00

63 lines
1.5 KiB
JavaScript

import LiveEditor from "./cell_editor/live_editor";
import { getAttributeOrThrow } from "../lib/attribute";
const CellEditor = {
mounted() {
this.props = this.getProps();
this.handleEvent(
`cell_editor_init:${this.props.cellId}:${this.props.tag}`,
({ source_view, language, intellisense, read_only }) => {
const editorContainer = this.el.querySelector(
`[data-element="editor-container"]`
);
// Remove the content placeholder
editorContainer.firstElementChild.remove();
const editorEl = document.createElement("div");
editorContainer.appendChild(editorEl);
this.liveEditor = new LiveEditor(
this,
editorEl,
this.props.cellId,
this.props.tag,
source_view.source,
source_view.revision,
language,
intellisense,
read_only
);
this.el.dispatchEvent(
new CustomEvent("lb:cell:editor_created", {
detail: { tag: this.props.tag, liveEditor: this.liveEditor },
bubbles: true,
})
);
}
);
},
destroyed() {
if (this.liveEditor) {
this.el.dispatchEvent(
new CustomEvent("lb:cell:editor_removed", {
detail: { tag: this.props.tag },
bubbles: true,
})
);
this.liveEditor.dispose();
}
},
getProps() {
return {
cellId: getAttributeOrThrow(this.el, "data-cell-id"),
tag: getAttributeOrThrow(this.el, "data-tag"),
};
},
};
export default CellEditor;