Skip to content

Commit

Permalink
feat(lib): handle textarea tab / shift tab events (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac committed Jul 27, 2020
1 parent 3252f39 commit 67b0da7
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/templates/nuxt-content.dev.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<template>
<div :class="['nuxt-content-container', { 'is-editing': isEditing}]">
<div :class="['nuxt-content-container', { 'is-editing': isEditing }]">
<textarea
v-show="isEditing"
v-model="file"
ref="textarea"
class="nuxt-content-editor"
@keyup.stop="onType"
@keydown.tab.exact.prevent="onTabRight"
@keydown.tab.shift.prevent="onTabLeft"
@blur="toggleEdit"
/>
<nuxt-content-dev
Expand Down Expand Up @@ -96,7 +98,27 @@ export default {
onType () {
const el = this.$refs.textarea
el.style.height = el.scrollHeight + 'px';
el.style.height = el.scrollHeight + 'px'
},
onTabRight (event) {
let text = this.file,
originalSelectionStart = event.target.selectionStart,
textStart = text.slice(0, originalSelectionStart),
textEnd = text.slice(originalSelectionStart)
this.file = `${textStart}\t${textEnd}`
event.target.value = this.file // required to make the cursor stay in place.
event.target.selectionEnd = event.target.selectionStart = originalSelectionStart + 1
},
onTabLeft (event) {
let text = this.file,
originalSelectionStart = event.target.selectionStart,
textStart = text.slice(0, originalSelectionStart),
textEnd = text.slice(originalSelectionStart)
this.file = `${textStart.replace(/\t$/, '')}${textEnd}`
event.target.value = this.file // required to make the cursor stay in place.
event.target.selectionEnd = event.target.selectionStart = originalSelectionStart - 1
}
}
}
Expand Down

0 comments on commit 67b0da7

Please sign in to comment.