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-10-13 21:08:29 +02:00
|
|
|
:style="{'height': `calc(${textAreaHeight}px - 2px + 1rem)`}"
|
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'
|
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-10-13 20:37:03 +02:00
|
|
|
const INPUT_BORDER_PX = 2
|
2021-10-13 21:08:29 +02:00
|
|
|
const LINE_HEIGHT = 1.5 // using getComputedStyles().lineHeight returns an (wrong) absolute pixel value, we need the factor to do calculations with it.
|
2021-09-26 20:22:28 +02:00
|
|
|
|
|
|
|
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-10-13 20:37:03 +02:00
|
|
|
textAreaHeight: null,
|
|
|
|
initialTextAreaHeight: null,
|
2021-07-17 23:21:46 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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) {
|
2021-10-13 21:08:29 +02:00
|
|
|
// Calculating the textarea height based on lines of input in it. That is more reliable when removing a
|
|
|
|
// line from the input.
|
|
|
|
const numberOfLines = newVal.split(/\r\n|\r|\n/).length
|
|
|
|
const fontSize = parseInt(window.getComputedStyle(this.$refs.newTaskInput, null).getPropertyValue('font-size'))
|
2021-09-26 20:22:28 +02:00
|
|
|
|
2021-10-13 21:08:29 +02:00
|
|
|
this.textAreaHeight = numberOfLines * fontSize * LINE_HEIGHT + INPUT_BORDER_PX
|
2021-09-26 20:22:28 +02:00
|
|
|
},
|
|
|
|
},
|
2021-10-13 20:37:03 +02:00
|
|
|
mounted() {
|
|
|
|
this.initialTextAreaHeight = this.$refs.newTaskInput.scrollHeight + INPUT_BORDER_PX
|
|
|
|
},
|
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-07 14:10:52 +02:00
|
|
|
this.$store.dispatch('tasks/createNewTask', {
|
|
|
|
title: this.newTaskTitle,
|
|
|
|
listId: this.$store.state.auth.settings.defaultListId,
|
|
|
|
position: 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>
|