Skip to content

Commit 0c3bf7b

Browse files
committed
feat: UTC support for current time
1 parent 31ea8f5 commit 0c3bf7b

File tree

9 files changed

+52
-6
lines changed

9 files changed

+52
-6
lines changed

docs/.vitepress/theme/components/AdvancedGanttDemo.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const enableMinutes = ref(false)
2727
const currentTime = ref(true)
2828
const currentTimeLabel = ref('Now')
2929
const locale = ref('en')
30+
const utc = ref(false)
31+
3032
3133
// Display Configuration
3234
const hideTimeaxis = ref(false)
@@ -442,6 +444,12 @@ const formattedEventLog = computed(() => {
442444
>
443445
</label>
444446
</div>
447+
<div class="setting-item">
448+
<label>
449+
UTC Current time:
450+
<input type="checkbox" v-model="utc">
451+
</label>
452+
</div>
445453
</div>
446454
</div>
447455

@@ -744,6 +752,7 @@ const formattedEventLog = computed(() => {
744752
:milestones="milestones"
745753
:enableConnectionCreation="enableConnectionCreation"
746754
:enableConnectionDeletion="enableConnectionDeletion"
755+
:utc="utc"
747756
@click-bar="handleEvent($event, 'Bar Click')"
748757
@drag-bar="handleEvent($event, 'Bar Drag')"
749758
@sort="handleEvent($event, 'Sort Change')"

docs/.vitepress/theme/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import isSameOrAfter from "dayjs/plugin/isSameOrAfter.js"
88
import isBetween from "dayjs/plugin/isBetween.js"
99
import weekOfYear from "dayjs/plugin/weekOfYear"
1010
import dayOfYear from "dayjs/plugin/dayOfYear.js"
11+
import utc from "dayjs/plugin/utc"
1112

1213

1314
export function extendDayjs() {
@@ -17,6 +18,7 @@ export function extendDayjs() {
1718
dayjs.extend(weekOfYear)
1819
dayjs.extend(isoWeek)
1920
dayjs.extend(dayOfYear)
21+
dayjs.extend(utc)
2022
}
2123

2224
const GanttDemo = defineClientComponent(() => {

docs/components/g-gantt-chart.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Here's a minimal example of using the GGanttChart component:
7676
| defaultProgressResizable | `boolean` | `true` | Enable progress adjustment through dragging |
7777
| enableConnectionCreation | `boolean` | `false` | Enable the possibility to draw connections |
7878
| enableConnectionDelete | `boolean` | `false` | Enable the possibility to delete connections |
79+
| utc | `boolean` | `false` | Set the current time position based on UTC |
7980

8081

8182
### Events
@@ -105,6 +106,7 @@ Here's a minimal example of using the GGanttChart component:
105106
| connection-delete | `{ sourceBar: GanttBarObject, targetBar: GanttBarObject, e: MouseEvent }` | Delete a connection |
106107

107108

109+
108110
### Slots
109111

110112
| Slot Name | Props | Description |

src/components/GGanttChart.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ const props = withDefaults(defineProps<GGanttChartProps>(), {
124124
showProgress: true,
125125
defaultProgressResizable: true,
126126
enableConnectionCreation: false,
127-
enableConnectionDeletion: false
127+
enableConnectionDeletion: false,
128+
utc: false
128129
})
129130
130131
// Events

src/components/GGanttCurrentTime.vue

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref } from "vue"
2+
import { computed, ref } from "vue"
33
import useTimePositionMapping from "../composables/useTimePositionMapping"
44
import dayjs from "dayjs"
55
import provideConfig from "../provider/provideConfig"
@@ -8,15 +8,23 @@ import { useIntervalFn } from "@vueuse/core"
88
99
const { mapTimeToPosition } = useTimePositionMapping()
1010
const currentMoment = ref(dayjs())
11-
const { colors, dateFormat, currentTimeLabel } = provideConfig()
11+
const { colors, dateFormat, currentTimeLabel, utc } = provideConfig()
1212
const xDist = ref()
1313
1414
const loopTime = () => {
15-
currentMoment.value = dayjs()
15+
const now = utc.value ? dayjs().utc() : dayjs()
16+
currentMoment.value = now
1617
const format = dateFormat.value || "YYYY-MM-DD HH:mm:ss"
1718
xDist.value = mapTimeToPosition(dayjs(currentMoment.value, format).format(format))
1819
}
1920
useIntervalFn(loopTime, 1000)
21+
22+
const currentTimeDisplay = computed(() => {
23+
if (utc.value) {
24+
return `${currentTimeLabel.value} (UTC)`
25+
}
26+
return currentTimeLabel.value
27+
})
2028
</script>
2129

2230
<template>
@@ -34,7 +42,7 @@ useIntervalFn(loopTime, 1000)
3442
/>
3543
<span class="g-grid-current-time-text" :style="{ color: colors.markerCurrentTime }">
3644
<slot name="current-time-label">
37-
{{ currentTimeLabel }}
45+
{{ currentTimeDisplay }}
3846
</slot>
3947
</span>
4048
</div>

src/hy-vue-gantt.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import advancedFormat from "dayjs/plugin/advancedFormat"
99
import customParseFormat from "dayjs/plugin/customParseFormat.js"
1010
import dayOfYear from "dayjs/plugin/dayOfYear.js"
1111
import localizedFormat from "dayjs/plugin/localizedFormat"
12+
import utc from "dayjs/plugin/utc"
1213

1314
import GGanttChart from "./components/GGanttChart.vue"
1415
import GGanttRow from "./components/GGanttRow.vue"
@@ -103,6 +104,7 @@ export function extendDayjs() {
103104
dayjs.extend(advancedFormat)
104105
dayjs.extend(dayOfYear)
105106
dayjs.extend(localizedFormat)
107+
dayjs.extend(utc)
106108
}
107109

108110
export type {

src/types/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export interface GGanttChartProps {
6565
showLabel?: boolean
6666
showProgress?: boolean
6767
defaultProgressResizable?: boolean
68+
utc?: boolean
6869
}
6970

7071
export type GGanttChartConfig = ToRefs<Required<GGanttChartProps>> & {

tests/components/GGanttCurrentTime.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ vi.mock("../composables/useTimePositionMapping", () => ({
99
})
1010
}))
1111

12+
vi.mock("dayjs", () => {
13+
const mockDayjs = vi.fn((input) => ({
14+
format: vi.fn().mockReturnValue(input || "2024-01-01"),
15+
diff: vi.fn().mockReturnValue(60),
16+
add: vi.fn().mockImplementation((value, unit) => mockDayjs("2024-01-02")),
17+
subtract: vi.fn().mockImplementation((value, unit) => mockDayjs("2023-12-31")),
18+
locale: vi.fn().mockReturnValue(mockDayjs),
19+
utc: vi.fn()
20+
}))
21+
22+
// Add static methods and properties that dayjs uses
23+
mockDayjs.extend = vi.fn()
24+
mockDayjs.locale = vi.fn()
25+
26+
return {
27+
default: mockDayjs,
28+
__esModule: true
29+
}
30+
})
31+
1232
describe("GGanttCurrentTime", () => {
1333
beforeEach(() => {
1434
mockMapTimeToPosition.mockClear()

tests/vitestSetup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ beforeAll(() => {
9090
locale: ref("en"),
9191
sortable: ref(true),
9292
enableRowDragAndDrop: ref(true),
93-
showLabel: ref(true)
93+
showLabel: ref(true),
94+
utc: ref(false)
9495
},
9596
[EMIT_BAR_EVENT_KEY]: () => {},
9697
[BAR_CONTAINER_KEY]: ref(barContainerElement),

0 commit comments

Comments
 (0)