forked from massCodeIO/massCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.vue
More file actions
286 lines (253 loc) · 7.92 KB
/
Tree.vue
File metadata and controls
286 lines (253 loc) · 7.92 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
<script setup lang="ts">
import type { Ref } from 'vue'
import type { PerfectScrollbarExpose } from 'vue3-perfect-scrollbar'
import type { Node, Position } from './types'
import { languages } from '@/components/editor/grammars/languages'
import * as ContextMenu from '@/components/ui/shadcn/context-menu'
import { useApp, useDialog, useFolders, useSnippets } from '@/composables'
import { i18n } from '@/electron'
import { scrollToElement } from '@/utils'
import CustomIcons from './custom-icons/CustomIcons.vue'
import { treeKeys } from './keys'
import TreeNode from './TreeNode.vue'
interface Props {
modelValue: Node[]
selectedId?: string | number
contextMenuHandler?: () => Promise<boolean>
focusHandler?: (isFocused: Ref) => void
}
interface Emits {
(e: 'update:modelValue', value: Node[]): void
(e: 'clickNode', value: { id: number, event?: MouseEvent }): void
(
e: 'dragNode',
value: { nodes: Node[], target: Node, position: Position },
): void
(e: 'toggleNode', value: Node): void
}
const props = withDefaults(defineProps<Props>(), {
contextMenuHandler: () => Promise.resolve(true),
})
const emit = defineEmits<Emits>()
const {
createFolderAndSelect,
deleteFolder,
renameFolderId,
folders,
updateFolder,
getFolderByIdFromTree,
getFolders,
selectedFolderIds,
clearFolderSelection,
selectFolder,
} = useFolders()
const { state } = useApp()
const { clearSnippetsState } = useSnippets()
const contextMenuTriggerRef = useTemplateRef('contextMenuTriggerRef')
const scrollRef = useTemplateRef<PerfectScrollbarExpose>('scrollRef')
const hoveredNodeId = ref('')
const isHoveredByIdDisabled = ref(false)
const contextNode = ref<Node | null>(null)
const isContextMultiSelection = computed(() => {
if (!contextNode.value)
return false
if (selectedFolderIds.value.length <= 1)
return false
return selectedFolderIds.value.includes(contextNode.value.id)
})
function clickNode(id: number, event?: MouseEvent) {
return emit('clickNode', { id, event })
}
function dragNode(nodes: Node[], target: Node, position: Position) {
return emit('dragNode', { nodes, target, position })
}
function toggleNode(node: Node) {
return emit('toggleNode', node)
}
/**
* Поскольку в TreeNode есть @contextmenu.stop, то для того чтобы
* предотвратить высплытие в родительский узел для корректной подсветки,
* используем программную отправку события contextmenu.
* Так же такое решение избавит от n кол-ва ContextMenu на каждый узел.
*/
function contextMenu(node: Node, event: MouseEvent) {
contextNode.value = node
contextMenuTriggerRef.value?.dispatchEvent(
new MouseEvent('contextmenu', {
bubbles: false,
clientX: event.clientX,
clientY: event.clientY,
}),
)
}
async function onDeleteFolder() {
if (!contextNode.value)
return
const { confirm } = useDialog()
const activeBeforeDelete = state.folderId
const targetIds = selectedFolderIds.value.includes(contextNode.value.id)
? [...selectedFolderIds.value]
: [contextNode.value.id]
const folderName = getFolderByIdFromTree(
folders.value,
contextNode.value.id,
)?.name
const isConfirmed = await confirm({
title:
targetIds.length > 1
? i18n.t('messages:confirm.delete', {
name: i18n.t('sidebar.folders'),
})
: i18n.t('messages:confirm.delete', { name: folderName }),
description: i18n.t('messages:warning:allSnippetsMoveToTrash'),
})
if (!isConfirmed)
return
await Promise.all(targetIds.map(id => deleteFolder(id, false)))
await getFolders(false)
if (activeBeforeDelete && targetIds.includes(activeBeforeDelete)) {
clearSnippetsState()
const fallbackId = selectedFolderIds.value[0]
if (fallbackId) {
await selectFolder(fallbackId)
scrollToElement(`[id="${fallbackId}"]`)
}
else {
clearFolderSelection()
}
}
nextTick(() => {
scrollRef.value?.ps?.update()
})
}
function onRenameFolder() {
// FIXME: Костыль для того чтобы input в TreeNode фокусировался,
// разобраться почему не работает nextTick
setTimeout(() => {
if (!contextNode.value)
return
renameFolderId.value = contextNode.value.id
}, 100)
}
function onSelectLanguage(language: string) {
if (!contextNode.value) {
return
}
updateFolder(contextNode.value.id, {
defaultLanguage: language,
})
}
function onSetCustomIcon() {
if (!contextNode.value)
return
const { showDialog } = useDialog()
showDialog({
title: i18n.t('action.setCustomIcon'),
content: h(CustomIcons, {
nodeId: contextNode.value.id,
}),
})
}
async function onRemoveCustomIcon() {
if (!contextNode.value)
return
updateFolder(contextNode.value.id, {
icon: null,
})
await getFolders()
}
provide(treeKeys, {
clickNode,
contextMenu,
dragNode,
focusHandler: props.focusHandler,
isHoveredByIdDisabled,
toggleNode,
})
</script>
<template>
<PerfectScrollbar
v-if="modelValue.length"
ref="scrollRef"
:options="{ minScrollbarLength: 20, suppressScrollX: true }"
>
<ContextMenu.Root>
<ContextMenu.Trigger as-child>
<div
ref="contextMenuTriggerRef"
data-folder-tree
v-bind="$attrs"
>
<TreeNode
v-for="(node, index) in modelValue"
:key="node.id"
:node="node"
:nodes="modelValue"
:index="index"
:hovered-node-id="hoveredNodeId"
>
<template #default="slotProps">
<slot
v-if="slotProps && slotProps.node"
:node="slotProps.node"
:deep="slotProps.deep"
:hovered-node-id="hoveredNodeId"
/>
<template v-else>
{{ node.name }} s
</template>
</template>
</TreeNode>
</div>
</ContextMenu.Trigger>
<ContextMenu.Content>
<template v-if="isContextMultiSelection">
<ContextMenu.Item @click="onDeleteFolder">
{{ i18n.t("action.delete.common") }}
</ContextMenu.Item>
</template>
<template v-else>
<ContextMenu.Item @click="createFolderAndSelect(contextNode?.id)">
{{ i18n.t("action.new.folder") }}
</ContextMenu.Item>
<ContextMenu.Separator />
<ContextMenu.Item @click="onRenameFolder">
{{ i18n.t("action.rename") }}
</ContextMenu.Item>
<ContextMenu.Item @click="onDeleteFolder">
{{ i18n.t("action.delete.common") }}
</ContextMenu.Item>
<ContextMenu.Separator />
<ContextMenu.Item @click="onSetCustomIcon">
{{ i18n.t("action.setCustomIcon") }}
</ContextMenu.Item>
<ContextMenu.Item
v-if="contextNode?.icon"
@click="onRemoveCustomIcon"
>
{{ i18n.t("action.removeCustomIcon") }}
</ContextMenu.Item>
<ContextMenu.Separator />
<ContextMenu.Sub>
<ContextMenu.SubTrigger>
{{ i18n.t("action.defaultLanguage") }}
</ContextMenu.SubTrigger>
<ContextMenu.SubContent>
<PerfectScrollbar :options="{ minScrollbarLength: 20 }">
<div class="max-h-[250px]">
<ContextMenu.Item
v-for="language in languages"
:key="language.value"
@click="onSelectLanguage(language.value)"
>
{{ language.name }}
</ContextMenu.Item>
</div>
</PerfectScrollbar>
</ContextMenu.SubContent>
</ContextMenu.Sub>
</template>
</ContextMenu.Content>
</ContextMenu.Root>
</PerfectScrollbar>
</template>