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

86.66% Statements 13/15
62.5% Branches 5/8
80% Functions 4/5
92.85% Lines 13/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 32 33 34 35 36 37 38 39 40 41 42 43 44                  1x   51x     51x 51x 51x 51x         51x       2316x 2316x 2316x       2313x 2313x     2313x              
import { atom } from "nanostores";
import { Slice } from "../slice";
import type { EditorContext } from "../editor-context";
import { ProjectSlice } from "./project-slice";
import { CHART } from "../components";
import { DEFAULT_CHART_SIZE } from "../types";
import type { Entity } from "../../entity-manager";
 
export class ChartSlice extends Slice {
  static readonly sliceKey = "chart";
 
  $selectedChartId = atom<string | null>(null);
 
  constructor(ctx: EditorContext) {
    super(ctx);
    const charts = this.getCharts();
    Eif (charts.length > 0) {
      this.$selectedChartId.set(charts[0]!.id);
    }
  }
 
  getCharts(): Entity[] {
    return this.ctx.get(ProjectSlice).entityManager.entitiesWithComponent(CHART);
  }
 
  getSelectedChart(): Entity | undefined {
    const id = this.$selectedChartId.get();
    Iif (!id) return undefined;
    return this.ctx.get(ProjectSlice).entityManager.get(id);
  }
 
  getChartSize(): number {
    const chart = this.getSelectedChart();
    const chartComponent = chart
      ? this.ctx.get(ProjectSlice).entityManager.getComponent(chart, CHART)
      : undefined;
    return chartComponent?.size ?? DEFAULT_CHART_SIZE;
  }
 
  setSelectedChartId(id: string | null): void {
    this.$selectedChartId.set(id);
  }
}