From bb8b2728e7a3d60dc8538dea84691f577a988479 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Wed, 7 Feb 2024 19:48:09 +0000 Subject: [PATCH] feat: make grid options togglable (#7828) --- core/grid.ts | 39 ++++++++++++++++++++++++++++++++++++--- core/workspace_svg.ts | 1 - 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/core/grid.ts b/core/grid.ts index 952ed018a..28e460fa0 100644 --- a/core/grid.ts +++ b/core/grid.ts @@ -20,11 +20,12 @@ import {GridOptions} from './options.js'; * Class for a workspace's grid. */ export class Grid { - private readonly spacing: number; - private readonly length: number; + private spacing: number; + private length: number; + private scale: number = 1; private readonly line1: SVGElement; private readonly line2: SVGElement; - private readonly snapToGrid: boolean; + private snapToGrid: boolean; /** * @param pattern The grid's SVG pattern, created during injection. @@ -52,6 +53,37 @@ export class Grid { this.snapToGrid = options['snap'] ?? false; } + /** + * Sets the spacing between the centers of the grid lines. + * + * This does not trigger snapping to the newly spaced grid. If you want to + * snap blocks to the grid programmatically that needs to be triggered + * on individual top-level blocks. The next time a block is dragged and + * dropped it will snap to the grid if snapping to the grid is enabled. + */ + setSpacing(spacing: number) { + this.spacing = spacing; + this.update(this.scale); + } + + /** Sets the length of the grid lines. */ + setLength(length: number) { + this.length = length; + this.update(this.scale); + } + + /** + * Sets whether blocks should snap to the grid or not. + * + * Setting this to true does not trigger snapping. If you want to snap blocks + * to the grid programmatically that needs to be triggered on individual + * top-level blocks. The next time a block is dragged and dropped it will + * snap to the grid. + */ + setSnapToGrid(snap: boolean) { + this.snapToGrid = snap; + } + /** * Whether blocks should snap to the grid, based on the initial configuration. * @@ -90,6 +122,7 @@ export class Grid { * @internal */ update(scale: number) { + this.scale = scale; const safeSpacing = this.spacing * scale; this.pattern.setAttribute('width', `${safeSpacing}`); diff --git a/core/workspace_svg.ts b/core/workspace_svg.ts index fe1386587..0eb7e8087 100644 --- a/core/workspace_svg.ts +++ b/core/workspace_svg.ts @@ -2535,7 +2535,6 @@ export class WorkspaceSvg extends Workspace implements IASTNodeLocationSvg { * Get the grid object for this workspace, or null if there is none. * * @returns The grid object for this workspace. - * @internal */ getGrid(): Grid | null { return this.grid;