diff options
Diffstat (limited to 'src/new_fields')
-rw-r--r-- | src/new_fields/Doc.ts | 9 | ||||
-rw-r--r-- | src/new_fields/List.ts | 2 | ||||
-rw-r--r-- | src/new_fields/util.ts | 16 |
3 files changed, 20 insertions, 7 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts index 113380ce8..b2979af11 100644 --- a/src/new_fields/Doc.ts +++ b/src/new_fields/Doc.ts @@ -25,6 +25,7 @@ export type FieldResult<T extends Field = Field> = Opt<T> | FieldWaiting<Extract export const Update = Symbol("Update"); export const Self = Symbol("Self"); +const SelfProxy = Symbol("SelfProxy"); export const WidthSym = Symbol("Width"); export const HeightSym = Symbol("Height"); @@ -48,6 +49,7 @@ export class Doc extends RefField { deleteProperty: deleteProperty, defineProperty: () => { throw new Error("Currently properties can't be defined on documents using Object.defineProperty"); }, }); + this[SelfProxy] = doc; if (!id || forceSave) { DocServer.CreateField(SerializationHelper.Serialize(doc)); } @@ -68,7 +70,7 @@ export class Doc extends RefField { const field = value[key]; if (!(field instanceof ObjectField)) continue; field[Parent] = this[Self]; - field[OnUpdate] = updateFunction(this[Self], key, field); + field[OnUpdate] = updateFunction(this[Self], key, field, this[SelfProxy]); } } @@ -81,8 +83,9 @@ export class Doc extends RefField { } private [Self] = this; - public [WidthSym] = () => { return NumCast(this.__fields.width); } // bcz: is this the right way to access width/height? it didn't work with : this.width - public [HeightSym] = () => { return NumCast(this.__fields.height); } + private [SelfProxy]: any; + public [WidthSym] = () => NumCast(this.__fields.width); // bcz: is this the right way to access width/height? it didn't work with : this.width + public [HeightSym] = () => NumCast(this.__fields.height); } export namespace Doc { diff --git a/src/new_fields/List.ts b/src/new_fields/List.ts index 5852e2c30..ff10a3f73 100644 --- a/src/new_fields/List.ts +++ b/src/new_fields/List.ts @@ -218,7 +218,7 @@ class ListImpl<T extends Field> extends ObjectField { } [Copy]() { - let copiedData = this.__fields.map(f => f instanceof ObjectField ? f[Copy]() : f); + let copiedData = this[Self].__fields.map(f => f instanceof ObjectField ? f[Copy]() : f); let deepCopy = new ListImpl<T>(copiedData as any); return deepCopy; } diff --git a/src/new_fields/util.ts b/src/new_fields/util.ts index 128817ab8..bbd8157f6 100644 --- a/src/new_fields/util.ts +++ b/src/new_fields/util.ts @@ -31,7 +31,7 @@ export const setter = action(function (target: any, prop: string | symbol | numb throw new Error("Can't put the same object in multiple documents at the same time"); } value[Parent] = target; - value[OnUpdate] = updateFunction(target, prop, value); + value[OnUpdate] = updateFunction(target, prop, value, receiver); } if (curValue instanceof ObjectField) { delete curValue[Parent]; @@ -86,9 +86,19 @@ export function deleteProperty(target: any, prop: string | number | symbol) { throw new Error("Currently properties can't be deleted from documents, assign to undefined instead"); } -export function updateFunction(target: any, prop: any, value: any) { +export function updateFunction(target: any, prop: any, value: any, receiver: any) { + let current = ObjectField.MakeCopy(value); return (diff?: any) => { - if (!diff) diff = { '$set': { ["fields." + prop]: SerializationHelper.Serialize(value) } }; + if (true || !diff) { + diff = { '$set': { ["fields." + prop]: SerializationHelper.Serialize(value) } }; + const oldValue = current; + const newValue = ObjectField.MakeCopy(value); + current = newValue; + UndoManager.AddEvent({ + redo() { receiver[prop] = newValue; }, + undo() { receiver[prop] = oldValue; } + }); + } target[Update](diff); }; }
\ No newline at end of file |