Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
'url' => '/notes/undo',
'verb' => 'POST',
],
[
'name' => 'notes#autotitle',
'url' => '/notes/{id}/autotitle',
'verb' => 'PUT',
'requirements' => ['id' => '\d+'],
],
[
'name' => 'notes#update',
'url' => '/notes/{id}',
Expand Down
26 changes: 19 additions & 7 deletions lib/Controller/NotesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function index(int $pruneBefore = 0) : JSONResponse {
return $note->getData([ 'content' ]);
}
}, $data['notes']);
if ($lastViewedNote) {
if ($lastViewedNote && !$pruneBefore) {
// check if note exists
try {
$this->notesService->get($userId, $lastViewedNote);
Expand Down Expand Up @@ -191,14 +191,26 @@ public function undo(
/**
* @NoAdminRequired
*/
public function update(int $id, string $content, bool $autotitle) : JSONResponse {
return $this->helper->handleErrorResponse(function () use ($id, $content, $autotitle) {
public function autotitle(int $id) : JSONResponse {
return $this->helper->handleErrorResponse(function () use ($id) {
$note = $this->notesService->get($this->helper->getUID(), $id);
$note->setContent($content);
if ($autotitle) {
$title = $this->notesService->getTitleFromContent($content);
$note->setTitle($title);
$oldTitle = $note->getTitle();
$newTitle = $this->notesService->getTitleFromContent($note->getContent());
if ($oldTitle !== $newTitle) {
$note->setTitle($newTitle);
}
return $note->getTitle();
});
}


/**
* @NoAdminRequired
*/
public function update(int $id, string $content) : JSONResponse {
return $this->helper->handleErrorResponse(function () use ($id, $content) {
$note = $this->notesService->get($this->helper->getUID(), $id);
$note->setContent($content);
return $note->getData();
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ export default {
if (availableNotes.length > 0) {
this.routeToNote(availableNotes[0].id)
} else {
this.$router.push({ name: 'welcome' })
if (this.$route.name !== 'welcome') {
this.$router.push({ name: 'welcome' })
}
}
},

Expand Down
14 changes: 13 additions & 1 deletion src/NotesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const createNote = category => {

function _updateNote(note) {
return axios
.put(url('/notes/' + note.id), { content: note.content, autotitle: note.autotitle })
.put(url('/notes/' + note.id), { content: note.content })
.then(response => {
const updated = response.data
note.saveError = false
Expand All @@ -186,6 +186,18 @@ function _updateNote(note) {
})
}

export const autotitleNote = noteId => {
return axios
.put(url('/notes/' + noteId + '/autotitle'))
.then((response) => {
store.commit('setNoteAttribute', { noteId: noteId, attribute: 'title', value: response.data })
})
.catch(err => {
console.error(err)
handleSyncError(t('notes', 'Updating title for note {id} has failed.', { id: noteId }), err)
})
}

export const undoDeleteNote = (note) => {
return axios
.post(url('/notes/undo'), note)
Expand Down
47 changes: 31 additions & 16 deletions src/components/Note.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,12 @@
>
{{ t('notes', 'Details') }}
</ActionButton>
<ActionButton v-if="!preview"
icon="icon-toggle"
v-tooltip.left="t('notes', 'CTRL + /')"
@click="onTogglePreview"
>
{{ t('notes', 'Preview') }}
</ActionButton>
<ActionButton v-else
icon="icon-rename"
<ActionButton
v-tooltip.left="t('notes', 'CTRL + /')"
:icon="preview ? 'icon-rename' : 'icon-toggle'"
@click="onTogglePreview"
>
{{ t('notes', 'Edit') }}
{{ preview ? t('notes', 'Edit') : t('notes', 'Preview') }}
</ActionButton>
<ActionButton
icon="icon-fullscreen"
Expand Down Expand Up @@ -63,7 +56,8 @@ import {
import { showError } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'

import { fetchNote, refreshNote, saveNote, saveNoteManually, routeIsNewNote } from '../NotesService'
import { config } from '../config'
import { fetchNote, refreshNote, saveNote, saveNoteManually, autotitleNote, routeIsNewNote } from '../NotesService'
import TheEditor from './EditorEasyMDE'
import ThePreview from './EditorMarkdownIt'
import store from '../store'
Expand Down Expand Up @@ -99,6 +93,7 @@ export default {
preview: false,
actionsOpen: false,
autosaveTimer: null,
autotitleTimer: null,
refreshTimer: null,
etag: null,
}
Expand All @@ -111,6 +106,9 @@ export default {
title() {
return this.note ? this.note.title : ''
},
isNewNote() {
return routeIsNewNote(this.$route)
},
isManualSave() {
return store.state.app.isManualSave
},
Expand Down Expand Up @@ -240,7 +238,7 @@ export default {
this.refreshTimer = setTimeout(() => {
this.refreshTimer = null
this.refreshNote()
}, 10000)
}, config.interval.note.refresh * 1000)
},

refreshNote() {
Expand All @@ -260,18 +258,36 @@ export default {
...this.note,
content: newContent,
unsaved: true,
autotitle: routeIsNewNote(this.$route),
}
store.commit('updateNote', note)
this.$forceUpdate()

// queue auto saving note content
if (this.autosaveTimer === null) {
this.autosaveTimer = setTimeout(() => {
this.autosaveTimer = null
saveNote(note.id)
}, 2000)
}, config.interval.note.autosave * 1000)
}

// (re-) start auto refresh timer
// TODO should be after save is finished
this.startRefreshTimer()

// stop old autotitle timer
if (this.autotitleTimer !== null) {
clearTimeout(this.autotitleTimer)
this.autotitleTimer = null
}
// start autotitle timer if note is new
if (this.isNewNote) {
this.autotitleTimer = setTimeout(() => {
this.autotitleTimer = null
if (this.isNewNote) {
autotitleNote(note.id)
}
}, config.interval.note.autotitle * 1000)
}
}
},

Expand All @@ -293,9 +309,8 @@ export default {
onManualSave() {
const note = {
...this.note,
autotitle: routeIsNewNote(this.$route),
}
store.commit('add', note)
store.commit('updateNote', note)
saveNoteManually(this.note.id)
},
},
Expand Down
9 changes: 9 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const config = {
interval: {
note: {
autosave: 1,
autotitle: 2,
refresh: 10,
},
},
}
1 change: 0 additions & 1 deletion src/store/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ const mutations = {
// don't update meta-data over full data
if (updated.content !== undefined || note.content === undefined) {
note.content = updated.content
Vue.set(note, 'autotitle', updated.autotitle)
Vue.set(note, 'unsaved', updated.unsaved)
Vue.set(note, 'error', updated.error)
Vue.set(note, 'errorMessage', updated.errorMessage)
Expand Down