-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstudio.view.ts
535 lines (415 loc) · 12.7 KB
/
studio.view.ts
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
namespace $.$$ {
export class $hyoo_studio extends $.$hyoo_studio {
preview_show( next?: boolean ) {
return this.$.$mol_state_arg.value( 'preview', next?.valueOf && ( next ? '' : null ) ) !== null
}
inspector_show( next?: boolean ) {
return this.$.$mol_state_arg.value( 'inspect', next?.valueOf && ( next ? '' : null ) ) !== null
}
code_show( next?: boolean ) {
return this.$.$mol_state_arg.value( 'raw', next?.valueOf && ( next ? '' : null ) ) !== null
}
readme_show( next?: boolean ) {
if ( next ) this.readme_selected( this.base() )
return this.Demo().readme_page( next )
}
@ $mol_mem
pages() {
return [
this.Edit(),
... this.readme_show() ? [ this.Readme_page() ] : [],
... this.code_show() ? [ this.Source_page() ] : [],
... this.inspector_show() ? [ this.Inspect() ] : [],
... this.preview_show() ? [ this.Preview() ] : [],
]
}
@ $mol_mem
pack( next?: string ) {
return this.$.$mol_state_arg.value( 'pack', next ) ?? super.pack()
}
@ $mol_mem
source_tree( next?: string ): string {
return this.$.$mol_state_arg.value( 'source', next ) ?? super.source_tree()
}
@ $mol_mem
source_css( next?: string ): string {
return this.$.$mol_state_arg.value( 'source_css', next ) ?? super.source_css()
}
@ $mol_mem
source_js( next?: string ): string {
return this.$.$mol_state_arg.value( 'source_js', next ) ?? super.source_js()
}
@ $mol_mem
preview_html() {
const self = this.self()
const script = new URL( 'web.js', this.pack() ).toString()
const theme = this.Theme().theme()
return `
<html mol_view_root>
<body mol_view_root>
<script src="${ script }"></script>
<script>${ this.self_code() }</script>
<div mol_view_root="${ self }" mol_theme="${ theme }" style="background:none"></div>
</body>
</html>
`
}
@ $mol_mem
preview_window() {
this.$.$mol_wait_rest()
this.preview_html()
this.preview_show()
return super.preview_window()
}
@ $mol_mem
inspect_graph() {
const win = this.preview_window()
try {
win['$mol_view'].autobind()
return $mol_wire_graph( win['$mol_view']['autobind()'] )
} catch( error: any ) {
if( 'then' in error ) return $mol_fail_hidden( new Promise( ( done, fail )=> error.then( done, fail ) ) )
$mol_fail_hidden( new Error( error.message ) )
}
}
@ $mol_mem
inspect_stat() {
return this.inspect_graph().depth_nodes( Math.min ).map( nodes => nodes.length )
}
inspect_stat_depth() {
return Object.keys( this.inspect_stat() ).map( Number )
}
readme_selected( next?: string ) {
return $mol_state_arg.value( 'demo', next ) ?? ''
}
@ $mol_mem
library() {
const uri = new URL( 'web.view.tree', this.pack() ).toString()
const str = this.$.$mol_fetch.text( uri )
const predef = '$mol_view $mol_object\n\tdom_name \\\n\tstyle *\n\tevent *\n\tfield *\n\tattr *\n\tsub /\n\ttitle \\\n'
const tree = this.$.$mol_tree2_from_string( predef + str )
const norm = this.$.$mol_view_tree2_normalize( tree )
return norm
}
@ $mol_mem
tree( next?: $mol_tree2 ) {
const source = this.source_tree( next && next.toString() ).replace( /\n?$/, '\n' )
const tree = this.$.$mol_view_tree2_normalize(
this.$.$mol_tree2_from_string( source )
).kids[0]
return tree
}
@ $mol_mem
united() {
const lib = this.library()
return lib.clone([
... lib.kids,
this.tree(),
])
}
@ $mol_mem
self( next?: string ) {
const tree = this.tree()
if( !next ) return tree.type
this.tree(
tree.struct( next, tree.kids )
)
return next
}
@ $mol_mem
base( next?: string ) {
const self = this.tree()
const base = this.$.$mol_view_tree2_class_super( self )
if( !next ) return base.type
this.tree(
self.clone([
self.struct( next )
])
)
return this.readme_selected( next )
}
@ $mol_mem
self_code() {
const tree = this.tree()
const code = this.$.$mol_tree2_text_to_string_mapped_js(
this.$.$mol_tree2_js_to_text(
this.$.$mol_view_tree2_to_js(
tree.list([ tree ])
)
)
)
const class_name = this.self()
return `
$.$mol_wire_auto = parent.$mol_wire_auto
$.${ class_name } = ${ code }
$.${ class_name } = class ${ class_name } extends $.${ class_name } {
${ this.source_js() }
}
;${ this.source_js_decorators() };
$.$mol_style_attach(${ class_name }, ${ this.source_css() })
`
}
@ $mol_mem
base_options() {
return this.library().kids.map( cl => cl.type ).reverse()
}
@ $mol_mem
class_list() {
return this.library().kids.map( cl => cl.type )
}
@ $mol_mem_key
prop_sign( prop_name: string, next?: string ) {
if( next !== undefined ) {
const tree = this.prop_tree( prop_name )
this.prop_tree( prop_name, tree.struct( next, tree.kids ) )
return next
}
return this.props_map( this.self() ).get( prop_name )?.type || ''
}
@ $mol_mem_key
props_map( base: string ) {
const lib = this.united()
const all = new Map< string, $mol_tree2 >()
const add = ( prop: $mol_tree2 )=> {
const name = this.$.$mol_view_tree2_prop_parts( prop ).name
all.set( name, prop )
}
const collect = ( cl: string )=> {
const sup = lib.select( cl, null ).kids[0]
if( !sup ) return
collect( sup.type )
for( const prop of sup.kids ) add( prop )
}
collect( base )
return all
}
@ $mol_mem_key
props_of( base: string ) {
const lib = this.united()
return lib.list( [ ...this.props_map( base ).values() ].reverse() )
}
props_all() {
return this.props_of( this.self() )
}
@ $mol_mem
prop_filtered() {
return this.props_all().kids.filter(
$mol_match_text(
this.prop_filter(),
prop => [ prop.type ],
)
)
}
@ $mol_mem_key
prop_name( prop_name: string ) {
return prop_name
}
@ $mol_mem
props() {
const props = this.prop_filtered().flatMap( prop => {
const name = this.$.$mol_view_tree2_prop_parts( prop ).name
return name == 'undefined' ? [] : [this.Prop( name )]
} )
return props
}
@ $mol_mem
prop_add_allow() {
const query = this.prop_filter()
if( !query.trim() ) return false
const all = this.props_all()
return all.kids.every( prop => prop.type !== query )
}
prop_add() {
const query = this.prop_filter()
const tree = this.tree()
this.tree(
tree.insert(
tree.struct( query, [
tree.struct( 'null' ),
] ),
null,
query,
)
)
this.prop_filter( '' )
}
@ $mol_mem
props_controls() {
return [
this.Prop_filter(),
... this.prop_add_allow() ? [ this.Prop_add() ] : [],
]
}
@$mol_mem_key
Bound_prop( id: string ) {
const name = id.split( ':' )[ 0 ]
const obj = new this.$.$hyoo_studio_prop()
obj.name = () => this.prop_name( name )
obj.sign = ( next?: string ) => this.prop_sign( name, next )
obj.tree = ( next?: any ) => this.prop_tree( name, next )
obj.props_of = ( klass: any ) => this.props_of( klass )
obj.props_bindable = () => this.props_all()
obj.class_list = () => this.class_list()
obj.Bound_prop = ( id: any ) => this.Bound_prop( id )
return obj
}
@ $mol_mem_key
prop_tree( prop_name: string, next?: $mol_tree2 ) {
const sign = this.prop_sign( prop_name ) || prop_name
if( next !== undefined ) {
this.tree( this.tree().insert( next, this.base(), sign ) )
return next
}
return this.props_all().select( sign ).kids[0] ?? null
}
@ $mol_mem
soure_type_current( next?: string ) {
const options = Object.entries(this.Source_type().switch_options())
if (next === undefined) {
const title = this.$.$mol_state_arg.value('raw_type')
const [value] = options.find(([value, label]) => label === title) ?? ['0']
return value
}
const next_title = options[Number(next)]?.[1]
this.$.$mol_state_arg.value('raw_type', next_title ?? options[0][1])
return next
}
props_css( class_body: string ) {
const props: Map< string/*prop name*/, string/*code*/ > = new Map
let code_start = 0
let body_start = 0
let brace_count = 0
const class_name = this.self().slice(1)
for( let i = 0; i < class_body.length; i++ ) {
const char = class_body[ i ]
if( char == '}' ) {
brace_count--
if( brace_count !== 0 ) continue
const attr = class_body.slice( code_start, body_start ).match( /\[([\w]+)\]/ )?.[1]
const name = attr?.replace( class_name + '_', '' )
if( !name ) continue
props.set( name, class_body.slice( code_start, i + 1 ).trim()! )
code_start = i + 1
}
else if( char == '{' ) {
if( brace_count === 0 ) body_start = i
brace_count++
}
}
console.log('props', props)
console.log('brace_count', brace_count)
if( brace_count !== 0 ) throw new Error('Curly braces is not balanced')
return props
}
@ $mol_mem_key
source_css_prop_default( prop_name: string ) {
const class_name = this.self().slice(1)
return `[${class_name}_${prop_name.toLowerCase()}] {\n\t\n}`
}
@ $mol_mem_key
source_css_prop( prop_name: string, next?: string ) {
const prop_name_lower = prop_name.toLowerCase()
try {
const props = this.props_css( this.source_css() )
if( next === undefined ) {
if( props.get( prop_name_lower ) ) return props.get( prop_name_lower )!
return this.source_css_prop_default( prop_name_lower )
}
props.set( prop_name_lower, next )
this.source_css( [...props.values()].join('\n\n') )
return next
} catch (error) {
if( !this.$.$mol_promise_like( error ) ) {
this.$.$mol_fail_log( error )
}
return this.source_css_prop_default( prop_name_lower )
}
}
@ $mol_mem
source_css_switch( next?: string ) {
const prop_name = this.$.$mol_state_arg.value('raw_prop')
if (!prop_name) return this.source_css(next)
return this.source_css_prop(prop_name, next)
}
@ $mol_mem
source_js_decorators() {
const list = new Array<string>()
const add = (prop_name: string, key: boolean, next: boolean) => {
if (!key && !next) return
if (this.source_js().includes(`/*${prop_name}*/`))
list.push(`($mol_mem${key ? '_key' : ''}(($.${ this.self() }.prototype), "${ prop_name }"));`)
}
this.props().forEach(prop => add(prop.name(), prop.multiple(), prop.changeable()))
return list.join('\n')
}
props_js( class_body: string ) {
const props: Map< string/*prop name*/, string/*code*/ > = new Map
let code_start = 0
let body_start = 0
let brace_count = 0
for( let i = 0; i < class_body.length; i++ ) {
const char = class_body[ i ]
if( char == '}' ) {
brace_count--
if( brace_count !== 0 ) continue
const name = class_body.slice( code_start, body_start ).match( /([\w]+)[\s]*\(/ )?.[1]
if( !name ) continue
props.set( name, class_body.slice( code_start, i + 1 ).trim()! )
code_start = i + 1
}
else if( char == '{' ) {
if( brace_count === 0 ) body_start = i
brace_count++
}
}
if( brace_count !== 0 ) throw new Error('Curly braces is not balanced')
return props
}
@ $mol_mem_key
source_js_prop_default( prop_name: string ) {
const multiple = this.Prop(prop_name).multiple()
const changeable = this.Prop(prop_name).changeable()
const params = [...multiple ? ['key'] : [], ...changeable ? ['next'] : []].join(', ')
return `${prop_name}( ${params} ) {\n\t\n}`
}
@ $mol_mem_key
source_js_prop( prop_name: string, next?: string ) {
try {
const props = this.props_js( this.source_js() )
if( next === undefined ) {
if( props.get( prop_name ) ) return props.get( prop_name )!
return this.source_js_prop_default( prop_name )
}
props.set( prop_name, next )
this.source_js( [...props.values()].join('\n\n') )
return next
} catch (error) {
if( !this.$.$mol_promise_like( error ) ) {
this.$.$mol_fail_log( error )
}
return this.source_js_prop_default( prop_name )
}
}
@ $mol_mem
source_js_switch( next?: string ) {
const prop_name = this.$.$mol_state_arg.value('raw_prop')
if (!prop_name) return this.source_js(next)
return this.source_js_prop(prop_name, next)
}
source_prop_name() {
return super.source_prop_name().replaceAll('{prop_name}', this.$.$mol_state_arg.value('raw_prop')!)
}
@ $mol_action
source_prop_exit() {
this.$.$mol_state_arg.value('raw_prop', null)
}
@ $mol_mem
source_page_body() {
const show = this.$.$mol_state_arg.value('raw_prop') !== null && this.$.$mol_state_arg.value('raw_type') !== 'view.tree'
return [
... show ? [this.Source_prop_switch()] : [],
this.Source_type(),
]
}
}
}