fix animation
This commit is contained in:
@@ -30,6 +30,22 @@ const cloneObjectList = (objects: EditorObject[]): EditorObject[] =>
|
||||
const objectListsEqual = (a: EditorObject[], b: EditorObject[]) =>
|
||||
JSON.stringify(a) === JSON.stringify(b);
|
||||
|
||||
/** یک آرایهٔ objects برای state و صفحهٔ جاری در pages (جلوگیری از از دست رفتن فیلدهایی مثل entranceAnimation) */
|
||||
const withSyncedCurrentPageObjects = (
|
||||
state: { objects: EditorObject[]; pages: Page[]; currentPageId: string | null },
|
||||
nextObjects: EditorObject[],
|
||||
) => {
|
||||
if (!state.currentPageId) {
|
||||
return { objects: nextObjects };
|
||||
}
|
||||
return {
|
||||
objects: nextObjects,
|
||||
pages: state.pages.map((p) =>
|
||||
p.id === state.currentPageId ? { ...p, objects: nextObjects } : p,
|
||||
),
|
||||
};
|
||||
};
|
||||
|
||||
let objectGestureSnapshot: EditorObject[] | null = null;
|
||||
|
||||
export type {
|
||||
@@ -248,7 +264,6 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
|
||||
});
|
||||
},
|
||||
beginObjectGesture: () => {
|
||||
if (objectGestureSnapshot !== null) return;
|
||||
objectGestureSnapshot = cloneObjectList(get().objects);
|
||||
},
|
||||
endObjectGesture: () => {
|
||||
@@ -336,12 +351,7 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
|
||||
if (i === -1 || i >= objs.length - 1) return;
|
||||
[objs[i], objs[i + 1]] = [objs[i + 1]!, objs[i]!];
|
||||
if (state.currentPageId) {
|
||||
set({
|
||||
objects: objs,
|
||||
pages: state.pages.map((p) =>
|
||||
p.id === state.currentPageId ? { ...p, objects: objs } : p,
|
||||
),
|
||||
});
|
||||
set(withSyncedCurrentPageObjects(state, objs));
|
||||
return;
|
||||
}
|
||||
set({ objects: objs });
|
||||
@@ -354,12 +364,7 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
|
||||
if (i <= 0) return;
|
||||
[objs[i], objs[i - 1]] = [objs[i - 1]!, objs[i]!];
|
||||
if (state.currentPageId) {
|
||||
set({
|
||||
objects: objs,
|
||||
pages: state.pages.map((p) =>
|
||||
p.id === state.currentPageId ? { ...p, objects: objs } : p,
|
||||
),
|
||||
});
|
||||
set(withSyncedCurrentPageObjects(state, objs));
|
||||
return;
|
||||
}
|
||||
set({ objects: objs });
|
||||
@@ -398,30 +403,12 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
|
||||
set({ guides });
|
||||
},
|
||||
updateObject: (id, updates) => {
|
||||
const state = get();
|
||||
if (state.currentPageId) {
|
||||
set((state) => ({
|
||||
objects: state.objects.map((obj) =>
|
||||
obj.id === id ? { ...obj, ...updates } : obj,
|
||||
),
|
||||
pages: state.pages.map((p) =>
|
||||
p.id === state.currentPageId
|
||||
? {
|
||||
...p,
|
||||
objects: p.objects.map((obj) =>
|
||||
obj.id === id ? { ...obj, ...updates } : obj,
|
||||
),
|
||||
}
|
||||
: p,
|
||||
),
|
||||
}));
|
||||
return;
|
||||
}
|
||||
set((state) => ({
|
||||
objects: state.objects.map((obj) =>
|
||||
set((state) => {
|
||||
const nextObjects = state.objects.map((obj) =>
|
||||
obj.id === id ? { ...obj, ...updates } : obj,
|
||||
),
|
||||
}));
|
||||
);
|
||||
return withSyncedCurrentPageObjects(state, nextObjects);
|
||||
});
|
||||
},
|
||||
deleteObject: (id, options) => {
|
||||
if (!options?.skipHistory) {
|
||||
@@ -535,17 +522,12 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
|
||||
get().commitObjectHistoryBeforeChange();
|
||||
const state = get();
|
||||
if (state.currentPageId) {
|
||||
set((state) => ({
|
||||
objects: reorderByIds(state.objects, draggedId, targetId),
|
||||
pages: state.pages.map((page) =>
|
||||
page.id === state.currentPageId
|
||||
? {
|
||||
...page,
|
||||
objects: reorderByIds(page.objects, draggedId, targetId),
|
||||
}
|
||||
: page,
|
||||
set((state) =>
|
||||
withSyncedCurrentPageObjects(
|
||||
state,
|
||||
reorderByIds(state.objects, draggedId, targetId),
|
||||
),
|
||||
}));
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -962,24 +944,8 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
|
||||
if (!result) return;
|
||||
|
||||
if (state.currentPageId) {
|
||||
const pageToGroup = state.pages.find(
|
||||
(p) => p.id === state.currentPageId,
|
||||
);
|
||||
if (!pageToGroup) return;
|
||||
const pageResult = buildGroupResult(
|
||||
pageToGroup.objects,
|
||||
objectIds,
|
||||
result.groupId,
|
||||
);
|
||||
if (!pageResult) return;
|
||||
|
||||
set((state) => ({
|
||||
objects: result.nextObjects,
|
||||
pages: state.pages.map((p) =>
|
||||
p.id === state.currentPageId
|
||||
? { ...p, objects: pageResult.nextObjects }
|
||||
: p,
|
||||
),
|
||||
...withSyncedCurrentPageObjects(state, result.nextObjects),
|
||||
selectedObjectId: result.groupId,
|
||||
selectedObjectIds: [result.groupId],
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user