Skip to content

Commit 3c1d13f

Browse files
committed
finish tree-rewriting code
1 parent 6224959 commit 3c1d13f

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

daemonist/src/api/api-model.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ export namespace API {
110110
export interface EditNodeChange {
111111
action: "edit";
112112
node_id: string;
113-
content?: string;
113+
content: string;
114114
note?: string;
115-
checked?: string;
115+
checked?: boolean;
116116
checkbox?: boolean;
117117
heading?: DynalistModel.HeadingLevel;
118118
color?: DynalistModel.Color;

daemonist/src/api/dynalist-client.ts

+50-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { API } from "./api-model";
22
import { APIDispatcher } from "./dispatcher";
33
import { DynalistModel } from "../dynalist-model";
4+
import * as _ from "lodash";
45

56
interface FetchRequest {
67
url: string;
@@ -18,11 +19,26 @@ export class DynalistClient {
1819
}
1920

2021
public applyChanges = async (newTrees: DynalistModel.PotentialNodeTree[]) => {
21-
console.log("applyChanges called");
22+
console.log("applyChanges called...");
2223
for (const newTree of newTrees) {
2324
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+
];
2540

41+
// competitive programming habits die hard
2642
const parent: number[] = [-1];
2743
const flat: DynalistModel.AbstractNodeTree[] = [newTree];
2844
const index: number[] = [0];
@@ -38,22 +54,25 @@ export class DynalistClient {
3854
}
3955
};
4056
bfs(newTree, 0);
57+
console.log(flat);
4158
for (const child of flat.slice(1)) {
4259
const { content, note, checked, checkbox, heading, color } = child;
43-
additions.push({
60+
rootAndAdditions.push({
4461
action: "insert",
4562
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+
])
5272
});
5373
}
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)
5776
);
5877
const deletionsAndMoves: API.DocumentChange.NodeChange[] = [];
5978
for (const child of oldTree.children) {
@@ -62,6 +81,7 @@ export class DynalistClient {
6281
node_id: child.key.nodeId
6382
});
6483
}
84+
6585
for (let i = 1; i < flat.length; ++i) {
6686
deletionsAndMoves.push({
6787
action: "move",
@@ -179,9 +199,26 @@ export class DynalistClient {
179199
>(API.DocumentRead.ENDPOINT, {
180200
file_id: documentId
181201
});
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");
182219
return {
183220
id: documentId,
184-
nodes: body.nodes.map(rawNode => this.transformNode(rawNode, documentId)),
221+
nodes: nodes.filter(node => rootedIds.includes(node.key.nodeId)),
185222
title: body.title
186223
};
187224
};
@@ -225,7 +262,6 @@ export class DynalistClient {
225262
method: "POST"
226263
});
227264
const body = await response.json();
228-
console.log(body);
229265
if (body._code !== "Ok") return Promise.reject(body._msg);
230266
else return body as Response;
231267
};

0 commit comments

Comments
 (0)