Skip to content

Commit e1d243c

Browse files
committed
save content when changed (with bugs)
1 parent d9d9653 commit e1d243c

File tree

3 files changed

+60
-21
lines changed

3 files changed

+60
-21
lines changed

lib/controller/main.ts

+44-12
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,36 @@ import WebClientModel, { Notebook } from '../model/main';
33

44
interface User {
55
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,
914
};
1015

1116
export default class WebClientController{
1217
private _view:WebClientView;
1318
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;
1722
constructor(view:WebClientView, model:WebClientModel){
1823
this._view = view;
1924
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+
};
2136
this._initModel();
2237
this._initViewListener();
2338
}
@@ -30,12 +45,13 @@ export default class WebClientController{
3045

3146
// 用户登录
3247
app.$on('user.login', (data: any) => {
33-
this._view.setData('userInfo', {
48+
this._user = {
3449
isLogin: true,
3550
name: data.userInfo.name,
3651
avatarUrl: data.userInfo.avatarUrl,
3752
labels: [],
38-
});
53+
};
54+
this._view.setData('userInfo', this._user);
3955

4056
const token = localStorage.getItem('TOONOTE-TOKEN');
4157
if(token){
@@ -50,6 +66,21 @@ export default class WebClientController{
5066
this._view.setData('editor', {
5167
content: note.content
5268
});
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.');
5384
});
5485
}
5586

@@ -59,11 +90,12 @@ export default class WebClientController{
5990

6091

6192
private _fillEmptyData(){
93+
this._view.setData('state', this._state);
6294
this._view.setData('editor', {
63-
content: '',
95+
content: this._state.editorContent,
6496
});
65-
this._view.setData('userInfo', {});
66-
this._view.setData('notebook', {});
97+
this._view.setData('userInfo', this._user);
98+
this._view.setData('notebook', this._notebook);
6799
}
68100

69101
private async _refreshData(notebookId?: string){

lib/view/components/Main.vue

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</template>
1515

1616
<script type="ts">
17-
import { createComponent, reactive } from '@vue/composition-api';
17+
import { createComponent, reactive, watch } from '@vue/composition-api';
1818
1919
import { getData } from '../dataInjector';
2020
@@ -33,6 +33,13 @@ export default createComponent({
3333
setup(props, ctx){
3434
const editor = reactive(getData('editor'));
3535
const userInfo = reactive(getData('userInfo'));
36+
37+
watch(() => {
38+
ctx.root.$webClient.$emit('editor.change', {
39+
content: editor.data.content
40+
});
41+
});
42+
3643
return {
3744
editor,
3845
userInfo,

lib/view/components/NoteTree.vue

+8-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
draggable="true"
4343
class="icon note"
4444
:key="note.id"
45-
:class="{active:note.id === state.currentActiveNoteId}"
45+
:class="{active:note.id === state.data.currentNoteId}"
4646
v-for="note in notes"
4747
@contextmenu="showContextMenu('note', note.id)"
4848
@click.stop="switchActiveNote(note.id)"
@@ -66,10 +66,10 @@ import { getData } from '../dataInjector';
6666
6767
export default createComponent({
6868
setup(props, ctx){
69+
const state = reactive(getData('state'));
6970
const notebook = reactive(getData('notebook'));
70-
const state = reactive({
71+
const localState = reactive({
7172
currentContextMenuNoteId: '',
72-
currentActiveNoteId: '',
7373
});
7474
7575
const foldMap = ref({});
@@ -97,12 +97,11 @@ export default createComponent({
9797
}; */
9898
9999
const showContextMenu = function(type, id){
100-
// console.log(state.currentContextMenuNoteId);
101-
state.currentContextMenuNoteId = id;
100+
localState.currentContextMenuNoteId = id;
102101
};
103102
104103
const hideContextMenu = function(){
105-
state.currentContextMenuNoteId = '';
104+
localState.currentContextMenuNoteId = '';
106105
}
107106
108107
const drop = function(){};
@@ -119,19 +118,20 @@ export default createComponent({
119118
120119
const deleteNote = function(){
121120
ctx.root.$webClient.$emit('note.delete', {
122-
id: state.currentContextMenuNoteId,
121+
id: localState.currentContextMenuNoteId,
123122
});
124123
}
125124
126125
const switchActiveNote = function(id){
127-
state.currentActiveNoteId = id;
126+
state.data.currentNoteId = id;
128127
ctx.root.$webClient.$emit('note.switchActive', {
129128
id
130129
});
131130
}
132131
133132
return {
134133
state,
134+
localState,
135135
notebook,
136136
foldMap,
137137
switchFold,

0 commit comments

Comments
 (0)