Molodetz blogrol volgens DPP-template
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
import hljs from "../../vendor/highlight.es.min.js";
|
||||
import { Component } from "./Component.js";
|
||||
import { Toast } from "../utils/Toast.js";
|
||||
|
||||
export class Code extends Component {
|
||||
static tag = "dp-code";
|
||||
|
||||
setup() {
|
||||
requestAnimationFrame(() => this.enhance());
|
||||
}
|
||||
|
||||
enhance() {
|
||||
const code = this.querySelector("pre > code");
|
||||
if (!code) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
hljs.highlightElement(code);
|
||||
} catch (error) {
|
||||
console.debug("highlight failed", error);
|
||||
}
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.className = "copy-button";
|
||||
button.textContent = "Kopieer";
|
||||
button.addEventListener("click", async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(code.textContent);
|
||||
Toast.flash(button, "Gekopieerd", 1500, "Kopieer");
|
||||
} catch {
|
||||
Toast.flash(button, "Mislukt", 1500, "Kopieer");
|
||||
}
|
||||
});
|
||||
this.prepend(button);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user