1
1
import { API } from "./api-model" ;
2
2
import { APIDispatcher } from "./dispatcher" ;
3
3
import { DynalistModel } from "../dynalist-model" ;
4
+ import * as _ from "lodash" ;
4
5
5
6
interface FetchRequest {
6
7
url : string ;
@@ -18,11 +19,26 @@ export class DynalistClient {
18
19
}
19
20
20
21
public applyChanges = async ( newTrees : DynalistModel . PotentialNodeTree [ ] ) => {
21
- console . log ( "applyChanges called" ) ;
22
+ console . log ( "applyChanges called... " ) ;
22
23
for ( const newTree of newTrees ) {
23
24
const oldTree = await this . getNodeTree ( newTree . key ) ;
24
- const additions : API . DocumentChange . NodeChange [ ] = [ ] ;
25
+ const rootAndAdditions : API . DocumentChange . NodeChange [ ] = [
26
+ {
27
+ action : "edit" ,
28
+ node_id : oldTree . key . nodeId ,
29
+ content : "" , // default
30
+ ..._ . pick < DynalistModel . PotentialNodeTree > ( newTree , [
31
+ "content" ,
32
+ "note" ,
33
+ "checked" ,
34
+ "checkbox" ,
35
+ "heading" ,
36
+ "color"
37
+ ] )
38
+ }
39
+ ] ;
25
40
41
+ // competitive programming habits die hard
26
42
const parent : number [ ] = [ - 1 ] ;
27
43
const flat : DynalistModel . AbstractNodeTree [ ] = [ newTree ] ;
28
44
const index : number [ ] = [ 0 ] ;
@@ -38,22 +54,25 @@ export class DynalistClient {
38
54
}
39
55
} ;
40
56
bfs ( newTree , 0 ) ;
57
+ console . log ( flat ) ;
41
58
for ( const child of flat . slice ( 1 ) ) {
42
59
const { content, note, checked, checkbox, heading, color } = child ;
43
- additions . push ( {
60
+ rootAndAdditions . push ( {
44
61
action : "insert" ,
45
62
parent_id : newTree . key . nodeId ,
46
- content,
47
- note,
48
- checked,
49
- checkbox,
50
- heading,
51
- color
63
+ content : "" , //default
64
+ ..._ . pick < DynalistModel . AbstractNodeTree > ( child , [
65
+ "content" ,
66
+ "note" ,
67
+ "checked" ,
68
+ "checkbox" ,
69
+ "heading" ,
70
+ "color"
71
+ ] )
52
72
} ) ;
53
73
}
54
- const newKeys = await this . editDocument (
55
- newTree . key . documentId ,
56
- additions
74
+ const newKeys = [ newTree . key ] . concat (
75
+ await this . editDocument ( newTree . key . documentId , rootAndAdditions )
57
76
) ;
58
77
const deletionsAndMoves : API . DocumentChange . NodeChange [ ] = [ ] ;
59
78
for ( const child of oldTree . children ) {
@@ -62,6 +81,7 @@ export class DynalistClient {
62
81
node_id : child . key . nodeId
63
82
} ) ;
64
83
}
84
+
65
85
for ( let i = 1 ; i < flat . length ; ++ i ) {
66
86
deletionsAndMoves . push ( {
67
87
action : "move" ,
@@ -179,9 +199,26 @@ export class DynalistClient {
179
199
> ( API . DocumentRead . ENDPOINT , {
180
200
file_id : documentId
181
201
} ) ;
202
+ // strip extra nodes from document
203
+ const nodes = body . nodes . map ( rawNode =>
204
+ this . transformNode ( rawNode , documentId )
205
+ ) ;
206
+ const nodeMap = new Map < string , DynalistModel . ConcreteNode > (
207
+ nodes . map (
208
+ ( node ) : [ string , DynalistModel . ConcreteNode ] => [ node . key . nodeId , node ]
209
+ )
210
+ ) ;
211
+ const rootedIds = [ ] ;
212
+ function dfs ( nodeId : string ) {
213
+ for ( const child of nodeMap . get ( nodeId ) . children ) {
214
+ dfs ( child . nodeId ) ;
215
+ }
216
+ rootedIds . push ( nodeId ) ;
217
+ }
218
+ dfs ( "root" ) ;
182
219
return {
183
220
id : documentId ,
184
- nodes : body . nodes . map ( rawNode => this . transformNode ( rawNode , documentId ) ) ,
221
+ nodes : nodes . filter ( node => rootedIds . includes ( node . key . nodeId ) ) ,
185
222
title : body . title
186
223
} ;
187
224
} ;
@@ -225,7 +262,6 @@ export class DynalistClient {
225
262
method : "POST"
226
263
} ) ;
227
264
const body = await response . json ( ) ;
228
- console . log ( body ) ;
229
265
if ( body . _code !== "Ok" ) return Promise . reject ( body . _msg ) ;
230
266
else return body as Response ;
231
267
} ;
0 commit comments