mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-01-07 23:47:53 +08:00
b3b79afed4
* Restructure hook files * Simplify app.js * Refactor hooks * Implement password toggle with JS commands
50 lines
1.2 KiB
JavaScript
50 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;
|