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: Add snapping to workspace comments. #8070

Merged
merged 4 commits into from
May 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 5 additions & 13 deletions core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,19 +420,11 @@ export class BlockSvg
if (this.getParent()) return;
if (this.isInFlyout) return;
const grid = this.workspace.getGrid();
if (!grid || !grid.shouldSnap()) return;

const spacing = grid.getSpacing();
const half = spacing / 2;
const xy = this.getRelativeToSurfaceXY();
const dx = Math.round(
Math.round((xy.x - half) / spacing) * spacing + half - xy.x,
);
const dy = Math.round(
Math.round((xy.y - half) / spacing) * spacing + half - xy.y,
);
if (dx || dy) {
this.moveBy(dx, dy, ['snap']);
if (!grid?.shouldSnap()) return;
const currentXY = this.getRelativeToSurfaceXY();
const alignedXY = grid.alignXY(currentXY);
if (alignedXY !== currentXY) {
this.moveTo(alignedXY, ['snap']);
}
}

Expand Down
14 changes: 14 additions & 0 deletions core/comments/rendered_workspace_comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export class RenderedWorkspaceComment
override moveTo(location: Coordinate, reason?: string[] | undefined): void {
super.moveTo(location, reason);
this.view.moveTo(location);
this.snapToGrid();
}

/**
Expand Down Expand Up @@ -208,6 +209,7 @@ export class RenderedWorkspaceComment

/** Ends the drag on the comment. */
endDrag(): void {
this.snapToGrid();
this.dragStrategy.endDrag();
}

Expand Down Expand Up @@ -247,4 +249,16 @@ export class RenderedWorkspaceComment
);
contextMenu.show(e, menuOptions, this.workspace.RTL);
}

/** Snap this comment to the nearest grid point. */
snapToGrid(): void {
if (this.isDeadOrDying()) return;
const grid = this.workspace.getGrid();
if (!grid?.shouldSnap()) return;
const currentXY = this.getRelativeToSurfaceXY();
const alignedXY = grid.alignXY(currentXY);
if (alignedXY !== currentXY) {
this.moveTo(alignedXY, ['snap']);
}
}
}
20 changes: 20 additions & 0 deletions core/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// Former goog.module ID: Blockly.Grid

import * as dom from './utils/dom.js';
import {Coordinate} from './utils/coordinate.js';
import {Svg} from './utils/svg.js';
import {GridOptions} from './options.js';

Expand Down Expand Up @@ -184,6 +185,25 @@ export class Grid {
this.pattern.setAttribute('y', `${y}`);
}

/**
* Given a coordinate, return the nearest coordinate aligned to the grid.
*
* @param xy A workspace coordinate.
* @returns Workspace coordinate of nearest grid point.
* If there's no change, return the same coordinate object.
*/
alignXY(xy: Coordinate): Coordinate {
const spacing = this.getSpacing();
const half = spacing / 2;
const x = Math.round(Math.round((xy.x - half) / spacing) * spacing + half);
const y = Math.round(Math.round((xy.y - half) / spacing) * spacing + half);
if (x === xy.x && y === xy.y) {
// No change.
return xy;
}
return new Coordinate(x, y);
}

/**
* Create the DOM for the grid described by options.
*
Expand Down