forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockContextMenu.vue
More file actions
291 lines (274 loc) · 8.61 KB
/
BlockContextMenu.vue
File metadata and controls
291 lines (274 loc) · 8.61 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<template>
<div>
<ContextMenu
v-if="contextMenuVisible"
:pos-x="posX"
:pos-y="posY"
:options="contextMenuOptions"
@select="handleContextMenuSelect"
v-on-click-outside="() => (contextMenuVisible = false)" />
<NewComponent v-if="block" :block="block" v-model="showNewComponentDialog"></NewComponent>
<NewBlockTemplate v-if="block" :block="block" v-model="showBlockTemplateDialog"></NewBlockTemplate>
</div>
</template>
<script setup lang="ts">
import type Block from "@/block";
import ContextMenu from "@/components/ContextMenu.vue";
import NewBlockTemplate from "@/components/Modals/NewBlockTemplate.vue";
import NewComponent from "@/components/Modals/NewComponent.vue";
import useBuilderStore from "@/stores/builderStore";
import useCanvasStore from "@/stores/canvasStore";
import useComponentStore from "@/stores/componentStore";
import getBlockTemplate from "@/utils/blockTemplate";
import { confirm, detachBlockFromComponent, getBlockCopy, triggerCopyEvent } from "@/utils/helpers";
import { vOnClickOutside } from "@vueuse/components";
import { useStorage } from "@vueuse/core";
import { Ref, nextTick, ref } from "vue";
import { toast } from "vue-sonner";
const componentStore = useComponentStore();
const canvasStore = useCanvasStore();
const builderStore = useBuilderStore();
const contextMenuVisible = ref(false);
const posX = ref(0);
const posY = ref(0);
const triggeredFromLayersPanel = ref(false);
const block = ref(null) as unknown as Ref<Block>;
const showNewComponentDialog = ref(false);
const showBlockTemplateDialog = ref(false);
const target = ref(null) as unknown as Ref<HTMLElement>;
const showContextMenu = (event: MouseEvent, refBlock: Block) => {
block.value = refBlock;
// check if the event is triggered from layers panel
target.value = event.target as HTMLElement;
const layersPanel = target.value.closest(".block-layers");
triggeredFromLayersPanel.value = Boolean(layersPanel);
if (block.value.isRoot()) return;
contextMenuVisible.value = true;
posX.value = event.pageX;
posY.value = event.pageY;
event.preventDefault();
event.stopPropagation();
};
const handleContextMenuSelect = (action: CallableFunction) => {
action();
contextMenuVisible.value = false;
};
const copiedStyle = useStorage("copiedStyle", { blockId: "", style: {} }, sessionStorage) as Ref<StyleCopy>;
const copyStyle = () => {
copiedStyle.value = {
blockId: block.value.blockId,
style: block.value.getStylesCopy(),
};
};
const pasteStyle = () => {
block.value.updateStyles(copiedStyle.value?.style as BlockStyleObjects);
};
const duplicateBlock = () => {
block.value.duplicateBlock();
};
const contextMenuOptions: ContextMenuOption[] = [
{
label: "Edit HTML",
action: () => {
canvasStore.editHTML(block.value);
},
condition: () => block.value.isHTML(),
},
{ label: "Copy", action: () => triggerCopyEvent() },
{ label: "Copy Style", action: copyStyle },
{
label: "Paste Style",
action: pasteStyle,
condition: () => Boolean(copiedStyle.value.blockId && copiedStyle.value?.blockId !== block.value.blockId),
},
{ label: "Duplicate", action: duplicateBlock },
{
label: "Convert To Collection",
action: () => {
block.value.isRepeaterBlock = true;
toast.warning("Please select a collection");
},
condition: () =>
block.value.isContainer() &&
!block.value.isRoot() &&
!block.value.isRepeater() &&
!block.value.isChildOfComponentBlock() &&
!block.value.isExtendedFromComponent(),
},
{
label: "Remove Collection",
action: () => {
block.value.isRepeaterBlock = false;
block.value.dataKey = {};
},
condition: () => block.value.isRepeater(),
},
{
label: "Wrap In Container",
action: () => {
const newBlockObj = getBlockTemplate("fit-container");
const parentBlock = block.value.getParentBlock();
if (!parentBlock) return;
const selectedBlocks = canvasStore.activeCanvas?.selectedBlocks || [];
const blockPosition = Math.min(...selectedBlocks.map(parentBlock.getChildIndex.bind(parentBlock)));
const newBlock = parentBlock?.addChild(newBlockObj, blockPosition);
let width = null as string | null;
// move selected blocks to newBlock
selectedBlocks
.sort((a: Block, b: Block) => parentBlock.getChildIndex(a) - parentBlock.getChildIndex(b))
.forEach((block: Block) => {
parentBlock?.removeChild(block);
newBlock?.addChild(block);
if (!width) {
const blockWidth = block.getStyle("width") as string | undefined;
if (blockWidth && (blockWidth == "auto" || blockWidth.endsWith("%"))) {
width = "100%";
}
}
});
if (width) {
newBlock?.setStyle("width", width);
}
nextTick(() => {
if (newBlock) {
newBlock.selectBlock();
}
});
},
condition: () => {
if (block.value.isRoot()) return false;
if (canvasStore.activeCanvas?.selectedBlocks.length === 1) return true;
// check if all selected blocks are siblings
const parentBlock = block.value.getParentBlock();
if (!parentBlock) return false;
const selectedBlocks = canvasStore.activeCanvas?.selectedBlocks || [];
return selectedBlocks.every((block: Block) => block.getParentBlock() === parentBlock);
},
},
{
label: "Repeat Block",
action: () => {
const repeaterBlockObj = getBlockTemplate("repeater");
const parentBlock = block.value.getParentBlock();
if (!parentBlock) return;
const repeaterBlock = parentBlock.addChild(repeaterBlockObj, parentBlock.getChildIndex(block.value));
repeaterBlock.addChild(getBlockCopy(block.value));
parentBlock.removeChild(block.value);
repeaterBlock.selectBlock();
toast.warning("Please select a collection");
},
condition: () =>
!block.value.isRoot() && !block.value.isRepeater() && !block.value.isChildOfComponentBlock(),
},
{
label: "Reset Overrides",
condition: () => canvasStore.activeCanvas?.activeBreakpoint !== "desktop",
disabled: () => !block.value?.hasOverrides(canvasStore.activeCanvas?.activeBreakpoint || "desktop"),
action: () => {
block.value.resetOverrides(canvasStore.activeCanvas?.activeBreakpoint || "desktop");
},
},
{
label: "Reset Changes",
action: () => {
if (block.value.hasChildren()) {
confirm("Reset changes in child blocks as well?").then((confirmed) => {
block.value.resetChanges(confirmed);
});
} else {
block.value.resetChanges();
}
},
condition: () => block.value.isExtendedFromComponent(),
},
{
label: "Sync Component",
condition: () => Boolean(block.value.extendedFromComponent),
action: () => {
block.value.syncWithComponent();
},
},
{
label: "Reset Component",
condition: () => Boolean(block.value.extendedFromComponent),
action: () => {
confirm("Are you sure you want to reset?").then((confirmed) => {
if (confirmed) {
block.value.resetWithComponent();
}
});
},
},
{
label: "Edit Component",
action: () => {
componentStore.editComponent(block.value);
},
condition: () => block.value.isExtendedFromComponent(),
},
{
label: "Save as Block Template",
action: () => {
showBlockTemplateDialog.value = true;
},
condition: () => !block.value.isExtendedFromComponent() && Boolean(window.is_developer_mode),
},
{
label: "Save As Component",
action: () => (showNewComponentDialog.value = true),
condition: () => !block.value.isExtendedFromComponent(),
},
{
label: "Detach Component",
action: () => {
const newBlock = detachBlockFromComponent(block.value, null);
if (newBlock) {
newBlock.selectBlock();
}
block.value.getParentBlock()?.replaceChild(block.value, newBlock);
},
condition: () => Boolean(block.value.extendedFromComponent),
},
{
label: "Rename",
action: () => {
const layerLabel = target.value?.closest("[data-block-layer-id]")?.querySelector(".layer-label");
if (layerLabel) {
layerLabel.dispatchEvent(new Event("dblclick"));
nextTick(() => {
// selct all text in the layerLabel
const range = document.createRange();
range.selectNodeContents(layerLabel);
const selection = window.getSelection();
if (selection) {
selection.removeAllRanges();
selection.addRange(range);
}
});
}
},
condition: () =>
!block.value.isRoot() && !block.value.isChildOfComponentBlock() && triggeredFromLayersPanel.value,
},
{
label: "Delete",
action: () => {
const selectedBlocks = canvasStore.activeCanvas?.selectedBlocks || [];
selectedBlocks.forEach((selectedBlock: Block) => {
canvasStore.activeCanvas?.removeBlock(selectedBlock);
});
},
condition: () => {
return (
!block.value.isRoot() &&
!block.value.isChildOfComponentBlock() &&
block.value.isVisible() &&
Boolean(block.value.getParentBlock())
);
},
},
];
defineExpose({
showContextMenu,
});
</script>