Skip to content

Commit

Permalink
feat: make grid options togglable (#7828)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega authored Feb 7, 2024
1 parent e67dd0d commit bb8b272
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
39 changes: 36 additions & 3 deletions core/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -90,6 +122,7 @@ export class Grid {
* @internal
*/
update(scale: number) {
this.scale = scale;
const safeSpacing = this.spacing * scale;

this.pattern.setAttribute('width', `${safeSpacing}`);
Expand Down
1 change: 0 additions & 1 deletion core/workspace_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bb8b272

Please sign in to comment.