forked from nextcloud/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteItem.vue
More file actions
261 lines (246 loc) · 6.13 KB
/
NoteItem.vue
File metadata and controls
261 lines (246 loc) · 6.13 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
<template>
<NcListItem
:title="title"
:active="isSelected"
:to="{ name: 'note', params: { noteId: note.id.toString() } }"
@update:menuOpen="onMenuChange"
@click="onNoteSelected(note.id)"
>
<template #subtitle>
{{ categoryTitle }}
</template>
<template #icon>
<AlertOctagonIcon v-if="note.error"
slot="icon"
:size="20"
fill-color="#E9322D"
/>
<StarIcon v-else-if="note.favorite"
slot="icon"
:size="20"
fill-color="#FC0"
/>
<FileDocumentOutlineIcon v-else
slot="icon"
:size="20"
fill-color="var(--color-text-lighter)"
/>
</template>
<template #actions>
<NcActionButton :icon="actionFavoriteIcon" @click="onToggleFavorite">
{{ actionFavoriteText }}
</NcActionButton>
<NcActionButton v-if="!showCategorySelect" @click="showCategorySelect = true">
<template #icon>
<FolderIcon :size="20" />
</template>
{{ categoryTitle }}
</NcActionButton>
<NcActionInput
v-else
:value="note.category"
type="multiselect"
label="label"
track-by="id"
:multiple="false"
:options="categories"
:disabled="loading.category"
:taggable="true"
@input="onCategoryChange"
@search-change="onCategoryChange"
>
<template #icon>
<FolderIcon :size="20" />
</template>
{{ t('notes', 'Change category') }}
</NcActionInput>
<NcActionButton v-if="!renaming" @click="startRenaming">
<PencilIcon slot="icon" :size="20" />
{{ t('notes', 'Rename') }}
</NcActionButton>
<NcActionInput v-else
v-model.trim="newTitle"
:disabled="!renaming"
:placeholder="t('notes', 'Rename note')"
:show-trailing-button="true"
@input="onInputChange($event)"
@submit="onRename"
>
<PencilIcon slot="icon" :size="20" />
</NcActionInput>
<NcActionSeparator />
<NcActionButton v-if="!note.readonly" :icon="actionDeleteIcon" @click="onDeleteNote">
{{ t('notes', 'Delete note') }}
</NcActionButton>
</template>
</NcListItem>
</template>
<script>
import { NcListItem, NcActionButton, NcActionSeparator, NcActionInput } from '@nextcloud/vue'
import AlertOctagonIcon from 'vue-material-design-icons/AlertOctagon.vue'
import FileDocumentOutlineIcon from 'vue-material-design-icons/FileDocumentOutline.vue'
import FolderIcon from 'vue-material-design-icons/Folder.vue'
import PencilIcon from 'vue-material-design-icons/Pencil.vue'
import StarIcon from 'vue-material-design-icons/Star.vue'
import { categoryLabel, routeIsNewNote } from '../Util.js'
import { showError } from '@nextcloud/dialogs'
import { setFavorite, setTitle, fetchNote, deleteNote, setCategory } from '../NotesService.js'
export default {
name: 'NoteItem',
components: {
AlertOctagonIcon,
FileDocumentOutlineIcon,
FolderIcon,
NcActionButton,
NcListItem,
StarIcon,
NcActionSeparator,
NcActionInput,
PencilIcon,
},
props: {
note: {
type: Object,
required: true,
},
},
data() {
return {
loading: {
note: false,
category: false,
},
newTitle: '',
renaming: false,
showCategorySelect: false,
}
},
computed: {
isSelected() {
return this.$store.getters.getSelectedNote() === this.note.id
},
title() {
return this.note.title + (this.note.unsaved ? ' *' : '')
},
categoryTitle() {
return categoryLabel(this.note.category)
},
actionFavoriteText() {
return this.note.favorite ? this.t('notes', 'Remove from favorites') : this.t('notes', 'Add to favorites')
},
actionFavoriteIcon() {
let icon = this.note.favorite ? 'icon-star-dark' : 'icon-starred'
if (this.loading.favorite) {
icon += ' loading'
}
return icon
},
actionCategoryText() {
return categoryLabel(this.note.category)
},
actionDeleteIcon() {
return 'icon-delete' + (this.loading.delete ? ' loading' : '')
},
categories() {
return [
{
id: '',
label: categoryLabel(''),
},
...this.$store.getters.getCategories(0, false).map((category) => ({
id: category,
label: categoryLabel(category),
})),
]
},
},
methods: {
onMenuChange(state) {
this.actionsOpen = state
this.showCategorySelect = false
},
onNoteSelected(noteId) {
this.$emit('note-selected', noteId)
},
onToggleFavorite() {
this.loading.favorite = true
setFavorite(this.note.id, !this.note.favorite)
.catch(() => {
})
.then(() => {
this.loading.favorite = false
this.actionsOpen = false
})
},
onCategorySelected() {
this.actionsOpen = false
this.$emit('category-selected', this.note.category)
},
startRenaming() {
this.renaming = true
this.newTitle = this.note.title
this.$emit('start-renaming', this.note.id)
},
onInputChange(event) {
this.newTitle = event.target.value.toString()
},
async onCategoryChange(result) {
this.showCategorySelect = false
const category = result?.id ?? result?.label ?? null
if (category !== null && this.note.category !== category) {
this.loading.category = true
await setCategory(this.note.id, category)
this.loading.category = false
}
},
async onRename() {
const newTitle = this.newTitle.toString()
if (!newTitle) {
return
}
this.loading.note = true
setTitle(this.note.id, newTitle)
.then(() => {
this.newTitle = ''
})
.catch((e) => {
console.error('Failed to rename note', e)
showError(this.t('notes', 'Error while renaming note.'))
})
.finally(() => {
this.loading.note = false
})
if (routeIsNewNote(this.$route)) {
this.$router.replace({
name: 'note',
params: { noteId: this.note.id.toString() },
})
}
this.renaming = false
},
async onDeleteNote() {
this.loading.delete = true
try {
const note = await fetchNote(this.note.id)
if (note.errorType) {
throw new Error('Note has errors')
}
await deleteNote(this.note.id, () => {
this.$emit('note-deleted', note)
this.loading.delete = false
this.actionsOpen = false
})
} catch (e) {
showError(this.t('notes', 'Error during preparing note for deletion.'))
this.loading.delete = false
this.actionsOpen = false
}
},
},
}
</script>
<style scoped>
.material-design-icon {
width: 44px;
}
</style>