-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathgrid.ts
More file actions
282 lines (255 loc) · 8.04 KB
/
grid.ts
File metadata and controls
282 lines (255 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* @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 {BlocklyOptions} from './blockly_options.js';
import {IGrid, IGridProvider} from './interfaces/i_grid.js';
import {GridOptions} from './options.js';
import * as registry from './registry.js';
import {Coordinate} from './utils/coordinate.js';
import * as dom from './utils/dom.js';
import {Svg} from './utils/svg.js';
export class GridProvider implements IGridProvider {
/**
* 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
*/
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;
}
/**
* Parse the user-specified grid options, using reasonable defaults where
* behaviour is unspecified. See grid documentation:
* https://developers.google.com/blockly/guides/configure/web/grid
*
* @param options Dictionary of options.
* @returns Normalized grid options.
*/
parseGridOptions(options: BlocklyOptions): GridOptions {
const grid = options['grid'] || {};
const gridOptions = {} as GridOptions;
gridOptions.spacing = Number(grid['spacing']) || 0;
gridOptions.colour = grid['colour'] || '#888';
gridOptions.length =
grid['length'] === undefined ? 1 : Number(grid['length']);
gridOptions.snap = gridOptions.spacing > 0 && !!grid['snap'];
return gridOptions;
}
createGrid(pattern: SVGElement, options: GridOptions): IGrid {
return new Grid(pattern, options);
}
}
/**
* Class for a workspace's grid.
*/
export class Grid implements IGrid {
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);
}
/**
* Get the spacing of the grid points (in px).
*
* @returns The spacing of the grid points.
*/
getSpacing(): number {
return this.spacing;
}
/** Sets the length of the grid lines. */
setLength(length: number) {
this.length = length;
this.update(this.scale);
}
/** Get the length of the grid lines (in px). */
getLength(): number {
return this.length;
}
/**
* 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.
*
* @returns True if blocks should snap, false otherwise.
*/
shouldSnap(): boolean {
return this.snapToGrid;
}
/**
* 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}`);
}
/**
* 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);
}
}
registry.register(registry.Type.GRID_PROVIDER, registry.DEFAULT, GridProvider);