forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseCanvasEvents.ts
More file actions
244 lines (227 loc) · 8.35 KB
/
useCanvasEvents.ts
File metadata and controls
244 lines (227 loc) · 8.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import type Block from "@/block";
import useBuilderStore from "@/stores/builderStore";
import useCanvasStore from "@/stores/canvasStore";
import { CanvasHistory } from "@/types/Builder/BuilderCanvas";
import getBlockTemplate from "@/utils/blockTemplate";
import {
addPxToNumber,
getBlock,
getBlockInfo,
getNumberFromPx,
isBlock,
isTargetEditable,
} from "@/utils/helpers";
import { clamp, useEventListener } from "@vueuse/core";
import { Ref } from "vue";
const builderStore = useBuilderStore();
const canvasStore = useCanvasStore();
export function useCanvasEvents(
container: Ref<HTMLElement>,
canvasProps: CanvasProps,
canvasHistory: CanvasHistory,
selectedBlocks: Ref<Block[]>,
getRootBlock: () => Block,
findBlock: (blockId: string) => Block | null,
) {
let counter = 0;
useEventListener(container, "mousedown", (ev: MouseEvent) => {
if (builderStore.mode === "move") {
return;
}
const initialX = ev.clientX;
const initialY = ev.clientY;
if (builderStore.mode === "select") {
return;
} else {
const pauseId = canvasHistory.value?.pause();
ev.stopPropagation();
let element = document.elementFromPoint(ev.x, ev.y) as HTMLElement;
let block = getRootBlock();
if (element) {
if (element.dataset.blockId) {
block = findBlock(element.dataset.blockId) || block;
}
}
let parentBlock = getRootBlock();
if (element.dataset.blockId) {
parentBlock = findBlock(element.dataset.blockId) || parentBlock;
while (parentBlock && !parentBlock.canHaveChildren()) {
parentBlock = parentBlock.getParentBlock() || getRootBlock();
}
}
const child = getBlockTemplate(builderStore.mode);
const parentElement = document.body.querySelector(
`.canvas [data-block-id="${parentBlock.blockId}"]`,
) as HTMLElement;
const parentOldPosition = parentBlock.getStyle("position");
if (parentOldPosition === "static" || parentOldPosition === "inherit" || !parentOldPosition) {
parentBlock.setBaseStyle("position", "relative");
}
const parentElementBounds = parentElement.getBoundingClientRect();
let x = (ev.x - parentElementBounds.left) / canvasProps.scale;
let y = (ev.y - parentElementBounds.top) / canvasProps.scale;
const parentWidth = getNumberFromPx(getComputedStyle(parentElement).width);
const parentHeight = getNumberFromPx(getComputedStyle(parentElement).height);
const childBlock = parentBlock.addChild(child);
childBlock.setBaseStyle("position", "absolute");
childBlock.setBaseStyle("top", addPxToNumber(y));
childBlock.setBaseStyle("left", addPxToNumber(x));
if (builderStore.mode === "container" || builderStore.mode === "repeater") {
const colors = ["#ededed", "#e2e2e2", "#c7c7c7"];
childBlock.setBaseStyle("backgroundColor", colors[counter % colors.length]);
counter++;
}
const mouseMoveHandler = (mouseMoveEvent: MouseEvent) => {
if (builderStore.mode === "text") {
return;
} else {
mouseMoveEvent.preventDefault();
let width = (mouseMoveEvent.clientX - initialX) / canvasProps.scale;
let height = (mouseMoveEvent.clientY - initialY) / canvasProps.scale;
width = clamp(width, 0, parentWidth);
height = clamp(height, 0, parentHeight);
const setFullWidth = width === parentWidth;
childBlock.setBaseStyle("width", setFullWidth ? "100%" : addPxToNumber(width));
childBlock.setBaseStyle("height", addPxToNumber(height));
}
};
useEventListener(document, "mousemove", mouseMoveHandler);
useEventListener(
document,
"mouseup",
() => {
document.removeEventListener("mousemove", mouseMoveHandler);
parentBlock.setBaseStyle("position", parentOldPosition || "static");
childBlock.setBaseStyle("position", "static");
childBlock.setBaseStyle("top", "auto");
childBlock.setBaseStyle("left", "auto");
setTimeout(() => {
builderStore.mode = "select";
}, 50);
if (builderStore.mode === "text") {
pauseId && canvasHistory.value?.resume(pauseId, true);
canvasStore.editableBlock = childBlock;
return;
}
if (parentBlock.isGrid()) {
childBlock.setStyle("width", "auto");
childBlock.setStyle("height", "100%");
} else {
if (getNumberFromPx(childBlock.getStyle("width")) < 100) {
childBlock.setBaseStyle("width", "100%");
}
if (getNumberFromPx(childBlock.getStyle("height")) < 100) {
childBlock.setBaseStyle("height", "200px");
}
}
pauseId && canvasHistory.value?.resume(pauseId, true);
},
{ once: true },
);
}
});
useEventListener(container, "mousedown", (ev: MouseEvent) => {
if (builderStore.mode === "move") {
container.value.style.cursor = "grabbing";
const initialX = ev.clientX;
const initialY = ev.clientY;
const initialTranslateX = canvasProps.translateX;
const initialTranslateY = canvasProps.translateY;
const mouseMoveHandler = (mouseMoveEvent: MouseEvent) => {
mouseMoveEvent.preventDefault();
const diffX = (mouseMoveEvent.clientX - initialX) / canvasProps.scale;
const diffY = (mouseMoveEvent.clientY - initialY) / canvasProps.scale;
canvasProps.translateX = initialTranslateX + diffX;
canvasProps.translateY = initialTranslateY + diffY;
};
useEventListener(document, "mousemove", mouseMoveHandler);
useEventListener(
document,
"mouseup",
() => {
document.removeEventListener("mousemove", mouseMoveHandler);
container.value.style.cursor = "grab";
},
{ once: true },
);
ev.stopPropagation();
ev.preventDefault();
}
});
useEventListener(document, "keydown", (ev: KeyboardEvent) => {
// make sure reference container is not hidden or not editable
if (!container.value.offsetParent || isTargetEditable(ev) || selectedBlocks.value.length !== 1) {
return;
}
const selectedBlock = selectedBlocks.value[0];
const selectBlock = (block: Block | null) => {
// TODO: Use canvas's selectBlock instead of canvasStore's to avoid mixup with other canvas
if (block) canvasStore.selectBlock(block, null, true, true);
return !!block;
};
const selectSibling = (direction: "previous" | "next", fallback: () => void) => {
selectBlock(selectedBlock.getSiblingBlock(direction)) || fallback();
};
const selectParent = () => selectBlock(selectedBlock.getParentBlock());
const selectFirstChild = () => selectBlock(selectedBlock.children[0]);
const selectNextSiblingOrParent = () => {
let sibling = selectedBlock.getSiblingBlock("next");
let parentBlock = selectedBlock.getParentBlock();
while (!sibling && parentBlock) {
sibling = parentBlock.getSiblingBlock("next");
parentBlock = parentBlock.getParentBlock();
}
selectBlock(sibling);
};
const selectLastChildInTree = (block: Block) => {
let currentBlock = block;
while (builderStore.activeLayers?.isExpandedInTree(currentBlock)) {
const lastChild = currentBlock.getLastChild() as Block;
if (!lastChild) break;
currentBlock = lastChild;
}
selectBlock(currentBlock);
};
const arrowKeyHandlers = {
ArrowLeft: () => {
builderStore.activeLayers?.isExpandedInTree(selectedBlock)
? builderStore.activeLayers.toggleExpanded(selectedBlock)
: selectSibling("previous", selectParent);
},
ArrowRight: () => {
selectedBlock.hasChildren() && selectedBlock.isVisible()
? (builderStore.activeLayers?.toggleExpanded(selectedBlock), selectFirstChild())
: selectNextSiblingOrParent();
},
ArrowUp: () => {
const previousSibling = selectedBlock.getSiblingBlock("previous");
previousSibling ? selectLastChildInTree(previousSibling) : selectParent();
},
ArrowDown: () => {
builderStore.activeLayers?.isExpandedInTree(selectedBlock) &&
selectedBlock.hasChildren() &&
selectedBlock.isVisible()
? selectFirstChild()
: selectNextSiblingOrParent();
},
};
const handler = arrowKeyHandlers[ev.key as keyof typeof arrowKeyHandlers];
if (handler) {
handler();
ev.preventDefault();
}
});
useEventListener(container, "mouseover", handleMouseOver);
}
function handleMouseOver(e: MouseEvent) {
if (!isBlock(e)) {
canvasStore.activeCanvas?.setHoveredBlock(null);
return;
}
if (builderStore.mode === "move" || canvasStore.activeCanvas?.resizingBlock) return;
const block = getBlock(e);
const { breakpoint } = getBlockInfo(e);
canvasStore.activeCanvas?.setHoveredBlock(block?.blockId || null);
canvasStore.activeCanvas?.setHoveredBreakpoint(breakpoint);
e.stopPropagation();
}