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-11-30 21:20:40 +01:00
|
|
|
:disabled="taskService.loading || undefined"
|
|
|
|
class="add-task-textarea input"
|
2021-07-17 23:21:46 +02:00
|
|
|
:placeholder="$t('list.list.addPlaceholder')"
|
2021-11-30 21:20:40 +01:00
|
|
|
rows="1"
|
2021-07-17 23:21:46 +02:00
|
|
|
v-focus
|
|
|
|
v-model="newTaskTitle"
|
|
|
|
ref="newTaskInput"
|
|
|
|
@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-11-30 21:20:40 +01:00
|
|
|
class="add-task-button"
|
|
|
|
:disabled="newTaskTitle === '' || taskService.loading || undefined"
|
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>
|
2021-11-30 21:20:40 +01:00
|
|
|
<quick-add-magic v-else />
|
2021-07-17 23:21:46 +02:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2021-11-30 21:20:40 +01:00
|
|
|
<script setup lang="ts">
|
|
|
|
import {ref, watch, unref, shallowReactive} from 'vue'
|
|
|
|
import {useI18n} from 'vue-i18n'
|
|
|
|
import {useStore} from 'vuex'
|
|
|
|
import { tryOnMounted, debouncedWatch, useWindowSize, MaybeRef } from '@vueuse/core'
|
|
|
|
|
2021-07-17 23:21:46 +02:00
|
|
|
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-11-30 21:20:40 +01:00
|
|
|
function cleanupTitle(title: string) {
|
2021-09-26 20:22:28 +02:00
|
|
|
return title.replace(/^((\* |\+ |- )(\[ \] )?)/g, '')
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:20:40 +01:00
|
|
|
function useAutoHeightTextarea(value: MaybeRef<string>) {
|
|
|
|
const textarea = ref<HTMLInputElement>()
|
|
|
|
const minHeight = ref(0)
|
|
|
|
|
|
|
|
// adapted from https://github.com/LeaVerou/stretchy/blob/47f5f065c733029acccb755cae793009645809e2/src/stretchy.js#L34
|
|
|
|
function resize(textareaEl: HTMLInputElement|undefined) {
|
|
|
|
if (!textareaEl) return
|
|
|
|
|
|
|
|
let empty
|
|
|
|
|
|
|
|
// the value here is the the attribute value
|
|
|
|
if (!textareaEl.value && textareaEl.placeholder) {
|
|
|
|
empty = true
|
|
|
|
textareaEl.value = textareaEl.placeholder
|
2021-07-17 23:21:46 +02:00
|
|
|
}
|
2021-11-30 21:20:40 +01:00
|
|
|
|
|
|
|
const cs = getComputedStyle(textareaEl)
|
|
|
|
|
|
|
|
textareaEl.style.minHeight = ''
|
|
|
|
textareaEl.style.height = '0'
|
|
|
|
const offset = textareaEl.offsetHeight - parseFloat(cs.paddingTop) - parseFloat(cs.paddingBottom)
|
|
|
|
const height = textareaEl.scrollHeight + offset + 'px'
|
|
|
|
|
|
|
|
textareaEl.style.height = height
|
|
|
|
|
|
|
|
// calculate min-height for the first time
|
|
|
|
if (!minHeight.value) {
|
|
|
|
minHeight.value = parseFloat(height)
|
|
|
|
}
|
|
|
|
|
|
|
|
textareaEl.style.minHeight = minHeight.value.toString()
|
|
|
|
|
|
|
|
|
|
|
|
if (empty) {
|
|
|
|
textareaEl.value = ''
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tryOnMounted(() => {
|
|
|
|
if (textarea.value) {
|
|
|
|
// we don't want scrollbars
|
|
|
|
textarea.value.style.overflowY = 'hidden'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const { width: windowWidth } = useWindowSize()
|
|
|
|
|
|
|
|
debouncedWatch(
|
|
|
|
windowWidth,
|
|
|
|
() => resize(textarea.value),
|
|
|
|
{ debounce: 200 },
|
|
|
|
)
|
|
|
|
|
|
|
|
// It is not possible to get notified of a change of the value attribute of a textarea without workarounds (setTimeout)
|
|
|
|
// So instead we watch the value that we bound to it.
|
|
|
|
watch(
|
|
|
|
() => [textarea.value, unref(value)],
|
|
|
|
() => resize(textarea.value),
|
|
|
|
{
|
|
|
|
immediate: true, // calculate initial size
|
|
|
|
flush: 'post', // resize after value change is rendered to DOM
|
2021-09-26 20:22:28 +02:00
|
|
|
},
|
2021-11-30 21:20:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return textarea
|
|
|
|
}
|
|
|
|
|
|
|
|
const emit = defineEmits(['taskAdded'])
|
|
|
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
defaultPosition: {
|
|
|
|
type: Number,
|
|
|
|
required: false,
|
2021-07-17 23:21:46 +02:00
|
|
|
},
|
2021-11-30 21:20:40 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const taskService = shallowReactive(new TaskService())
|
|
|
|
const errorMessage = ref('')
|
|
|
|
|
|
|
|
const newTaskTitle = ref('')
|
|
|
|
const newTaskInput = useAutoHeightTextarea(newTaskTitle)
|
|
|
|
|
|
|
|
const { t } = useI18n()
|
|
|
|
const store = useStore()
|
|
|
|
|
|
|
|
async function addTask() {
|
|
|
|
if (newTaskTitle.value === '') {
|
|
|
|
errorMessage.value = t('list.create.addTitleRequired')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errorMessage.value = ''
|
|
|
|
|
|
|
|
if (taskService.loading) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const taskTitleBackup = newTaskTitle.value
|
|
|
|
const newTasks = newTaskTitle.value.split(/[\r\n]+/).map(async uncleanedTitle => {
|
|
|
|
const title = cleanupTitle(uncleanedTitle)
|
|
|
|
if (title === '') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const task = await store.dispatch('tasks/createNewTask', {
|
|
|
|
title,
|
|
|
|
listId: store.state.auth.settings.defaultListId,
|
|
|
|
position: props.defaultPosition,
|
|
|
|
})
|
|
|
|
emit('taskAdded', task)
|
|
|
|
return task
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
newTaskTitle.value = ''
|
|
|
|
await Promise.all(newTasks)
|
|
|
|
} catch (e: any) {
|
|
|
|
newTaskTitle.value = taskTitleBackup
|
|
|
|
if (e?.message === 'NO_LIST') {
|
|
|
|
errorMessage.value = t('list.create.addListRequired')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleEnter(e: KeyboardEvent) {
|
|
|
|
// 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()
|
|
|
|
addTask()
|
2021-07-17 23:21:46 +02:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.task-add {
|
|
|
|
margin-bottom: 0;
|
|
|
|
}
|
2021-09-26 20:22:28 +02:00
|
|
|
|
2021-11-30 21:20:40 +01:00
|
|
|
.add-task-button {
|
|
|
|
height: 2.5rem;
|
2021-09-26 20:22:28 +02:00
|
|
|
}
|
2021-11-30 21:20:40 +01:00
|
|
|
.add-task-textarea {
|
|
|
|
transition: border-color $transition;
|
|
|
|
resize: none;
|
2021-11-02 19:11:24 +01:00
|
|
|
}
|
2021-07-17 23:21:46 +02:00
|
|
|
</style>
|