Skip to content

Commit 42246f2

Browse files
committed
fix local resolver order for add/delete/pastePod
1 parent 0b22d1a commit 42246f2

File tree

7 files changed

+98
-60
lines changed

7 files changed

+98
-60
lines changed

cpkernel/src/resolver_local.js

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,52 @@ async function readpods(dir) {
5757
return d;
5858
}
5959

60+
async function pastePod({ appDir, reponame, id, parentId, index }) {
61+
let jsonfile = path.join(appDir, reponame, ".pods", `${id}.json`);
62+
let jobj = JSON.parse(await fs.promises.readFile(jsonfile));
63+
// remove from old parent
64+
{
65+
let parent_json = path.join(
66+
appDir,
67+
reponame,
68+
".pods",
69+
`${jobj.parent}.json`
70+
);
71+
let parent_obj = JSON.parse(await fs.promises.readFile(parent_json));
72+
let oldindex = parent_obj.children.indexOf(id);
73+
if (jobj.parent == parentId && index > oldindex) {
74+
// moving within the same deck.
75+
// from 5 to 3: simply remove 5 and insert into 3
76+
// from 5 to 7: remove 5, insert to 6
77+
index -= 1;
78+
}
79+
parent_obj.children.splice(oldindex, 1);
80+
await fs.promises.writeFile(
81+
parent_json,
82+
JSON.stringify(parent_obj, null, 2)
83+
);
84+
}
85+
86+
{
87+
// add to the new parent
88+
let parent_json = path.join(appDir, reponame, ".pods", `${parentId}.json`);
89+
let parent_obj = JSON.parse(await fs.promises.readFile(parent_json));
90+
parent_obj.children.splice(index, 0, id);
91+
await fs.promises.writeFile(
92+
parent_json,
93+
JSON.stringify(parent_obj, null, 2)
94+
);
95+
}
96+
// set the pod data itself
97+
jobj.parent = parentId;
98+
await fs.promises.writeFile(jsonfile, JSON.stringify(jobj, null, 2));
99+
100+
return true;
101+
}
102+
60103
export function getResolvers(appDir) {
61104
if (!fs.existsSync(appDir)) {
62-
fs.mkdirSync(appDir, {recursive: true});
105+
fs.mkdirSync(appDir, { recursive: true });
63106
}
64107
return {
65108
Query: {
@@ -238,11 +281,17 @@ export function getResolvers(appDir) {
238281
);
239282
return true;
240283
},
241-
pastePod: async (_, { id, parentId, index, column }) => {
242-
return false;
243-
},
244-
pastePods: async (_, { ids, parentId, index, column }) => {
245-
return false;
284+
pastePods: async (_, { reponame, ids, parentId, index }) => {
285+
for (let id of ids) {
286+
await pastePod({
287+
appDir,
288+
reponame,
289+
id,
290+
parentId,
291+
index,
292+
});
293+
index += 1;
294+
}
246295
},
247296
updatePod: async (_, { reponame, username, input }, { userId }) => {
248297
let { id } = input;
@@ -264,7 +313,7 @@ export function getResolvers(appDir) {
264313
`${child.parent}.json`
265314
);
266315
let parent = JSON.parse(await fs.promises.readFile(parent_fname));
267-
parent.children.splice(child.index, 1);
316+
parent.children.splice(parent.children.indexOf(child.id), 1);
268317
await fs.promises.writeFile(
269318
parent_fname,
270319
JSON.stringify(parent, null, 2)

cpkernel/src/typedefs.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ export const typeDefs = gql`
9999
reponame: String
100100
username: String
101101
): Boolean
102-
pastePod(id: String, parentId: String, index: Int, column: Int): Boolean
103-
pastePods(ids: [String], parentId: String, index: Int, column: Int): Boolean
102+
pastePods(reponame: String, ids: [String], parentId: String, index: Int, column: Int): Boolean
104103
updatePod(reponame: String, username: String, input: PodInput): Boolean
105104
clearUser: Boolean
106105
clearRepo: Boolean

ui/src/components/repo/toolbar.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export function UpButton({ pod }) {
261261
dispatch(
262262
qActions.remoteAdd({
263263
parent: pod.parent,
264-
index: pod.index,
264+
anchor: pod.id,
265265
type: pod.type === "DECK" ? "DECK" : pod.type,
266266
lang: pod.lang,
267267
column: pod.column,
@@ -279,7 +279,7 @@ export function UpButton({ pod }) {
279279
dispatch(
280280
qActions.remotePaste({
281281
parent: pod.parent,
282-
index: pod.index,
282+
anchor: pod.id,
283283
column: pod.column,
284284
})
285285
);
@@ -304,7 +304,8 @@ export function DownButton({ pod }) {
304304
dispatch(
305305
qActions.remoteAdd({
306306
parent: pod.parent,
307-
index: pod.index + 1,
307+
anchor: pod.id,
308+
shift: 1,
308309
type: pod.type === "DECK" ? "DECK" : pod.type,
309310
lang: pod.lang,
310311
column: pod.column,
@@ -326,7 +327,8 @@ export function DownButton({ pod }) {
326327
dispatch(
327328
qActions.remotePaste({
328329
parent: pod.parent,
329-
index: pod.index + 1,
330+
anchor: pod.id,
331+
shift: 1,
330332
column: pod.column,
331333
})
332334
);

ui/src/lib/queue/middleware.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ export default (storeAPI) => (next) => (action) => {
4444
{
4545
// construct the ID here so that the client and the server got the same ID
4646
let payload = produce(action.payload, (draft) => {
47+
let { parent, index, anchor, shift } = draft;
48+
if (!index) {
49+
index = storeAPI.getState().repo.pods[parent].children.findIndex(({ id }) => id === anchor)
50+
if (index == -1) throw new Error("Cannot find anchoar pod:", anchor)
51+
index += shift | 0
52+
draft.index = index
53+
}
4754
const id = "CP" + nanoid();
4855
draft.id = id;
4956
});
@@ -107,7 +114,7 @@ export default (storeAPI) => (next) => (action) => {
107114
break;
108115
case "REMOTE_PASTE":
109116
{
110-
let { parent, index, column } = action.payload;
117+
let { parent, index, anchor, shift, column } = action.payload;
111118
let pods = store.getState().repo.pods;
112119
// get all clipped pods
113120
let clip = Object.entries(pods)
@@ -119,11 +126,18 @@ export default (storeAPI) => (next) => (action) => {
119126
}
120127
// clear last clip
121128
storeAPI.dispatch(repoSlice.actions.clearLastClip());
129+
// compute index
130+
if (!index) {
131+
index = storeAPI.getState().repo.pods[parent].children.findIndex(({ id }) => id === anchor)
132+
if (index == -1) throw new Error("Cannot find anchoar pod:", anchor)
133+
index += shift | 0
134+
}
135+
let tmpindex = index;
122136
for (let id of clip) {
123137
storeAPI.dispatch(
124-
repoSlice.actions.pastePod({ parent, index, column, id })
138+
repoSlice.actions.pastePod({ parent, index: tmpindex, column, id })
125139
);
126-
index += 1;
140+
tmpindex += 1;
127141
}
128142
storeAPI.dispatch(
129143
repoSlice.actions.addPodQueue({

ui/src/lib/queue/queueActions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const loopPodQueue = createAsyncThunk(
3333

3434
case "REMOTE_PASTE":
3535
{
36-
return await doRemotePastePod(action.payload);
36+
return await doRemotePastePod({ reponame, ...action.payload });
3737
}
3838
break;
3939

ui/src/lib/reducers/pod.js

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ export function addPod(state, action) {
88
}
99
// update all other siblings' index
1010
// FIXME this might cause other pods to be re-rendered
11-
state.pods[parent].children.forEach(({ id }) => {
12-
if (state.pods[id].index >= index) {
13-
state.pods[id].index += 1;
14-
}
15-
});
1611
const pod = {
1712
content: "",
1813
column: 1,
@@ -41,13 +36,10 @@ export function addPod(state, action) {
4136
// TODO the children no longer need to be ordered
4237
// TODO the frontend should handle differently for the children
4338
// state.pods[parent].children.splice(index, 0, id);
44-
state.pods[parent].children.push({ id, type: pod.type });
39+
state.pods[parent].children.splice(index, 0, { id, type: pod.type })
4540
// DEBUG sort-in-place
4641
// TODO I can probably insert
4742
// CAUTION the sort expects -1,0,1, not true/false
48-
state.pods[parent].children.sort(
49-
(a, b) => state.pods[a.id].index - state.pods[b.id].index
50-
);
5143
pod.ns = computeNamespace(state.pods, id);
5244
}
5345

@@ -58,11 +50,6 @@ export function deletePod(state, action) {
5850
const index = parent.children.map(({ id }) => id).indexOf(id);
5951

6052
// update all other siblings' index
61-
parent.children.forEach(({ id }) => {
62-
if (state.pods[id].index >= index) {
63-
state.pods[id].index -= 1;
64-
}
65-
});
6653
// remove all
6754
parent.children.splice(index, 1);
6855
toDelete.forEach((id) => {
@@ -75,34 +62,20 @@ export function pastePod(state, action) {
7562
let pod = state.pods[id];
7663
// 1. remove the clipped pod
7764
let oldparent = state.pods[pod.parent];
78-
oldparent.children.splice(
79-
oldparent.children.map(({ id }) => id).indexOf(pod.id),
80-
1
81-
);
82-
oldparent.children.forEach(({ id }) => {
83-
if (state.pods[id].index > pod.index) {
84-
state.pods[id].index -= 1;
85-
}
86-
});
65+
let oldindex = oldparent.children.map(({ id }) => id).indexOf(pod.id)
66+
if (oldindex == -1) {
67+
throw new Error("Pod not found", pod.id)
68+
}
69+
oldparent.children.splice(oldindex, 1);
70+
if (oldparent.id === parent && index > oldindex) {
71+
index -= 1
72+
}
8773
// 2. insert into the new position
8874
let newparent = state.pods[parent];
89-
if (newparent === oldparent && index > pod.index) {
90-
// need special adjustment if parent is not changed.
91-
index -= 1;
92-
}
93-
newparent.children.forEach(({ id }) => {
94-
if (state.pods[id].index >= index) {
95-
state.pods[id].index += 1;
96-
}
97-
});
98-
newparent.children.push({ id: pod.id, type: pod.type });
75+
newparent.children.splice(index, 0, { id: pod.id, type: pod.type });
9976
// 3. set the data of the pod itself
10077
pod.parent = parent;
10178
pod.column = column;
102-
pod.index = index;
103-
newparent.children.sort(
104-
(a, b) => state.pods[a.id].index - state.pods[b.id].index
105-
);
10679
// 4. update namespace
10780
function helper(node) {
10881
node.ns = computeNamespace(state.pods, node.id);

ui/src/lib/remote/fetch.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ export function normalize(pods) {
116116
console.log(pod);
117117
pod.children = pod.children
118118
? pod.children.map((id) => ({
119-
id: res[id].id,
120-
type: res[id].type,
121-
}))
119+
id: res[id].id,
120+
type: res[id].type,
121+
}))
122122
: [];
123123
// change children.id format
124124
// UDPATE Or, I just put {id,type} in the children array
@@ -313,7 +313,7 @@ export async function doRemoteUpdatePod({ pod, reponame, username }) {
313313
return result;
314314
}
315315

316-
export async function doRemotePastePod({ clip, parent, index, column }) {
316+
export async function doRemotePastePod({ clip, reponame, parent, index, column }) {
317317
const res = await fetch(graphql_url, {
318318
method: "POST",
319319
headers: {
@@ -324,14 +324,15 @@ export async function doRemotePastePod({ clip, parent, index, column }) {
324324
body: JSON.stringify({
325325
// movePod(id: String, parentId: String, index: Int): Boolean
326326
query: `
327-
mutation PastePods($ids: [String], $parentId: String, $index: Int, $column: Int) {
328-
pastePods(ids: $ids, parentId: $parentId, index: $index, column: $column)
327+
mutation PastePods($reponame: String, $ids: [String], $parentId: String, $index: Int, $column: Int) {
328+
pastePods(reponame: $reponame, ids: $ids, parentId: $parentId, index: $index, column: $column)
329329
}`,
330330
variables: {
331331
ids: clip,
332332
parentId: parent,
333333
index,
334334
column,
335+
reponame,
335336
},
336337
}),
337338
});

0 commit comments

Comments
 (0)