forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockLayers.vue
More file actions
244 lines (226 loc) · 6.77 KB
/
BlockLayers.vue
File metadata and controls
244 lines (226 loc) · 6.77 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
<template>
<div>
<draggable
class="block-tree"
:list="blocks"
:group="{ name: 'block-tree' }"
item-key="blockId"
@add="updateParent"
:disabled="disableDraggable">
<template #item="{ element }">
<div
:data-block-layer-id="element.blockId"
:title="element.blockId"
class="min-w-24 cursor-pointer select-none rounded border border-transparent bg-surface-white bg-opacity-50 text-base text-ink-gray-7"
@click.stop="selectBlock(element, $event)"
@mouseover.stop="canvasStore.activeCanvas?.setHoveredBlock(element.blockId)"
@mouseleave.stop="canvasStore.activeCanvas?.setHoveredBlock(null)">
<span
class="group my-[7px] flex items-center gap-1.5 pr-[2px] font-medium"
:style="{ paddingLeft: `${indent}px` }"
:class="{
'!opacity-50': !element.isVisible() || isParentHidden,
}">
<div>
<div class="scroll-into-view-anchor absolute ml-20"></div>
</div>
<FeatherIcon
:name="isExpanded(element) ? 'chevron-down' : 'chevron-right'"
class="h-3 w-3 text-ink-gray-4"
:class="{
'ml-[-18px]': adjustForRoot,
}"
v-if="element.children && element.children.length && !element.isRoot()"
@click.stop="toggleExpanded(element)" />
<FeatherIcon
:name="element.getIcon()"
class="h-3 w-3"
:class="{
'text-purple-500 opacity-80 dark:opacity-100 dark:brightness-125 dark:saturate-[0.3]':
element.isExtendedFromComponent(),
}"
v-if="!Boolean(element.extendedFromComponent)" />
<BlocksIcon
class="mr-1 h-3 w-3"
:class="{
'text-purple-500 opacity-80 dark:opacity-100 dark:brightness-125 dark:saturate-[0.3]':
element.isExtendedFromComponent(),
}"
v-if="Boolean(element.extendedFromComponent)" />
<span
class="layer-label min-h-[1em] min-w-[2em] max-w-64 truncate"
:contenteditable="element.editable"
:title="element.blockId"
:class="{
'text-purple-500 opacity-80 dark:opacity-100 dark:brightness-125 dark:saturate-[0.3]':
element.isExtendedFromComponent(),
}"
@dblclick="
(ev) => {
element.editable = true;
// focus
const target = ev.target as HTMLElement;
target.focus();
}
"
@keydown.enter.stop.prevent="element.editable = false"
@blur="setBlockName($event, element)">
{{ element.getBlockDescription() }}
</span>
<!-- toggle visibility -->
<FeatherIcon
v-if="!element.isRoot() && !isParentHidden"
:name="element.isVisible() ? 'eye' : 'eye-off'"
class="invisible ml-auto mr-2 h-3 w-3 group-hover:visible"
@click.stop="element.toggleVisibility()" />
</span>
<div v-if="canShowChildLayer(element)">
<BlockLayers
:blocks="element.children"
:ref="childLayer"
:is-parent-hidden="isParentHidden || !element.isVisible()"
:indent="childIndent"
:disable-draggable="
Boolean(element.children.length && element.children[0].isChildOfComponentBlock())
" />
</div>
</div>
</template>
</draggable>
</div>
</template>
<script setup lang="ts">
import type Block from "@/block";
import useBuilderStore from "@/stores/builderStore";
import useCanvasStore from "@/stores/canvasStore";
import { FeatherIcon } from "frappe-ui";
import { ref, watch } from "vue";
import draggable from "vuedraggable";
import BlockLayers from "./BlockLayers.vue";
import BlocksIcon from "./Icons/Blocks.vue";
type LayerInstance = InstanceType<typeof BlockLayers>;
const canvasStore = useCanvasStore();
const builderStore = useBuilderStore();
const childLayers = ref<LayerInstance[]>([]);
const childLayer = (el: LayerInstance) => {
if (el) {
childLayers.value.push(el);
}
};
const props = withDefaults(
defineProps<{
blocks: Block[];
indent?: number;
adjustForRoot?: boolean;
disableDraggable?: boolean;
isParentHidden?: boolean;
}>(),
{
blocks: () => [],
indent: 0,
adjustForRoot: true,
disableDraggable: false,
isParentHidden: false,
},
);
interface LayerBlock extends Block {
editable: boolean;
}
let childIndent = props.indent + 24;
if (!props.adjustForRoot) {
childIndent = props.indent + 32;
}
const setBlockName = (ev: Event, block: LayerBlock) => {
const target = ev.target as HTMLElement;
block.blockName = target.innerText.trim();
block.editable = false;
};
const expandedLayers = ref(new Set(["root"]));
const isExpanded = (block: Block) => {
return expandedLayers.value.has(block.blockId);
};
// TODO: Refactor this!
const toggleExpanded = (block: Block) => {
if (block.isRoot()) {
return;
}
if (!blockExits(block)) {
const child = childLayers.value.find((layer) => layer.blockExitsInTree(block)) as LayerInstance;
if (child) {
child.toggleExpanded(block);
}
}
if (isExpanded(block)) {
expandedLayers.value.delete(block.blockId);
} else {
expandedLayers.value.add(block.blockId);
}
};
// @ts-ignore
const isExpandedInTree = (block: Block) => {
if (!blockExits(block)) {
const child = childLayers.value.find((layer) => layer.blockExitsInTree(block)) as LayerInstance;
if (child) {
return child.isExpandedInTree(block);
}
}
return isExpanded(block);
};
const blockExits = (block: Block) => {
return props.blocks.find((b) => b.blockId === block.blockId);
};
const canShowChildLayer = (block: Block) => {
return (isExpanded(block) && block.hasChildren()) || (block.canHaveChildren() && !block.hasChildren());
};
watch(
() => canvasStore.activeCanvas?.selectedBlockIds,
() => {
if (canvasStore.activeCanvas?.selectedBlocks.length) {
canvasStore.activeCanvas?.selectedBlocks.forEach((block: Block) => {
if (block) {
let parentBlock = block.getParentBlock();
// open all parent blocks
while (parentBlock && !parentBlock.isRoot()) {
expandedLayers.value.add(parentBlock?.blockId);
parentBlock = parentBlock.getParentBlock();
}
}
});
}
},
{ immediate: true, deep: true },
);
// @ts-ignore
const updateParent = (event) => {
event.item.__draggable_context.element.parentBlock = canvasStore.activeCanvas?.findBlock(
event.to.closest("[data-block-layer-id]").dataset.blockLayerId,
);
};
const blockExitsInTree = (block: Block) => {
if (blockExits(block)) {
return true;
}
for (const layer of childLayers.value) {
if (layer.blockExitsInTree(block)) {
return true;
}
}
return false;
};
const selectBlock = (block: Block, event: MouseEvent) => {
canvasStore.selectBlock(block, event, false, true);
};
defineExpose({
toggleExpanded,
isExpandedInTree,
blockExitsInTree,
});
</script>
<style>
.hovered-block {
@apply border-blue-300 text-gray-700 dark:border-blue-900 dark:text-gray-500;
}
.block-selected {
@apply border-blue-400 text-gray-900 dark:border-blue-700 dark:text-gray-200;
}
</style>