Skip to content

Commit

Permalink
feat(notes): use state sync for indent/outdent as well
Browse files Browse the repository at this point in the history
  • Loading branch information
thesophiaxu committed Jan 13, 2022
1 parent 54c26ff commit c30141c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 37 deletions.
85 changes: 54 additions & 31 deletions packages/unigraph-dev-common/src/api/unigraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,61 @@ export const getObjectAs = (object: any, type: 'primitive') => {
return object;
};

export const uidMerger = (objValue: any, srcValue: any) => {
if (_.isArray(objValue) && !_.isArray(srcValue)) {
srcValue = [srcValue];
} else if (!_.isArray(objValue) && _.isArray(srcValue)) {
objValue = [objValue];
}
/**
* Merges a single source object to a target object.
*
* NOTE: this function mutates the target object.
*
* @param target
* @param source
*/
export const deepMerge = (target: any, source: any) => {
const recurse = (targ: any, src: any) => {
if (_.isArray(targ) && !_.isArray(src)) {
src = [src];
} else if (!_.isArray(targ) && _.isArray(src)) {
targ = [targ];
}

if (_.isArray(objValue) && _.isArray(srcValue)) {
const uids: string[] = [];
const [[primObj, objObj], [primSrc, objSrc]] = [objValue, srcValue].map((arr) => {
const objs: any[] = [];
const prims: any[] = [];
arr.forEach((el) => {
if (typeof el?.uid === 'string') {
objs.push(el);
uids.push(el.uid);
} else prims.push(el);
if (_.isArray(targ) && _.isArray(src)) {
const uids: string[] = [];
const [[primObj, objObj], [primSrc, objSrc]] = [targ, src].map((arr) => {
const objs: any[] = [];
const prims: any[] = [];
arr.forEach((el) => {
if (typeof el?.uid === 'string') {
objs.push(el);
uids.push(el.uid);
} else prims.push(el);
});
return [prims, objs];
});
return [prims, objs];
});
const finPrims = _.uniq(primObj.concat(primSrc));
const finObjs: any[] = [];
_.uniq(uids).forEach((uid) => {
const obj = objObj.find((el) => el.uid === uid);
const src = objSrc.find((el) => el.uid === uid);
if (obj && src) finObjs.push(_.mergeWith(obj, src, uidMerger));
else if (obj) finObjs.push(obj);
else if (src) finObjs.push(src);
});
return [...finPrims, ...finObjs];
}
return undefined;
const finPrims = _.uniq(primObj.concat(primSrc));
const finObjs: any[] = [];
_.uniq(uids).forEach((uid) => {
const obj = objObj.find((el) => el.uid === uid);
const srcc = objSrc.find((el) => el.uid === uid);
if (obj && srcc) finObjs.push(recurse(obj, srcc));
else if (obj) finObjs.push(obj);
else if (srcc) finObjs.push(srcc);
});
return [...finPrims, ...finObjs];
}

if (targ?.uid && src?.uid && targ.uid !== src.uid) {
return src;
}

// Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties
for (const key of Object.keys(src)) {
if (src[key] instanceof Object) Object.assign(src[key], recurse(targ[key], src[key]));
}

// Join `target` and modified `source`
return Object.assign(targ || {}, src);
};

return recurse(target, source);
};

// TODO: Switch to prototype-based, faster helper functions
Expand Down Expand Up @@ -453,7 +476,7 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
// Merge updater object with existing one
const newObj = JSON.parse(JSON.stringify(subResults[subId]));
const changeLoc = findUid(newObj, uid);
_.mergeWith(changeLoc, JSON.parse(JSON.stringify(newObject)), uidMerger);
deepMerge(changeLoc, JSON.parse(JSON.stringify(newObject)));
augmentStubs(changeLoc, subResults[subId]);
subResults[subId] = newObj;
setTimeout(() => {
Expand Down
12 changes: 6 additions & 6 deletions packages/unigraph-dev-common/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function augmentStubs(objWithStubs: any, origObj: any) {
} else {
if (curr.uid && seen.includes(curr.uid)) return;
if (curr.uid) seen = [...seen, curr.uid];
if (curr.uid && JSON.stringify(curr, getCircularReplacer()).length >= 100) {
if (curr.uid && Object.keys(curr).length >= 2) {
uidDict[curr.uid] = curr;
}
Object.keys(curr).forEach((el) => {
Expand All @@ -115,17 +115,17 @@ export function augmentStubs(objWithStubs: any, origObj: any) {
} else {
if (curr.uid && seen.includes(curr.uid)) return;
if (curr.uid) seen = [...seen, curr.uid];
Object.keys(curr).forEach((el) => {
recurseObj(curr[el], seen);
});
if (
curr.uid &&
uidDict[curr.uid] &&
Object.keys(curr).length <= 2 &&
JSON.stringify(curr, getCircularReplacer()).length < 50
_.difference(Object.keys(uidDict[curr.uid]), Object.keys(curr)).length > 0
) {
_.merge(curr, uidDict[curr.uid], JSON.parse(JSON.stringify(curr)));
Object.assign(curr, uidDict[curr.uid], JSON.parse(JSON.stringify(curr)));
}
Object.keys(curr).forEach((el) => {
recurseObj(curr[el], seen);
});
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/unigraph-dev-explorer/src/examples/notes/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export const indentChild = (data: any, context: NoteEditorContext, index: number
false,
context.callbacks.subsId,
parents,
true,
);
window.unigraph.touch(parents.map((el) => el.uid));
context.edited.current = true;
Expand Down Expand Up @@ -371,6 +372,7 @@ export const unindentChild = async (data: any, context: NoteEditorContext, paren
false,
[],
parents,
true,
);
window.unigraph.touch(parents.map((el) => el.uid));
await window.unigraph.deleteItemFromArray(delUidPar, delUidChild);
Expand Down

0 comments on commit c30141c

Please sign in to comment.