@@ -3,21 +3,36 @@ import WebClientModel, { Notebook } from '../model/main';
3
3
4
4
interface User {
5
5
isLogin : boolean ,
6
- name : string ,
7
- avatarUrl : string ,
8
- labels : string [ ] ,
6
+ name ?: string ,
7
+ avatarUrl ?: string ,
8
+ labels ?: string [ ] ,
9
+ } ;
10
+
11
+ interface State {
12
+ currentNoteId : string ,
13
+ editorContent : string ,
9
14
} ;
10
15
11
16
export default class WebClientController {
12
17
private _view :WebClientView ;
13
18
private _model :WebClientModel ;
14
- // private _user: User;
15
- // private _notebook: Notebook;
16
- private _editorContent : string ;
19
+ private _user :User ;
20
+ private _notebook : Notebook ;
21
+ private _state : State ;
17
22
constructor ( view :WebClientView , model :WebClientModel ) {
18
23
this . _view = view ;
19
24
this . _model = model ;
20
- this . _editorContent = '' ;
25
+ this . _user = {
26
+ isLogin : false ,
27
+ } ;
28
+ this . _notebook = {
29
+ title : 'loading' ,
30
+ categories : { }
31
+ } ;
32
+ this . _state = {
33
+ currentNoteId : '' ,
34
+ editorContent : '' ,
35
+ } ;
21
36
this . _initModel ( ) ;
22
37
this . _initViewListener ( ) ;
23
38
}
@@ -30,12 +45,13 @@ export default class WebClientController{
30
45
31
46
// 用户登录
32
47
app . $on ( 'user.login' , ( data : any ) => {
33
- this . _view . setData ( 'userInfo' , {
48
+ this . _user = {
34
49
isLogin : true ,
35
50
name : data . userInfo . name ,
36
51
avatarUrl : data . userInfo . avatarUrl ,
37
52
labels : [ ] ,
38
- } ) ;
53
+ } ;
54
+ this . _view . setData ( 'userInfo' , this . _user ) ;
39
55
40
56
const token = localStorage . getItem ( 'TOONOTE-TOKEN' ) ;
41
57
if ( token ) {
@@ -50,6 +66,21 @@ export default class WebClientController{
50
66
this . _view . setData ( 'editor' , {
51
67
content : note . content
52
68
} ) ;
69
+ this . _state . currentNoteId = note . id ;
70
+ } ) ;
71
+
72
+ // 笔记内容发生变化
73
+ app . $on ( 'editor.change' , async ( data : any ) => {
74
+ // console.log('currentNoteId', this._state.currentNoteId);
75
+ // console.log('content', data.content);
76
+ // todo: 第一次变更不需要保存
77
+ // todo: 内容与id必须得对应,要不然会串
78
+ if ( ! this . _state . currentNoteId ) return ;
79
+ if ( ! data . content ) return ;
80
+ await this . _model . Note . update ( this . _state . currentNoteId , {
81
+ content : data . content
82
+ } ) ;
83
+ // console.log('content change saved.');
53
84
} ) ;
54
85
}
55
86
@@ -59,11 +90,12 @@ export default class WebClientController{
59
90
60
91
61
92
private _fillEmptyData ( ) {
93
+ this . _view . setData ( 'state' , this . _state ) ;
62
94
this . _view . setData ( 'editor' , {
63
- content : '' ,
95
+ content : this . _state . editorContent ,
64
96
} ) ;
65
- this . _view . setData ( 'userInfo' , { } ) ;
66
- this . _view . setData ( 'notebook' , { } ) ;
97
+ this . _view . setData ( 'userInfo' , this . _user ) ;
98
+ this . _view . setData ( 'notebook' , this . _notebook ) ;
67
99
}
68
100
69
101
private async _refreshData ( notebookId ?: string ) {
0 commit comments