Skip to content

Commit 3f5f69f

Browse files
Merge pull request #3 from saschamuellercerpro/remove-item-management
remove item management
2 parents 3e81283 + 9c8ed8e commit 3f5f69f

File tree

1 file changed

+3
-99
lines changed

1 file changed

+3
-99
lines changed

src/components/Board/Board.tsx

Lines changed: 3 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -767,107 +767,11 @@ const Board = React.forwardRef<BoardActions, BoardProps>(
767767

768768
const loadItems = useCallback(
769769
(canvas: Canvas) => {
770-
const prevItems = canvas.getObjects();
771-
// Find items to remove
772-
// debugger;
773-
const itemsToRemove = prevItems.filter(
774-
(prevItem) =>
775-
!items.some((item) => item.id === prevItem?.name) &&
776-
!items.some((item) => prevItem?.name?.includes("_" + item.id)),
777-
);
778-
779-
const itemsToAdd = _.cloneDeep(
780-
items.filter(
781-
(item) => !prevItems.some((prevItem) => item.id === prevItem?.name),
782-
),
783-
);
784-
785-
// Find items to change (only border colors supported)
786-
items.forEach((item) => {
787-
if (itemsToAdd.includes(item)) {
788-
return;
789-
}
790-
791-
const canvasItem = canvas
792-
.getObjects()
793-
.find((obj) => obj?.name === item.id) as fabric.Object;
794-
if (!canvasItem) return;
795-
const canvasItemFlagNote = canvas
796-
.getObjects()
797-
.find((obj) => obj?.name?.includes("_" + item.id)) as fabric.Object;
798-
const scaledCoords = item.coords.map((p) =>
799-
fabricUtils.toScaledCoord({
800-
cInfo: { width: canvas.getWidth(), height: canvas.getHeight() },
801-
iInfo: { width: imageSize.width, height: imageSize.height },
802-
coord: p,
803-
scaleRatio,
804-
}),
805-
);
806-
if (scaledCoords.length < 4) return;
807-
808-
const [p1, p2, p3, p4] = scaledCoords;
809-
const width = Math.sqrt(
810-
Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2),
811-
);
812-
const height = Math.sqrt(
813-
Math.pow(p4.x - p1.x, 2) + Math.pow(p4.y - p1.y, 2),
814-
);
815-
const centerX = (p1.x + p2.x + p3.x + p4.x) / 4;
816-
const centerY = (p1.y + p2.y + p3.y + p4.y) / 4;
817-
const angle = Math.atan2(p2.y - p1.y, p2.x - p1.x) * (180 / Math.PI);
818-
const fabricObject = new fabric.Rect({
819-
left: centerX,
820-
top: centerY,
821-
width: width,
822-
height: height,
823-
fill: item.fillColor,
824-
stroke: item.borderColor,
825-
strokeWidth: 1,
826-
selectable: item.selectable ?? true,
827-
hoverCursor: item.hoverCursor,
828-
moveCursor: item.moveCursor,
829-
name: item.id,
830-
angle: angle,
831-
originX: "center",
832-
originY: "center",
833-
});
834-
835-
if (
836-
// numberFlagPosition
837-
!canvasItemFlagNote?.name?.includes(
838-
"_position" + item.numberFlagPosition,
839-
) ||
840-
// numberFlag
841-
!canvasItemFlagNote?.name?.endsWith("_" + item.numberFlag) ||
842-
// Coords
843-
fabricObject.getCoords() !== canvasItem.getCoords() ||
844-
// Numberflagsize
845-
!canvasItemFlagNote?.name?.includes(
846-
"_size" + item.numberFlagSize,
847-
) ||
848-
// Bordercolor
849-
canvasItem.stroke !== item.borderColor ||
850-
// Fillcolor
851-
canvasItem.fill !== item.fillColor
852-
) {
853-
itemsToRemove.push(canvasItem);
854-
if (canvasItemFlagNote) {
855-
itemsToRemove.push(canvasItemFlagNote);
856-
}
857-
itemsToAdd.push(item);
858-
}
770+
canvas.getObjects().forEach(function (object) {
771+
canvas.remove(object);
859772
});
860773

861-
// Remove items
862-
itemsToRemove.forEach((item) => {
863-
const objCorner = canvas
864-
.getObjects()
865-
.find((obj) => obj?.name?.endsWith("_" + item.name));
866-
canvas.remove(item);
867-
objCorner && canvas.remove(objCorner);
868-
});
869-
870-
itemsToAdd.forEach((item) => {
774+
items.forEach((item) => {
871775
const scaledCoords = item.coords.map((p) =>
872776
fabricUtils.toScaledCoord({
873777
cInfo: { width: canvas.getWidth(), height: canvas.getHeight() },

0 commit comments

Comments
 (0)