-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathApp.vue
More file actions
358 lines (322 loc) · 10.2 KB
/
App.vue
File metadata and controls
358 lines (322 loc) · 10.2 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!--
- SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<EditorHint v-if="editorHint" @close="editorHint=false" />
<NcContent v-else app-name="notes" :content-class="{loading: loading.notes}">
<NcAppNavigation :class="{loading: loading.notes, 'icon-error': error}">
<NcAppNavigationNew
v-show="!loading.notes && !error"
:text="t('notes', 'New note')"
@click="onNewNote"
>
<PlusIcon slot="icon" :size="20" />
</NcAppNavigationNew>
<template #list>
<CategoriesList v-show="!loading.notes"
v-if="numNotes"
/>
</template>
<template #footer>
<ul class="app-navigation-entry__settings">
<NcAppNavigationItem
:name="t('notes', 'Notes settings')"
@click.prevent="openSettings"
>
<CogIcon slot="icon" :size="20" />
</NcAppNavigationItem>
</ul>
<AppSettings v-if="!loading.notes && error !== true" :open.sync="settingsVisible" @reload="reloadNotes" />
</template>
</NcAppNavigation>
<NcAppContent v-if="error">
<div style="margin: 2em;">
<h2>{{ t('notes', 'Error') }}</h2>
<p>{{ error }}</p>
<p>{{ t('notes', 'Please see Nextcloud server log for details.') }}</p>
</div>
</NcAppContent>
<router-view v-else @note-deleted="onNoteDeleted" />
</NcContent>
</template>
<script>
import NcAppContent from '@nextcloud/vue/components/NcAppContent'
import NcAppNavigation from '@nextcloud/vue/components/NcAppNavigation'
import NcAppNavigationNew from '@nextcloud/vue/components/NcAppNavigationNew'
import NcAppNavigationItem from '@nextcloud/vue/components/NcAppNavigationItem'
import NcContent from '@nextcloud/vue/components/NcContent'
import { loadState } from '@nextcloud/initial-state'
import { showSuccess, TOAST_UNDO_TIMEOUT, TOAST_PERMANENT_TIMEOUT } from '@nextcloud/dialogs'
import '@nextcloud/dialogs/style.css'
import PlusIcon from 'vue-material-design-icons/Plus.vue'
import CogIcon from 'vue-material-design-icons/CogOutline.vue'
import AppSettings from './components/AppSettings.vue'
import CategoriesList from './components/CategoriesList.vue'
import EditorHint from './components/Modal/EditorHint.vue'
import { config } from './config.js'
import { fetchNotes, noteExists, createNote, undoDeleteNote } from './NotesService.js'
import store from './store.js'
export default {
name: 'App',
components: {
AppSettings,
CategoriesList,
CogIcon,
EditorHint,
NcAppContent,
NcAppNavigation,
NcAppNavigationNew,
NcAppNavigationItem,
NcContent,
PlusIcon,
},
data() {
return {
filter: {
category: null,
},
loading: {
notes: true,
create: false,
},
error: false,
undoNotification: null,
undoTimer: null,
deletedNotes: [],
refreshTimer: null,
editorHint: loadState('notes', 'editorHint', '') === 'yes' && window.OCA.Text?.createEditor,
settingsVisible: false,
}
},
computed: {
numNotes() {
return store.getters.numNotes()
},
notes() {
return store.state.notes.notes
},
filteredNotes() {
return store.getters.getFilteredNotes()
},
},
created() {
store.commit('setDocumentTitle', document.title)
window.addEventListener('beforeunload', this.onClose)
document.addEventListener('visibilitychange', this.onVisibilityChange)
this.loadNotes()
},
destroyed() {
document.removeEventListener('visibilitychange', this.onVisibilityChange)
this.stopRefreshTimer()
},
methods: {
async loadNotes() {
console.log('[App.loadNotes] Starting initial load')
// Skip refresh if in search mode - search results should not be overwritten
const searchText = store.state.app.searchText
if (searchText && searchText.trim() !== '') {
console.log('[App.loadNotes] Skipping - in search mode with query:', searchText)
this.startRefreshTimer(config.interval.notes.refresh)
return
}
try {
// Load only the first chunk on initial load (50 notes)
// Subsequent chunks will be loaded on-demand when scrolling
const data = await fetchNotes(50, null)
console.log('[App.loadNotes] fetchNotes returned:', data)
if (data === null) {
// nothing changed (304 response)
console.log('[App.loadNotes] 304 Not Modified - no changes')
return
}
if (data && data.noteIds) {
console.log('[App.loadNotes] Success - received', data.noteIds.length, 'note IDs')
console.log('[App.loadNotes] Next cursor:', data.chunkCursor)
this.error = false
// Route to default note after first chunk
this.routeDefault(0)
// Store cursor for next chunk (will be used by scroll handler)
store.commit('setNotesChunkCursor', data.chunkCursor || null)
} else if (this.loading.notes) {
// only show error state if not loading in background
console.log('[App.loadNotes] Error - no noteIds in response')
this.error = data?.errorMessage || true
} else {
console.error('Server error while updating list of notes: ' + (data?.errorMessage || 'Unknown error'))
}
} catch (err) {
// only show error state if not loading in background
if (this.loading.notes) {
this.error = true
}
console.error('[App.loadNotes] Exception:', err)
} finally {
this.loading.notes = false
this.startRefreshTimer(config.interval.notes.refresh)
}
},
startRefreshTimer(seconds) {
if (this.refreshTimer === null && document.visibilityState === 'visible') {
this.refreshTimer = setTimeout(() => {
this.refreshTimer = null
this.loadNotes()
}, seconds * 1000)
}
},
stopRefreshTimer() {
if (this.refreshTimer !== null) {
clearTimeout(this.refreshTimer)
this.refreshTimer = null
}
},
onVisibilityChange() {
if (document.visibilityState === 'visible') {
this.startRefreshTimer(config.interval.notes.refreshAfterHidden)
} else {
this.stopRefreshTimer()
}
},
reloadNotes() {
if (this.$route.path !== '/') {
this.$router.push('/')
}
store.commit('removeAllNotes')
store.commit('clearSyncCache')
this.loading.notes = true
this.loadNotes()
},
routeDefault(defaultNoteId) {
console.log('[App.routeDefault] Called with defaultNoteId:', defaultNoteId)
console.log('[App.routeDefault] Current route:', this.$route.name, 'noteId:', this.$route.params.noteId)
// Don't redirect if user is already on a specific note route
// (the note will be fetched individually even if not in the loaded chunk)
if (this.$route.name === 'note' && this.$route.params.noteId) {
console.log('[App.routeDefault] Already on note route, skipping redirect')
return
}
// Only redirect if no note route is set (e.g., on welcome page)
if (this.$route.name !== 'note') {
console.log('[App.routeDefault] Not on note route, routing to default')
if (noteExists(defaultNoteId)) {
this.routeToNote(defaultNoteId)
} else {
this.routeFirst()
}
}
},
routeFirst() {
const availableNotes = this.filteredNotes.filter(note => !note.error && !note.deleting)
if (availableNotes.length > 0) {
this.routeToNote(availableNotes[0].id)
} else {
if (this.$route.name !== 'welcome') {
this.$router.push({ name: 'welcome' })
}
}
},
routeToNote(id, query) {
const noteId = id.toString()
if (this.$route.name !== 'note' || this.$route.params.noteId !== noteId) {
this.$router.push({
name: 'note',
params: { noteId },
query,
})
}
},
openSettings() {
this.settingsVisible = true
},
onNewNote() {
if (this.loading.create) {
return
}
this.loading.create = true
createNote(store.getters.getSelectedCategory())
.then(note => {
this.routeToNote(note.id, { new: null })
})
.catch(() => {
})
.finally(() => {
this.loading.create = false
})
},
onNoteDeleted(note) {
this.deletedNotes.push(note)
this.clearUndoTimer()
let label
if (this.deletedNotes.length === 1) {
label = this.t('notes', 'Deleted {title}', { title: note.title })
} else {
label = this.n('notes', 'Deleted {number} note', 'Deleted {number} notes', this.deletedNotes.length, { number: this.deletedNotes.length })
}
if (this.undoNotification === null) {
const action = '<button class="undo">' + this.t('notes', 'Undo Delete') + '</button>'
this.undoNotification = showSuccess(
'<span class="deletedLabel">' + label + '</span> ' + action,
{ isHTML: true, timeout: TOAST_PERMANENT_TIMEOUT, onRemove: this.onUndoNotificationClosed },
)
this.undoNotification.toastElement.getElementsByClassName('undo')
.forEach(element => { element.onclick = this.onUndoDelete })
} else {
this.undoNotification.toastElement.getElementsByClassName('deletedLabel')
.forEach(element => { element.textContent = label })
}
this.undoTimer = setTimeout(this.onRemoveUndoNotification, TOAST_UNDO_TIMEOUT)
this.routeFirst()
},
clearUndoTimer() {
if (this.undoTimer) {
clearTimeout(this.undoTimer)
this.undoTimer = null
}
},
onUndoDelete() {
const number = this.deletedNotes.length
this.deletedNotes.forEach(note => undoDeleteNote(note))
this.onRemoveUndoNotification()
if (number === 1) {
showSuccess(this.t('notes', 'Note recovered'))
} else {
showSuccess(this.n('notes', 'Recovered {number} note', 'Recovered {number} notes', number, { number }))
}
},
onUndoNotificationClosed() {
if (this.undoNotification) {
this.undoNotification = null
this.onRemoveUndoNotification()
}
},
onRemoveUndoNotification() {
this.deletedNotes = []
if (this.undoNotification) {
this.undoNotification.hideToast()
this.undoNotification = null
}
this.clearUndoTimer()
},
onClose(event) {
if (!this.notes.every(note => !note.unsaved)) {
event.preventDefault()
return this.t('notes', 'There are unsaved notes. Leaving the page will discard all changes!')
}
},
},
}
</script>
<style scoped lang="scss">
// Source for footer fix: https://github.com/nextcloud/server/blob/master/apps/files/src/views/Navigation.vue
.app-navigation-entry__settings {
height: auto !important;
overflow: hidden !important;
padding-top: 0 !important;
// Prevent shrinking or growing
flex: 0 0 auto;
padding-inline-end: 3px;
padding-bottom: 3px;
padding-inline-start: 3px;
margin: 0 3px;
}
</style>