clear code + split to multi components

This commit is contained in:
hamid zarghami
2026-01-07 15:59:16 +03:30
parent 7a2bdfba56
commit 1b710555bc
8 changed files with 1076 additions and 774 deletions
@@ -0,0 +1,63 @@
import { useEditorStore, type EditorObject } from "../../store/editorStore";
export const useObjectHandlers = () => {
const { objects, updateObject, groupObjects, ungroupObjects, updateCellValue, setEditingCell, editingCell } =
useEditorStore();
const handleObjectUpdate = (id: string, updates: Partial<EditorObject>) => {
const obj = objects.find((o) => o.id === id);
if (!obj) return;
// اگر group object است و position تغییر کرد، همه objectهای داخل group را جابجا کن
if (obj.type === "group" && (updates.x !== undefined || updates.y !== undefined)) {
const groupMembers = objects.filter((o) => o.groupId === id);
const deltaX = updates.x !== undefined ? updates.x - (obj.x || 0) : 0;
const deltaY = updates.y !== undefined ? updates.y - (obj.y || 0) : 0;
// به‌روزرسانی group object
updateObject(id, updates);
// به‌روزرسانی همه اعضای گروه
groupMembers.forEach((member) => {
updateObject(member.id, {
x: (member.x || 0) + deltaX,
y: (member.y || 0) + deltaY,
});
});
} else if (obj.groupId && (updates.x !== undefined || updates.y !== undefined)) {
// اگر object در یک گروه است و position تغییر کرد، فقط object را به‌روزرسانی کن
// bounds group فقط موقع transform به‌روزرسانی می‌شود
updateObject(id, updates);
} else {
updateObject(id, updates);
}
};
const handleGroupWithMask = (objectId: string, maskId: string) => {
groupObjects([objectId, maskId]);
};
const handleUngroupFromMask = (groupId: string) => {
ungroupObjects(groupId);
};
const handleCellEditorSave = (text: string) => {
if (editingCell) {
updateCellValue(editingCell.tableId, editingCell.cellId, text);
setEditingCell(null);
}
};
const handleCellEditorCancel = () => {
setEditingCell(null);
};
return {
handleObjectUpdate,
handleGroupWithMask,
handleUngroupFromMask,
handleCellEditorSave,
handleCellEditorCancel,
};
};