From aeb11c0ffe80d903fd976dd72bfc164e38769803 Mon Sep 17 00:00:00 2001 From: HPDell Date: Mon, 23 Jun 2025 12:23:44 +0100 Subject: [PATCH 1/2] fix: event not showing when start earlier than time range --- lib.typ | 9 +++++---- test/day-view.typ | 3 ++- util/utils.typ | 12 ++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib.typ b/lib.typ index 7a9fd2f..35cb749 100644 --- a/lib.typ +++ b/lib.typ @@ -20,7 +20,8 @@ template: (:), // A stroke style to control the style of the default stroke, or a function taking two parameters `(x, y)` to control the stroke. The first row is the dates, and the first column is the times. line-style: none, - datetime-format: "[year]-[month]-[day]" + date-format: "[year]-[month]-[day]", + time-format: "[hour]:[minute]" ) = { let items = events-to-calendar-items(events, hour-range.at(0)) let day-list = items.keys() @@ -43,7 +44,7 @@ rows: (auto, ) + (minute-height,) * hours * 60 + (8pt,), fill: white, stroke: stroke-rule, - [], ..day-list.map(i => day(i)).map(d => (style.header)(d.display(datetime-format))), + [], ..day-list.map(i => day(i)).map(d => (style.header)(d.display(date-format))), ..array.range(hours * 60 + 1).map(y => { array.range(days + 1).map(x => { if x == 0 { @@ -54,14 +55,14 @@ } else [] } else { if items.at(day-list.at(x - 1)).keys().contains(str(y)) { - let (last, body) = items.at(day-list.at(x - 1)).at(str(y)) + let (last, event) = items.at(day-list.at(x - 1)).at(str(y)) show: block.with(inset: (x: 2pt, y: 0pt), width: 100%) place({ block( width: 100%, height: (last) * minute-height, { - (style.event)(..(minutes-to-datetime(y + minutes-offset), body)) + (style.event)(..(event, time-format)) } ) }) diff --git a/test/day-view.typ b/test/day-view.typ index a938945..a6f231b 100644 --- a/test/day-view.typ +++ b/test/day-view.typ @@ -7,11 +7,12 @@ ("2024-1-1", "10:15", "11:10", [Tutorial]), ("2024-1-1", (11, 35), (12, 35), [Shopping]), ("2024-1-1", 13.55, 15.00, [Lecture 2]), + ("2024-1-2", 7.00, 9.00, [Lecture 2]), ("2024-1-2", 9.30, 11.30, [Lecture 2]), ("2024-1-2", 13.45, 14.30, [Tutorial]), ) -#calendar(events, hour-range: (8, 15), datetime-format: "[day]/[month]/[year]") +#calendar(events, hour-range: (8, 15), date-format: "[day]/[month]/[year]") = ICS diff --git a/util/utils.typ b/util/utils.typ index 2457dd8..ec65c1d 100644 --- a/util/utils.typ +++ b/util/utils.typ @@ -18,11 +18,11 @@ let stime = if type(value.at(1)) == datetime { value.at(1) } else if type(value.at(1)) == array { time(..value.at(1)) } else { time(value.at(1)) } let etime = if type(value.at(2)) == datetime { value.at(2) } else if type(value.at(2)) == array { time(..value.at(2)) } else { time(value.at(2)) } let body = if value.len() > 3 { value.at(3) } else { none } - let istart = calc.min((stime.hour() - start), 24) * 60 + calc.min(stime.minute(), 60) - let iend = calc.min((etime.hour() - start), 24) * 60 + calc.min(etime.minute(), 60) + let istart = calc.clamp((stime.hour() - start), 0, 24) * 60 + calc.min(stime.minute(), 60) + let iend = calc.clamp((etime.hour() - start), 0, 24) * 60 + calc.min(etime.minute(), 60) let ilast = iend - istart if ilast > 0 { - dict.at(kday).insert(str(istart), (ilast, body)) + dict.at(kday).insert(str(istart), (ilast, (stime, etime, ..value.slice(3,)))) } } dict @@ -35,7 +35,7 @@ [#{day}] } -#let default-item-style(time, body) = { +#let default-item-style(event, time-format) = { show: block.with( fill: white, height: 100%, @@ -51,10 +51,10 @@ set par(leading: 4pt) if time != none { terms( - terms.item(time.display("[hour]:[minute]"), body) + terms.item(event.at(0).display(time-format), event.last()) ) } else { - body + event.last() } } From 4b30bff9a21a761c89596f77a3f20ab180c38cd8 Mon Sep 17 00:00:00 2001 From: HPDell Date: Mon, 23 Jun 2025 12:40:49 +0100 Subject: [PATCH 2/2] fix: clip when events outbounds --- lib.typ | 2 +- test/day-view.typ | 2 +- util/utils.typ | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib.typ b/lib.typ index 35cb749..d659d34 100644 --- a/lib.typ +++ b/lib.typ @@ -23,7 +23,7 @@ date-format: "[year]-[month]-[day]", time-format: "[hour]:[minute]" ) = { - let items = events-to-calendar-items(events, hour-range.at(0)) + let items = events-to-calendar-items(events, hour-range) let day-list = items.keys() let days = day-list.len() let hours = hour-range.at(1) - hour-range.at(0) diff --git a/test/day-view.typ b/test/day-view.typ index a6f231b..5c5e0dc 100644 --- a/test/day-view.typ +++ b/test/day-view.typ @@ -9,7 +9,7 @@ ("2024-1-1", 13.55, 15.00, [Lecture 2]), ("2024-1-2", 7.00, 9.00, [Lecture 2]), ("2024-1-2", 9.30, 11.30, [Lecture 2]), - ("2024-1-2", 13.45, 14.30, [Tutorial]), + ("2024-1-2", 13.45, 15.20, [Tutorial]), ) #calendar(events, hour-range: (8, 15), date-format: "[day]/[month]/[year]") diff --git a/util/utils.typ b/util/utils.typ index ec65c1d..a51a67f 100644 --- a/util/utils.typ +++ b/util/utils.typ @@ -6,7 +6,8 @@ return datetime(hour: h, minute: m, second: 0) } -#let events-to-calendar-items(events, start) = { +#let events-to-calendar-items(events, hour-range) = { + let (dstart, dend) = hour-range let days = events.map(i => i.at(0)).dedup().map(i => if type(i) == datetime { i } else { day(..(i,).flatten()) }) let dict = days.map(i => (i.display("[year]-[month]-[day]"), (:))).to-dict() for value in events { @@ -18,8 +19,9 @@ let stime = if type(value.at(1)) == datetime { value.at(1) } else if type(value.at(1)) == array { time(..value.at(1)) } else { time(value.at(1)) } let etime = if type(value.at(2)) == datetime { value.at(2) } else if type(value.at(2)) == array { time(..value.at(2)) } else { time(value.at(2)) } let body = if value.len() > 3 { value.at(3) } else { none } - let istart = calc.clamp((stime.hour() - start), 0, 24) * 60 + calc.min(stime.minute(), 60) - let iend = calc.clamp((etime.hour() - start), 0, 24) * 60 + calc.min(etime.minute(), 60) + let istart = calc.clamp((stime.hour() - dstart), 0, 24) * 60 + calc.min(stime.minute(), 60) + let iend = calc.clamp((etime.hour() - dstart), 0, 24) * 60 + calc.min(etime.minute(), 60) + iend = calc.clamp(iend, istart + 1, (dend - dstart) * 60) let ilast = iend - istart if ilast > 0 { dict.at(kday).insert(str(istart), (ilast, (stime, etime, ..value.slice(3,))))