Skip to content

Commit

Permalink
Merge pull request #1078 from nextcloud/bugfix/noid/autotitle
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusknorr authored Jul 17, 2023
2 parents 9b20006 + 19e0a92 commit 8e5e315
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
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

0 comments on commit 8e5e315

Please sign in to comment.