@@ -27,6 +27,8 @@ type EventLayout = {
2727type ActiveGroup = {
2828 end: number ;
2929 coverLevel: number ;
30+ columns: number ;
31+ leftColumnEnd: number ;
3032};
3133
3234type PlacedEvent = {
@@ -249,6 +251,53 @@ function layoutTitleGroup(group: CalendarEvent[], coverLevel: number): PlacedEve
249251
250252// ------------ cover stack ------------
251253
254+ function placedLeftColumnEnd(placedEvents : PlacedEvent []) {
255+ let end = Number .NEGATIVE_INFINITY;
256+
257+ for (const placedEvent of placedEvents ) {
258+ if (placedEvent .layout .column !== 0 ) continue ;
259+
260+ end = Math .max (end , eventEnd (placedEvent .event ));
261+ }
262+
263+ return end ;
264+ }
265+
266+ function placeInFreeLeftColumn(
267+ group : CalendarEvent [],
268+ activeGroups : ActiveGroup [],
269+ ): PlacedEvent | undefined {
270+ if (group .length !== 1 ) {
271+ return undefined ;
272+ }
273+
274+ const event = group [0 ];
275+ const activeGroup = activeGroups [activeGroups .length - 1 ];
276+
277+ if (
278+ ! event ||
279+ ! activeGroup ||
280+ activeGroup .columns <= 1 ||
281+ activeGroup .leftColumnEnd > eventStart (event )
282+ ) {
283+ return undefined ;
284+ }
285+
286+ const end = eventEnd (event );
287+
288+ activeGroup .leftColumnEnd = end ;
289+ activeGroup .end = Math .max (activeGroup .end , end );
290+
291+ return {
292+ event ,
293+ layout: {
294+ column: 0 ,
295+ columns: activeGroup .columns ,
296+ coverLevel: activeGroup .coverLevel ,
297+ },
298+ };
299+ }
300+
252301function titleGroupEnd(group : CalendarEvent []) {
253302 const firstEvent = group [0 ];
254303 if (! firstEvent ) {
@@ -334,6 +383,12 @@ function layoutEvents(events: CalendarEvent[]): LaidOutEvent[] {
334383
335384 removeInactiveGroups (activeGroups , eventStart (firstEvent ));
336385
386+ const reusedEvent = placeInFreeLeftColumn (group , activeGroups );
387+ if (reusedEvent ) {
388+ layouts .set (reusedEvent .event , reusedEvent .layout );
389+ continue ;
390+ }
391+
337392 const coverLevel = nextCoverLevel (activeGroups );
338393 const placedEvents = layoutTitleGroup (group , coverLevel );
339394
@@ -344,6 +399,8 @@ function layoutEvents(events: CalendarEvent[]): LaidOutEvent[] {
344399 activeGroups .push ({
345400 end: titleGroupEnd (group ),
346401 coverLevel ,
402+ columns: placedEvents [0 ]?.layout .columns ?? 1 ,
403+ leftColumnEnd: placedLeftColumnEnd (placedEvents ),
347404 });
348405 }
349406
0 commit comments