Fix Schedule Helper UI (#13515)

This commit is contained in:
Zack Barett 2022-08-29 20:51:07 -05:00 committed by GitHub
parent 260855abbb
commit 8fd99fcb15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 21 deletions

View File

@ -71,7 +71,8 @@ export const formatTime24h = (dateObj: Date) =>
const formatTime24hMem = memoizeOne( const formatTime24hMem = memoizeOne(
() => () =>
new Intl.DateTimeFormat(undefined, { // en-GB to fix Chrome 24:59 to 0:59 https://stackoverflow.com/a/60898146
new Intl.DateTimeFormat("en-GB", {
hour: "numeric", hour: "numeric",
minute: "2-digit", minute: "2-digit",
hour12: false, hour12: false,

View File

@ -6,6 +6,7 @@ import interactionPlugin from "@fullcalendar/interaction";
import timeGridPlugin from "@fullcalendar/timegrid"; import timeGridPlugin from "@fullcalendar/timegrid";
// @ts-ignore // @ts-ignore
import timegridStyle from "@fullcalendar/timegrid/main.css"; import timegridStyle from "@fullcalendar/timegrid/main.css";
import { isSameDay } from "date-fns";
import { import {
css, css,
CSSResultGroup, CSSResultGroup,
@ -39,7 +40,6 @@ const defaultFullCalendarConfig: CalendarOptions = {
locales: allLocales, locales: allLocales,
firstDay: 1, firstDay: 1,
dayHeaderFormat: { weekday: "short", month: undefined, day: undefined }, dayHeaderFormat: { weekday: "short", month: undefined, day: undefined },
slotLabelFormat: { hour: "numeric", minute: undefined, meridiem: "narrow" },
}; };
@customElement("ha-schedule-form") @customElement("ha-schedule-form")
@ -168,10 +168,17 @@ class HaScheduleForm extends LitElement {
const config: CalendarOptions = { const config: CalendarOptions = {
...defaultFullCalendarConfig, ...defaultFullCalendarConfig,
locale: this.hass.language, locale: this.hass.language,
slotLabelFormat: {
hour: "numeric",
minute: undefined,
hour12: useAmPm(this.hass.locale),
meridiem: useAmPm(this.hass.locale) ? "narrow" : false,
},
eventTimeFormat: { eventTimeFormat: {
hour: useAmPm(this.hass.locale) ? "numeric" : "2-digit", hour: useAmPm(this.hass.locale) ? "numeric" : "2-digit",
minute: useAmPm(this.hass.locale) ? "numeric" : "2-digit", minute: undefined,
hour12: useAmPm(this.hass.locale), hour12: useAmPm(this.hass.locale),
meridiem: useAmPm(this.hass.locale) ? "narrow" : false,
}, },
}; };
@ -235,18 +242,17 @@ class HaScheduleForm extends LitElement {
private _handleSelect(info: { start: Date; end: Date }) { private _handleSelect(info: { start: Date; end: Date }) {
const { start, end } = info; const { start, end } = info;
if (start.getDay() !== end.getDay()) {
this.calendar!.unselect();
return;
}
const day = weekdays[start.getDay()]; const day = weekdays[start.getDay()];
const value = [...this[`_${day}`]]; const value = [...this[`_${day}`]];
const newValue = { ...this._item }; const newValue = { ...this._item };
const endFormatted = formatTime24h(end);
value.push({ value.push({
from: formatTime24h(start), from: formatTime24h(start),
to: formatTime24h(end), to:
isSameDay(start, end) || endFormatted === "0:00"
? "24:00"
: endFormatted,
}); });
newValue[day] = value; newValue[day] = value;
@ -254,45 +260,51 @@ class HaScheduleForm extends LitElement {
fireEvent(this, "value-changed", { fireEvent(this, "value-changed", {
value: newValue, value: newValue,
}); });
if (isSameDay(start, end)) {
this.calendar!.unselect();
}
} }
private _handleEventResize(info: any) { private _handleEventResize(info: any) {
const { id, start, end } = info.event; const { id, start, end } = info.event;
if (start.getDay() !== end.getDay()) {
info.revert();
return;
}
const [day, index] = id.split("-"); const [day, index] = id.split("-");
const value = this[`_${day}`][parseInt(index)]; const value = this[`_${day}`][parseInt(index)];
const newValue = { ...this._item }; const newValue = { ...this._item };
const endFormatted = formatTime24h(end);
newValue[day][index] = { newValue[day][index] = {
from: value.from, from: value.from,
to: formatTime24h(end), to:
isSameDay(start, end) || endFormatted === "0:00"
? "24:00"
: endFormatted,
}; };
fireEvent(this, "value-changed", { fireEvent(this, "value-changed", {
value: newValue, value: newValue,
}); });
if (isSameDay(start, end)) {
info.revert();
}
} }
private _handleEventDrop(info: any) { private _handleEventDrop(info: any) {
const { id, start, end } = info.event; const { id, start, end } = info.event;
if (start.getDay() !== end.getDay()) {
info.revert();
return;
}
const [day, index] = id.split("-"); const [day, index] = id.split("-");
const newDay = weekdays[start.getDay()]; const newDay = weekdays[start.getDay()];
const newValue = { ...this._item }; const newValue = { ...this._item };
const endFormatted = formatTime24h(end);
const event = { const event = {
from: formatTime24h(start), from: formatTime24h(start),
to: formatTime24h(end), to:
isSameDay(start, end) || endFormatted === "0:00"
? "24:00"
: endFormatted,
}; };
if (newDay === day) { if (newDay === day) {
@ -307,6 +319,10 @@ class HaScheduleForm extends LitElement {
fireEvent(this, "value-changed", { fireEvent(this, "value-changed", {
value: newValue, value: newValue,
}); });
if (isSameDay(start, end)) {
info.revert();
}
} }
private _handleEventClick(info: any) { private _handleEventClick(info: any) {
@ -367,6 +383,9 @@ class HaScheduleForm extends LitElement {
.fc-scroller { .fc-scroller {
overflow-x: visible !important; overflow-x: visible !important;
} }
.fc-v-event .fc-event-time {
white-space: inherit;
}
`, `,
]; ];
} }