-
Notifications
You must be signed in to change notification settings - Fork 28
/
dev.html
54 lines (44 loc) · 1.81 KB
/
dev.html
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
<script>
let me = {};
let regexes={
string1: {reg: /"(.*?)"/g, style:"string", wrap:'"'},
string2: {reg: /'(.*?)'/g, style:"string", wrap:"'"},
numbers: {reg: /(\b\d+\b)/g, style:"number"},
hex: {reg: /(\B#\w+)/g, style:"number"},
reserved: {reg: /\b(new|var|let|if|do|function|while|switch|for|foreach|in|continue|break)(?=[^\w])/g, style:"reserved"},
globals: {reg: /\b(document|window|Array|String|Object|Number|Math|\$)(?=[^\w])/g, style:"globals"},
js: {reg: /\b(getElementsBy(TagName|ClassName|Name)|getElementById|typeof|instanceof)(?=[^\w])/g, style:"js"},
methods: {reg:/((?<=\.)\w+)/g, style:"method"},
//htmlTags: {reg: /(<[^\&]*>)/g, style:"html"},
blockComments: {reg: /(\/\*.*\*\/)/g, style:"comment"},
inlineComments: {reg: /(\/\/.*)/g, style:"comment"},
source: {reg: /\b(source|\$)(?=[^\w])/g, style:"source"},
target: {reg: /\b(target|\$)(?=[^\w])/g, style:"target"},
}
let editor = document.createElement("code");
let textarea = document.createElement("textarea");
editor.contentEditable = "true";
editor.spellcheck = false;
editor.onkeydown = (e)=>{
e.stopPropagation();
}
editor.onblur = function(){
onChange(editor.innerText);
highlight();
}
//parent.appendChild(editor);
me.setValue=(value)=>{
editor.innerText = value;
highlight();
}
function highlight(){
textarea.innerText=editor.innerText;
let text = textarea.innerHTML;
for (let key in regexes){
let reg = regexes[key];
let wrap = reg.wrap || "";
text = text.replace(reg.reg,'<span class="' + reg.style + '">'+wrap+'$1'+wrap+'</span>');
}
editor.innerHTML = text;
}
</script>