summaryrefslogtreecommitdiff
path: root/apps/web/src/types.ts
blob: dc26c89d5a8a99b8d42242c7b1230e45beaa089b (plain)
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
45
46
47
48
49
50
51
52
53
54
55
import { Immutable } from "immer";
import Ratio from "./math/Ratio";

export type Cell = Immutable<{
  value?: string;
  widthRatio: Ratio;
}>;

export interface Row {
  cells: Cell[];
}

export interface Part {
  title?: string;
  rows: Row[];
}

export interface Grid {
  id: string;
  baseCellSize: number;
  baseCellWidthRatio: Ratio;
  parts: Part[];
}

export interface Doc {
  grids: Grid[];
}

export interface RowRef {
  partIndex: number;
  rowIndex: number;
}

export interface CellRef {
  partIndex: number;
  rowIndex: number;
  cellIndex: number;
}

export function cellRefEquals(a: CellRef, b: CellRef): boolean {
  return (
    a.partIndex === b.partIndex &&
    a.rowIndex === b.rowIndex &&
    a.cellIndex === b.cellIndex
  );
}

export function renderedRowIndexToRef(
  grid: Grid,
  renderedRowIndex: number,
): RowRef {
  const partIndex = renderedRowIndex % grid.parts.length;
  const rowIndex = Math.floor(renderedRowIndex / grid.parts.length);
  return { partIndex, rowIndex };
}