2021-07-17 23:21:46 +02:00
|
|
|
<template>
|
|
|
|
<div class="task-add">
|
|
|
|
<div class="field is-grouped">
|
2021-08-13 21:47:15 +02:00
|
|
|
<p class="control has-icons-left is-expanded">
|
2021-09-26 20:22:28 +02:00
|
|
|
<textarea
|
2021-08-20 17:00:03 +02:00
|
|
|
:disabled="taskService.loading || null"
|
2021-07-17 23:21:46 +02:00
|
|
|
class="input"
|
|
|
|
:placeholder="$t('list.list.addPlaceholder')"
|
|
|
|
type="text"
|
|
|
|
v-focus
|
|
|
|
v-model="newTaskTitle"
|
|
|
|
ref="newTaskInput"
|
2021-09-26 20:22:28 +02:00
|
|
|
:style="{'height': `${textAreaHeight}px`}"
|
2021-07-17 23:21:46 +02:00
|
|
|
@keyup="errorMessage = ''"
|
2021-09-26 20:22:28 +02:00
|
|
|
@keydown.enter="handleEnter"
|
2021-07-17 23:21:46 +02:00
|
|
|
/>
|
|
|
|
<span class="icon is-small is-left">
|
|
|
|
<icon icon="tasks"/>
|
|
|
|
</span>
|
|
|
|
</p>
|
|
|
|
<p class="control">
|
|
|
|
<x-button
|
2021-08-20 17:00:03 +02:00
|
|
|
:disabled="newTaskTitle === '' || taskService.loading || null"
|
2021-07-17 23:21:46 +02:00
|
|
|
@click="addTask()"
|
|
|
|
icon="plus"
|
2021-08-13 21:47:15 +02:00
|
|
|
:loading="taskService.loading"
|
2021-07-17 23:21:46 +02:00
|
|
|
>
|
|
|
|
{{ $t('list.list.add') }}
|
|
|
|
</x-button>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<p class="help is-danger" v-if="errorMessage !== ''">
|
|
|
|
{{ errorMessage }}
|
|
|
|
</p>
|
|
|
|
<quick-add-magic v-if="errorMessage === ''"/>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import TaskService from '../../services/task'
|
|
|
|
import createTask from '@/components/tasks/mixins/createTask'
|
2021-07-25 15:27:15 +02:00
|
|
|
import QuickAddMagic from '@/components/tasks/partials/quick-add-magic.vue'
|
2021-07-17 23:21:46 +02:00
|
|
|
|
2021-09-26 20:22:28 +02:00
|
|
|
const INITIAL_SCROLL_HEIGHT = 40
|
|
|
|
|
|
|
|
const cleanupTitle = title => {
|
|
|
|
return title.replace(/^((\* |\+ |- )(\[ \] )?)/g, '')
|
|
|
|
}
|
|
|
|
|
2021-07-17 23:21:46 +02:00
|
|
|
export default {
|
|
|
|
name: 'add-task',
|
2021-08-23 21:18:12 +02:00
|
|
|
emits: ['taskAdded'],
|
2021-07-17 23:21:46 +02:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
newTaskTitle: '',
|
2021-09-08 11:59:38 +02:00
|
|
|
taskService: new TaskService(),
|
2021-07-17 23:21:46 +02:00
|
|
|
errorMessage: '',
|
2021-09-26 20:22:28 +02:00
|
|
|
textAreaHeight: INITIAL_SCROLL_HEIGHT,
|
2021-07-17 23:21:46 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
mixins: [
|
|
|
|
createTask,
|
|
|
|
],
|
|
|
|
components: {
|
|
|
|
QuickAddMagic,
|
|
|
|
},
|
2021-07-28 21:56:29 +02:00
|
|
|
props: {
|
|
|
|
defaultPosition: {
|
|
|
|
type: Number,
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
},
|
2021-09-26 20:22:28 +02:00
|
|
|
watch: {
|
|
|
|
newTaskTitle(newVal) {
|
|
|
|
let scrollHeight = this.$refs.newTaskInput.scrollHeight
|
|
|
|
if (scrollHeight < INITIAL_SCROLL_HEIGHT || newVal === '') {
|
|
|
|
scrollHeight = INITIAL_SCROLL_HEIGHT
|
|
|
|
}
|
|
|
|
|
|
|
|
this.textAreaHeight = scrollHeight
|
|
|
|
},
|
|
|
|
},
|
2021-07-17 23:21:46 +02:00
|
|
|
methods: {
|
|
|
|
addTask() {
|
|
|
|
if (this.newTaskTitle === '') {
|
|
|
|
this.errorMessage = this.$t('list.create.addTitleRequired')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.errorMessage = ''
|
|
|
|
|
2021-08-13 21:47:15 +02:00
|
|
|
if (this.taskService.loading) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-26 20:22:28 +02:00
|
|
|
const newTasks = []
|
|
|
|
this.newTaskTitle.split(/[\r\n]+/).forEach(t => {
|
2021-09-29 21:01:54 +02:00
|
|
|
const title = cleanupTitle(t)
|
|
|
|
if (title === '') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-26 20:22:28 +02:00
|
|
|
newTasks.push(
|
2021-09-29 21:01:54 +02:00
|
|
|
this.createNewTask(title, 0, this.$store.state.auth.settings.defaultListId, this.defaultPosition)
|
2021-09-26 20:22:28 +02:00
|
|
|
.then(task => {
|
|
|
|
this.$emit('taskAdded', task)
|
|
|
|
return task
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
Promise.all(newTasks)
|
|
|
|
.then(() => {
|
2021-07-17 23:21:46 +02:00
|
|
|
this.newTaskTitle = ''
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
if (e === 'NO_LIST') {
|
|
|
|
this.errorMessage = this.$t('list.create.addListRequired')
|
|
|
|
return
|
|
|
|
}
|
2021-08-25 12:28:29 +02:00
|
|
|
this.$message.error(e)
|
2021-07-17 23:21:46 +02:00
|
|
|
})
|
|
|
|
},
|
2021-09-26 20:22:28 +02:00
|
|
|
handleEnter(e) {
|
|
|
|
// when pressing shift + enter we want to continue as we normally would. Otherwise, we want to create
|
|
|
|
// the new task(s). The vue event modifier don't allow this, hence this method.
|
|
|
|
if (e.shiftKey) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault()
|
|
|
|
this.addTask()
|
|
|
|
},
|
2021-07-17 23:21:46 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.task-add {
|
|
|
|
margin-bottom: 0;
|
|
|
|
|
|
|
|
.button {
|
|
|
|
height: 2.5rem;
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 20:22:28 +02:00
|
|
|
|
|
|
|
.input, .textarea {
|
|
|
|
transition: border-color $transition;
|
|
|
|
}
|
2021-07-17 23:21:46 +02:00
|
|
|
</style>
|