From 9ad388bd796182784af225a73e076a1d4ee38449 Mon Sep 17 00:00:00 2001 From: DarkPhoenix2704 Date: Tue, 20 Feb 2024 07:16:04 +0000 Subject: [PATCH] feat(nc-gui): week view drag and drop --- .../smartsheet/calendar/WeekView.vue | 84 ++++++++++++++++++- .../composables/useCalendarViewStore.ts | 46 ++++++++-- 2 files changed, 121 insertions(+), 9 deletions(-) diff --git a/packages/nc-gui/components/smartsheet/calendar/WeekView.vue b/packages/nc-gui/components/smartsheet/calendar/WeekView.vue index 7e14f46e2b..142507a805 100644 --- a/packages/nc-gui/components/smartsheet/calendar/WeekView.vue +++ b/packages/nc-gui/components/smartsheet/calendar/WeekView.vue @@ -5,12 +5,15 @@ import type { Row } from '~/lib' const emits = defineEmits(['expand-record']) -const { selectedDateRange, formattedData, calendarRange, selectedDate, displayField, calDataType } = useCalendarViewStoreOrThrow() +const { selectedDateRange, formattedData, calendarRange, selectedDate, displayField, calDataType, updateRowProperty } = + useCalendarViewStoreOrThrow() const container = ref(null) const { width: containerWidth } = useElementSize(container) +const meta = inject(MetaInj, ref()) + const weekDates = computed(() => { const startOfWeek = new Date(selectedDateRange.value.start) const endOfWeek = new Date(selectedDateRange.value.end) @@ -180,6 +183,81 @@ const calendarData = computed(() => { return recordsInRange }) + +const dragStart = (event: DragEvent, record: Row) => { + const eventRect = (event.target as HTMLElement).getBoundingClientRect() + const initialClickOffsetX = event.clientX - eventRect.left + const initialClickOffsetY = event.clientY - eventRect.top + + event.dataTransfer?.setData( + 'text/plain', + JSON.stringify({ + record, + initialClickOffsetY, + initialClickOffsetX, + }), + ) +} + +const dropEvent = (event: DragEvent) => { + event.preventDefault() + const data = event.dataTransfer?.getData('text/plain') + if (data) { + const { + record, + initialClickOffsetY, + initialClickOffsetX, + }: { + record: Row + initialClickOffsetY: number + initialClickOffsetX: number + } = JSON.parse(data) + const { top, height, width, left } = container.value.getBoundingClientRect() + + const percentY = (event.clientY - top - initialClickOffsetY - window.scrollY) / height + + const percentX = (event.clientX - left - initialClickOffsetX - window.scrollX) / width + + const day = Math.floor(percentX * 7) + + const newStartDate = dayjs(selectedDateRange.value.start).add(day, 'day') + + let endDate + + const newRow = { + ...record, + row: { + ...record.row, + [record.rowMeta.range.fk_from_col.title]: dayjs(newStartDate).format('YYYY-MM-DD'), + }, + } + + const updateProperty = [record.rowMeta.range.fk_from_col.title] + + if (record.rowMeta.range.fk_to_col) { + const diffDays = dayjs(record.row[record.rowMeta.range.fk_to_col.title]).diff( + record.row[record.rowMeta.range.fk_from_col.title], + 'day', + ) + endDate = dayjs(newStartDate).add(diffDays, 'day') + newRow.row[record.rowMeta.range.fk_to_col.title] = dayjs(endDate).format('YYYY-MM-DD') + updateProperty.push(record.rowMeta.range.fk_to_col.title) + } + + if (!newRow) return + + updateRowProperty(newRow, updateProperty, false) + + formattedData.value = formattedData.value.map((r) => { + const pk = extractPkFromRow(r.row, meta.value.columns) + + if (pk === extractPkFromRow(newRow.row, meta.value.columns)) { + return newRow + } + return r + }) + } +}