lexgui 0.6.5 → 0.6.6

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.
@@ -0,0 +1,417 @@
1
+ // @jxarco
2
+
3
+ import { LX } from 'lexgui';
4
+
5
+ if( !LX )
6
+ {
7
+ throw( "lexgui.js missing!" );
8
+ }
9
+
10
+ const CPP_KEY_WORDS = ['int', 'float', 'double', 'bool', 'char', 'wchar_t', 'const', 'static_cast', 'dynamic_cast', 'new', 'delete', 'void', 'true', 'false', 'auto', 'struct', 'typedef', 'nullptr', 'NULL', 'unsigned', 'namespace'];
11
+ const CLASS_WORDS = ['uint32_t', 'uint64_t', 'uint8_t'];
12
+ const STATEMENT_WORDS = ['for', 'if', 'else', 'return', 'continue', 'break', 'case', 'switch', 'while', 'import', 'from'];
13
+
14
+ const JS_KEY_WORDS = ['var', 'let', 'const', 'static', 'function', 'null', 'undefined', 'new', 'delete', 'true', 'false', 'NaN', 'this'];
15
+ const HTML_ATTRIBUTES = ['html', 'charset', 'rel', 'src', 'href', 'crossorigin', 'type', 'lang'];
16
+ const HTML_TAGS = ['html', 'DOCTYPE', 'head', 'meta', 'title', 'link', 'script', 'body', 'style'];
17
+
18
+ let mainContainer = document.body;
19
+
20
+ function SET_DOM_TARGET( element )
21
+ {
22
+ mainContainer = element;
23
+ }
24
+
25
+ window.SET_DOM_TARGET = SET_DOM_TARGET;
26
+
27
+ function MAKE_LINE_BREAK()
28
+ {
29
+ mainContainer.appendChild( document.createElement('br') );
30
+ }
31
+
32
+ window.MAKE_LINE_BREAK = MAKE_LINE_BREAK;
33
+
34
+ function MAKE_HEADER( string, type, id )
35
+ {
36
+ console.assert(string && type);
37
+ let header = document.createElement(type);
38
+ header.innerHTML = string;
39
+ if(id) header.id = id;
40
+ mainContainer.appendChild( header );
41
+ }
42
+
43
+ window.MAKE_HEADER = MAKE_HEADER;
44
+
45
+ function MAKE_PARAGRAPH( string, sup )
46
+ {
47
+ console.assert(string);
48
+ let paragraph = document.createElement(sup ? 'sup' : 'p');
49
+ paragraph.className = "leading-relaxed";
50
+ paragraph.innerHTML = string;
51
+ mainContainer.appendChild( paragraph );
52
+ }
53
+
54
+ window.MAKE_PARAGRAPH = MAKE_PARAGRAPH;
55
+
56
+ function MAKE_CODE( text, language = "js" )
57
+ {
58
+ console.assert(text);
59
+
60
+ text.replaceAll('<', '&lt;');
61
+ text.replaceAll('>', '&gt;');
62
+
63
+ let highlight = "";
64
+ let content = "";
65
+
66
+ const getHTML = ( h, c ) => {
67
+ return `<span class="${ h }">${ c }</span>`;
68
+ };
69
+
70
+ for( let i = 0; i < text.length; ++i )
71
+ {
72
+ const char = text[ i ];
73
+ const string = text.substr( i );
74
+
75
+ const endLineIdx = string.indexOf( '\n' );
76
+ const line = string.substring( 0, endLineIdx > -1 ? endLineIdx : undefined );
77
+
78
+ if( char == '@' )
79
+ {
80
+ const str = line.substr( 1 );
81
+
82
+ if ( !( str.indexOf( '@' ) > -1 ) && !( str.indexOf( '[' ) > -1 ) )
83
+ {
84
+ continue;
85
+ }
86
+
87
+ let html = null;
88
+
89
+ const tagIndex = str.indexOf( '@' );
90
+ const skipTag = ( str[ tagIndex - 1 ] == '|' );
91
+
92
+ // Highlight is specified
93
+ if( text[ i + 1 ] == '[' )
94
+ {
95
+ highlight = str.substr( 1, 3 );
96
+ content = str.substring( 5, tagIndex );
97
+
98
+ if( skipTag )
99
+ {
100
+ const newString = str.substring( 6 + content.length );
101
+ const preContent = content;
102
+ const postContent = newString.substring( 0, newString.indexOf( '@' ) );
103
+ const finalContent = preContent.substring( 0, preContent.length - 1 ) + '@' + postContent;
104
+ html = getHTML( highlight, finalContent );
105
+
106
+ const ogContent = preContent + '@' + postContent;
107
+ text = text.replace( `@[${ highlight }]${ ogContent }@`, html );
108
+ }
109
+ else
110
+ {
111
+ html = getHTML( highlight, content );
112
+ text = text.replace( `@[${ highlight }]${ content }@`, html );
113
+ }
114
+ }
115
+ else
116
+ {
117
+ content = str.substring( 0, tagIndex );
118
+ if( skipTag )
119
+ {
120
+ const preContent = str.substring( 0, str.indexOf( '@' ) - 1 );
121
+ content = str.substring( preContent.length + 1 );
122
+ content = preContent + content.substring( 0, content.substring( 1 ).indexOf( '@' ) + 1 );
123
+ text = text.substr( 0, i ) + '@' + content + '@' + text.substr( i + content.length + 3 );
124
+ }
125
+
126
+ if( language == "cpp" && CPP_KEY_WORDS.includes( content ) )
127
+ {
128
+ highlight = "kwd";
129
+ }
130
+ else if( language == "js" && JS_KEY_WORDS.includes( content ) )
131
+ {
132
+ highlight = "kwd";
133
+ }
134
+ else if( CLASS_WORDS.includes( content ) )
135
+ {
136
+ highlight = "cls";
137
+ }
138
+ else if( STATEMENT_WORDS.includes( content ) )
139
+ {
140
+ highlight = "lit";
141
+ }
142
+ else if( HTML_TAGS.includes( content ) )
143
+ {
144
+ highlight = "tag";
145
+ }
146
+ else if( HTML_ATTRIBUTES.includes( content ) )
147
+ {
148
+ highlight = "atn";
149
+ }
150
+ else if( ( content[ 0 ] == '"' && content[ content.length - 1 ] == '"' ) ||
151
+ ( content[ 0 ] == "'" && content[ content.length - 1 ] == "'" ) ||
152
+ ( content[ 0 ] == "`" && content[ content.length - 1 ] == "`" ) )
153
+ {
154
+ highlight = "str";
155
+ }
156
+ else if( parseFloat( content ) != NaN )
157
+ {
158
+ highlight = "dec";
159
+ }
160
+ else
161
+ {
162
+ console.error( "ERROR[Code Parsing]: Unknown highlight type: " + content );
163
+ return;
164
+ }
165
+
166
+ html = getHTML( highlight, content );
167
+ text = text.replace( `@${ content }@`, html );
168
+ }
169
+
170
+ i += ( html.length - 1 );
171
+ }
172
+ }
173
+
174
+ let container = document.createElement('div');
175
+ container.className = "code-container";
176
+ let pre = document.createElement('pre');
177
+ let code = document.createElement('code');
178
+ code.innerHTML = text;
179
+
180
+ let button = document.createElement('button');
181
+ button.title = "Copy code sample";
182
+ button.appendChild(LX.makeIcon( "Copy" ));
183
+ button.addEventListener('click', COPY_SNIPPET.bind(this, button));
184
+ container.appendChild( button );
185
+
186
+ pre.appendChild( code );
187
+ container.appendChild( pre );
188
+ mainContainer.appendChild( container );
189
+ }
190
+
191
+ window.MAKE_CODE = MAKE_CODE;
192
+
193
+ function MAKE_BULLET_LIST( list )
194
+ {
195
+ console.assert(list && list.length > 0);
196
+ let ul = document.createElement('ul');
197
+ for( var el of list ) {
198
+ let li = document.createElement('li');
199
+ li.className = "leading-loose";
200
+ li.innerHTML = el;
201
+ ul.appendChild( li );
202
+ }
203
+ mainContainer.appendChild( ul );
204
+ }
205
+
206
+ window.MAKE_BULLET_LIST = MAKE_BULLET_LIST;
207
+
208
+ function ADD_CODE_LIST_ITEM( item, target )
209
+ {
210
+ target = target ?? window.listQueued;
211
+ let split = (item.constructor === Array);
212
+ if( split && item[0].constructor === Array ) {
213
+ MAKE_CODE_BULLET_LIST( item, target );
214
+ return;
215
+ }
216
+ let li = document.createElement('li');
217
+ li.className = "leading-loose";
218
+ li.innerHTML = split ? ( item.length == 2 ? INLINE_CODE( item[0] ) + ": " + item[1] : INLINE_CODE( item[0] + " <span class='desc'>(" + item[1] + ")</span>" ) + ": " + item[2] ) : INLINE_CODE( item );
219
+ target.appendChild( li );
220
+ }
221
+
222
+ window.ADD_CODE_LIST_ITEM = ADD_CODE_LIST_ITEM;
223
+
224
+ function MAKE_CLASS_METHOD( name, desc, params, ret )
225
+ {
226
+ START_CODE_BULLET_LIST();
227
+ let paramsHTML = "";
228
+ for( var p of params ) {
229
+ const name = p[ 0 ];
230
+ const type = p[ 1 ];
231
+ paramsHTML += name + ": <span class='desc'>" + type + "</span>" + ( params.indexOf( p ) != (params.length - 1) ? ', ' : '' );
232
+ }
233
+ let li = document.createElement('li');
234
+ li.innerHTML = INLINE_CODE( "<span class='method'>" + name + " (" + paramsHTML + ")" + ( ret ? (": " + ret) : "" ) + "</span>" );
235
+ window.listQueued.appendChild( li );
236
+ END_CODE_BULLET_LIST();
237
+ MAKE_PARAGRAPH( desc );
238
+ }
239
+
240
+ window.MAKE_CLASS_METHOD = MAKE_CLASS_METHOD;
241
+
242
+ function MAKE_CLASS_CONSTRUCTOR( name, params, language = "js" )
243
+ {
244
+ let paramsHTML = "";
245
+ for( var p of params ) {
246
+ const str1 = p[ 0 ]; // cpp: param js: name
247
+ const str2 = p[ 1 ]; // cpp: defaultValue js: type
248
+ if( language == "cpp" ) {
249
+ paramsHTML += str1 + ( str2 ? " = <span class='desc'>" + str2 + "</span>" : "" ) + ( params.indexOf( p ) != (params.length - 1) ? ', ' : '' );
250
+ } else if( language == "js" ) {
251
+ paramsHTML += str1 + ": <span class='desc'>" + str2 + "</span>" + ( params.indexOf( p ) != (params.length - 1) ? ', ' : '' );
252
+ }
253
+ }
254
+ let pr = document.createElement('p');
255
+ pr.innerHTML = INLINE_CODE( "<span class='constructor'>" + name + "(" + paramsHTML + ")" + "</span>" );
256
+ mainContainer.appendChild( pr );
257
+ }
258
+
259
+ window.MAKE_CLASS_CONSTRUCTOR = MAKE_CLASS_CONSTRUCTOR;
260
+
261
+ function MAKE_CODE_BULLET_LIST( list, target )
262
+ {
263
+ console.assert(list && list.length > 0);
264
+ let ul = document.createElement('ul');
265
+ for( var el of list ) {
266
+ ADD_CODE_LIST_ITEM( el, ul );
267
+ }
268
+ if( target ) {
269
+ target.appendChild( ul );
270
+ } else {
271
+ mainContainer.appendChild( ul );
272
+ }
273
+ }
274
+
275
+ window.MAKE_CODE_BULLET_LIST = MAKE_CODE_BULLET_LIST;
276
+
277
+ function START_CODE_BULLET_LIST()
278
+ {
279
+ let ul = document.createElement('ul');
280
+ window.listQueued = ul;
281
+ }
282
+
283
+ window.START_CODE_BULLET_LIST = START_CODE_BULLET_LIST;
284
+
285
+ function END_CODE_BULLET_LIST()
286
+ {
287
+ console.assert( window.listQueued );
288
+ mainContainer.appendChild( window.listQueued );
289
+ window.listQueued = undefined;
290
+ }
291
+
292
+ window.END_CODE_BULLET_LIST = END_CODE_BULLET_LIST;
293
+
294
+ function INLINE_LINK( string, href )
295
+ {
296
+ console.assert(string && href);
297
+ return `<a href="` + href + `">` + string + `</a>`;
298
+ }
299
+
300
+ window.INLINE_LINK = INLINE_LINK;
301
+
302
+ function INLINE_PAGE( string, page )
303
+ {
304
+ console.assert(string && page);
305
+ return `<a onclick="loadPage('` + page + `')">` + string + `</a>`;
306
+ }
307
+
308
+ window.INLINE_PAGE = INLINE_PAGE;
309
+
310
+ function INLINE_CODE( string, codeClass )
311
+ {
312
+ console.assert(string);
313
+ return `<code class="inline ${ codeClass ?? "" }">` + string + `</code>`;
314
+ }
315
+
316
+ window.INLINE_CODE = INLINE_CODE;
317
+
318
+ function COPY_SNIPPET( b )
319
+ {
320
+ b.innerHTML = "";
321
+ b.appendChild(LX.makeIcon( "Check" ));
322
+ b.classList.add('copied');
323
+
324
+ setTimeout( () => {
325
+ b.innerHTML = "";
326
+ b.appendChild(LX.makeIcon( "Copy" ));
327
+ b.classList.remove('copied');
328
+ }, 2000 );
329
+
330
+ navigator.clipboard.writeText( b.dataset['snippet'] ?? b.parentElement.innerText );
331
+ console.log("Copied!");
332
+ }
333
+
334
+ function INSERT_IMAGE( src, caption = "", parent )
335
+ {
336
+ let img = document.createElement('img');
337
+ img.src = src;
338
+ img.alt = caption;
339
+ img.className = "my-1";
340
+ ( parent ?? mainContainer ).appendChild( img );
341
+ }
342
+
343
+ window.INSERT_IMAGE = INSERT_IMAGE;
344
+
345
+ function INSERT_IMAGES( sources, captions = [], width, height )
346
+ {
347
+ const mobile = navigator && /Android|iPhone/i.test(navigator.userAgent);
348
+
349
+ if( !mobile )
350
+ {
351
+ let div = document.createElement('div');
352
+ div.style.width = width ?? "auto";
353
+ div.style.height = height ?? "256px";
354
+ div.className = "flex flex-row justify-center";
355
+
356
+ for( let i = 0; i < sources.length; ++i )
357
+ {
358
+ INSERT_IMAGE( sources[ i ], captions[ i ], div );
359
+ }
360
+
361
+ mainContainer.appendChild( div );
362
+ }
363
+ else
364
+ {
365
+ for( let i = 0; i < sources.length; ++i )
366
+ {
367
+ INSERT_IMAGE( sources[ i ], captions[ i ] );
368
+ }
369
+ }
370
+ }
371
+
372
+ window.INSERT_IMAGES = INSERT_IMAGES;
373
+
374
+ function INSERT_VIDEO( src, caption = "", controls = true, autoplay = false )
375
+ {
376
+ let video = document.createElement( 'video' );
377
+ video.src = src;
378
+ video.controls = controls;
379
+ video.autoplay = autoplay;
380
+ if( autoplay )
381
+ {
382
+ video.muted = true;
383
+ }
384
+ video.loop = true;
385
+ video.alt = caption;
386
+ mainContainer.appendChild( video );
387
+ }
388
+
389
+ window.INSERT_VIDEO = INSERT_VIDEO;
390
+
391
+ function MAKE_NOTE( string, title = "Note" )
392
+ {
393
+ console.assert( string );
394
+
395
+ let note = document.createElement( 'div' );
396
+ note.className = "note";
397
+
398
+ let header = document.createElement( 'div' );
399
+ header.className = "note-header";
400
+ header.appendChild( LX.makeIcon( "NotepadText" ) );
401
+ header.innerHTML += "<b>" + title + "</b>";
402
+
403
+ let body = document.createElement( 'div' );
404
+ body.className = "note-body";
405
+ body.innerHTML = string;
406
+
407
+ note.appendChild( header );
408
+ note.appendChild( body );
409
+ mainContainer.appendChild( note );
410
+ }
411
+
412
+ window.MAKE_NOTE = MAKE_NOTE;
413
+
414
+ export { SET_DOM_TARGET, MAKE_LINE_BREAK, MAKE_HEADER, MAKE_PARAGRAPH, MAKE_CODE, MAKE_BULLET_LIST,
415
+ MAKE_CLASS_METHOD, MAKE_CLASS_CONSTRUCTOR, MAKE_CODE_BULLET_LIST, START_CODE_BULLET_LIST, ADD_CODE_LIST_ITEM,
416
+ END_CODE_BULLET_LIST, INLINE_LINK, INLINE_PAGE, INLINE_CODE, INSERT_IMAGE, INSERT_IMAGES, INSERT_VIDEO, MAKE_NOTE
417
+ }