diff options
author | usodhi <61431818+usodhi@users.noreply.github.com> | 2020-10-16 17:09:07 +0530 |
---|---|---|
committer | usodhi <61431818+usodhi@users.noreply.github.com> | 2020-10-16 17:09:07 +0530 |
commit | 9b82c151533ec336155d20b9e1641dc2d7a5cf81 (patch) | |
tree | ad707ddb8edb999301a0c658d170c9da89671bbc /src/fields/util.ts | |
parent | b19972afc64bb9029b78c633e1841988af76adab (diff) | |
parent | e6d700159cb6c273a2df0fd02469e72aa5b48c86 (diff) |
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into restored_server_monitor
Diffstat (limited to 'src/fields/util.ts')
-rw-r--r-- | src/fields/util.ts | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/src/fields/util.ts b/src/fields/util.ts index d48011194..a374c7f54 100644 --- a/src/fields/util.ts +++ b/src/fields/util.ts @@ -162,7 +162,7 @@ export function GetEffectiveAcl(target: any, user?: string): symbol { } function getPropAcl(target: any, prop: string | symbol | number) { - if (prop === UpdatingFromServer || target[UpdatingFromServer] || prop == AclSym) return AclAdmin; // requesting the UpdatingFromServer prop or AclSym must always go through to keep the local DB consistent + if (prop === UpdatingFromServer || target[UpdatingFromServer] || prop === AclSym) return AclAdmin; // requesting the UpdatingFromServer prop or AclSym must always go through to keep the local DB consistent if (prop && DocServer.PlaygroundFields?.includes(prop.toString())) return AclEdit; // playground props are always editable return GetEffectiveAcl(target); } @@ -363,47 +363,59 @@ export function deleteProperty(target: any, prop: string | number | symbol) { } export function updateFunction(target: any, prop: any, value: any, receiver: any) { - let current = ObjectField.MakeCopy(value); + let lastValue = ObjectField.MakeCopy(value); return (diff?: any) => { const op = diff?.op === "$addToSet" ? { '$addToSet': { ["fields." + prop]: SerializationHelper.Serialize(new List<Doc>(diff.items)) } } : diff?.op === "$remFromSet" ? { '$remFromSet': { ["fields." + prop]: SerializationHelper.Serialize(new List<Doc>(diff.items)) } } : { '$set': { ["fields." + prop]: SerializationHelper.Serialize(value) } }; !op.$set && ((op as any).length = diff.length); - - const oldValue = current; + const prevValue = ObjectField.MakeCopy(lastValue as List<any>); + lastValue = ObjectField.MakeCopy(value); const newValue = ObjectField.MakeCopy(value); - current = newValue; + if (!(value instanceof CursorField) && !(value?.some?.((v: any) => v instanceof CursorField))) { !receiver[UpdatingFromServer] && UndoManager.AddEvent( diff?.op === "$addToSet" ? { redo: () => { - receiver[prop].push(...diff.items); + receiver[prop].push(...diff.items.map((item: any) => item.value())); + lastValue = ObjectField.MakeCopy(receiver[prop]); }, undo: action(() => { - const curList = receiver[prop]; - //while (curList[ForwardUpates]) curList = curList[ForwardUpates]; diff.items.forEach((doc: any) => { - const ind = curList.indexOf(doc.value()); - ind !== -1 && curList.splice(ind, 1); + const ind = receiver[prop].indexOf(doc.value()); + ind !== -1 && receiver[prop].splice(ind, 1); }); + lastValue = ObjectField.MakeCopy(receiver[prop]); }) } : diff?.op === "$remFromSet" ? { redo: action(() => { - const curList = receiver[prop]; diff.items.forEach((doc: any) => { - const ind = curList.indexOf(doc.value()); - ind !== -1 && curList.splice(ind, 1); + const ind = receiver[prop].indexOf(doc.value()); + ind !== -1 && receiver[prop].splice(ind, 1); }); + lastValue = ObjectField.MakeCopy(receiver[prop]); }), - undo: () => receiver[prop].push(...diff.items) + undo: () => { + diff.items.map((item: any) => { + const ind = (prevValue as List<any>).indexOf(diff.items[0].value()); + ind !== -1 && receiver[prop].indexOf(diff.items[0].value()) === -1 && receiver[prop].splice(ind, 0, item); + }); + lastValue = ObjectField.MakeCopy(receiver[prop]); + } } : { - redo: () => receiver[prop] = newValue, - undo: () => receiver[prop] = oldValue + redo: () => { + receiver[prop] = ObjectField.MakeCopy(newValue as List<any>); + lastValue = ObjectField.MakeCopy(receiver[prop]); + }, + undo: () => { + receiver[prop] = ObjectField.MakeCopy(prevValue as List<any>); + lastValue = ObjectField.MakeCopy(receiver[prop]); + } }); } target[Update](op); |