-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
textarea.view.ts
115 lines (80 loc) · 2.25 KB
/
textarea.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
namespace $.$$ {
/**
* An input field for entering multiline text.
* @see https://mol.hyoo.ru/#!section=demos/demo=mol_textarea_demo
*/
export class $mol_textarea extends $.$mol_textarea {
indent_inc() {
let text = this.value()
let [ from, to ] = this.selection()
const rows = text.split( '\n' )
let start = 0
for( let i = 0; i < rows.length; ++i ) {
let end = start + rows[i].length
if( end >= from && start <= to ) {
if( to === from || start !== to ) {
rows[i] = '\t' + rows[i]
to += 1
end += 1
}
}
start = end + 1
}
this.value( rows.join('\n') )
this.selection([ from + 1, to ])
}
indent_dec() {
let text = this.value()
let [ from, to ] = this.selection()
const rows = text.split( '\n' )
let start = 0
for( let i = 0; i < rows.length; ++i ) {
const end = start + rows[i].length
if( end >= from && start <= to && rows[i].startsWith( '\t' ) ) {
rows[i] = rows[i].slice( 1 )
to -= 1
if( start < from ) from -= 1
}
start = end + 1
}
this.value( rows.join('\n') )
this.selection([ from, to ])
}
symbol_insert( event: KeyboardEvent ) {
const symbol = event.shiftKey
? this.symbols_alt_shift()[ $mol_keyboard_code[ event.keyCode ] ]
: event.ctrlKey
? this.symbols_alt_ctrl()[ $mol_keyboard_code[ event.keyCode ] ]
: this.symbols_alt()[ $mol_keyboard_code[ event.keyCode ] ]
if( !symbol ) return
event.preventDefault()
document.execCommand( 'insertText', false, symbol )
}
@ $mol_mem
clickable( next?: boolean ) {
if( !this.enabled() ) return true
return next ?? false
}
hover( event : PointerEvent ) {
this.clickable( event.ctrlKey )
}
press( event : KeyboardEvent ) {
if( event.altKey ) {
this.symbol_insert( event )
} else {
switch( event.keyCode ) {
case !event.shiftKey && $mol_keyboard_code.tab : this.indent_inc() ; break
case event.shiftKey && $mol_keyboard_code.tab : this.indent_dec() ; break
default : return
}
event.preventDefault()
}
}
row_numb( index: number ) {
return index
}
syntax() {
return this.$.$mol_syntax2_md_code
}
}
}