-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
code-block.js
60 lines (46 loc) · 1.36 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
import Block from '../blots/block';
import Delta from 'rich-text/lib/delta';
import Parchment from 'parchment';
import logger from '../lib/logger';
let debug = logger('quill:code-block');
class TokenAttributor extends Parchment.Attributor.Class {
value(domNode) {
return undefined;
}
}
let CodeToken = new TokenAttributor('token', 'hljs', {
scope: Parchment.Scope.INLINE
});
class CodeBlock extends Block {
format(name, value) {
// TODO allow changing language
debug.warn(`format(${name}, ${value}) called on code block`);
}
formatAt(index, length, name, value) {
debug.warn(`formatAt(${index}, ${length}, ${name}, ${value}) called on code block`);
}
formats() {
return {
'code-block': this.domNode.getAttribute('data-language') || true
};
}
getDelta() {
return this.domNode.innerText.split('\n').reduce((delta, text) => {
return delta.insert(text).insert('\n', this.formats());
}, new Delta());
}
highlight() {
if (this.domNode.innerHTML === this.html) return;
this.domNode.innerHTML = this.domNode.innerText;
hljs.highlightBlock(this.domNode);
this.build();
this.html = this.domNode.innerHTML;
}
optimize(mutations) {
super.optimize(mutations);
this.highlight();
}
}
CodeBlock.blotName = 'code-block';
CodeBlock.tagName = 'PRE';
export { CodeToken, CodeBlock as default };