@@ -30,6 +30,7 @@ export default {
3030 return {
3131 loading: false ,
3232 editor: null ,
33+ shouldAutotitle: true ,
3334 }
3435 },
3536
@@ -83,13 +84,19 @@ export default {
8384 }
8485 this ? .editor ? .destroy ()
8586 this .loading = true
87+ this .shouldAutotitle = undefined
8688 this .editor = (await window .OCA .Text .createEditor ({
8789 el: this .$refs .editor ,
8890 fileId: parseInt (this .noteId ),
8991 readOnly: false ,
9092 onUpdate : ({ markdown }) => {
9193 if (this .note ) {
92- this .onEdit ({ content: markdown, unsaved: true })
94+ const unsaved = !! (this .note ? .content && this .note .content !== markdown)
95+ if (this .shouldAutotitle === undefined ) {
96+ const title = this .getTitle (markdown)
97+ this .shouldAutotitle = this .isNewNote || (title !== ' ' && title === this .note .title )
98+ }
99+ this .onEdit ({ content: markdown, unsaved: unsaved})
93100 }
94101 },
95102 }))
@@ -108,9 +115,26 @@ export default {
108115 fileUpdated ({ fileid }) {
109116 if (this .note .id === fileid) {
110117 this .onEdit ({ unsaved: false })
111- queueCommand (fileid, ' autotitle' )
118+ if (this .shouldAutotitle ) {
119+ queueCommand (fileid, ' autotitle' )
120+ }
112121 }
113122 },
123+
124+ getTitle (content ) {
125+ const firstLine = content .split (' \n ' )[0 ] ?? ' '
126+ const title = firstLine
127+ // See NoteUtil::sanitisePath
128+ .replaceAll (/ ^ \s * [*+-] \s + / gmu , ' ' )
129+ .replaceAll (/ ^ [.\s ] + / gmu , ' ' )
130+ .replaceAll (/ \* | \| | \/ | \\ | :| "| '| <| >| \? / gmu , ' ' )
131+ // See NoteUtil::stripMarkdown
132+ .replaceAll (/ ^ #+ \s + (. *? )\s * #* $ / gmu , ' $1' )
133+ .replaceAll (/ ^ (=+ | -+ )$ / gmu , ' ' )
134+ .replaceAll (/ (\* + | _+ )(. *? )\\ 1/ gmu , ' $2' )
135+ .replaceAll (/ \s / gmu , ' ' )
136+ return title .length > 0 ? title : t (' notes' , ' New note' )
137+ },
114138 },
115139}
116140< / script>
0 commit comments