livebook/assets/js/hooks/highlight.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

51 lines
1.2 KiB
JavaScript

import { getAttributeOrThrow } from "../lib/attribute";
import { highlight } from "./cell_editor/live_editor/monaco";
import { findChildOrThrow } from "../lib/utils";
/**
* A hook used to highlight source code in the root element.
*
* ## Configuration
*
* * `data-language` - language of the source code
*
* The element should have two children:
*
* * `[data-source]` - an element containing the source code to be
* highlighted
*
* * `[data-target]` - the element to render highlighted code into
*/
const Highlight = {
mounted() {
this.props = this.getProps();
this.sourceEl = findChildOrThrow(this.el, "[data-source]");
this.targetEl = findChildOrThrow(this.el, "[data-target]");
this.updateDOM();
},
updated() {
this.props = this.getProps();
this.updateDOM();
},
getProps() {
return {
language: getAttributeOrThrow(this.el, "data-language"),
};
},
updateDOM() {
const code = this.sourceEl.innerText;
highlight(code, this.props.language).then((html) => {
this.targetEl.innerHTML = html;
this.el.setAttribute("data-highlighted", "");
});
},
};
export default Highlight;