Compare commits

...

4 Commits

Author SHA1 Message Date
Ayobami 34cb733ed7 Fix: issue 11: objects not snapping. and increase snap threshold 2025-07-06 22:06:31 +01:00
Ayobami 20618725d5 Fix: add download excel while downloading image 2025-07-06 21:27:02 +01:00
Ayobami d55b1cb9b7 Fix: copy and past to add support for multiple selected objects 2025-07-06 21:16:59 +01:00
Ayobami 12546d02c4 Fix: undo and redo state management 2025-07-06 21:06:47 +01:00
+189 -110
View File
@@ -155,12 +155,10 @@ export const DockBuilder = () => {
const dockDataArr = (json.objects || [])
.filter((object) => object.dockData && !object.snapClone)
.map((object) => object.dockData);
if (dockDataArr.length > 0) {
// Download as Excel
const worksheet = XLSX.utils.json_to_sheet(dockDataArr);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Dock Data");
XLSX.writeFile(workbook, `paradise_dock_data_${timestamp}.xlsx`);
downloadExcel(dockDataArr);
}
}, [editorMemo]);
@@ -414,95 +412,148 @@ export const DockBuilder = () => {
};
function edgeDetectionAndSnap(options) {
if (editorMemo.getZoom() !== 1) {
if (this.isZoomed) {
return;
}
// TODO: Edge detection and snap to object within snap range
const selectedObj = options.target;
if (!selectedObj.dockData) return;
if (options && options.target) {
const selectedObj = options.target.setCoords();
// TODO: Detect if the selected object is in the allowed categories
const snapCategories = [
DockPanelCategories.Accessories,
DockPanelCategories.BoatLift2,
DockPanelCategories.BoatLift4,
];
// TODO: Detect if the object is within the snap range of the selected object
// This is handled by the edgeDetection function which checks if objects are within snap threshold
editorMemo.forEachObject((obj) => {
if (obj === selectedObj) return;
if (!snapCategories.includes(selectedObj.dockData.itemName)) return;
const detectedObjBound = obj.getBoundingRect();
const selectedObjBound = selectedObj.getBoundingRect();
let closestSnapPoint = null;
let minDistance = edgeSnapThreshold;
// Only process image objects that are not snap clones
if (!obj.snapClone && obj.type === "image") {
// TODO: handle rotation of 0, 90, 180, 270 degrees
editorMemo.forEachObject((obj) => {
if (obj === selectedObj || !obj.dockData) return;
// Handle TOP edge snapping
if (edgeDetection(selectedObj, obj, "top")) {
let newTop = selectedObjBound.top;
// TODO: Only consider objects in the allowed categories
if (!snapCategories.includes(obj.dockData.itemName)) return;
// Handle different rotation combinations for top snapping
if ([0, nintyDeg].includes(obj.angle)) {
if ([oneEightyDeg, twoSeventyDeg].includes(selectedObj.angle)) {
newTop = obj.oCoords.tl.y;
} else {
newTop = obj.oCoords.tl.y - selectedObjBound.height;
}
} else {
if ([oneEightyDeg, twoSeventyDeg].includes(selectedObj.angle)) {
newTop = obj.oCoords.tl.y - detectedObjBound.height;
} else {
newTop =
obj.oCoords.tl.y -
(selectedObjBound.height + detectedObjBound.height);
}
}
// TODO: handle rotation of 0, 90, 180, 270 degrees
const rotation = obj.angle % 360;
const isRotated = rotation !== 0 && rotation !== 180;
// Apply the new position
selectedObj.setPositionByOrigin(
{ x: selectedObj.oCoords.tl.x, y: newTop },
"left",
"top"
);
selectedObj.setCoords();
editorMemo.renderAll();
}
// Handle BOTTOM edge snapping
else if (edgeDetection(selectedObj, obj, "bottom")) {
let newTop = selectedObjBound.top;
const objCenter = obj.getCenterPoint();
const selectedCenter = selectedObj.getCenterPoint();
// Handle different rotation combinations for bottom snapping
if ([0, nintyDeg].includes(obj.angle)) {
if ([oneEightyDeg, twoSeventyDeg].includes(selectedObj.angle)) {
newTop =
obj.oCoords.tl.y +
(detectedObjBound.height + selectedObjBound.height);
} else {
newTop = obj.oCoords.tl.y + detectedObjBound.height;
}
} else {
if ([oneEightyDeg, twoSeventyDeg].includes(selectedObj.angle)) {
newTop = obj.oCoords.tl.y + selectedObjBound.height;
} else {
newTop = obj.oCoords.tl.y;
}
}
// Calculate potential snap points
const snapPoints = [];
// Apply the new position
selectedObj.setPositionByOrigin(
{ x: selectedObj.oCoords.tl.x, y: newTop },
"left",
"top"
);
selectedObj.setCoords();
editorMemo.renderAll();
}
// Handle LEFT edge snapping
else if (edgeDetection(selectedObj, obj, "left")) {
let newLeft = selectedObjBound.left;
// Center point
snapPoints.push(objCenter);
// Handle different rotation combinations for left snapping
if ([0, twoSeventyDeg].includes(obj.angle)) {
if ([oneEightyDeg, nintyDeg].includes(selectedObj.angle)) {
newLeft = obj.oCoords.tl.x;
} else {
newLeft = obj.oCoords.tl.x - selectedObjBound.width;
}
} else {
if ([oneEightyDeg, nintyDeg].includes(selectedObj.angle)) {
newLeft = obj.oCoords.tl.x - detectedObjBound.width;
} else {
newLeft =
obj.oCoords.tl.x -
(selectedObjBound.width + detectedObjBound.width);
}
}
// Edge points
if (!isRotated || rotation === 0 || rotation === 180) {
// Horizontal edges
snapPoints.push(
new fabric.Point(objCenter.x, obj.getBoundingRect().top)
);
snapPoints.push(
new fabric.Point(
objCenter.x,
obj.getBoundingRect().top + obj.getBoundingRect().height
)
);
}
// Apply the new position
selectedObj.setPositionByOrigin(
{ x: newLeft, y: selectedObj.oCoords.tl.y },
"left",
"top"
);
selectedObj.setCoords();
editorMemo.renderAll();
}
// Handle RIGHT edge snapping
else if (edgeDetection(selectedObj, obj, "right")) {
let newLeft = selectedObjBound.left;
if (!isRotated || rotation === 90 || rotation === 270) {
// Vertical edges
snapPoints.push(
new fabric.Point(obj.getBoundingRect().left, objCenter.y)
);
snapPoints.push(
new fabric.Point(
obj.getBoundingRect().left + obj.getBoundingRect().width,
objCenter.y
)
);
}
// Handle different rotation combinations for right snapping
if ([0, twoSeventyDeg].includes(obj.angle)) {
if ([oneEightyDeg, nintyDeg].includes(selectedObj.angle)) {
newLeft =
obj.oCoords.tl.x +
(detectedObjBound.width + selectedObjBound.width);
} else {
newLeft = obj.oCoords.tl.x + detectedObjBound.width;
}
} else {
if ([oneEightyDeg, nintyDeg].includes(selectedObj.angle)) {
newLeft = obj.oCoords.tl.x + selectedObjBound.width;
} else {
newLeft = obj.oCoords.tl.x;
}
}
// Find closest snap point
snapPoints.forEach((point) => {
const distance = Math.sqrt(
Math.pow(selectedCenter.x - point.x, 2) +
Math.pow(selectedCenter.y - point.y, 2)
);
if (distance < minDistance) {
minDistance = distance;
closestSnapPoint = point;
// Apply the new position
selectedObj.setPositionByOrigin(
{ x: newLeft, y: selectedObj.oCoords.tl.y },
"left",
"top"
);
selectedObj.setCoords();
editorMemo.renderAll();
}
}
});
});
// Snap to closest point if found
if (closestSnapPoint) {
const selectedCenter = selectedObj.getCenterPoint();
selectedObj.set({
left: selectedObj.left + (closestSnapPoint.x - selectedCenter.x),
top: selectedObj.top + (closestSnapPoint.y - selectedCenter.y),
});
selectedObj.setCoords();
editorMemo.renderAll();
}
editorMemo.defaultCursor = "default";
@@ -609,26 +660,45 @@ export const DockBuilder = () => {
const onUndoClick = useCallback(() => {
const undoResult = stack.undo();
if (undoResult && undoResult.currentState) {
editorMemo.loadFromJSON(undoResult.currentState, () => {
editorMemo.renderAll();
if (undoResult.currentCount === -1) {
return 0;
}
if (undoResult.currentCount === 0) {
editorMemo.getObjects().forEach((obj) => {
if (obj !== editorMemo.backgroundImage) {
if (obj.type == "image") {
editorMemo.remove(obj).renderAll();
}
}
const data = editorMemo.toJSON(["dockData"]);
localStorage.setItem("dock", JSON.stringify(data));
});
} else if (undoResult.currentCount > 0) {
const json = JSON.parse(undoResult.currentState);
editorMemo.loadFromJSON(
json,
() => {
editorMemo.renderAll();
},
(obj) => {}
);
}
}, [editorMemo]);
const onRedoClick = useCallback(() => {
const redoResult = stack.redo();
if (redoResult && redoResult.currentState) {
// currentState is an array, get the first element
const state = Array.isArray(redoResult.currentState)
? redoResult.currentState[0]
: redoResult.currentState;
if (state) {
editorMemo.loadFromJSON(state, () => {
editorMemo.renderAll();
});
}
if (!redoResult.currentState) {
return 0;
}
localStorage.setItem("dock", redoResult.currentState);
editorMemo.loadFromJSON(
JSON.parse(redoResult.currentState),
() => {
editorMemo.renderAll();
},
(o) => {}
);
}, [editorMemo]);
const onPrintScreen = useCallback(() => {
@@ -680,33 +750,42 @@ export const DockBuilder = () => {
const CopySelection = () => {
const activeObject = editorMemo.getActiveObject();
if (activeObject) {
activeObject.clone(function (cloned) {
activeObject.clone(
function (cloned) {
clipboard = cloned;
},
// Ensure dockData is copied
if (activeObject.dockData) {
cloned.dockData = { ...activeObject.dockData };
}
clipboard = cloned;
});
["dockData"]
);
}
};
const PasteSelection = () => {
if (clipboard) {
clipboard.clone(function (clonedObj) {
clipboard.clone(
function (clonedObj) {
// Offset the pasted object so it's visible
clonedObj.set({
left: clonedObj.left + 20,
top: clonedObj.top + 20,
evented: true,
});
// Add support for multiple selected objects
if (clonedObj.type === "activeSelection") {
clonedObj.canvas = editorMemo;
clonedObj.forEachObject(function (obj) {
editorMemo.add(obj);
});
clonedObj.setCoords();
} else {
editorMemo.add(clonedObj);
}
editorMemo.setActiveObject(clonedObj);
editorMemo.requestRenderAll();
},
// Ensure dockData is copied
if (clipboard.dockData) {
clonedObj.dockData = { ...clipboard.dockData };
}
// Offset the pasted object so it's visible
clonedObj.set({
left: (clonedObj.left || 0) + 20,
top: (clonedObj.top || 0) + 20,
evented: true,
});
editorMemo.add(clonedObj);
editorMemo.setActiveObject(clonedObj);
editorMemo.requestRenderAll();
});
["dockData"]
);
}
};
@@ -806,13 +885,13 @@ export const DockBuilder = () => {
fabric.devicePixelRatio = 1;
fabric.Group.prototype.hasControls = true;
fabric.Group.prototype.snapAngle = 45;
fabric.Group.prototype.snapThreshold = 5;
fabric.Group.prototype.snapThreshold = 10;
fabric.Group.prototype.stroke = "#0f75bc";
fabric.Object.prototype.snapAngle = 45;
// Optimize object rendering
fabric.Object.prototype.objectCaching = true;
fabric.Object.prototype.snapThreshold = 5;
fabric.Object.prototype.snapThreshold = 10;
fabric.Object.prototype.setControlsVisibility({
tl: false, //top-left
mt: false, // middle-top