Skip to content

Commit 9866e81

Browse files
committedMay 28, 2022
handle enter similarly to the nextcloud notes android application
Signed-off-by: Felix Nüsse <[email protected]>
1 parent e8260e8 commit 9866e81

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed
 

‎src/components/EditorEasyMDE.vue

+35-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<button class="button" @click="makeStrikethrough">
1414
<span class="icon-strike"></span>
1515
</button>
16-
<button class="button" @click="makeMonospace">
16+
<button class="button" @click="makeH1">
1717
<span class="icon-h1"></span>
1818
</button>
1919
<button class="button" @click="insertLink">
@@ -53,6 +53,7 @@ import {
5353
ActionButton,
5454
} from '@nextcloud/vue'
5555
import EmojiPicker from '@nextcloud/vue/dist/Components/EmojiPicker'
56+
import * as CodeMirror from "codemirror";
5657
5758
export default {
5859
name: 'EditorEasyMDE',
@@ -128,13 +129,15 @@ export default {
128129
this.mde.codemirror.addKeyMap({
129130
Home: 'goLineLeft',
130131
End: 'goLineRight',
132+
Enter: this.onEnter,
131133
})
132134
133135
// pass event for changes
134136
this.mde.codemirror.on('change', () => {
135137
this.$emit('input', this.mde.value())
136138
})
137139
140+
138141
// listen for click/touch events in order to toggle checkboxes
139142
document.querySelectorAll('.CodeMirror-code').forEach((codeElement) => {
140143
codeElement.addEventListener('mousedown', this.onClickCodeElement)
@@ -282,6 +285,11 @@ export default {
282285
this.insertTextInfront('- [ ] ')
283286
},
284287
288+
289+
makeH1() {
290+
this.insertTextInfront('# ')
291+
},
292+
285293
makeBold() {
286294
this.surroundText('**')
287295
},
@@ -312,6 +320,32 @@ export default {
312320
313321
reverseString(string) {
314322
return string.split('').reverse().join('')
323+
},
324+
325+
onEnter() {
326+
const cursor = this.mde.codemirror.getCursor();
327+
const text = this.mde.codemirror.getValue().split('\n')[cursor.line].trim()
328+
329+
if(text.startsWith("- [ ]") && text !== "- [ ]") {
330+
this.insertText("\n")
331+
this.insertText("- [ ] ")
332+
this.mde.codemirror.setCursor(this.mde.codemirror.getCursor())
333+
return;
334+
}
335+
if(text.startsWith("- [x]")) {
336+
this.insertText("\n")
337+
this.insertText("- [ ] ")
338+
this.mde.codemirror.setCursor(this.mde.codemirror.getCursor())
339+
return;
340+
}
341+
if(text.startsWith("-") && text !== "-" && !text.startsWith("- [")) {
342+
this.insertText("\n")
343+
this.insertText(" - ")
344+
this.mde.codemirror.setCursor(this.mde.codemirror.getCursor())
345+
return;
346+
}
347+
348+
return CodeMirror.Pass
315349
}
316350
},
317351
}

0 commit comments

Comments
 (0)
Please sign in to comment.