@@ -8,47 +8,86 @@ class AppView extends Component {
8
8
console . log ( 'AppView props' , props )
9
9
let strs = "abcdefghigklmnopqrst"
10
10
let strArray = strs . split ( "" )
11
- // let nodes = []
12
- // _.forEach(strArray,(v,k)=>{
13
- // console.log(k,v)
14
- // nodes.push({
15
- // id: k,
16
- // text: v
17
- // })
18
- // })
11
+
12
+ // this.state = {
13
+ // data: {children:[{id: 'default'}] },
14
+ // }
15
+ this . buildDefaultData ( ) ;
16
+
17
+ if ( false ) this . buildTestData ( ) ;
18
+ }
19
+ componentWillMount = ( ) => {
20
+ const json = localStorage . getItem ( "NodeJSON" ) ;
21
+ if ( json ) {
22
+ this . parseJson ( json ) ;
23
+ } else {
24
+ this . buildDefaultData ( ) ;
25
+ }
26
+
27
+
28
+ document . onkeydown = ( e ) => {
29
+ let currKey = 0 ;
30
+ e = e || event ;
31
+ currKey = e . keyCode || e . which || e . charCode ; //支持IE、FF
32
+ if ( currKey == 83 && e . ctrlKey ) {
33
+ console . log ( 'Ctrl + S, save' , e , event )
34
+ this . save ( ) ;
35
+ if ( event ) {
36
+ e . returnValue = false ;
37
+ } else {
38
+ e . preventDefault ( ) ;
39
+ }
40
+ }
41
+ // if(currKey==27){ // 按 Esc
42
+ // //要做的事情
43
+ // }
44
+ // if(currKey==113){ // 按 F2
45
+ // //要做的事情
46
+ // }
47
+ } ;
48
+ }
49
+ componentWillUnmount = ( ) => {
50
+ this . save ( ) ;
51
+ }
52
+ save = ( ) => {
53
+ const json = this . toJson ( )
54
+ localStorage . setItem ( "NodeJSON" , json ) ;
55
+
56
+ }
57
+
58
+
59
+
60
+ buildTestData = ( ) => {
19
61
let originData = [
62
+ {
63
+ id : 1 ,
64
+ text : '1' ,
65
+ children : [
20
66
{
21
- id : 1 ,
22
- text : '1' ,
23
- children : [
24
- {
25
- id : 2 ,
26
- text : '2' ,
27
- } ,
28
- {
29
- id : 3 ,
30
- text : '3' ,
31
- } ,
32
- ]
67
+ id : 2 ,
68
+ text : '2' ,
33
69
} ,
34
70
{
35
- id : 4 ,
36
- text : '4' ,
37
- children : [
38
- {
39
- id : 5 ,
40
- text : '5' ,
41
- } ,
42
- {
43
- id : 6 ,
44
- text : '6' ,
45
- } ,
46
- ]
47
- }
48
- ] ;
49
-
50
- // let releations = {}
51
- // releations
71
+ id : 3 ,
72
+ text : '3' ,
73
+ } ,
74
+ ]
75
+ } ,
76
+ {
77
+ id : 4 ,
78
+ text : '4' ,
79
+ children : [
80
+ {
81
+ id : 5 ,
82
+ text : '5' ,
83
+ } ,
84
+ {
85
+ id : 6 ,
86
+ text : '6' ,
87
+ } ,
88
+ ]
89
+ }
90
+ ] ;
52
91
let { nodes, releations} = this . parseOriginData ( { id : 'root' } , originData )
53
92
let rootNode = { id : 'root' }
54
93
this . generateStateData ( rootNode , nodes , releations )
@@ -57,39 +96,42 @@ class AppView extends Component {
57
96
nodes,
58
97
releations
59
98
}
60
- // this.state = {
61
- // data: [
62
- // {
63
- // id: 1,
64
- // text: 'a',
65
- // children: [
66
- // {
67
- // id: 2,
68
- // text: 'b',
69
- // },
70
- // {
71
- // id: 3,
72
- // text: 'c',
73
- // },
74
- // ]
75
- // },
76
- // {
77
- // id: 4,
78
- // text: 'a',
79
- // children: [
80
- // {
81
- // id: 5,
82
- // text: 'b',
83
- // },
84
- // {
85
- // id: 6,
86
- // text: 'c',
87
- // },
88
- // ]
89
- // }
90
- // ],
91
- // version: 1
92
- // }
99
+ }
100
+
101
+ buildDefaultData = ( ) => {
102
+ let originData = [
103
+ {
104
+ id : 'default' ,
105
+ text : '' ,
106
+ }
107
+ ] ;
108
+ let { nodes, releations} = this . parseOriginData ( { id : 'root' } , originData )
109
+ let rootNode = { id : 'root' }
110
+ this . generateStateData ( rootNode , nodes , releations )
111
+ this . state = {
112
+ data : rootNode ,
113
+ nodes,
114
+ releations,
115
+ focusId : 'default' ,
116
+ }
117
+ }
118
+
119
+ toJson = ( ) => {
120
+ const { data, node, releations} = this . state ;
121
+ const json = JSON . stringify ( data . children ) ;
122
+ return json ;
123
+ }
124
+
125
+ parseJson = ( json ) => {
126
+ const object = JSON . parse ( json )
127
+ let { nodes, releations} = this . parseOriginData ( { id : 'root' } , object )
128
+ let rootNode = { id : 'root' }
129
+ this . generateStateData ( rootNode , nodes , releations )
130
+ this . setState ( {
131
+ data : rootNode ,
132
+ nodes,
133
+ releations
134
+ } )
93
135
}
94
136
95
137
parseOriginData = ( root , data ) => {
@@ -207,7 +249,7 @@ class AppView extends Component {
207
249
data : rootNode ,
208
250
nodes,
209
251
releations
210
- } )
252
+ } , ( ) => this . save ( ) )
211
253
212
254
// let temp = data
213
255
// while(!_.isEmpty(temp)) {
@@ -377,7 +419,7 @@ class AppView extends Component {
377
419
data : rootNode ,
378
420
nodes,
379
421
releations
380
- } )
422
+ } , ( ) => this . save ( ) )
381
423
382
424
// _.flattenDeep(data).find( (obj)=> obj.id == id).text = text
383
425
// this.setState({data})
@@ -432,7 +474,7 @@ class AppView extends Component {
432
474
433
475
}
434
476
onFocusChanged = ( id , isFocus ) => {
435
- console . log ( 'onFocusChanged' , id , isFocus )
477
+ // console.log('onFocusChanged',id, isFocus)
436
478
let { focusId} = this . state
437
479
if ( isFocus ) focusId = id
438
480
else focusId = null
@@ -447,16 +489,19 @@ class AppView extends Component {
447
489
let new_releation = { id : new_id }
448
490
let node = _ . find ( releations , ( d ) => d . id == id )
449
491
let firstChild = _ . find ( releations , ( d ) => d . parent_id == id && d . left_id == null )
450
- let rightNode = _ . find ( releations , ( d ) => d . parent_id == id && d . left_id == id )
492
+ let rightNode = _ . find ( releations , ( d ) => d . parent_id == node . parent_id && d . left_id == id )
451
493
452
494
if ( firstChild ) {
453
495
// 插入第一个孩子位置
496
+ console . log ( '插入第一个孩子位置' , node , firstChild )
454
497
new_releation . parent_id = node . id
455
498
new_releation . left_id = null
456
499
new_releation . right_id = firstChild . id
457
500
firstChild . left_id = new_releation . id
458
501
} else {
459
502
// 插入当前位置后面
503
+ console . log ( '插入当前位置后面' , node , rightNode )
504
+
460
505
new_releation . parent_id = node . parent_id
461
506
new_releation . left_id = node . id
462
507
new_releation . right_id = null
@@ -479,7 +524,7 @@ class AppView extends Component {
479
524
nodes,
480
525
releations,
481
526
focusId : new_releation . id
482
- } )
527
+ } , ( ) => this . save ( ) )
483
528
console . log ( 'end onPressEnter setState' , rootNode , nodes , _ . cloneDeep ( releations ) )
484
529
this . printReleations ( _ . cloneDeep ( releations ) )
485
530
0 commit comments