fix multiday events and add start for monthView

This commit is contained in:
Finnley
2024-01-08 22:07:09 +01:00
parent 77c357a0a1
commit 8754572c7a

91
main.js
View File

@@ -4,6 +4,8 @@ const ItemView = obsidian.ItemView;
const getIcon = obsidian.getIcon;
const DateFormat = {
months: ["januarie", "februarie", "maart", "april", "mei", "juni", "juli", "augustis", "september", "oktober", "november", "december"],
daysLong: ["Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "Zondag"],
week: "y[0-9]{2}w[0-9]{2}",
days: ["zo", "ma", "di", "wo", "do", "vrij", "za"],
time: "[0-9]{2}:[0-9]{2}[ap]m",
@@ -290,6 +292,9 @@ class CalendarView extends ItemView
container.empty();
this.eventsEls = {};
// mount view
this.mountView = conatiner.createEl("div", { cls: "frcal__mountView" });
// month
this.monthEl = container.createEl("div", { cls: "frcal__month" });
@@ -346,6 +351,61 @@ class CalendarView extends ItemView
this.addEvents();
this.updateWeek();
}
openMountView(startMonth = null, endMonth = null)
{
this.mountView.empty();
if (startMonth == null)
{
startMounth = moment().substract(1, "years");
}
if (endMonth == null)
{
endMounth = moment().add(1, "years");
}
if (startMonth.day() != 1)
{
if (startMonth.day() == 0)
{
startMonth.day(-6);
}
else
{
startMonth.day(1);
}
}
if (endMonth.day() != 1)
{
if (endMonth.day() == 0)
{
endMonth.day(-6);
}
else
{
endMonth.day(1);
}
}
let lastMonth = startMonth.year() * 12 + startMonth.month() - 1;
let table = this.monthView.createEl('table');
for (let week = moment(startMonth); week < endMonth; week.add(1, "weeks"))
{
let row = table.createEl("tr");
if (lastMonth != week.year() * 12 + week.month())
{
lastMonth += 1;
let span = moment(week).add(1, "months").subtract(1, "day").isoWeek() - week.isoWeek();
row.createEl("td", { attr: { rowspan: span } }).innerText = DateFormat.month[week.month()];
}
row.createEl('td', { class: 'frcal__week' }).innerText = week.isoWeek();
}
}
async onClose()
{
@@ -394,9 +454,8 @@ class CalendarView extends ItemView
updateWeek()
{
let months = ["januarie", "februarie", "maart", "april", "mei", "juni", "juli", "augustis", "september", "oktober", "november", "december"];
let monthF = parseSingleDate(this.week + " ma 00:00am",).month();
this.mountFirstSpanEl.innerText = (monthF+1).toString() + " - " + months[monthF];
this.mountFirstSpanEl.innerText = (monthF+1).toString() + " - " + DateFormat.months[monthF];
this.weekNumEl.innerHTML = this.week.substring(0, 3) + '<br/>' + this.week.substring(3, 6);
@@ -404,7 +463,7 @@ class CalendarView extends ItemView
let monthS = zoDate.month();
if (monthF != monthS)
{
this.mountSecondSpanEl.innerText = (monthS+1).toString() + " - " + months[monthS];
this.mountSecondSpanEl.innerText = (monthS+1).toString() + " - " + DateFormat.months[monthS];
let dow = zoDate.date();
@@ -419,11 +478,10 @@ class CalendarView extends ItemView
}
let days = ["Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "Zondag"];
for (let day = 0; day < 7; day++)
{
let date = parseSingleDate(this.week + " " + DateFormat.days[(day + 1) % 7] + " 12:00pm");
this.dayHeads[day].innerHTML = days[day] + "<br/>" + date.date().toString();
this.dayHeads[day].innerHTML = DateFormat.daysLong[day] + "<br/>" + date.date().toString();
}
this.updateEvents();
@@ -454,10 +512,9 @@ class CalendarView extends ItemView
{
event = this.plugin.data[event];
let date = parseDate(event['date'])
if (
(date[0] > start && date[0] < end)
|| (date[1] > start && date[1] < end)
)
let startInWeek = (date[0] > start && date[0] < end);
let endInWeek = (date[1] > start && date[1] < end);
if (startInWeek && endInWeek)
{
let res = [...event.date.matchAll("(?:" + DateFormat.days.join('|') + ")")];
if ((res.length <= 1) || (res[0][0] == res[1][0]))
@@ -467,15 +524,15 @@ class CalendarView extends ItemView
else
{
let date_split = event.date.split('-');
let start = DateFormat.days.indexOf(res[0][0]), end = DateFormat.days.indexOf(res[1][0]);
let day_start = DateFormat.days.indexOf(res[0][0]), day_end = DateFormat.days.indexOf(res[1][0]);
let part = JSON.parse(JSON.stringify(event));
// first day
part.date = date_split[0].match(DateFormat.week)[0] + ' ' + DateFormat.days[start] + ' ' + date_split[0].match(DateFormat.time)[0];
part.date += '-' + date_split[1].match(DateFormat.week)[0] + ' ' + DateFormat.days[start] + ' 23:59pm';
part.date = date_split[0].match(DateFormat.week)[0] + ' ' + DateFormat.days[day_start] + ' ' + date_split[0].match(DateFormat.time)[0];
part.date += '-' + date_split[1].match(DateFormat.week)[0] + ' ' + DateFormat.days[day_start] + ' 23:59pm';
this.renderEvent(part);
// middel days
for (let day = start+1; day < ((end == 0) ? 7 : end); day++)
for (let day = day_start+1; day < ((day_end == 0) ? 7 : day_end); day++)
{
part.date = date_split[0].match(DateFormat.week)[0] + ' ' + DateFormat.days[day] + ' 12:00am';
part.date += '-' + date_split[1].match(DateFormat.week)[0] + ' ' + DateFormat.days[day] + ' 23:59pm';
@@ -483,11 +540,15 @@ class CalendarView extends ItemView
}
// last day
part.date = date_split[0].match(DateFormat.week)[0] + ' ' + DateFormat.days[end] + ' 12:00am';
part.date += '-' + date_split[1].match(DateFormat.week)[0] + ' ' + DateFormat.days[end] + ' ' + date_split[0].match(DateFormat.time)[0];
part.date = date_split[0].match(DateFormat.week)[0] + ' ' + DateFormat.days[day_end] + ' 12:00am';
part.date += '-' + date_split[1].match(DateFormat.week)[0] + ' ' + DateFormat.days[day_end] + ' ' + date_split[0].match(DateFormat.time)[0];
this.renderEvent(part);
}
}
else if (startInWeek || endInWeek)
{
console.warn('multi week event not suported', event);
}
}
}