-
-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathItem.vue
More file actions
274 lines (242 loc) · 6.79 KB
/
Item.vue
File metadata and controls
274 lines (242 loc) · 6.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
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
<script setup lang="ts">
import type { SnippetsResponse } from '@/services/api/generated'
import * as ContextMenu from '@/components/ui/shadcn/context-menu'
import { useApp, useDialog, useSnippets } from '@/composables'
import { LibraryFilter } from '@/composables/types'
import { i18n } from '@/electron'
import { onClickOutside } from '@vueuse/core'
import { format } from 'date-fns'
interface Props {
snippet: SnippetsResponse[0]
}
const props = defineProps<Props>()
const {
selectedSnippetId,
highlightedSnippetIds,
highlightedFolderId,
selectedSnippetIdBeforeSearch,
isFocusedSnippetName,
selectedLibrary,
} = useApp()
const {
selectSnippet,
isSearch,
selectFirstSnippet,
duplicateSnippet,
selectedSnippetIds,
updateSnippet,
updateSnippets,
deleteSnippet,
deleteSnippets,
displayedSnippets,
} = useSnippets()
const { confirm } = useDialog()
const isFocused = ref(false)
const snippetRef = ref<HTMLDivElement>()
const isSelected = computed(() => selectedSnippetId.value === props.snippet.id)
const isInMultiSelection = computed(
() =>
selectedSnippetIds.value.length > 1
&& selectedSnippetIds.value.includes(props.snippet.id),
)
const isHighlighted = computed(() =>
highlightedSnippetIds.value.has(props.snippet.id),
)
const isDuplicateDisabled = computed(
() => highlightedSnippetIds.value.size > 1,
)
const folderName = computed(() => {
if (props.snippet.folder) {
return props.snippet.folder.name
}
if (props.snippet.isDeleted) {
return i18n.t('sidebar.trash')
}
return i18n.t('sidebar.inbox')
})
function onSnippetClick(id: number, event: MouseEvent) {
selectSnippet(id, event.shiftKey)
if (!isSearch.value) {
selectedSnippetIdBeforeSearch.value = id
}
isFocused.value = true
}
function onClickContextMenu() {
highlightedFolderId.value = undefined
highlightedSnippetIds.value.clear()
highlightedSnippetIds.value.add(props.snippet.id)
if (selectedSnippetIds.value.length > 1) {
selectedSnippetIds.value.forEach(id =>
highlightedSnippetIds.value.add(id),
)
}
}
async function onDelete() {
if (selectedSnippetIds.value.length > 1) {
const isAllSoftDeleted = displayedSnippets.value?.every(s => s.isDeleted)
if (isAllSoftDeleted) {
const isConfirmed = await confirm({
title: i18n.t('dialog:deleteConfirmMultipleSnippets', {
count: selectedSnippetIds.value.length,
}),
content: i18n.t('dialog:noUndo'),
})
if (isConfirmed) {
await deleteSnippets(selectedSnippetIds.value)
}
}
else {
// Мягкое удаление
const snippetsData = selectedSnippetIds.value?.map(() => ({
folderId: null,
isDeleted: 1,
}))
await updateSnippets(selectedSnippetIds.value, snippetsData)
}
}
else if (props.snippet.isDeleted) {
const isConfirmed = await confirm({
title: i18n.t('dialog:deleteConfirm', { name: props.snippet.name }),
content: i18n.t('dialog:noUndo'),
})
if (isConfirmed) {
await deleteSnippet(props.snippet.id)
}
}
else {
// Мягкое удаление
await updateSnippet(props.snippet.id, {
folderId: null,
isDeleted: 1,
})
}
if (
selectedSnippetIds.value.length > 1
|| selectedSnippetId.value === props.snippet.id
) {
selectFirstSnippet()
}
}
async function onDuplicate() {
await duplicateSnippet(props.snippet.id)
selectFirstSnippet()
isFocusedSnippetName.value = true
}
function onDragStart(event: DragEvent) {
const ids
= selectedSnippetIds.value.length > 1
? selectedSnippetIds.value
: [props.snippet.id]
event.dataTransfer?.setData('snippetIds', JSON.stringify(ids))
const el = document.createElement('div')
if (selectedSnippetIds.value.length > 1) {
el.className
= 'fixed left-[-100%] text-fg truncate max-w-[200px] flex items-center'
el.id = 'ghost'
el.innerHTML = `
<span class="rounded-full bg-primary text-white px-2 py-0.5 text-xs ml-3">
${selectedSnippetIds.value.length}
</span>
`
}
else {
el.className = 'fixed left-[-100%] text-fg truncate max-w-[200px]'
el.id = 'ghost'
el.innerHTML = props.snippet.name
}
document.body.appendChild(el)
event.dataTransfer?.setDragImage(el, 0, 0)
setTimeout(() => el.remove(), 0)
}
onClickOutside(snippetRef, () => {
isFocused.value = false
highlightedSnippetIds.value.clear()
})
</script>
<template>
<div
ref="snippetRef"
data-snippet-item
class="border-border relative px-1 not-first:border-t focus-visible:outline-none [&+.is-selected+div]:border-transparent"
:class="{
'is-selected': isSelected,
'is-multi-selected': isInMultiSelection,
'is-focused': isFocused,
'is-highlighted': isHighlighted,
}"
draggable="true"
@click="(event) => onSnippetClick(snippet.id, event)"
@contextmenu="onClickContextMenu"
@dragstart.stop="onDragStart"
>
<ContextMenu.Root>
<ContextMenu.Trigger>
<div class="flex flex-col p-2 select-none">
<div
class="mb-2 min-w-0 overflow-hidden text-ellipsis whitespace-nowrap"
>
{{ snippet.name || i18n.t("snippet.untitled") }}
</div>
<div class="meta text-text-muted flex justify-between text-xs">
<div>
{{ folderName }}
</div>
<div>
{{ format(new Date(snippet.createdAt), "dd.MM.yyyy") }}
</div>
</div>
</div>
</ContextMenu.Trigger>
<ContextMenu.Content>
<ContextMenu.Item
v-if="selectedLibrary !== LibraryFilter.Trash"
:disabled="isDuplicateDisabled"
@click="onDuplicate"
>
{{ i18n.t("duplicate") }}
</ContextMenu.Item>
<ContextMenu.Item @click="onDelete">
{{
selectedLibrary === LibraryFilter.Trash
? i18n.t("delete")
: i18n.t("moveToTrash")
}}
</ContextMenu.Item>
</ContextMenu.Content>
</ContextMenu.Root>
</div>
</template>
<style lang="scss">
@reference "../../styles.css";
[data-snippet-item] {
&.is-selected {
@apply bg-list-selection text-list-selection-fg rounded-md;
.meta {
@apply text-list-selection-fg;
}
}
&.is-multi-selected {
@apply bg-list-selection/80 text-list-selection-fg rounded-md;
.meta {
@apply text-list-selection-fg;
}
}
&.is-focused:not(.is-multi-selected) {
@apply bg-list-focus text-list-focus-fg rounded-md;
.meta {
@apply text-list-focus-fg;
}
}
&.is-highlighted {
@apply outline-list-focus rounded-md outline-2 -outline-offset-2;
&.is-focused,
&.is-selected,
&.is-multi-selected {
@apply bg-bg text-list-selection-fg;
.meta {
@apply text-list-selection-fg;
}
}
}
}
</style>