Files
blockly/core/grid.ts
Beka Westberg 48228e47e3 chore: merge develop into v11 to fix CI (#7893)
* fix: destroy connection highlight when the connection is disposed (#7830)

* fix: fix reference docs toc generation (#7832)

* fix: insertion marker previewer finding wrong connection for different conn counts (#7833)

* feat: make grid options togglable (#7828)

* chore(deps): Bump prettier from 3.1.1 to 3.2.5 (#7831)

Bumps [prettier](https://github.com/prettier/prettier) from 3.1.1 to 3.2.5.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/3.1.1...3.2.5)

---
updated-dependencies:
- dependency-name: prettier
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* fix: disposing of connection previewer (#7834)

* fix: typo in Click event class docs

* chore: rollup of updates from translatewiki for 10.4.0 (#7856)

* chore: update metadata for v10.4.0 (#7857)

* release: update version number to 10.4.0

* fix: connection previewer disposing too early

* Merge pull request #7859 from BeksOmega/fix/previewer-disposing

fix: connection previewer disposing too early
(cherry picked from commit da3ec253bf)

* release: update version number to 10.4.1

* chore: move connection previewer out of subfolder (#7835)

* chore(deps-dev): Bump undici from 5.26.3 to 5.28.3 (#7862)

Bumps [undici](https://github.com/nodejs/undici) from 5.26.3 to 5.28.3.
- [Release notes](https://github.com/nodejs/undici/releases)
- [Commits](https://github.com/nodejs/undici/compare/v5.26.3...v5.28.3)

---
updated-dependencies:
- dependency-name: undici
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* chore: update webdriverio to fix chrome endpoint issue (#7875)

* fix: insertion marker drag scaling (#7874)

* fix: insertion marker drag scaling

* chore: added docs for other params

* fix: restore respecting snap radius (#7873)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Maribeth Bottorff <maribethb@google.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Elvis Adomnica <elvis.adomnica@gmail.com>
Co-authored-by: Rachel Fenichel <fenichel@google.com>
2024-03-08 14:24:58 -08:00

230 lines
6.2 KiB
TypeScript

/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Object for configuring and updating a workspace grid in
* Blockly.
*
* @class
*/
// Former goog.module ID: Blockly.Grid
import * as dom from './utils/dom.js';
import {Svg} from './utils/svg.js';
import {GridOptions} from './options.js';
/**
* Class for a workspace's grid.
*/
export class Grid {
private spacing: number;
private length: number;
private scale: number = 1;
private readonly line1: SVGElement;
private readonly line2: SVGElement;
private snapToGrid: boolean;
/**
* @param pattern The grid's SVG pattern, created during injection.
* @param options A dictionary of normalized options for the grid.
* See grid documentation:
* https://developers.google.com/blockly/guides/configure/web/grid
*/
constructor(
private pattern: SVGElement,
options: GridOptions,
) {
/** The spacing of the grid lines (in px). */
this.spacing = options['spacing'] ?? 0;
/** How long the grid lines should be (in px). */
this.length = options['length'] ?? 1;
/** The horizontal grid line, if it exists. */
this.line1 = pattern.firstChild as SVGElement;
/** The vertical grid line, if it exists. */
this.line2 = this.line1 && (this.line1.nextSibling as SVGElement);
/** Whether blocks should snap to the 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.
*
* @returns True if blocks should snap, false otherwise.
* @internal
*/
shouldSnap(): boolean {
return this.snapToGrid;
}
/**
* Get the spacing of the grid points (in px).
*
* @returns The spacing of the grid points.
* @internal
*/
getSpacing(): number {
return this.spacing;
}
/**
* Get the ID of the pattern element, which should be randomized to avoid
* conflicts with other Blockly instances on the page.
*
* @returns The pattern ID.
* @internal
*/
getPatternId(): string {
return this.pattern.id;
}
/**
* Update the grid with a new scale.
*
* @param scale The new workspace scale.
* @internal
*/
update(scale: number) {
this.scale = scale;
const safeSpacing = this.spacing * scale;
this.pattern.setAttribute('width', `${safeSpacing}`);
this.pattern.setAttribute('height', `${safeSpacing}`);
let half = Math.floor(this.spacing / 2) + 0.5;
let start = half - this.length / 2;
let end = half + this.length / 2;
half *= scale;
start *= scale;
end *= scale;
this.setLineAttributes(this.line1, scale, start, end, half, half);
this.setLineAttributes(this.line2, scale, half, half, start, end);
}
/**
* Set the attributes on one of the lines in the grid. Use this to update the
* length and stroke width of the grid lines.
*
* @param line Which line to update.
* @param width The new stroke size (in px).
* @param x1 The new x start position of the line (in px).
* @param x2 The new x end position of the line (in px).
* @param y1 The new y start position of the line (in px).
* @param y2 The new y end position of the line (in px).
*/
private setLineAttributes(
line: SVGElement,
width: number,
x1: number,
x2: number,
y1: number,
y2: number,
) {
if (line) {
line.setAttribute('stroke-width', `${width}`);
line.setAttribute('x1', `${x1}`);
line.setAttribute('y1', `${y1}`);
line.setAttribute('x2', `${x2}`);
line.setAttribute('y2', `${y2}`);
}
}
/**
* Move the grid to a new x and y position, and make sure that change is
* visible.
*
* @param x The new x position of the grid (in px).
* @param y The new y position of the grid (in px).
* @internal
*/
moveTo(x: number, y: number) {
this.pattern.setAttribute('x', `${x}`);
this.pattern.setAttribute('y', `${y}`);
}
/**
* Create the DOM for the grid described by options.
*
* @param rnd A random ID to append to the pattern's ID.
* @param gridOptions The object containing grid configuration.
* @param defs The root SVG element for this workspace's defs.
* @returns The SVG element for the grid pattern.
* @internal
*/
static createDom(
rnd: string,
gridOptions: GridOptions,
defs: SVGElement,
): SVGElement {
/*
<pattern id="blocklyGridPattern837493" patternUnits="userSpaceOnUse">
<rect stroke="#888" />
<rect stroke="#888" />
</pattern>
*/
const gridPattern = dom.createSvgElement(
Svg.PATTERN,
{'id': 'blocklyGridPattern' + rnd, 'patternUnits': 'userSpaceOnUse'},
defs,
);
// x1, y1, x1, x2 properties will be set later in update.
if ((gridOptions['length'] ?? 1) > 0 && (gridOptions['spacing'] ?? 0) > 0) {
dom.createSvgElement(
Svg.LINE,
{'stroke': gridOptions['colour']},
gridPattern,
);
if (gridOptions['length'] ?? 1 > 1) {
dom.createSvgElement(
Svg.LINE,
{'stroke': gridOptions['colour']},
gridPattern,
);
}
} else {
// Edge 16 doesn't handle empty patterns
dom.createSvgElement(Svg.LINE, {}, gridPattern);
}
return gridPattern;
}
}