summaryrefslogtreecommitdiff
path: root/web/src/types.ts
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/types.ts')
-rw-r--r--web/src/types.ts50
1 files changed, 10 insertions, 40 deletions
diff --git a/web/src/types.ts b/web/src/types.ts
index b41bb9a..dc26c89 100644
--- a/web/src/types.ts
+++ b/web/src/types.ts
@@ -1,9 +1,10 @@
+import { Immutable } from "immer";
import Ratio from "./math/Ratio";
-export interface Cell {
+export type Cell = Immutable<{
value?: string;
widthRatio: Ratio;
-}
+}>;
export interface Row {
cells: Cell[];
@@ -44,42 +45,11 @@ export function cellRefEquals(a: CellRef, b: CellRef): boolean {
);
}
-export function mapRowsInRange(
- doc: Doc,
- gridId: string,
- startRef: CellRef,
- endRef: CellRef,
- mapFn: (row: Row, ref: RowRef) => Row,
-): Doc {
- const firstPartIndex = Math.min(startRef.partIndex, endRef.partIndex);
- const lastPartIndex = Math.max(startRef.partIndex, endRef.partIndex);
- const firstRowIndex = Math.min(startRef.rowIndex, endRef.rowIndex);
- const lastRowIndex = Math.max(startRef.rowIndex, endRef.rowIndex);
-
- return {
- ...doc,
- grids: doc.grids.map((grid) => {
- if (grid.id !== gridId) return grid;
-
- return {
- ...grid,
- parts: grid.parts.map((part, partIndex) => {
- if (partIndex < firstPartIndex || partIndex > lastPartIndex) {
- return part;
- }
-
- return {
- ...part,
- rows: part.rows.map((row, rowIndex) => {
- if (rowIndex < firstRowIndex || rowIndex > lastRowIndex) {
- return row;
- }
-
- return mapFn(row, { partIndex, rowIndex });
- }),
- };
- }),
- };
- }),
- };
+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 };
}