Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make grid options togglable #7828

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading