summaryrefslogtreecommitdiff
path: root/web/src/components/grid/drawGrid.ts
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/components/grid/drawGrid.ts')
-rw-r--r--web/src/components/grid/drawGrid.ts136
1 files changed, 1 insertions, 135 deletions
diff --git a/web/src/components/grid/drawGrid.ts b/web/src/components/grid/drawGrid.ts
index 498abd5..da83c8e 100644
--- a/web/src/components/grid/drawGrid.ts
+++ b/web/src/components/grid/drawGrid.ts
@@ -2,33 +2,14 @@ import { RangeSelection, Selection } from "../../selection";
import { CellRef } from "../../types";
import { getRenderedCell, RenderedCell, RenderedGrid } from "./renderGrid";
-interface GridStyles {
+export interface GridStyles {
bgFill: string;
borderStroke: string;
cellStroke: string;
- selelectionRangeFill: string;
- selectionRangeStroke: string;
- activeCellStroke: string;
cellValueFont: string;
cellValueLineHeight: string;
}
-export function getGridStyles(el: HTMLElement): GridStyles {
- const style = window.getComputedStyle(el);
- const prop = (k: string) => style.getPropertyValue(k);
-
- return {
- bgFill: prop("--grid-bg-fill"),
- borderStroke: prop("--grid-border-stroke"),
- cellStroke: prop("--grid-cell-stroke"),
- selelectionRangeFill: prop("--grid-selection-range-fill"),
- selectionRangeStroke: prop("--grid-selection-range-stroke"),
- activeCellStroke: prop("--grid-active-cell-stroke"),
- cellValueFont: prop("font"),
- cellValueLineHeight: prop("line-height"),
- };
-}
-
function excursion(ctx: CanvasRenderingContext2D, f: () => void) {
ctx.save();
f();
@@ -78,111 +59,6 @@ function strokeGridLines(
});
}
-function strokeActiveCell(
- ctx: CanvasRenderingContext2D,
- styles: GridStyles,
- grid: RenderedGrid,
- cell: RenderedCell,
-) {
- const isLastCell = cell.rect.bottomRight.x === grid.rect.bottomRight.x;
- const isLastRow = cell.rect.bottomRight.y === grid.rect.bottomRight.y;
-
- ctx.strokeStyle = styles.activeCellStroke;
- ctx.lineWidth = 2;
-
- ctx.strokeRect(
- cell.rect.topLeft.x + 1,
- cell.rect.topLeft.y + 1,
- isLastCell ? cell.rect.width - 2 : cell.rect.width - 1,
- isLastRow ? cell.rect.height - 2 : cell.rect.height - 1,
- );
-}
-
-function drawCellRange(
- ctx: CanvasRenderingContext2D,
- styles: GridStyles,
- grid: RenderedGrid,
- start: CellRef,
- end: CellRef,
- { stroke }: { stroke: boolean },
-) {
- const startCell = getRenderedCell(grid, start);
- const endCell = getRenderedCell(grid, end);
-
- if (!startCell || !endCell) return;
-
- const rect = startCell.rect.extend(endCell.rect);
-
- const isRightEdge = rect.bottomRight.x === grid.rect.bottomRight.x;
- const isBottomEdge = rect.bottomRight.y === grid.rect.bottomRight.y;
-
- ctx.fillStyle = styles.selelectionRangeFill;
-
- ctx.fillRect(
- rect.topLeft.x + 1,
- rect.topLeft.y + 1,
- isRightEdge ? rect.width - 2 : rect.width - 1,
- isBottomEdge ? rect.height - 2 : rect.height - 1,
- );
-
- if (!stroke) return;
-
- ctx.strokeStyle = styles.selectionRangeStroke;
-
- ctx.strokeRect(
- rect.topLeft.x + 0.5,
- rect.topLeft.y + 0.5,
- isRightEdge ? rect.width - 1 : rect.width,
- isBottomEdge ? rect.height - 1 : rect.height,
- );
-}
-
-function drawPendingSelection(
- ctx: CanvasRenderingContext2D,
- styles: GridStyles,
- grid: RenderedGrid,
- selection: Selection,
-) {
- if (selection.gridId !== grid.id) return;
-
- const activeCell = getRenderedCell(grid, selection.activeCellRef);
-
- if (!activeCell) return;
-
- if (selection instanceof RangeSelection) {
- excursion(ctx, () => {
- drawCellRange(ctx, styles, grid, selection.range[0], selection.range[1], {
- stroke: false,
- });
- });
- }
-
- excursion(ctx, () => strokeActiveCell(ctx, styles, grid, activeCell));
-}
-
-function drawSelection(
- ctx: CanvasRenderingContext2D,
- styles: GridStyles,
- grid: RenderedGrid,
- selection: Selection,
-) {
- if (selection.gridId !== grid.id) return;
-
- const activeCell = getRenderedCell(grid, selection.activeCellRef);
-
- if (!activeCell) return;
-
- if (selection instanceof RangeSelection) {
- excursion(ctx, () => {
- drawCellRange(ctx, styles, grid, selection.range[0], selection.range[1], {
- stroke: true,
- });
- });
- }
-
- excursion(ctx, () => strokeActiveCell(ctx, styles, grid, activeCell));
-}
-
function drawCellValues(
ctx: CanvasRenderingContext2D,
styles: GridStyles,
@@ -207,19 +83,9 @@ export default function drawGrid(
ctx: CanvasRenderingContext2D,
styles: GridStyles,
grid: RenderedGrid,
- selection?: Selection,
- pendingSelection?: Selection,
) {
excursion(ctx, () => fillBackground(ctx, styles, grid));
excursion(ctx, () => strokeGridLines(ctx, styles, grid));
excursion(ctx, () => strokeGrid(ctx, styles, grid));
excursion(ctx, () => drawCellValues(ctx, styles, grid));
-
- if (pendingSelection) {
- excursion(ctx, () =>
- drawPendingSelection(ctx, styles, grid, pendingSelection),
- );
- } else if (selection) {
- excursion(ctx, () => drawSelection(ctx, styles, grid, selection));
- }
}