@@ -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