x4js 1.4.22 → 1.4.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,288 +0,0 @@
1
- /**
2
- * ___ ___ __
3
- * \ \_/ / / _
4
- * \ / /_| |_
5
- * / _ \____ _|
6
- * /__/ \__\ |_|
7
- *
8
- * @file texthiliter.ts
9
- * @author Etienne Cochard
10
- *
11
- * Copyright (c) 2019-2022 R-libre ingenierie
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining a copy
14
- * of this software and associated documentation files (the "Software"), to deal
15
- * in the Software without restriction, including without limitation the rights
16
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
17
- * of the Software, and to permit persons to whom the Software is furnished to do so,
18
- * subject to the following conditions:
19
- * The above copyright notice and this permission notice shall be included in all copies
20
- * or substantial portions of the Software.
21
- *
22
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
23
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
24
- * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
- **/
29
-
30
- /**
31
- * idea came from https://www.cdolivet.com/editarea
32
- */
33
-
34
- import { Component, CProps, CEventMap } from './component'
35
- import { EvChange } from './x4_events'
36
-
37
- interface TextHiliterEventMap extends CEventMap {
38
- change: EvChange;
39
- }
40
-
41
-
42
- interface TextHiliterProps extends CProps {
43
- text: string;
44
- kwList?: Set<string>;
45
- change?: EvChange;
46
- }
47
-
48
-
49
- export class TextHiliter extends Component<TextHiliterProps,TextHiliterEventMap> {
50
-
51
- private m_text: string;
52
- private m_ed: Component;
53
- private m_hi: Component;
54
- private m_top: number;
55
- private m_kwList: Set<string>;
56
-
57
- constructor( props: TextHiliterProps ) {
58
- super( props );
59
-
60
- this.m_kwList = props.kwList;
61
- this.m_top = 0;
62
- this.m_text = props.text ?? '';
63
-
64
- this.mapPropEvents( props, 'change' );
65
- }
66
-
67
- /** @ignore */
68
- render( ) {
69
- this.setContent( [
70
- this.m_hi = new Component( {
71
- tag: 'span',
72
- cls: '@fit @syntax-hiliter',
73
- }),
74
- this.m_ed = new Component( {
75
- tag: 'textarea',
76
- cls: '@fit',
77
- width: '100%',
78
- attrs: {
79
- spellcheck: 'false',
80
- wrap: 'off',
81
- },
82
- dom_events: {
83
- input: ()=>this._hiliteText(),
84
- scroll: ()=> this._updateScroll( ),
85
- keydown: (e)=> this._keydown(e),
86
- }
87
- }),
88
-
89
- ])
90
- }
91
-
92
- componentCreated() {
93
- super.componentCreated( );
94
- this.value = this.m_text;
95
- }
96
-
97
- get value( ) : string {
98
-
99
- if( this.dom ) {
100
- return (<HTMLTextAreaElement>this.m_ed.dom).value;
101
- }
102
- else {
103
- return this.m_text;
104
- }
105
- }
106
-
107
- set value( t: string ) {
108
- if( this.dom ) {
109
- (<HTMLTextAreaElement>this.m_ed.dom).value = t;
110
- }
111
-
112
- this.m_text = t;
113
- this._hiliteText( );
114
- }
115
-
116
- private _keydown( e: KeyboardEvent ) {
117
- if(e.key=='Tab' ){
118
- e.preventDefault();
119
- e.stopPropagation( );
120
-
121
- let dom = <HTMLTextAreaElement>this.m_ed.dom;
122
-
123
- let ss = dom.selectionStart;
124
- let se = dom.selectionEnd;
125
- dom.setRangeText( '\t', ss, se );
126
- dom.setSelectionRange( ss+1, ss+1 );
127
- dom.dispatchEvent( new Event( 'input' ) );
128
- }
129
- else if( e.key=='Enter' ) {
130
- e.stopPropagation( );
131
- }
132
- }
133
-
134
- private _hiliteText( ) {
135
- let text = (<HTMLTextAreaElement>this.m_ed.dom).value;
136
-
137
- if( !this.m_hi.dom.firstChild ) {
138
- this.m_hi.dom.innerHTML = '<div style="position:absolute"></div>';
139
- }
140
-
141
- (<HTMLElement>this.m_hi.dom.firstChild).innerHTML = this._tokenize(text);
142
- // this._updateScroll( );
143
- }
144
-
145
- private _updateScroll( ) {
146
-
147
- this.startTimer('sync', 0, false, ( ) => {
148
- let top = this.m_ed.dom.scrollTop;
149
- if( top!=this.m_top ) {
150
- this.m_hi.dom.scrollTop = top;
151
- this.m_top = top;
152
- }
153
- this.m_hi.dom.scrollLeft = this.m_ed.dom.scrollLeft;
154
- //this.m_hi.setStyleValue( 'width', this.m_ed.dom.clientWidth );
155
- });
156
- }
157
-
158
- private _escape( text: string ) : string {
159
- text = text.replace( /&/gm, '&amp;' );
160
- text = text.replace( /</gm, '&lt;' );
161
- text = text.replace( />/gm, '&gt;' );
162
- return text;
163
- }
164
-
165
-
166
-
167
- private _tokenize( text: string ) : string {
168
-
169
- const reNUM = /\d/;
170
- const reNUM2 = /[\d.]/;
171
- const rePUNC = /\+|-|,|\/|\*|=|%|!|\||;|\.|\[|\]|\{|\|\(|\)|}|<|>|&/;
172
- const reKW = /[a-zA-Z_]/;
173
- const reKW2 = /[a-zA-Z0-9_]/;
174
-
175
- let result = '';
176
-
177
- let i = 0;
178
- let length = text.length;
179
- let s;
180
-
181
- console.time( "hilite" );
182
-
183
- while( i<length ) {
184
-
185
- let c = text.charAt( i );
186
-
187
- // numbers
188
- if( reNUM.test(c) ) {
189
-
190
- let s = i;
191
- do {
192
- c = text.charAt( ++i );
193
- } while( reNUM2.test(c) && i<length );
194
-
195
- result += '<span class="num">' + text.substring( s, i ) + '</span>';
196
- continue;
197
- }
198
-
199
- // keywords
200
- if( this.m_kwList ) {
201
- if( reKW.test(c) ) {
202
-
203
- let s = i;
204
-
205
- do {
206
- c = text.charAt( ++i );
207
- } while( reKW2.test(c) && i<length );
208
-
209
- let kw = text.substring( s, i );
210
- if( this.m_kwList.has( kw ) ) {
211
- result += '<span class="kword">' + kw + '</span>';
212
- }
213
- else {
214
- result += kw;
215
- }
216
-
217
- continue;
218
- }
219
- }
220
-
221
- if( c=='#' ) {
222
- let ne = text.indexOf( '\n', i+1 );
223
- if( ne<0 ) {
224
- ne = text.length;
225
- }
226
-
227
- result += '<span class="cmt">' + this._escape(text.substring( i, ne )) + '</span>';
228
- i = ne;
229
- continue;
230
- }
231
-
232
- // comments
233
- if( c=='/' ) {
234
- let cn = text.charAt( i+1 );
235
- if( cn=='*' ) {
236
- let ne = text.indexOf( '*/', i+2 );
237
- if( ne<0 ) {
238
- ne = text.length;
239
- }
240
-
241
- result += '<span class="cmt">' + this._escape(text.substring( i, ne+2 )) + '</span>';
242
- i = ne+2;
243
- continue;
244
- }
245
- else if( cn=='/' ) {
246
- let ne = text.indexOf( '\n', i+2 );
247
- if( ne<0 ) {
248
- ne = text.length;
249
- }
250
-
251
- result += `<span class="cmt">${this._escape(text.substring( i, ne ))}</span>`;
252
- i = ne;
253
- continue;
254
- }
255
- }
256
-
257
- // punctuation
258
- if( rePUNC.test(c) ) {
259
- s = i;
260
- do {
261
- c = text.charAt( ++i );
262
- } while( rePUNC.test(c) && i<length );
263
-
264
- result += `<span class="punc">${text.substring( s, i )}</span>`;
265
- continue;
266
- }
267
-
268
- // strings
269
- if( c=='"' || c=='\'' || c=='\`' ) {
270
- s = i;
271
-
272
- let delim = c;
273
- do {
274
- c = text.charAt( ++i );
275
- } while( c!=delim && i<length );
276
-
277
- result += `<span class="str">${this._escape(text.substring( s, ++i ))}</span>`;
278
- continue;
279
- }
280
-
281
- i++;
282
- result += c;
283
- }
284
-
285
- console.timeEnd( "hilite" );
286
- return result + '\n\n\n';
287
- }
288
- }