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')"
|
2021-11-02 19:11:24 +01:00
|
|
|
cols="1"
|
2021-07-17 23:21:46 +02:00
|
|
|
v-focus
|
|
|
|
v-model="newTaskTitle"
|
|
|
|
ref="newTaskInput"
|
2021-11-02 19:11:24 +01:00
|
|
|
:style="{
|
|
|
|
'minHeight': `${initialTextAreaHeight}px`,
|
|
|
|
'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-11-02 19:11:24 +01:00
|
|
|
// Calculating the textarea height based on lines of input in it.
|
|
|
|
// That is more reliable when removing a line from the input.
|
2021-10-13 21:08:29 +02:00
|
|
|
const numberOfLines = newVal.split(/\r\n|\r|\n/).length
|
2021-11-02 19:11:24 +01:00
|
|
|
const fontSize = parseFloat(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: {
|
2021-10-11 19:37:20 +02:00
|
|
|
async addTask() {
|
2021-07-17 23:21:46 +02:00
|
|
|
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-10-11 19:37:20 +02:00
|
|
|
const newTasks = this.newTaskTitle.split(/[\r\n]+/).map(async t => {
|
2021-09-29 21:01:54 +02:00
|
|
|
const title = cleanupTitle(t)
|
|
|
|
if (title === '') {
|
|
|
|
return
|
|
|
|
}
|
2021-10-26 21:29:58 +02:00
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
const task = await this.$store.dispatch('tasks/createNewTask', {
|
2021-10-26 21:29:58 +02:00
|
|
|
title,
|
2021-10-11 19:37:20 +02:00
|
|
|
listId: this.$store.state.auth.settings.defaultListId,
|
|
|
|
position: this.defaultPosition,
|
|
|
|
})
|
|
|
|
this.$emit('taskAdded', task)
|
|
|
|
return task
|
2021-09-26 20:22:28 +02:00
|
|
|
})
|
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
try {
|
|
|
|
await Promise.all(newTasks)
|
|
|
|
this.newTaskTitle = ''
|
2021-10-26 21:29:58 +02:00
|
|
|
} catch (e) {
|
2021-10-11 19:37:20 +02:00
|
|
|
if (e.message === 'NO_LIST') {
|
|
|
|
this.errorMessage = this.$t('list.create.addListRequired')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
throw 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-11-02 19:11:24 +01:00
|
|
|
|
|
|
|
.input {
|
|
|
|
resize: vertical;
|
|
|
|
}
|
2021-07-17 23:21:46 +02:00
|
|
|
</style>
|