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
5 changes: 3 additions & 2 deletions src/components/Note.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<NoteRich v-if="isRichMode" :note-id="noteId" />
<NotePlain v-else-if="isPlainMode" :note-id="noteId" />
<div v-else />
</template>
<script>
import NoteRich from './NoteRich.vue'
Expand All @@ -24,10 +25,10 @@ export default {

computed: {
isRichMode() {
return window.oc_appswebroots.text && store.state.app.settings.noteMode === 'rich'
return window.oc_appswebroots.text && store.state.app?.settings?.noteMode === 'rich'
},
isPlainMode() {
return store.state.app.settings.noteMode !== null && store.state.app.settings.noteMode !== 'rich'
return store.state.app?.settings?.noteMode === 'edit' || store.state.app?.settings?.noteMode === 'preview'
},
},
}
Expand Down
37 changes: 35 additions & 2 deletions src/components/NoteRich.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default {
return {
loading: false,
editor: null,
shouldAutotitle: true,
}
},

Expand All @@ -48,6 +49,7 @@ export default {
watch: {
$route(to, from) {
if (to.name !== from.name || to.params.noteId !== from.params.noteId) {
this.onClose(from.params.noteId)
this.fetchData()
}
},
Expand Down Expand Up @@ -83,13 +85,19 @@ export default {
}
this?.editor?.destroy()
this.loading = true
this.shouldAutotitle = undefined
this.editor = (await window.OCA.Text.createEditor({
el: this.$refs.editor,
fileId: parseInt(this.noteId),
readOnly: false,
onUpdate: ({ markdown }) => {
if (this.note) {
this.onEdit({ content: markdown, unsaved: true })
const unsaved = !!(this.note?.content && this.note.content !== markdown)
if (this.shouldAutotitle === undefined) {
const title = this.getTitle(markdown)
this.shouldAutotitle = this.isNewNote || (title !== '' && title === this.note.title)
}
this.onEdit({ content: markdown, unsaved })
}
},
}))
Expand All @@ -105,12 +113,37 @@ export default {
})
},

onClose(noteId) {
const note = store.getters.getNote(parseInt(noteId))
store.commit('updateNote', {
...note,
unsaved: false,
})
},

fileUpdated({ fileid }) {
if (this.note.id === fileid) {
this.onEdit({ unsaved: false })
queueCommand(fileid, 'autotitle')
if (this.shouldAutotitle) {
queueCommand(fileid, 'autotitle')
}
}
},

getTitle(content) {
const firstLine = content.split('\n')[0] ?? ''
const title = firstLine
// See NoteUtil::sanitisePath
.replaceAll(/^\s*[*+-]\s+/gmu, '')
.replaceAll(/^[.\s]+/gmu, '')
.replaceAll(/\*|\||\/|\\|:|"|'|<|>|\?/gmu, '')
// See NoteUtil::stripMarkdown
.replaceAll(/^#+\s+(.*?)\s*#*$/gmu, '$1')
.replaceAll(/^(=+|-+)$/gmu, '')
.replaceAll(/(\*+|_+)(.*?)\\1/gmu, '$2')
.replaceAll(/\s/gmu, ' ')
return title.length > 0 ? title : t('notes', 'New note')
},
},
}
</script>
Expand Down