-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-flask.js
103 lines (92 loc) · 3.24 KB
/
code-flask.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
91
92
93
94
95
96
97
98
99
100
101
102
103
import CodeFlask from 'codeflask'
export default class CodeFlaskElement extends HTMLElement {
constructor () {
super()
this.attachShadow({ mode: 'open' })
this.shadowRoot.innerHTML += `<style> :host { display: block; position: relative;
overflow-x: auto; min-height: 100px; }
.codeflask .token.title { color: var(--codeflask-title, #000) }
.codeflask .token.punctuation { color: var(--codeflask-punctuation, #6a6a6a) }
.codeflask .token.keyword { color: var(--codeflask-keyword, #8500ff) }
.codeflask .token.operator { color: var(--codeflask-operator, #ff5598) }
.codeflask .token.string { color: var(--codeflask-string, #41ad8f) }
.codeflask .token.comment { color: var(--codeflask-comment, #9badb7) }
.codeflask .token.function { color: var(--codeflask-function, #8500ff) }
.codeflask .token.boolean { color: var(--codeflask-boolean, #8500ff) }
.codeflask .token.number { color: var(--codeflask-number, #8500ff) }
.codeflask .token.selector { color: var(--codeflask-selector, #8500ff) }
.codeflask .token.property { color: var(--codeflask-property, #8500ff) }
.codeflask .token.tag { color: var(--codeflask-tag, #8500ff) }
.codeflask .token.attr-value { color: var(--codeflask-attr-value, #8500ff) }
.codeflask .token.url { color: var(--codeflask-url, #4040ff) }
.codeflask .token.italic { font-style: italic }
.codeflask .token.bold { font-weight: bolder }
</style>`
this.editorElement = document.createElement('div')
this.editorElement.style.width = '100%'
this.editorElement.style.height = '100%'
this.editorElement.style.position = 'absolute'
this.shadowRoot.appendChild(this.editorElement)
this.flask = new CodeFlask(this.editorElement, {
language: this.getAttribute('language') || 'markup',
wordWrap: this.hasAttribute('word-wrap'),
lineNumbers: this.lineNumbers,
rtl: this.dir === 'rtl',
defaultTheme: false,
styleParent: this.shadowRoot,
})
this.flask.onUpdate(_ => {
this.dispatchEvent(new CustomEvent('value-changed'))
})
this.value = this.getAttribute('value')
if (!this.value) {
this.initElement = document.createElement('div')
this.shadowRoot.appendChild(this.initElement)
this.initSlot = document.createElement('slot')
this.initElement.appendChild(this.initSlot)
}
}
connectedCallback () {
if (this.initSlot) {
let value = ''
for (const node of this.initSlot.assignedNodes()) {
value += node.outerHTML || node.textContent
}
this.initElement.removeChild(this.initSlot)
this.shadowRoot.removeChild(this.initElement)
if (value.length > 0) {
this.value = value
}
}
}
get value () {
return this.flask.getCode()
}
set value (val) {
this.removeAttribute('value')
return this.flask.updateCode(val)
}
get language () {
return this.flask.opts.language
}
set language (val) {
if (!val) {
return
}
this.flask.updateLanguage(val)
this.dispatchEvent(new CustomEvent('language-changed'))
}
get dir () {
return this.getAttribute('dir') || 'ltr'
}
get lineNumbers () {
return this.hasAttribute('line-numbers')
}
static get observedAttributes () {
return ['language', 'value']
}
attributeChangedCallback (name, oldValue, newValue) {
this[name] = newValue
}
}
customElements.define('code-flask', CodeFlaskElement)