try to keep the selected day when navigating the transaction calendar by month

This commit is contained in:
MaysWind
2025-06-01 01:10:27 +08:00
parent cde26b76b1
commit b4bff49104
3 changed files with 38 additions and 29 deletions
+16 -7
View File
@@ -884,18 +884,27 @@ export function getCombinedDateAndTimeValues(date: Date, timeValues: string[], i
return newDateTime;
}
export function getMonthFirstDayOrCurrentDayShortDate(unixTime: number): string {
export function getValidMonthDayOrCurrentDayShortDate(unixTime: number, currentShortDate: string): string {
const currentTime = moment();
let dateTime = moment.unix(unixTime);
let monthLastTime = moment.unix(getMonthLastUnixTimeBySpecifiedUnixTime(unixTime));
if (dateTime.year() === currentTime.year() && dateTime.month() === currentTime.month()) {
if (currentShortDate) {
const yearMonthDay = currentShortDate.split('-');
if (yearMonthDay.length === 3) {
const currentDay = parseInt(yearMonthDay[2]);
if (currentDay < monthLastTime.date()) {
return getShortDate(monthLastTime.set({ date: currentDay }));
}
}
}
if (monthLastTime.year() === currentTime.year() && monthLastTime.month() === currentTime.month()) {
return getShortDate(currentTime);
}
dateTime = dateTime.set({ hour: 0, minute: 0, second: 0, millisecond: 0 });
dateTime = dateTime.subtract(dateTime.date() - 1, 'days');
return getShortDate(dateTime);
return getShortDate(monthLastTime);
}
export function isDateRangeMatchFullYears(minTime: number, maxTime: number): boolean {
+12 -12
View File
@@ -698,7 +698,7 @@ import {
getDateRangeByBillingCycleDateType,
getRecentDateRangeIndex,
getFullMonthDateRange,
getMonthFirstDayOrCurrentDayShortDate
getValidMonthDayOrCurrentDayShortDate
} from '@/lib/datetime.ts';
import {
categoryTypeToTransactionType,
@@ -1123,7 +1123,7 @@ function init(initProps: TransactionListProps): void {
if (type) {
pageType.value = type.type;
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(query.value.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(query.value.minTime, currentCalendarDate.value);
if (pageType.value === TransactionListPageType.Calendar.type) {
const dateRange = getFullMonthDateRange(query.value.minTime, query.value.maxTime, firstDayOfWeek.value);
@@ -1136,7 +1136,7 @@ function init(initProps: TransactionListProps): void {
});
if (changed) {
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(query.value.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(query.value.minTime, currentCalendarDate.value);
updateUrlWhenChanged(changed);
return;
}
@@ -1219,7 +1219,7 @@ function reload(force: boolean, init: boolean): void {
function changePageType(type: number): void {
pageType.value = type;
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(query.value.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(query.value.minTime, currentCalendarDate.value);
if (pageType.value === TransactionListPageType.Calendar.type) {
const dateRange = getFullMonthDateRange(query.value.minTime, query.value.maxTime, firstDayOfWeek.value);
@@ -1230,7 +1230,7 @@ function changePageType(type: number): void {
maxTime: dateRange.maxTime,
minTime: dateRange.minTime
});
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(query.value.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(query.value.minTime, currentCalendarDate.value);
}
}
@@ -1269,12 +1269,12 @@ function changeDateFilter(dateRange: TimeRangeAndDateType | number | null): void
}
if (pageType.value === TransactionListPageType.Calendar.type) {
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(dateRange.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(dateRange.minTime, currentCalendarDate.value);
const fullMonthDateRange = getFullMonthDateRange(dateRange.minTime, dateRange.maxTime, firstDayOfWeek.value);
if (fullMonthDateRange) {
dateRange = fullMonthDateRange;
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(dateRange.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(dateRange.minTime, currentCalendarDate.value);
}
}
@@ -1303,14 +1303,14 @@ function changeCustomDateFilter(minTime: number, maxTime: number): void {
}
if (pageType.value === TransactionListPageType.Calendar.type) {
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(minTime, currentCalendarDate.value);
const dateRange = getFullMonthDateRange(minTime, maxTime, firstDayOfWeek.value);
if (dateRange) {
minTime = dateRange.minTime;
maxTime = dateRange.maxTime;
dateType = dateRange.dateType;
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(minTime, currentCalendarDate.value);
}
}
@@ -1339,7 +1339,7 @@ function changeCustomMonthDateFilter(yearMonth: string): void {
const dateType = getDateTypeByDateRange(minTime, maxTime, firstDayOfWeek.value, DateRangeScene.Normal);
if (pageType.value === TransactionListPageType.Calendar.type) {
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(minTime, currentCalendarDate.value);
}
if (query.value.dateType === dateType && query.value.maxTime === maxTime && query.value.minTime === minTime) {
@@ -1373,12 +1373,12 @@ function shiftDateRange(startTime: number, endTime: number, scale: number): void
}
if (pageType.value === TransactionListPageType.Calendar.type) {
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(newDateRange.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(newDateRange.minTime, currentCalendarDate.value);
const fullMonthDateRange = getFullMonthDateRange(newDateRange.minTime, newDateRange.maxTime, firstDayOfWeek.value);
if (fullMonthDateRange) {
newDateRange = fullMonthDateRange;
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(newDateRange.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(newDateRange.minTime, currentCalendarDate.value);
}
}
+10 -10
View File
@@ -642,7 +642,7 @@ import {
getDateRangeByDateType,
getDateRangeByBillingCycleDateType,
getFullMonthDateRange,
getMonthFirstDayOrCurrentDayShortDate
getValidMonthDayOrCurrentDayShortDate
} from '@/lib/datetime.ts';
import {
categoryTypeToTransactionType,
@@ -1034,7 +1034,7 @@ function loadMore(autoExpand: boolean): void {
function changePageType(type: number): void {
pageType.value = type;
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(query.value.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(query.value.minTime, currentCalendarDate.value);
if (pageType.value === TransactionListPageType.Calendar.type) {
const dateRange = getFullMonthDateRange(query.value.minTime, query.value.maxTime, firstDayOfWeek.value);
@@ -1047,7 +1047,7 @@ function changePageType(type: number): void {
});
if (changed) {
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(query.value.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(query.value.minTime, currentCalendarDate.value);
reload();
}
}
@@ -1089,12 +1089,12 @@ function changeDateFilter(dateType: number): void {
}
if (pageType.value === TransactionListPageType.Calendar.type) {
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(dateRange.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(dateRange.minTime, currentCalendarDate.value);
const fullMonthDateRange = getFullMonthDateRange(dateRange.minTime, dateRange.maxTime, firstDayOfWeek.value);
if (fullMonthDateRange) {
dateRange = fullMonthDateRange;
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(dateRange.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(dateRange.minTime, currentCalendarDate.value);
}
}
@@ -1123,14 +1123,14 @@ function changeCustomDateFilter(minTime: number, maxTime: number): void {
}
if (pageType.value === TransactionListPageType.Calendar.type) {
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(minTime, currentCalendarDate.value);
const dateRange = getFullMonthDateRange(minTime, maxTime, firstDayOfWeek.value);
if (dateRange) {
minTime = dateRange.minTime;
maxTime = dateRange.maxTime;
dateType = dateRange.dateType;
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(minTime, currentCalendarDate.value);
}
}
@@ -1157,7 +1157,7 @@ function changeCustomMonthDateFilter(yearMonth: string): void {
const dateType = getDateTypeByDateRange(minTime, maxTime, firstDayOfWeek.value, DateRangeScene.Normal);
if (pageType.value === TransactionListPageType.Calendar.type) {
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(minTime, currentCalendarDate.value);
}
const changed = transactionsStore.updateTransactionListFilter({
@@ -1189,12 +1189,12 @@ function shiftDateRange(minTime: number, maxTime: number, scale: number): void {
}
if (pageType.value === TransactionListPageType.Calendar.type) {
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(newDateRange.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(newDateRange.minTime, currentCalendarDate.value);
const fullMonthDateRange = getFullMonthDateRange(newDateRange.minTime, newDateRange.maxTime, firstDayOfWeek.value);
if (fullMonthDateRange) {
newDateRange = fullMonthDateRange;
currentCalendarDate.value = getMonthFirstDayOrCurrentDayShortDate(newDateRange.minTime);
currentCalendarDate.value = getValidMonthDayOrCurrentDayShortDate(newDateRange.minTime, currentCalendarDate.value);
}
}