|
1 | 1 | import { useLayoutEffect } from "react"; |
| 2 | +import { useSelector } from "react-redux"; |
| 3 | +import { Tool } from "@/components/toolbar/data"; |
2 | 4 | import { ElementType } from "@/components/workspace/elements"; |
3 | 5 | import { dataAttributes, ids, selectors } from "@/constants"; |
4 | 6 | import { default as store } from "@/store"; |
5 | 7 | import { clearAndSelectElements } from "@/store/reducers/editor"; |
6 | 8 | import { coordsWithTransform, d3Extended } from "@/utils"; |
7 | 9 |
|
8 | 10 | const useSelection = () => { |
| 11 | + const selectedTool = useSelector((state: any) => state.toolbar.selectedTool); |
9 | 12 | useLayoutEffect(() => { |
10 | 13 | const svg = d3Extended.selectById(ids.workspace); |
11 | 14 | if (svg.node()) { |
12 | | - const { top: workspaceTop, left: workspaceLeft } = d3Extended.selectionBounds( |
13 | | - d3Extended.selectById(ids.workspace) |
14 | | - ); |
15 | | - const selectionRect = { |
16 | | - element: null, |
17 | | - currentY: 0, |
18 | | - currentX: 0, |
19 | | - originX: 0, |
20 | | - originY: 0, |
21 | | - setElement: function (ele) { |
22 | | - this.element = ele; |
23 | | - }, |
24 | | - getNewAttributes: function () { |
25 | | - const x = this.currentX < this.originX ? this.currentX : this.originX; |
26 | | - const y = this.currentY < this.originY ? this.currentY : this.originY; |
27 | | - const width = Math.abs(this.currentX - this.originX); |
28 | | - const height = Math.abs(this.currentY - this.originY); |
29 | | - return { |
30 | | - x: x, |
31 | | - y: y, |
32 | | - width: width, |
33 | | - height: height |
34 | | - }; |
35 | | - }, |
36 | | - getCurrentAttributes: function () { |
37 | | - const transform = d3Extended.zoomTransform(document.querySelector(selectors.workspaceGroup)); |
38 | | - const { x, y } = coordsWithTransform({ x: +this.element.attr("x"), y: +this.element.attr("y") }, transform); |
39 | | - return { |
40 | | - x1: x, |
41 | | - y1: y, |
42 | | - x2: x + Number(this.element.attr("width")) / transform.k, |
43 | | - y2: y + Number(this.element.attr("height")) / transform.k |
44 | | - }; |
45 | | - }, |
46 | | - init: function (newX, newY) { |
47 | | - const rectElement = svg |
48 | | - .append("rect") |
49 | | - .attr("id", ids.workspaceSelection) |
50 | | - .attr("rx", 4) |
51 | | - .attr("ry", 4) |
52 | | - .attr("x", 0) |
53 | | - .attr("y", 0) |
54 | | - .attr("width", 0) |
55 | | - .attr("height", 0) |
56 | | - .classed("workspace-selection", true); |
57 | | - this.setElement(rectElement); |
58 | | - this.originX = newX; |
59 | | - this.originY = newY; |
60 | | - this.update(newX, newY); |
61 | | - }, |
62 | | - update: function (newX: number, newY: number) { |
63 | | - this.currentX = newX - (+this.element?.attr("width") || this.currentX === this.originX ? workspaceLeft : 0); |
64 | | - this.currentY = newY - (+this.element?.attr("height") || this.currentY === this.originY ? workspaceTop : 0); |
65 | | - const attributes = this.getNewAttributes(); |
66 | | - Object.keys(attributes).forEach((key) => { |
67 | | - this.element.attr(key, attributes[key]); |
68 | | - }); |
69 | | - }, |
70 | | - remove: function () { |
71 | | - this.element.remove(); |
72 | | - this.element = null; |
73 | | - } |
74 | | - }; |
| 15 | + if (selectedTool === Tool.Select) { |
| 16 | + const { top: workspaceTop, left: workspaceLeft } = d3Extended.selectionBounds( |
| 17 | + d3Extended.selectById(ids.workspace) |
| 18 | + ); |
| 19 | + const selectionRect = { |
| 20 | + element: null, |
| 21 | + currentY: 0, |
| 22 | + currentX: 0, |
| 23 | + originX: 0, |
| 24 | + originY: 0, |
| 25 | + setElement: function (ele) { |
| 26 | + this.element = ele; |
| 27 | + }, |
| 28 | + getNewAttributes: function () { |
| 29 | + const x = this.currentX < this.originX ? this.currentX : this.originX; |
| 30 | + const y = this.currentY < this.originY ? this.currentY : this.originY; |
| 31 | + const width = Math.abs(this.currentX - this.originX); |
| 32 | + const height = Math.abs(this.currentY - this.originY); |
| 33 | + return { |
| 34 | + x: x, |
| 35 | + y: y, |
| 36 | + width: width, |
| 37 | + height: height |
| 38 | + }; |
| 39 | + }, |
| 40 | + getCurrentAttributes: function () { |
| 41 | + const transform = d3Extended.zoomTransform(document.querySelector(selectors.workspaceGroup)); |
| 42 | + const { x, y } = coordsWithTransform({ x: +this.element.attr("x"), y: +this.element.attr("y") }, transform); |
| 43 | + return { |
| 44 | + x1: x, |
| 45 | + y1: y, |
| 46 | + x2: x + Number(this.element.attr("width")) / transform.k, |
| 47 | + y2: y + Number(this.element.attr("height")) / transform.k |
| 48 | + }; |
| 49 | + }, |
| 50 | + init: function (newX, newY) { |
| 51 | + const rectElement = svg |
| 52 | + .append("rect") |
| 53 | + .attr("id", ids.workspaceSelection) |
| 54 | + .attr("rx", 4) |
| 55 | + .attr("ry", 4) |
| 56 | + .attr("x", 0) |
| 57 | + .attr("y", 0) |
| 58 | + .attr("width", 0) |
| 59 | + .attr("height", 0) |
| 60 | + .classed("workspace-selection", true); |
| 61 | + this.setElement(rectElement); |
| 62 | + this.originX = newX; |
| 63 | + this.originY = newY; |
| 64 | + this.update(newX, newY); |
| 65 | + }, |
| 66 | + update: function (newX: number, newY: number) { |
| 67 | + this.currentX = newX - (+this.element?.attr("width") || this.currentX === this.originX ? workspaceLeft : 0); |
| 68 | + this.currentY = newY - (+this.element?.attr("height") || this.currentY === this.originY ? workspaceTop : 0); |
| 69 | + const attributes = this.getNewAttributes(); |
| 70 | + Object.keys(attributes).forEach((key) => { |
| 71 | + this.element.attr(key, attributes[key]); |
| 72 | + }); |
| 73 | + }, |
| 74 | + remove: function () { |
| 75 | + this.element.remove(); |
| 76 | + this.element = null; |
| 77 | + } |
| 78 | + }; |
75 | 79 |
|
76 | | - const dragStart = (e) => { |
77 | | - const p = d3Extended.pointer(e); |
78 | | - selectionRect.init(p[0], p[1]); |
79 | | - }; |
| 80 | + const dragStart = (e) => { |
| 81 | + const p = d3Extended.pointer(e); |
| 82 | + selectionRect.init(p[0], p[1]); |
| 83 | + }; |
80 | 84 |
|
81 | | - const dragMove = (e) => { |
82 | | - const p = d3Extended.pointer(e); |
83 | | - selectionRect.update(p[0], p[1]); |
84 | | - }; |
| 85 | + const dragMove = (e) => { |
| 86 | + const p = d3Extended.pointer(e); |
| 87 | + selectionRect.update(p[0], p[1]); |
| 88 | + }; |
85 | 89 |
|
86 | | - const dragEnd = () => { |
87 | | - const finalAttributes = selectionRect.getCurrentAttributes(); |
88 | | - selectionRect.remove(); |
89 | | - const elements = d3Extended.selectAll(`[${dataAttributes.element}]`); |
90 | | - const idsToSelect = []; |
| 90 | + const dragEnd = () => { |
| 91 | + const finalAttributes = selectionRect.getCurrentAttributes(); |
| 92 | + selectionRect.remove(); |
| 93 | + const elements = d3Extended.selectAll(`[${dataAttributes.element}]`); |
| 94 | + const idsToSelect = []; |
91 | 95 |
|
92 | | - elements.forEach((element) => { |
93 | | - const isSeat = element.attr(dataAttributes.elementType) === ElementType.Seat; |
94 | | - const x = isSeat ? +element.attr("cx") : +element.attr("x"); |
95 | | - const y = isSeat ? +element.attr("cy") : +element.attr("y"); |
96 | | - if ( |
97 | | - x >= finalAttributes.x1 && |
98 | | - x <= finalAttributes.x2 && |
99 | | - y >= finalAttributes.y1 && |
100 | | - y <= finalAttributes.y2 |
101 | | - ) { |
102 | | - const id = element.attr("id"); |
103 | | - if (!id?.includes("-label")) idsToSelect.push(id); |
104 | | - } |
105 | | - }); |
106 | | - store.dispatch(clearAndSelectElements(idsToSelect)); |
107 | | - }; |
108 | | - |
109 | | - svg.call(d3Extended.drag().on("drag", dragMove).on("start", dragStart).on("end", dragEnd)); |
| 96 | + elements.forEach((element) => { |
| 97 | + const isSeat = element.attr(dataAttributes.elementType) === ElementType.Seat; |
| 98 | + const x = isSeat ? +element.attr("cx") : +element.attr("x"); |
| 99 | + const y = isSeat ? +element.attr("cy") : +element.attr("y"); |
| 100 | + if ( |
| 101 | + x >= finalAttributes.x1 && |
| 102 | + x <= finalAttributes.x2 && |
| 103 | + y >= finalAttributes.y1 && |
| 104 | + y <= finalAttributes.y2 |
| 105 | + ) { |
| 106 | + const id = element.attr("id"); |
| 107 | + if (!id?.includes("-label")) idsToSelect.push(id); |
| 108 | + } |
| 109 | + }); |
| 110 | + store.dispatch(clearAndSelectElements(idsToSelect)); |
| 111 | + }; |
| 112 | + svg.call(d3Extended.drag().on("drag", dragMove).on("start", dragStart).on("end", dragEnd)); |
| 113 | + } else { |
| 114 | + svg.call(d3Extended.drag().on("drag", null).on("start", null).on("end", null)); |
| 115 | + } |
110 | 116 | } |
111 | | - }, []); |
| 117 | + }, [selectedTool]); |
112 | 118 | }; |
113 | 119 |
|
114 | 120 | export default useSelection; |
0 commit comments