-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-block.js
90 lines (71 loc) · 2.37 KB
/
code-block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(function (ace) {
const tmpl = document.createElement('template');
tmpl.innerHTML = `
<div>
<slot></slot>
</div>
`;
class CodeBlock extends HTMLElement {
constructor() {
super();
console.group("CodeBlock Constructor:");
//can be overridden by 'user styles
this.style.fontFamily = 'monospace';
this.style.display = 'contents';
console.debug("initializing ace for", this);
this.attachShadow({ mode: 'open' });
this.shadowRoot.appendChild(tmpl.content.cloneNode(true));
console.groupEnd();
}
connectedCallback() {
console.group("CodeBlock ConnectedCallback:");
this.editor = ace.edit(this.firstElementChild, {
readOnly: this.readOnly,
maxLines: this.maxLines, //make sure the editor has a height
theme: `ace/theme/${this.theme}`,
mode: `ace/mode/${this.lang}`,
});
console.debug("ace initialized", this.editor);
console.groupEnd();
}
// noinspection JSUnusedGlobalSymbols
static get observedAttributes() {
return ['lang'];
}
// noinspection JSUnusedGlobalSymbols
attributeChangedCallback(name, oldValue, newValue) {
if (this.editor && name === "lang") {
this.editor.mode = `/ace/mode/${newValue}`;
}
}
get lang() {
return this.getAttribute("lang") || "";
}
set lang(val) {
this.setAttribute("lang", val);
}
get theme() {
return this.getAttribute("theme") || 'monokai';
}
set theme(val) {
this.setAttribute("theme", val);
}
get readOnly() {
return this.hasAttribute("readOnly") && this.getAttribute("readOnly") !== 'false';
}
set readOnly(val) {
if (val) {
this.setAttribute("readOnly", "true");
} else {
this.removeAttribute("readOnly");
}
}
get maxLines() {
return +(this.getAttribute("maxLines") || "30");
}
set maxLines(val) {
this.setAttribute("maxLines", val);
}
}
window.customElements.define('code-block', CodeBlock);
})(window.ace || {});