mirror of
https://github.com/wassname/template.git
synced 2026-08-14 12:50:40 +08:00
Add d-code and d-math componenents. Needs styling.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import katex from "katex";
|
||||
|
||||
export const html = `
|
||||
<style>
|
||||
dt-math[block] {
|
||||
display: block;
|
||||
}
|
||||
</style>
|
||||
`;
|
||||
|
||||
export class Math extends HTMLElement {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
const options = {childList: true};
|
||||
let observer = new MutationObserver( (mutations) => {
|
||||
observer.disconnect();
|
||||
this.renderComponent();
|
||||
observer.observe(this, options);
|
||||
})
|
||||
// try a first render
|
||||
this.renderComponent();
|
||||
// listen for changes
|
||||
observer.observe(this, options);
|
||||
}
|
||||
|
||||
renderComponent() {
|
||||
const displayMode = this.hasAttribute("block");
|
||||
const options = { displayMode: displayMode };
|
||||
const renderedMath = katex.renderToString(this.textContent, options);
|
||||
this.innerHTML = html + renderedMath
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define("d-math", Math);
|
||||
Reference in New Issue
Block a user