-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_editor.js
185 lines (160 loc) · 5.4 KB
/
source_editor.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
export function colorize() {
const isEditor = this.tag == "plaintext";
// forward declarations:
var doStyle;
var doScript;
// markup colorizer
function doMarkup(tz) {
var tagStart = null; // [node,offset]
var tagScript = false;
var tagScriptType = false;
var tagStyle = false;
var textElement;
var tt;
while (tt = tz.nextToken()) {
if (isEditor && tz.element != textElement) {
textElement = tz.element;
textElement.setAttribute("type", "markup");
}
//stdout.println(tt,tz.attr,tz.value);
switch (tt) {
case "tag-start": {
tagStart = tz.tokenRange.start;
const tag = tz.markupTag;
tagScript = tag == "script";
tagStyle = tag == "style";
} break;
case "tag-head-end": {
(new Range(tagStart, tz.tokenRange.end)).highlight("tag");
if (tagScript) { tz.push(tagScriptType, "</script>"); doScript(tz, tagScriptType, true); }
else if (tagStyle) { tz.push("text/css", "</style>"); doStyle(tz, true); }
} break;
case "tag-end": tz.tokenRange.highlight("tag"); break;
case "tag-attr": if (tagScript && tz.markupAttributeName == "type") tagScriptType = tz.markupAttributeValue;
if (tz.markupAttributeName == "id") tz.tokenRange.highlight("tag-id");
break;
}
}
}
// script colorizer
doScript = function (tz, typ, embedded = false) {
const KEYWORDS =
{
"function": true, "var": true, "if": true,
"else": true, "while": true, "return": true, "for": true,
"break": true, "continue": true, "do": true, "switch": true,
"case": true, "default": true, "null": true, "super": true,
"new": true, "try": true, "catch": true, "finally": true,
"throw": true, "typeof": true, "instanceof": true, "in": true,
"let": true, "const": true, "get": true, "set": true,
"include": true, "like": true, "class": true, "of": true,
"this": true, "delete": true, "with": true, "extends": true,
"await": true, "async": true, "yield": true, "import": true,
"export": true,
};
const LITERALS = { "true": true, "false": true, "null": true, "undefined": true };
var firstElement = null;
var lastElement = null;
var tt;
loop: while (tt = tz.nextToken()) {
var el = tz.element;
firstElement = firstElement || el;
lastElement = el;
switch (tt) {
case "number": tz.tokenRange.highlight("number"); break;
case "number-unit": tz.tokenRange.highlight("number-unit"); break;
case "string": tz.tokenRange.highlight("string"); break;
case "name": // name token
{
var val = tz.tokenValue;
if (KEYWORDS[val])
tz.tokenRange.highlight("keyword");
else if (LITERALS[val])
tz.tokenRange.highlight("literal");
break;
}
case "comment": tz.tokenRange.highlight("comment"); break;
case "island-end":
// got </script>
tz.pop(); //pop tokenizer layer
break loop;
}
}
if (isEditor && embedded) {
for (var el = firstElement; el; el = el.nextElementSibling) {
if (el == lastElement)
break;
el.setAttribute("type", "script");
}
}
};
doStyle = function (tz, embedded = false) {
const KEYWORDS =
{
"rgb": true, "rgba": true, "url": true, "var": true,
"@import": true, "@media": true, "@set": true, "@const": true,
"@mixin": true, "@supports": true,
};
const LITERALS = { "inherit": true };
var firstElement;
var lastElement;
var tt;
loop: while (tt = tz.nextToken()) {
var el = tz.element;
if (!firstElement) firstElement = el;
lastElement = el;
switch (tt) {
case "number": tz.tokenRange.highlight("number"); break;
case "number-unit": tz.tokenRange.highlight("number-unit"); break;
case "string": tz.tokenRange.highlight("string"); break;
case "name":
{
var val = tz.tokenValue;
if (val[0] == '#')
tz.tokenRange.highlight("literal");
else if (KEYWORDS[val])
tz.tokenRange.highlight("keyword");
else if (LITERALS[val])
tz.tokenRange.highlight("literal");
break;
}
case "comment": tz.tokenRange.highlight("comment"); break;
case "island-end":
// got </style>
tz.pop(); //pop tokenizer layer
break loop;
}
}
if (isEditor && embedded) {
for (var el = firstElement; el; el = el.nextElementSibling) {
if (el == lastElement)
break;
el.setAttribute("type", "style");
}
}
};
var me = this;
function doIt() {
var syntax = me.getAttribute("type");
var tz = new Tokenizer(me, syntax);
if (syntax.endsWith("/htm") || syntax.endsWith("/html") || syntax.endsWith("/svg") || syntax.endsWith("/xml"))
doMarkup(tz);
else if (syntax.endsWith("/css"))
doStyle(tz);
else
doScript(tz, syntax);
}
doIt();
// redefine value property
Object.defineProperty(me, "value", {
get: function () {
return me.state.value;
},
set: function (value) {
me.state.value = value.replaceAll("\r\n", "\n");
doIt();
}
});
if (isEditor)
this.on("change", function () { this.timer(40, doIt); });
}