clear code + split to multi components
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import Konva from "konva";
|
||||
import { useEditorStore } from "../../store/editorStore";
|
||||
|
||||
export const useSelectionHandlers = () => {
|
||||
const {
|
||||
objects,
|
||||
selectedObjectIds,
|
||||
setSelectedObjectId,
|
||||
addToSelection,
|
||||
removeFromSelection,
|
||||
setSelectedTableId,
|
||||
setSelectedCellId,
|
||||
} = useEditorStore();
|
||||
|
||||
const handleSelect = (id: string, _node: Konva.Node, e?: Konva.KonvaEventObject<MouseEvent>) => {
|
||||
const obj = objects.find((o) => o.id === id);
|
||||
const isMultiSelect = e?.evt?.metaKey || e?.evt?.ctrlKey || e?.evt?.shiftKey;
|
||||
|
||||
// اگر object در یک group است، group را انتخاب کن نه object
|
||||
if (obj?.groupId && obj.type !== "group") {
|
||||
const groupObject = objects.find((o) => o.id === obj.groupId);
|
||||
if (groupObject) {
|
||||
if (isMultiSelect) {
|
||||
if (selectedObjectIds.includes(groupObject.id)) {
|
||||
removeFromSelection(groupObject.id);
|
||||
} else {
|
||||
addToSelection(groupObject.id);
|
||||
}
|
||||
} else {
|
||||
setSelectedObjectId(groupObject.id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (isMultiSelect) {
|
||||
// انتخاب چندتایی
|
||||
if (selectedObjectIds.includes(id)) {
|
||||
// اگر قبلاً انتخاب شده بود، از انتخاب خارج کن
|
||||
removeFromSelection(id);
|
||||
} else {
|
||||
// اگر انتخاب نشده بود، به انتخاب اضافه کن
|
||||
addToSelection(id);
|
||||
}
|
||||
} else {
|
||||
// انتخاب تکتایی
|
||||
setSelectedObjectId(id);
|
||||
}
|
||||
|
||||
if (obj?.type === "grid") {
|
||||
setSelectedTableId(id);
|
||||
} else if (!isMultiSelect) {
|
||||
setSelectedTableId(null);
|
||||
}
|
||||
|
||||
if (!isMultiSelect) {
|
||||
setSelectedCellId(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleCellClick = (tableId: string, cellId: string, e?: Konva.KonvaEventObject<MouseEvent>) => {
|
||||
const isMultiSelect = e?.evt?.metaKey || e?.evt?.ctrlKey || e?.evt?.shiftKey;
|
||||
|
||||
if (isMultiSelect) {
|
||||
// انتخاب چندتایی
|
||||
if (selectedObjectIds.includes(tableId)) {
|
||||
removeFromSelection(tableId);
|
||||
} else {
|
||||
addToSelection(tableId);
|
||||
}
|
||||
} else {
|
||||
setSelectedTableId(tableId);
|
||||
setSelectedObjectId(tableId);
|
||||
}
|
||||
|
||||
setSelectedCellId(cellId);
|
||||
};
|
||||
|
||||
const handleCellDblClick = (
|
||||
tableId: string,
|
||||
cellId: string,
|
||||
x: number,
|
||||
y: number,
|
||||
width: number,
|
||||
height: number,
|
||||
text: string
|
||||
) => {
|
||||
const { setEditingCell } = useEditorStore.getState();
|
||||
setEditingCell({ tableId, cellId, x, y, width, height, value: text });
|
||||
};
|
||||
|
||||
return {
|
||||
handleSelect,
|
||||
handleCellClick,
|
||||
handleCellDblClick,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user