All files / src/packlets/editor-core/slices columns-slice.ts

100% Statements 15/15
100% Branches 0/0
100% Functions 3/3
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31          1x   51x 51x   51x     102x 102x       107x 107x 107x 214x 580x 580x     107x 107x      
import { atom } from "nanostores";
import { Slice } from "../slice";
import type { ColumnDefinition, TimelineColumn } from "../types";
 
export class ColumnsSlice extends Slice {
  static readonly sliceKey = "columns";
 
  $columns = atom<TimelineColumn[]>([]);
  $timelineWidth = atom<number>(0);
 
  private providers: { priority: number; provider: () => ColumnDefinition[] }[] = [];
 
  registerColumnProvider(priority: number, provider: () => ColumnDefinition[]): void {
    this.providers.push({ priority, provider });
    this.providers.sort((a, b) => a.priority - b.priority);
  }
 
  refreshColumns(): void {
    let x = 0;
    const columns: TimelineColumn[] = [];
    for (const { provider } of this.providers) {
      for (const def of provider()) {
        columns.push({ ...def, x });
        x += def.width;
      }
    }
    this.$columns.set(columns);
    this.$timelineWidth.set(x + 1);
  }
}