Skip to content

Commit 8e5e315

Browse files
authored
Merge pull request #1078 from nextcloud/bugfix/noid/autotitle
2 parents 9b20006 + 19e0a92 commit 8e5e315

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/components/Note.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22
<NoteRich v-if="isRichMode" :note-id="noteId" />
33
<NotePlain v-else-if="isPlainMode" :note-id="noteId" />
4+
<div v-else />
45
</template>
56
<script>
67
import NoteRich from './NoteRich.vue'
@@ -24,10 +25,10 @@ export default {
2425
2526
computed: {
2627
isRichMode() {
27-
return window.oc_appswebroots.text && store.state.app.settings.noteMode === 'rich'
28+
return window.oc_appswebroots.text && store.state.app?.settings?.noteMode === 'rich'
2829
},
2930
isPlainMode() {
30-
return store.state.app.settings.noteMode !== null && store.state.app.settings.noteMode !== 'rich'
31+
return store.state.app?.settings?.noteMode === 'edit' || store.state.app?.settings?.noteMode === 'preview'
3132
},
3233
},
3334
}

src/components/NoteRich.vue

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default {
3030
return {
3131
loading: false,
3232
editor: null,
33+
shouldAutotitle: true,
3334
}
3435
},
3536
@@ -48,6 +49,7 @@ export default {
4849
watch: {
4950
$route(to, from) {
5051
if (to.name !== from.name || to.params.noteId !== from.params.noteId) {
52+
this.onClose(from.params.noteId)
5153
this.fetchData()
5254
}
5355
},
@@ -83,13 +85,19 @@ export default {
8385
}
8486
this?.editor?.destroy()
8587
this.loading = true
88+
this.shouldAutotitle = undefined
8689
this.editor = (await window.OCA.Text.createEditor({
8790
el: this.$refs.editor,
8891
fileId: parseInt(this.noteId),
8992
readOnly: false,
9093
onUpdate: ({ markdown }) => {
9194
if (this.note) {
92-
this.onEdit({ content: markdown, unsaved: true })
95+
const unsaved = !!(this.note?.content && this.note.content !== markdown)
96+
if (this.shouldAutotitle === undefined) {
97+
const title = this.getTitle(markdown)
98+
this.shouldAutotitle = this.isNewNote || (title !== '' && title === this.note.title)
99+
}
100+
this.onEdit({ content: markdown, unsaved })
93101
}
94102
},
95103
}))
@@ -105,12 +113,37 @@ export default {
105113
})
106114
},
107115
116+
onClose(noteId) {
117+
const note = store.getters.getNote(parseInt(noteId))
118+
store.commit('updateNote', {
119+
...note,
120+
unsaved: false,
121+
})
122+
},
123+
108124
fileUpdated({ fileid }) {
109125
if (this.note.id === fileid) {
110126
this.onEdit({ unsaved: false })
111-
queueCommand(fileid, 'autotitle')
127+
if (this.shouldAutotitle) {
128+
queueCommand(fileid, 'autotitle')
129+
}
112130
}
113131
},
132+
133+
getTitle(content) {
134+
const firstLine = content.split('\n')[0] ?? ''
135+
const title = firstLine
136+
// See NoteUtil::sanitisePath
137+
.replaceAll(/^\s*[*+-]\s+/gmu, '')
138+
.replaceAll(/^[.\s]+/gmu, '')
139+
.replaceAll(/\*|\||\/|\\|:|"|'|<|>|\?/gmu, '')
140+
// See NoteUtil::stripMarkdown
141+
.replaceAll(/^#+\s+(.*?)\s*#*$/gmu, '$1')
142+
.replaceAll(/^(=+|-+)$/gmu, '')
143+
.replaceAll(/(\*+|_+)(.*?)\\1/gmu, '$2')
144+
.replaceAll(/\s/gmu, ' ')
145+
return title.length > 0 ? title : t('notes', 'New note')
146+
},
114147
},
115148
}
116149
</script>

0 commit comments

Comments
 (0)