Browse Source

fix(nc-gui): datetime overlap

pull/7611/head
DarkPhoenix2704 9 months ago
parent
commit
97da99155b
  1. 196
      packages/nc-gui/components/smartsheet/calendar/DayView/DateTimeField.vue
  2. 2
      packages/nc-gui/components/smartsheet/calendar/SideMenu.vue

196
packages/nc-gui/components/smartsheet/calendar/DayView/DateTimeField.vue

@ -53,11 +53,19 @@ const recordsAcrossAllRange = computed<Row[]>(() => {
const endCol = range.fk_to_col const endCol = range.fk_to_col
if (fromCol && endCol) { if (fromCol && endCol) {
for (const record of formattedData.value) { for (const record of formattedData.value) {
const id = record.rowMeta.id ?? getRandomNumbers() const id = getRandomNumbers()
const startDate = dayjs(record.row[fromCol.title!]) let startDate = dayjs(record.row[fromCol.title!])
const endDate = dayjs(record.row[endCol.title!]) let endDate = dayjs(record.row[endCol.title!])
if (!startDate.isValid() || startDate.isAfter(endDate)) continue
if (!startDate.isValid()) continue if (startDate.isBefore(scheduleStart)) {
startDate = scheduleStart
}
if (endDate.isAfter(scheduleEnd)) {
endDate = scheduleEnd
}
const topInPixels = (startDate.hour() + startDate.minute() / 60) * 80 const topInPixels = (startDate.hour() + startDate.minute() / 60) * 80
const heightInPixels = Math.max((endDate.diff(startDate, 'minute') / 60) * 80, perRecordHeight) const heightInPixels = Math.max((endDate.diff(startDate, 'minute') / 60) * 80, perRecordHeight)
@ -74,12 +82,14 @@ const recordsAcrossAllRange = computed<Row[]>(() => {
overlaps[startMinutes] = [] overlaps[startMinutes] = []
} }
overlaps[startMinutes].push(id) overlaps[startMinutes].push(id)
startMinutes += 15 startMinutes += 10
} }
const finalTopInPixels = topInPixels + startHour + 1
const style: Partial<CSSStyleDeclaration> = { const style: Partial<CSSStyleDeclaration> = {
top: `${topInPixels + 10}px`, top: `${finalTopInPixels}px`,
height: `${heightInPixels}px`, height: `${heightInPixels - 5}px`,
} }
let position = 'none' let position = 'none'
@ -112,14 +122,29 @@ const recordsAcrossAllRange = computed<Row[]>(() => {
} }
} else if (fromCol) { } else if (fromCol) {
for (const record of formattedData.value) { for (const record of formattedData.value) {
const id = getRandomNumbers()
const startDate = dayjs(record.row[fromCol.title!]) const startDate = dayjs(record.row[fromCol.title!])
const scaleMin = (scheduleEnd - scheduleStart) / 60000 const endDate = dayjs(record.row[fromCol.title!]).add(1, 'hour')
const startHour = startDate.hour()
const endHour = endDate.hour()
const startMinutes = Math.max((startDate - scheduleStart) / 60000, 0) let startMinutes = startDate.minute() + startHour * 60
const endMinutes = Math.min((startDate - scheduleStart) / 60000, scaleMin) const endMinutes = endDate.minute() + endHour * 60
const height = ((endMinutes - startMinutes) / scaleMin) * 100 while (startMinutes < endMinutes) {
const top = (startMinutes / scaleMin) * 100 if (!overlaps[startMinutes]) {
overlaps[startMinutes] = []
}
overlaps[startMinutes].push(id)
startMinutes += 10
}
const topInPixels = (startDate.hour() + startDate.minute() / 60) * 80
const heightInPixels = Math.max((endDate.diff(startDate, 'minute') / 60) * 80, perRecordHeight)
const finalTopInPixels = topInPixels + startHour
recordsByRange.push({ recordsByRange.push({
...record, ...record,
@ -127,10 +152,10 @@ const recordsAcrossAllRange = computed<Row[]>(() => {
...record.rowMeta, ...record.rowMeta,
range: range as any, range: range as any,
style: { style: {
width: '100%', top: `${finalTopInPixels}px`,
left: `${50}px`, height: `${heightInPixels - 5}px`,
top: `${top}%`,
}, },
id,
position: 'rounded', position: 'rounded',
}, },
}) })
@ -144,23 +169,21 @@ const recordsAcrossAllRange = computed<Row[]>(() => {
for (const minutes in overlaps) { for (const minutes in overlaps) {
if (overlaps[minutes].includes(record.rowMeta.id)) { if (overlaps[minutes].includes(record.rowMeta.id)) {
maxOverlaps = Math.max(maxOverlaps, overlaps[minutes].length) maxOverlaps = Math.max(maxOverlaps, overlaps[minutes].length)
console.log(
record.row[displayField.value.title!],
maxOverlaps,
overlaps[minutes],
overlaps[minutes].indexOf(record.rowMeta.id),
)
overlapIndex = Math.max(overlaps[minutes].indexOf(record.rowMeta.id), overlapIndex) overlapIndex = Math.max(overlaps[minutes].indexOf(record.rowMeta.id), overlapIndex)
} }
} }
const width = 100 / maxOverlaps const spacing = 1
const left = width * overlapIndex const widthPerRecord = (100 - spacing * (maxOverlaps - 1)) / maxOverlaps
const leftPerRecord = (widthPerRecord + spacing) * overlapIndex
console.log('leftPerRecord', leftPerRecord, 'widthPerRecord', widthPerRecord)
record.rowMeta.style = { record.rowMeta.style = {
...record.rowMeta.style, ...record.rowMeta.style,
left: left === 0 ? '50px' : `calc(${left}% + 30px)`, left: `${leftPerRecord}%`,
width: `${width}%`, width: `calc(${widthPerRecord}%)`,
} }
return record return record
}) })
@ -173,7 +196,7 @@ const recordsAcrossAllRange = computed<Row[]>(() => {
<div <div
v-if="recordsAcrossAllRange.length" v-if="recordsAcrossAllRange.length"
ref="container" ref="container"
class="w-full relative h-[calc(100vh-10.8rem)] overflow-y-auto nc-scrollbar-md" class="w-full relative h-[calc(100vh-10rem)] overflow-y-auto nc-scrollbar-md"
> >
<div <div
v-for="(hour, index) in hours" v-for="(hour, index) in hours"
@ -188,41 +211,112 @@ const recordsAcrossAllRange = computed<Row[]>(() => {
{{ dayjs(hour).format('H A') }} {{ dayjs(hour).format('H A') }}
</div> </div>
<div></div> <div></div>
<NcDropdown
v-if="calendarRange.length > 1"
:class="{
'!block': hour.isSame(selectedTime),
'!hidden': !hour.isSame(selectedTime),
}"
auto-close
>
<NcButton
class="!group-hover:block mr-4 my-auto ml-auto z-10 top-0 bottom-0 !group-hover:block absolute"
size="xsmall"
type="secondary"
>
<component :is="iconMap.plus" class="h-4 w-4" />
</NcButton>
<template #overlay>
<NcMenu class="w-64">
<NcMenuItem> Select date field to add </NcMenuItem>
<NcMenuItem
v-for="(range, index) in calendarRange"
:key="index"
class="text-gray-800 font-semibold text-sm"
@click="
() => {
let record = {
row: {
[range.fk_from_col.title]: hour.format('YYYY-MM-DD HH:mm:ssZ'),
},
}
if (range.fk_to_col) {
record = {
row: {
...record.row,
[range.fk_to_col.title]: hour.add(1, 'hour').format('YYYY-MM-DD HH:mm:ssZ'),
},
}
}
emit('new-record', record)
}
"
>
<div class="flex items-center gap-1">
<LazySmartsheetHeaderCellIcon :column-meta="range.fk_from_col" />
<span class="ml-1">{{ range.fk_from_col!.title! }}</span>
</div>
</NcMenuItem>
</NcMenu>
</template>
</NcDropdown>
<NcButton <NcButton
v-else
:class="{ :class="{
'!block': hour.isSame(selectedTime), '!block': hour.isSame(selectedTime),
'!hidden': !hour.isSame(selectedTime), '!hidden': !hour.isSame(selectedTime),
}" }"
class="mr-4 my-auto ml-auto z-10 top-0 bottom-0 !group-hover:block absolute" class="!group-hover:block mr-4 my-auto ml-auto z-10 top-0 bottom-0 !group-hover:block absolute"
size="xsmall" size="xsmall"
type="secondary" type="secondary"
@click="emit('new-record', { row: {} })" @click="
() => {
let record = {
row: {
[calendarRange[0].fk_from_col.title]: hour.format('YYYY-MM-DD HH:mm:ssZ'),
},
}
if (calendarRange[0].fk_to_col) {
record = {
row: {
...record.row,
[calendarRange[0].fk_to_col.title]: hour.add(1, 'hour').format('YYYY-MM-DD HH:mm:ssZ'),
},
}
}
emit('new-record', record)
}
"
> >
<component :is="iconMap.plus" class="h-4 w-4 text-gray-700 transition-all" /> <component :is="iconMap.plus" class="h-4 w-4" />
</NcButton> </NcButton>
</div> </div>
<div class="absolute inset-0">
<div <div class="relative !ml-[50px]">
v-for="(record, rowIndex) in recordsAcrossAllRange" <div
:key="rowIndex" v-for="(record, rowIndex) in recordsAcrossAllRange"
:draggable="UITypes.DateTime === calDataType" :key="rowIndex"
:style="record.rowMeta.style" :draggable="UITypes.DateTime === calDataType"
class="absolute mt-2" :style="record.rowMeta.style"
@dragstart="dragStart($event, record)" class="absolute"
@dragover.prevent @dragstart="dragStart($event, record)"
> @dragover.prevent
<LazySmartsheetRow :row="record"> >
<LazySmartsheetCalendarRecordCard <LazySmartsheetRow :row="record">
:date="dayjs(record.row[record.rowMeta.range!.fk_from_col.title!]).format('HH:mm')" <LazySmartsheetCalendarRecordCard
:name="record.row[displayField!.title!]" :date="dayjs(record.row[record.rowMeta.range!.fk_from_col.title!]).format('HH:mm')"
:position="record.rowMeta.position" :name="record.row[displayField!.title!]"
:record="record" :position="record.rowMeta.position"
:resize="false" :record="record"
color="blue" :resize="false"
size="auto" color="blue"
@click="emit('expand-record', record)" size="auto"
/> @click="emit('expand-record', record)"
</LazySmartsheetRow> />
</LazySmartsheetRow>
</div>
</div>
</div> </div>
</div> </div>

2
packages/nc-gui/components/smartsheet/calendar/SideMenu.vue

@ -285,7 +285,7 @@ const sideBarListScrollHandle = useDebounceFn(async (e: Event) => {
@scroll="sideBarListScrollHandle" @scroll="sideBarListScrollHandle"
> >
<NcButton <NcButton
v-if="isUIAllowed('dataEdit')" v-if="isUIAllowed('dataEdit') && props.visible"
class="!absolute right-5 bottom-5 !h-12 !w-12" class="!absolute right-5 bottom-5 !h-12 !w-12"
type="secondary" type="secondary"
@click="emit('new-record', { row: {} })" @click="emit('new-record', { row: {} })"

Loading…
Cancel
Save