1
1
import { Element , h } from "./base/element" ;
2
+ import { Suggest } from "./base/suggest" ;
2
3
import { Cell , getStyleFromCell } from "../core/cell"
4
+ import { Formula } from "../core/formula" ;
3
5
4
6
export class Editor {
5
7
el : Element ;
@@ -8,15 +10,30 @@ export class Editor {
8
10
editor : Element ;
9
11
textarea : Element ;
10
12
textline : Element ; // 计算输入文本的宽度用的element
11
- change : ( v : Cell ) => void = ( v ) => { } ;
12
- constructor ( public defaultRowHeight : number ) {
13
+ suggest : Suggest ; // autocomplete show
13
14
15
+ change : ( v : Cell ) => void = ( v ) => { } ;
16
+ constructor ( public defaultRowHeight : number , public formulas : Array < Formula > ) {
17
+ const suggestList : any = formulas . map ( it => [ it . key , it . title ] )
14
18
this . el = h ( ) . children ( [ this . editor = h ( ) . class ( 'spreadsheet-editor' ) . children ( [
15
19
this . textarea = h ( 'textarea' )
16
20
. on ( 'input' , ( evt : Event ) => this . inputChange ( evt ) ) ,
17
21
this . textline = h ( ) . styles ( { visibility : 'hidden' , overflow : 'hidden' , position : 'fixed' , top : '0' , left : '0' } )
18
22
] )
19
- ] ) . hide ( )
23
+ , this . suggest = new Suggest ( suggestList , 180 ) ] )
24
+
25
+ this . suggest . itemClick = ( it ) => {
26
+ console . log ( '>>>>>>>>>>>>' , it )
27
+ const text = `=${ it [ 0 ] } ()` ;
28
+ if ( this . value ) {
29
+ this . value . text = text
30
+ }
31
+ this . textarea . val ( text ) ;
32
+ this . textline . html ( text ) ;
33
+ this . setTextareaRange ( text . length - 1 )
34
+ // (<any>this.textarea.el).setSelectionRange(text.length + 1, text.length + 1);
35
+ // setTimeout(() => (<any>this.textarea.el).focus(), 10)
36
+ }
20
37
}
21
38
22
39
onChange ( change : ( v : Cell ) => void ) {
@@ -28,8 +45,9 @@ export class Editor {
28
45
this . target = target ;
29
46
const text = this . setValue ( value )
30
47
this . el . show ( ) ;
31
- ( < any > this . textarea . el ) . setSelectionRange ( text . length , text . length ) ;
32
- setTimeout ( ( ) => ( < any > this . textarea . el ) . focus ( ) , 10 )
48
+ this . setTextareaRange ( text . length )
49
+ // (<any>this.textarea.el).setSelectionRange(text.length, text.length);
50
+ // setTimeout(() => (<any>this.textarea.el).focus(), 10)
33
51
this . reload ( ) ;
34
52
}
35
53
@@ -59,6 +77,13 @@ export class Editor {
59
77
this . textline . html ( '' )
60
78
}
61
79
80
+ private setTextareaRange ( position : number ) {
81
+ setTimeout ( ( ) => {
82
+ ( < any > this . textarea . el ) . setSelectionRange ( position , position ) ;
83
+ ( < any > this . textarea . el ) . focus ( )
84
+ } , 10 )
85
+ }
86
+
62
87
private inputChange ( evt : any ) {
63
88
const v = evt . target . value
64
89
if ( ! / ^ \s * $ / . test ( v ) ) {
@@ -67,11 +92,26 @@ export class Editor {
67
92
} else {
68
93
this . value = { text : v }
69
94
}
95
+ this . autocomplete ( v ) ;
70
96
}
71
97
this . textline . html ( v ) ;
72
98
this . reload ( )
73
99
}
74
100
101
+ private autocomplete ( v : string ) {
102
+ if ( v [ 0 ] === '=' ) {
103
+ if ( ! v . includes ( '(' ) ) {
104
+ const search = v . substring ( 1 )
105
+ console . log ( ':::;search word:' , search )
106
+ this . suggest . search ( this . editor , search ) ;
107
+ } else {
108
+ this . suggest . hide ( )
109
+ }
110
+ } else {
111
+ this . suggest . hide ( )
112
+ }
113
+ }
114
+
75
115
reload ( ) {
76
116
// setTimeout(() => {
77
117
if ( this . target ) {
0 commit comments