refactor: snapshot store to diff (#3155)

* refactor: snapshot store to diff

* change initial state position

* fix old snapshot format

* encapsulate json diff
This commit is contained in:
heheer
2024-11-21 13:12:42 +08:00
committed by GitHub
parent 4f55025906
commit 9b2c3b242a
11 changed files with 364 additions and 101 deletions

View File

@@ -0,0 +1,20 @@
import { create } from 'jsondiffpatch';
const createWorkflowDiffPatcher = () =>
create({
objectHash: (obj: any) => obj.id || obj.nodeId || obj._id,
propertyFilter: (name: string) => name !== 'selected'
});
const diffPatcher = createWorkflowDiffPatcher();
export const createDiff = <T extends Record<string, unknown>>(initialState?: T, newState?: T) => {
return diffPatcher.diff(initialState, newState);
};
export const applyDiff = <T extends Record<string, unknown>>(
initialState?: T,
diff?: ReturnType<typeof diffPatcher.diff>
) => {
return diffPatcher.patch(structuredClone(initialState), diff) as T;
};

View File

@@ -631,3 +631,14 @@ export const compareSnapshot = (
return isEqual(node1, node2);
};
// remove node size
export const simplifyNodes = (nodes: Node[]) => {
return nodes.map((node) => ({
id: node.id,
type: node.type,
position: node.position,
data: node.data,
zIndex: node.zIndex
}));
};