-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.view.ts
102 lines (79 loc) · 2.7 KB
/
tree.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
namespace $.$$ {
export class $hyoo_tree extends $.$hyoo_tree {
@ $mol_mem
pipeline( next?: string[] ) {
const str = this.$.$mol_state_arg.value( 'pipeline', next && next.join( '~' ) )
return str && str.split( '~' ).filter( Boolean ) || super.pipeline()
}
add( index: number, next?: string ) {
if( next ) {
this.pipeline([
... this.pipeline().slice( 0, index + 1 ),
next,
])
}
return ''
}
@ $mol_mem
source( next? : string ) {
return this.$.$mol_state_arg.value( 'source' , next ) ?? super.source()
}
@ $mol_mem_key
transform( index: number, next?: string ) {
let pipeline = this.pipeline()
if( next ) pipeline = this.pipeline([
... pipeline.slice( 0, index ),
next,
... pipeline.slice( index + 1 ),
])
return pipeline[ index ] ?? null
}
@ $mol_mem
transform_options() {
const map = this.transform_map()
const pipeline = this.pipeline()
const last = pipeline[ pipeline.length - 1 ]
const type = last ? map[ last as keyof typeof map ].output.split('.').filter( Boolean ).reverse() : [ 'text' ]
if( !type.length ) return Object.keys( map )
return (Object.keys( map ) as (keyof typeof map)[]).filter( id => {
const diff = $mol_diff_path( type , map[ id ].input.split('.').reverse() )
if( !diff.prefix.length ) return false
if( diff.suffix.every( s => s.length ) ) return false
return true
} )
}
@ $mol_mem_key
result( index: number ): string | $mol_tree2 | Uint8Array | $mol_wasm_module {
const func = this.pipeline()[ index ]
if( !func ) return ''
const arg = index ? this.result( index - 1 ) : this.source()
const val = ( this.$ as any )[ func ] as any as Function
if( $mol_func_is_class( val ) ) {
return new val( arg )
} else if( typeof val === 'function' ) {
return val.call( arg ) ?? ''
} else {
return ''
}
}
@ $mol_mem
result_text(): string {
let res = $mol_try( ()=> this.result( this.pipeline().length - 1 ) )
if( res instanceof Promise ) $mol_fail_hidden( res )
if( typeof res === 'string' ) return res
if( Object( res ) !== res ) return String( res )
if( res instanceof $mol_dom_context.Node ) return $mol_dom_serialize( res )
if( !Reflect.getPrototypeOf( Reflect.getPrototypeOf( res )! ) ) return JSON.stringify( res, null, '\t' )
if( Array.isArray( res ) ) return JSON.stringify( res, null, '\t' )
let mime = 'application/octet-stream'
if( res instanceof $mol_wasm_module ) {
res = new Uint8Array( res.buffer )
mime = 'application/wasm'
}
if( res instanceof Uint8Array ) {
return `data:${ mime };base64,${ $mol_base64_encode( res ) }`
}
return String( res )
}
}
}