From 4f4d2f8e482b416187e9938ec8c71c60bd7d2565 Mon Sep 17 00:00:00 2001 From: Andrey Platov <87076238+aplatoff@users.noreply.github.com> Date: Sat, 9 Oct 2021 10:54:08 +0200 Subject: [PATCH] initial `$move` implementation (#249) Signed-off-by: Andrey Platov --- packages/core/src/operator.ts | 11 +++++ .../src/components/KanbanView.svelte | 45 +++++++++++-------- server/mongo/src/storage.ts | 41 ++++++++++++++++- 3 files changed, 76 insertions(+), 21 deletions(-) diff --git a/packages/core/src/operator.ts b/packages/core/src/operator.ts index 74f1101229..005370b763 100644 --- a/packages/core/src/operator.ts +++ b/packages/core/src/operator.ts @@ -47,6 +47,16 @@ function $pull (document: Doc, keyval: Record): void { } } +function $move (document: Doc, keyval: Record): void { + const doc = document as any + for (const key in keyval) { + const arr = doc[key] as Array + const desc = keyval[key] + doc[key] = arr.filter(val => val !== desc.$value) + doc[key].splice(desc.$position, 0, desc.$value) + } +} + function $pushMixin (document: Doc, options: any): void { const doc = document as any const mixinId = options.$mixin @@ -74,6 +84,7 @@ function $inc (document: Doc, keyval: Record): void { const operators: Record = { $push, $pull, + $move, $pushMixin, $inc } diff --git a/plugins/view-resources/src/components/KanbanView.svelte b/plugins/view-resources/src/components/KanbanView.svelte index 875804369b..25d3af78b2 100644 --- a/plugins/view-resources/src/components/KanbanView.svelte +++ b/plugins/view-resources/src/components/KanbanView.svelte @@ -63,48 +63,51 @@ return x } - const statesQuery = createQuery() - $: if (_space) statesQuery.query(core.class.State, { _id: { $in: _space.states } }, result => { states = sort(result); console.log('states', sort(result)) }) + $: statesQuery.query(core.class.State, { _id: { $in: _space?.states ?? [] } }, result => { states = sort(result) }) const query = createQuery() - $: if (kanban) query.query(_class, { space }, result => { objects = sortObjects(result) }, options) + $: query.query(_class, { space }, result => { objects = sortObjects(result) }, options) function dragover(ev: MouseEvent, object: Doc) { - // if (dragswap(ev, i)) { if (dragCard !== object) { const dragover = objects.indexOf(object) - const dragging = objects.indexOf(dragCard) - objects[dragover] = dragCard - objects[dragging] = object + objects = objects.filter(x => x !== dragCard) + objects = [...objects.slice(0, dragover), dragCard, ...objects.slice(dragover)] } } + let currentOp: Promise | undefined + async function move(to: number, state: Ref) { - console.log('move version 12') + console.log('INITIAL', dragCardInitialPosition, 'TO', to) const id = dragCard._id const txes: TxCUD[] = [] - if (dragCardInitialState !== state) + if (dragCardInitialState !== state) { client.updateDoc(_class, space, id, { state }) - + // txes.push(client.txFactory.createTxUpdateDoc(_class, space, id, { state })) + } if (dragCardInitialPosition !== to) { - await client.updateDoc(view.class.Kanban, space, kanban._id, { - $pull: { - order: id - } - }) - client.updateDoc(view.class.Kanban, space, kanban._id, { - $push: { + $move: { order: { - $each: [id], + $value: id, $position: to } } }) + + // txes.push(client.txFactory.createTxUpdateDoc(view.class.Kanban, space, kanban._id, { + // $move: { + // order: { + // $value: id, + // $position: to + // } + // } + // })) // await client.updateDoc(core.class.SpaceWithStates, core.space.Model, space, { // $pull: { @@ -124,7 +127,10 @@ // if (txes.length > 0) { // const updateTx = client.txFactory.createTxBulkWrite(space, txes) - // await client.tx(updateTx) + // if (currentOp) { + // await currentOp + // } + // currentOp = client.tx(updateTx).then(() => console.log('move done')).catch(err => console.log('move error ' + err)) // } } @@ -175,6 +181,7 @@ {#each objects as object, j} {#if object.state === state._id} + {j}
{ dragover(ev, object) diff --git a/server/mongo/src/storage.ts b/server/mongo/src/storage.ts index 54419e98f7..6e7b77c21e 100644 --- a/server/mongo/src/storage.ts +++ b/server/mongo/src/storage.ts @@ -133,8 +133,45 @@ class MongoAdapter extends MongoAdapterBase { protected override async txUpdateDoc (tx: TxUpdateDoc): Promise { const domain = this.hierarchy.getDomain(tx.objectClass) - const op = isOperator(tx.operations) ? tx.operations : { $set: tx.operations } - await this.db.collection(domain).updateOne({ _id: tx.objectId }, op) + if (isOperator(tx.operations)) { + const operator = Object.keys(tx.operations)[0] + if (operator === '$move') { + const keyval = (tx.operations as any).$move + const arr = Object.keys(keyval)[0] + const desc = keyval[arr] + const ops = [ + { + updateOne: { + filter: { _id: tx.objectId }, + update: { + $pull: { + [arr]: desc.$value + } + } + } + }, + { + updateOne: { + filter: { _id: tx.objectId }, + update: { + $push: { + [arr]: { + $each: [desc.$value], + $position: desc.$position + } + } + } + } + } + ] + console.log('ops', ops) + await this.db.collection(domain).bulkWrite(ops as any) + } else { + await this.db.collection(domain).updateOne({ _id: tx.objectId }, tx.operations) + } + } else { + await this.db.collection(domain).updateOne({ _id: tx.objectId }, { $set: tx.operations }) + } } override tx (tx: Tx): Promise {