feat: 🎸 add enableWeekNote func

This commit is contained in:
Jin 2025-04-01 16:40:18 +02:00
parent c43cca6c22
commit 22f1441c4e
4 changed files with 61 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import type { EventData } from "../../components/app_context.js";
import dayjs, { Dayjs } from "dayjs"; import dayjs, { Dayjs } from "dayjs";
import utc from "dayjs/plugin/utc.js"; import utc from "dayjs/plugin/utc.js";
import isSameOrAfter from "dayjs/plugin/isSameOrAfter.js"; import isSameOrAfter from "dayjs/plugin/isSameOrAfter.js";
import type BAttribute from "../../../../becca/entities/battribute.js";
import "../../../stylesheets/calendar.css"; import "../../../stylesheets/calendar.css";
dayjs.extend(utc); dayjs.extend(utc);
@ -93,6 +94,7 @@ export default class CalendarWidget extends RightDropdownButtonWidget {
private activeDate: Dayjs | null = null; private activeDate: Dayjs | null = null;
private todaysDate!: Dayjs; private todaysDate!: Dayjs;
private date!: Dayjs; private date!: Dayjs;
private weekNoteEnable: boolean = false;
constructor(title: string = "", icon: string = "") { constructor(title: string = "", icon: string = "") {
super(title, icon, DROPDOWN_TPL); super(title, icon, DROPDOWN_TPL);
@ -168,6 +170,27 @@ export default class CalendarWidget extends RightDropdownButtonWidget {
ev.stopPropagation(); ev.stopPropagation();
}); });
this.$dropdownContent.on("click", ".calendar-week-number", async (ev) => {
if (!this.weekNoteEnable) {
return;
}
const week = $(ev.target).closest(".calendar-week-number").attr("data-calendar-week-number");
if (week) {
const note = await dateNoteService.getWeekNote(week);
if (note) {
appContext.tabManager.getActiveContext()?.setNote(note.noteId);
this.dropdown?.hide();
} else {
toastService.showError(t("calendar.cannot_find_week_note"));
}
}
ev.stopPropagation();
});
// Handle click events for the entire calendar widget // Handle click events for the entire calendar widget
this.$dropdownContent.on("click", (e) => { this.$dropdownContent.on("click", (e) => {
const $target = $(e.target); const $target = $(e.target);
@ -186,6 +209,19 @@ export default class CalendarWidget extends RightDropdownButtonWidget {
}); });
} }
private async getWeekNoteEnable() {
const noteId = await server.get<string[]>(`search/${encodeURIComponent('#calendarRoot')}`);
const noteAttributes = await server.get<BAttribute[]>(`notes/${noteId}/attributes`);
for (const attribute of noteAttributes) {
if (attribute.name === 'enableWeekNote') {
this.weekNoteEnable = true;
return
}
}
this.weekNoteEnable = false;
}
manageFirstDayOfWeek() { manageFirstDayOfWeek() {
this.firstDayOfWeek = options.getInt("firstDayOfWeek") || 0; this.firstDayOfWeek = options.getInt("firstDayOfWeek") || 0;
@ -272,6 +308,7 @@ export default class CalendarWidget extends RightDropdownButtonWidget {
} }
async dropdownShown() { async dropdownShown() {
await this.getWeekNoteEnable();
this.init(appContext.tabManager.getActiveContextNote()?.getOwnedLabelValue("dateNote") ?? null); this.init(appContext.tabManager.getActiveContextNote()?.getOwnedLabelValue("dateNote") ?? null);
} }
@ -308,11 +345,18 @@ export default class CalendarWidget extends RightDropdownButtonWidget {
} }
createWeekNumber(weekNumber: number) { createWeekNumber(weekNumber: number) {
const weekNumberText = String(weekNumber); const weekNoteId = this.date.local().format('YYYY-') + 'W' + String(weekNumber).padStart(2, '0');
const $newWeekNumber = $("<a>").addClass("calendar-date calendar-week-number").attr("data-calendar-week-number", 'W' + weekNumberText.padStart(2, '0'));
const $weekNumber = $("<span>").html(weekNumberText); let $newWeekNumber;
if (this.weekNoteEnable) {
// Utilize the hover effect of calendar-date
$newWeekNumber = $("<a>").addClass("calendar-date");
} else {
$newWeekNumber = $("<span>").addClass("calendar-week-number-disabled");
}
$newWeekNumber.addClass("calendar-week-number").attr("data-calendar-week-number", weekNoteId);
$newWeekNumber.append($("<span>").html(String(weekNumber)));
$newWeekNumber.append($weekNumber);
return $newWeekNumber; return $newWeekNumber;
} }

View File

@ -109,6 +109,17 @@
z-index: 2; z-index: 2;
} }
.calendar-dropdown-widget .calendar-week-number-disabled {
align-items: center;
color: var(--main-text-color);
display: flex;
flex-direction: column;
flex: 0 0 12.5%;
max-width: 12.5%;
padding: 0.4rem 0;
font-size: 120%;
}
.calendar-dropdown-widget .calendar-date { .calendar-dropdown-widget .calendar-date {
align-items: center; align-items: center;
color: var(--main-text-color); color: var(--main-text-color);

View File

@ -589,6 +589,7 @@
"sat": "六", "sat": "六",
"sun": "日", "sun": "日",
"cannot_find_day_note": "无法找到日记", "cannot_find_day_note": "无法找到日记",
"cannot_find_week_note": "无法找到周记",
"january": "一月", "january": "一月",
"febuary": "二月", "febuary": "二月",
"march": "三月", "march": "三月",

View File

@ -589,6 +589,7 @@
"sat": "Sat", "sat": "Sat",
"sun": "Sun", "sun": "Sun",
"cannot_find_day_note": "Cannot find day note", "cannot_find_day_note": "Cannot find day note",
"cannot_find_week_note": "Cannot find week note",
"january": "January", "january": "January",
"febuary": "February", "febuary": "February",
"march": "March", "march": "March",