forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxResizer.vue
More file actions
237 lines (216 loc) · 8.79 KB
/
BoxResizer.vue
File metadata and controls
237 lines (216 loc) · 8.79 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
<template>
<span
class="resize-dimensions absolute bottom-[-40px] right-[-40px] flex h-8 w-20 items-center justify-center whitespace-nowrap rounded-full bg-gray-600 p-2 text-sm text-white opacity-80"
v-if="resizing && !props.targetBlock.isText()">
{{ targetWidth }} x
{{ targetHeight }}
</span>
<span
class="resize-dimensions absolute bottom-[-40px] right-[-40px] flex h-8 w-fit items-center justify-center whitespace-nowrap rounded-full bg-gray-600 p-2 text-sm text-white opacity-80"
v-if="resizing && props.targetBlock.isText()">
{{ fontSize }}
</span>
<div
class="left-handle ew-resize pointer-events-auto absolute bottom-0 left-[-2px] top-0 w-2 border-none bg-transparent" />
<div
class="right-handle pointer-events-auto absolute bottom-0 right-[-2px] top-0 w-2 border-none bg-transparent"
:class="{ 'cursor-ew-resize': true }"
@mousedown.stop="handleRightResize" />
<div
class="top-handle ns-resize pointer-events-auto absolute left-0 right-0 top-[-2px] h-2 border-none bg-transparent" />
<div
class="bottom-handle pointer-events-auto absolute bottom-[-2px] left-0 right-0 h-2 border-none bg-transparent"
:class="{ 'cursor-ns-resize': true }"
@mousedown.stop="handleBottomResize" />
<div
class="pointer-events-auto absolute bottom-[-5px] right-[-5px] h-[12px] w-[12px] cursor-nwse-resize rounded-full border-[2.5px] border-blue-400 bg-white"
:class="{
'border-purple-400': targetBlock.isExtendedFromComponent(),
}"
v-show="!resizing"
@mousedown.stop.prevent="handleBottomCornerResize" />
</template>
<script setup lang="ts">
import type Block from "@/block";
import useCanvasStore from "@/stores/canvasStore";
import { getNumberFromPx } from "@/utils/helpers";
import { clamp } from "@vueuse/core";
import { computed, inject, onMounted, ref, watch } from "vue";
import guidesTracker from "../utils/guidesTracker";
const canvasStore = useCanvasStore();
const props = defineProps<{
targetBlock: Block;
target: HTMLElement | SVGElement;
}>();
const emit = defineEmits(["resizing"]);
const resizing = ref(false);
let guides = null as unknown as ReturnType<typeof guidesTracker>;
const canvasProps = inject("canvasProps") as CanvasProps;
onMounted(() => {
guides = guidesTracker(props.target as HTMLElement, canvasProps);
});
watch(resizing, () => {
if (resizing.value) {
if (canvasStore.activeCanvas) {
canvasStore.activeCanvas.history?.pause();
canvasStore.activeCanvas.resizingBlock = true;
}
emit("resizing", true);
} else {
if (canvasStore.activeCanvas) {
canvasStore.activeCanvas?.history?.resume(undefined, true, true);
canvasStore.activeCanvas.resizingBlock = false;
}
emit("resizing", false);
}
});
const targetWidth = computed(() => {
props.targetBlock.getStyle("width"); // to trigger reactivity
return Math.round(getNumberFromPx(getComputedStyle(props.target).getPropertyValue("width")));
});
const targetHeight = computed(() => {
props.targetBlock.getStyle("height"); // to trigger reactivity
return Math.round(getNumberFromPx(getComputedStyle(props.target).getPropertyValue("height")));
});
const fontSize = computed(() => {
props.targetBlock.getStyle("fontSize"); // to trigger reactivity
return Math.round(getNumberFromPx(getComputedStyle(props.target).getPropertyValue("font-size")));
});
const handleRightResize = (ev: MouseEvent) => {
const startX = ev.clientX;
const startHeight = (props.target as HTMLElement).offsetHeight;
const startWidth = (props.target as HTMLElement).offsetWidth;
const blockStartWidth = props.targetBlock.getStyle("width") as string;
const blockStartHeight = props.targetBlock.getStyle("height") as string;
const startFontSize = fontSize.value || 0;
// to disable cursor jitter
const docCursor = document.body.style.cursor;
document.body.style.cursor = window.getComputedStyle(ev.target as HTMLElement).cursor;
resizing.value = true;
guides.showX();
const mousemove = (mouseMoveEvent: MouseEvent) => {
const movement = (mouseMoveEvent.clientX - startX) / canvasProps.scale;
if (props.targetBlock.isText() && !props.targetBlock.hasChildren()) {
setFontSize(movement, startFontSize);
return mouseMoveEvent.preventDefault();
}
setWidth(movement, startWidth, blockStartWidth);
if (mouseMoveEvent.shiftKey) {
setHeight(movement, startHeight, blockStartHeight);
}
mouseMoveEvent.preventDefault();
};
document.addEventListener("mousemove", mousemove);
document.addEventListener(
"mouseup",
(mouseUpEvent) => {
document.body.style.cursor = docCursor;
document.removeEventListener("mousemove", mousemove);
mouseUpEvent.preventDefault();
resizing.value = false;
guides.hideX();
},
{ once: true },
);
};
const handleBottomResize = (ev: MouseEvent) => {
const startY = ev.clientY;
const startHeight = (props.target as HTMLElement).offsetHeight;
const startWidth = (props.target as HTMLElement).offsetWidth;
const blockStartWidth = props.targetBlock.getStyle("width") as string;
const blockStartHeight = props.targetBlock.getStyle("height") as string;
const startFontSize = fontSize.value || 0;
// to disable cursor jitter
const docCursor = document.body.style.cursor;
document.body.style.cursor = window.getComputedStyle(ev.target as HTMLElement).cursor;
resizing.value = true;
guides.showY();
const mousemove = (mouseMoveEvent: MouseEvent) => {
const movement = (mouseMoveEvent.clientY - startY) / canvasProps.scale;
if (props.targetBlock.isText() && !props.targetBlock.hasChildren()) {
setFontSize(movement, startFontSize);
return mouseMoveEvent.preventDefault();
}
setHeight(movement, startHeight, blockStartHeight);
if (mouseMoveEvent.shiftKey) {
setWidth(movement, startWidth, blockStartWidth);
}
mouseMoveEvent.preventDefault();
};
document.addEventListener("mousemove", mousemove);
document.addEventListener(
"mouseup",
(mouseUpEvent) => {
document.body.style.cursor = docCursor;
document.removeEventListener("mousemove", mousemove);
mouseUpEvent.preventDefault();
resizing.value = false;
guides.hideY();
},
{ once: true },
);
};
const handleBottomCornerResize = (ev: MouseEvent) => {
const startX = ev.clientX;
const startY = ev.clientY;
const startHeight = (props.target as HTMLElement).offsetHeight;
const startWidth = (props.target as HTMLElement).offsetWidth;
const blockStartWidth = props.targetBlock.getStyle("width") as string;
const blockStartHeight = props.targetBlock.getStyle("height") as string;
const startFontSize = fontSize.value || 0;
// to disable cursor jitter
const docCursor = document.body.style.cursor;
document.body.style.cursor = window.getComputedStyle(ev.target as HTMLElement).cursor;
resizing.value = true;
const mousemove = (mouseMoveEvent: MouseEvent) => {
const movementX = (mouseMoveEvent.clientX - startX) / canvasProps.scale;
const movementY = (mouseMoveEvent.clientY - startY) / canvasProps.scale;
if (props.targetBlock.isText() && !props.targetBlock.hasChildren()) {
setFontSize(movementY, startFontSize);
return mouseMoveEvent.preventDefault();
}
setWidth(movementX, startWidth, blockStartWidth);
setHeight(mouseMoveEvent.shiftKey ? movementX : movementY, startHeight, blockStartHeight);
mouseMoveEvent.preventDefault();
};
document.addEventListener("mousemove", mousemove);
document.addEventListener(
"mouseup",
(mouseUpEvent) => {
document.body.style.cursor = docCursor;
document.removeEventListener("mousemove", mousemove);
mouseUpEvent.preventDefault();
resizing.value = false;
},
{ once: true },
);
};
const setWidth = (movementX: number, startWidth: number, blockStartWidth: string) => {
const finalWidth = Math.round(startWidth + movementX);
if (blockStartWidth?.includes("%")) {
const parentWidth = props.target.parentElement?.offsetWidth || 0;
const movementPercent = (movementX / parentWidth) * 100;
const startWidthPercent = (startWidth / parentWidth) * 100;
const finalWidthPercent = Math.abs(Math.round(startWidthPercent + movementPercent));
props.targetBlock.setStyle("width", `${finalWidthPercent}%`);
} else {
props.targetBlock.setStyle("width", `${finalWidth}px`);
}
};
const setHeight = (movementY: number, startHeight: number, blockStartHeight: string) => {
const finalHeight = Math.round(startHeight + movementY);
if (blockStartHeight?.includes("%")) {
const parentHeight = props.target.parentElement?.offsetHeight || 0;
const movementPercent = (movementY / parentHeight) * 100;
const startHeightPercent = (startHeight / parentHeight) * 100;
const finalHeightPercent = Math.abs(Math.round(startHeightPercent + movementPercent));
props.targetBlock.setStyle("height", `${finalHeightPercent}%`);
} else {
props.targetBlock.setStyle("height", `${finalHeight}px`);
}
};
const setFontSize = (movement: number, startFontSize: number) => {
const fontSize = clamp(Math.round(startFontSize + 0.5 * movement), 10, 300);
props.targetBlock.setStyle("fontSize", `${fontSize}px`);
};
</script>