Browse Source

feat(nc-gui): date time field record render

pull/7611/head
DarkPhoenix2704 9 months ago
parent
commit
9ff87467de
  1. 98
      packages/nc-gui/components/smartsheet/calendar/DayView/DateField.vue
  2. 234
      packages/nc-gui/components/smartsheet/calendar/DayView/DateTimeField.vue
  3. 3
      packages/nc-gui/components/smartsheet/calendar/RecordCard.vue
  4. 11
      packages/nc-gui/components/smartsheet/calendar/index.vue

98
packages/nc-gui/components/smartsheet/calendar/DayView.vue → packages/nc-gui/components/smartsheet/calendar/DayView/DateField.vue

@ -1,5 +1,4 @@
<script lang="ts" setup>
import { UITypes } from 'nocodb-sdk'
import dayjs from 'dayjs'
import { type Row, computed, ref } from '#imports'
@ -13,14 +12,11 @@ const { isUIAllowed } = useRoles()
const displayField = computed(() => meta.value?.columns?.find((c) => c.pv && fields.value.includes(c)) ?? null)
const { selectedTime, selectedDate, calDataType, formattedData, formattedSideBarData, calendarRange, updateRowProperty } =
useCalendarViewStoreOrThrow()
const { selectedDate, formattedData, formattedSideBarData, calendarRange, updateRowProperty } = useCalendarViewStoreOrThrow()
const recordsAcrossAllRange = computed<Row[]>(() => {
let dayRecordCount = 0
const perRecordHeight = 40
const scheduleStart = dayjs(selectedDate.value).startOf('day')
const scheduleEnd = dayjs(selectedDate.value).endOf('day')
if (!calendarRange.value) return []
@ -39,8 +35,6 @@ const recordsAcrossAllRange = computed<Row[]>(() => {
const style: Partial<CSSStyleDeclaration> = {
top: `${(dayRecordCount - 1) * perRecordHeight}px`,
width: '100%',
// left: columnIndex === 0 && calDataType.value === UITypes.DateTime ? `calc(${left}% + 69px)` : `${left}%`,
}
let position = 'none'
@ -92,45 +86,8 @@ const recordsAcrossAllRange = computed<Row[]>(() => {
return recordsByRange
})
const hours = computed(() => {
const hours: Array<dayjs.Dayjs> = []
for (let i = 0; i < 24; i++) {
hours.push(
dayjs()
.hour(i)
.minute(0)
.second(0)
.millisecond(0)
.year(selectedDate.value.getFullYear() || dayjs().year())
.month(selectedDate.value.getMonth() || dayjs().month())
.date(selectedDate.value.getDate() || dayjs().date()),
)
}
return hours
})
const dragElement = ref<HTMLElement | null>(null)
const dragStart = (event: DragEvent, record: Row) => {
if (isUIAllowed('dataEdit')) return
dragElement.value = event.target as HTMLElement
dragElement.value.classList.add('hide')
dragElement.value.style.boxShadow = '0px 8px 8px -4px rgba(0, 0, 0, 0.04), 0px 20px 24px -4px rgba(0, 0, 0, 0.10)'
const eventRect = dragElement.value.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) => {
if (!isUIAllowed('dataEdit')) return
event.preventDefault()
@ -138,30 +95,18 @@ const dropEvent = (event: DragEvent) => {
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 - window.scrollY) / height
const percentX = (event.clientX - left - window.scrollX) / width
/*
const percentY = (event.clientY - top - initialClickOffsetY - window.scrollY) / height
const percentX = (event.clientX - left - initialClickOffsetX - window.scrollX) / width
*/
const fromCol = record.rowMeta.range?.fk_from_col
const toCol = record.rowMeta.range?.fk_to_col
if (!fromCol) return
const newStartDate = dayjs(selectedDate.value)
.startOf('day')
.add(percentY * 1440, 'minutes')
const newStartDate = dayjs(selectedDate.value).startOf('day')
let endDate
@ -230,44 +175,7 @@ const dropEvent = (event: DragEvent) => {
class="w-full relative h-[calc(100vh-10.8rem)] overflow-y-auto nc-scrollbar-md"
@drop="dropEvent"
>
<template v-if="calDataType === UITypes.DateTime">
<div
v-for="(hour, index) in hours"
:key="index"
:class="{
'!border-brand-500': hour.isSame(selectedTime),
}"
class="flex w-full min-h-20 relative border-1 group hover:bg-gray-50 border-white border-b-gray-100"
@click="selectedTime = hour.toDate()"
>
<div class="pt-2 px-4 text-xs text-gray-500 font-semibold h-20">
{{ dayjs(hour).format('H A') }}
</div>
<div></div>
<NcButton
:class="{
'!block': hour.isSame(selectedTime),
'!hidden': !hour.isSame(selectedTime),
}"
class="mr-4 my-auto ml-auto z-10 top-0 bottom-0 !group-hover:block absolute"
size="xsmall"
type="secondary"
@click="emit('new-record', { row: {} })"
>
<component :is="iconMap.plus" class="h-4 w-4 text-gray-700 transition-all" />
</NcButton>
</div>
</template>
<div
v-for="(record, rowIndex) in recordsAcrossAllRange"
:key="rowIndex"
:draggable="UITypes.DateTime === calDataType"
:style="record.rowMeta.style"
class="absolute mt-2"
@dragstart="dragStart($event, record)"
@dragover.prevent
>
<div v-for="(record, rowIndex) in recordsAcrossAllRange" :key="rowIndex" :style="record.rowMeta.style" class="absolute mt-2">
<LazySmartsheetRow :row="record">
<LazySmartsheetCalendarRecordCard
:date="dayjs(record.row[record.rowMeta.range!.fk_from_col.title!]).format('H:mm')"

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

@ -0,0 +1,234 @@
<script lang="ts" setup>
import { UITypes } from 'nocodb-sdk'
import dayjs from 'dayjs'
import { type Row, computed, ref } from '#imports'
const emit = defineEmits(['expand-record', 'new-record'])
const meta = inject(MetaInj, ref())
const fields = inject(FieldsInj, ref([]))
const container = ref()
const displayField = computed(() => meta.value?.columns?.find((c) => c.pv && fields.value.includes(c)) ?? null)
const { selectedDate, selectedTime, calDataType, formattedData, calendarRange } = useCalendarViewStoreOrThrow()
const hours = computed(() => {
const hours: Array<dayjs.Dayjs> = []
for (let i = 0; i < 24; i++) {
hours.push(
dayjs()
.hour(i)
.minute(0)
.second(0)
.millisecond(0)
.year(selectedDate.value.getFullYear() || dayjs().year())
.month(selectedDate.value.getMonth() || dayjs().month())
.date(selectedDate.value.getDate() || dayjs().date()),
)
}
return hours
})
function getRandomNumbers() {
const typedArray = new Uint8Array(10)
const randomValues = window.crypto.getRandomValues(typedArray)
return randomValues.join('')
}
const recordsAcrossAllRange = computed<Row[]>(() => {
const scheduleStart = dayjs(selectedDate.value).startOf('day')
const scheduleEnd = dayjs(selectedDate.value).endOf('day')
const overlaps = {}
const perRecordHeight = 40
if (!calendarRange.value) return []
let recordsByRange: Array<Row> = []
calendarRange.value.forEach((range) => {
const fromCol = range.fk_from_col
const endCol = range.fk_to_col
if (fromCol && endCol) {
for (const record of formattedData.value) {
const id = record.rowMeta.id ?? getRandomNumbers()
const startDate = dayjs(record.row[fromCol.title!])
const endDate = dayjs(record.row[endCol.title!])
if (!startDate.isValid()) continue
const topInPixels = (startDate.hour() + startDate.minute() / 60) * 80
const heightInPixels = Math.max((endDate.diff(startDate, 'minute') / 60) * 80, perRecordHeight)
const startHour = startDate.hour()
const endHour = endDate.hour()
let startMinutes = startDate.minute() + startHour * 60
const endMinutes = endDate.minute() + endHour * 60
while (startMinutes < endMinutes) {
if (!overlaps[startMinutes]) {
overlaps[startMinutes] = []
}
overlaps[startMinutes].push(id)
startMinutes += 15
}
const style: Partial<CSSStyleDeclaration> = {
top: `${topInPixels + 10}px`,
height: `${heightInPixels}px`,
}
let position = 'none'
const isSelectedDay = (date: dayjs.Dayjs) => date.isSame(selectedDate.value, 'day')
const isBeforeSelectedDay = (date: dayjs.Dayjs) => date.isBefore(selectedDate.value, 'day')
const isAfterSelectedDay = (date: dayjs.Dayjs) => date.isAfter(selectedDate.value, 'day')
if (isSelectedDay(startDate) && isSelectedDay(endDate)) {
position = 'rounded'
} else if (isBeforeSelectedDay(startDate) && isAfterSelectedDay(endDate)) {
position = 'none'
} else if (isSelectedDay(startDate) && isAfterSelectedDay(endDate)) {
position = 'leftRounded'
} else if (isBeforeSelectedDay(startDate) && isSelectedDay(endDate)) {
position = 'rightRounded'
} else {
position = 'none'
}
recordsByRange.push({
...record,
rowMeta: {
...record.rowMeta,
position,
style,
id,
range: range as any,
},
})
}
} else if (fromCol) {
for (const record of formattedData.value) {
const startDate = dayjs(record.row[fromCol.title!])
const scaleMin = (scheduleEnd - scheduleStart) / 60000
const startMinutes = Math.max((startDate - scheduleStart) / 60000, 0)
const endMinutes = Math.min((startDate - scheduleStart) / 60000, scaleMin)
const height = ((endMinutes - startMinutes) / scaleMin) * 100
const top = (startMinutes / scaleMin) * 100
recordsByRange.push({
...record,
rowMeta: {
...record.rowMeta,
range: range as any,
style: {
width: '100%',
left: `${50}px`,
top: `${top}%`,
},
position: 'rounded',
},
})
}
}
})
recordsByRange = recordsByRange.map((record) => {
let maxOverlaps = 1
let overlapIndex = 0
for (const minutes in overlaps) {
if (overlaps[minutes].includes(record.rowMeta.id)) {
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)
}
}
const width = 100 / maxOverlaps
const left = width * overlapIndex
record.rowMeta.style = {
...record.rowMeta.style,
left: left === 0 ? '50px' : `calc(${left}% + 30px)`,
width: `${width}%`,
}
return record
})
return recordsByRange
})
</script>
<template>
<div
v-if="recordsAcrossAllRange.length"
ref="container"
class="w-full relative h-[calc(100vh-10.8rem)] overflow-y-auto nc-scrollbar-md"
>
<div
v-for="(hour, index) in hours"
:key="index"
:class="{
'!border-brand-500': hour.isSame(selectedTime),
}"
class="flex w-full min-h-20 relative border-1 group hover:bg-gray-50 border-white border-b-gray-100"
@click="selectedTime = hour.toDate()"
>
<div class="pt-2 px-4 text-xs text-gray-500 font-semibold h-20">
{{ dayjs(hour).format('H A') }}
</div>
<div></div>
<NcButton
:class="{
'!block': hour.isSame(selectedTime),
'!hidden': !hour.isSame(selectedTime),
}"
class="mr-4 my-auto ml-auto z-10 top-0 bottom-0 !group-hover:block absolute"
size="xsmall"
type="secondary"
@click="emit('new-record', { row: {} })"
>
<component :is="iconMap.plus" class="h-4 w-4 text-gray-700 transition-all" />
</NcButton>
</div>
<div
v-for="(record, rowIndex) in recordsAcrossAllRange"
:key="rowIndex"
:draggable="UITypes.DateTime === calDataType"
:style="record.rowMeta.style"
class="absolute mt-2"
@dragstart="dragStart($event, record)"
@dragover.prevent
>
<LazySmartsheetRow :row="record">
<LazySmartsheetCalendarRecordCard
:date="dayjs(record.row[record.rowMeta.range!.fk_from_col.title!]).format('HH:mm')"
:name="record.row[displayField!.title!]"
:position="record.rowMeta.position"
:record="record"
:resize="false"
color="blue"
size="auto"
@click="emit('expand-record', record)"
/>
</LazySmartsheetRow>
</div>
</div>
<div v-else ref="container" class="w-full h-full flex text-md font-bold text-gray-500 items-center justify-center">
No Records in this day
</div>
</template>
<style lang="scss" scoped></style>

3
packages/nc-gui/components/smartsheet/calendar/RecordCard.vue

@ -6,7 +6,7 @@ interface Props {
color?: string
resize?: boolean
selected?: boolean
size?: 'small' | 'medium' | 'large'
size?: 'small' | 'medium' | 'large' | 'auto'
showDate?: boolean
position?: 'leftRounded' | 'rightRounded' | 'rounded' | 'none'
}
@ -31,6 +31,7 @@ const emit = defineEmits(['resize-start'])
'min-h-9': size === 'small',
'min-h-10': size === 'medium',
'min-h-12': size === 'large',
'h-full': size === 'auto',
'rounded-l-lg ml-3': position === 'leftRounded',
'rounded-r-lg mr-3': position === 'rightRounded',
'rounded-lg mx-3': position === 'rounded',

11
packages/nc-gui/components/smartsheet/calendar/index.vue

@ -1,5 +1,6 @@
<script lang="ts" setup>
import dayjs from 'dayjs'
import { UITypes } from 'nocodb-sdk'
import {
ActiveViewInj,
IsCalendarInj,
@ -39,6 +40,7 @@ provide(IsKanbanInj, ref(false))
provide(IsCalendarInj, ref(true))
const {
calDataType,
loadCalendarMeta,
loadCalendarData,
loadSidebarData,
@ -236,8 +238,13 @@ const headerText = computed(() => {
@expand-record="expandRecord"
@new-record="newRecord"
/>
<LazySmartsheetCalendarDayView
v-else-if="activeCalendarView === 'day'"
<LazySmartsheetCalendarDayViewDateField
v-else-if="activeCalendarView === 'day' && calDataType === UITypes.Date"
@expand-record="expandRecord"
@new-record="newRecord"
/>
<LazySmartsheetCalendarDayViewDateTimeField
v-else-if="activeCalendarView === 'day' && calDataType === UITypes.DateTime"
@expand-record="expandRecord"
@new-record="newRecord"
/>

Loading…
Cancel
Save