-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathpath_object.ts
More file actions
277 lines (248 loc) · 7.8 KB
/
path_object.ts
File metadata and controls
277 lines (248 loc) · 7.8 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
/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
// Former goog.module ID: Blockly.blockRendering.PathObject
import type {BlockSvg} from '../../block_svg.js';
import type {Connection} from '../../connection.js';
import {RenderedConnection} from '../../rendered_connection.js';
import type {BlockStyle} from '../../theme.js';
import {idGenerator} from '../../utils.js';
import {Coordinate} from '../../utils/coordinate.js';
import * as dom from '../../utils/dom.js';
import {Svg} from '../../utils/svg.js';
import type {ConstantProvider} from './constants.js';
import type {IPathObject} from './i_path_object.js';
/**
* An object that handles creating and setting each of the SVG elements
* used by the renderer.
*/
export class PathObject implements IPathObject {
svgRoot: SVGElement;
svgPath: SVGElement;
private clip: SVGElement;
private clipPath: SVGElement;
constants: ConstantProvider;
style: BlockStyle;
/** Highlight paths associated with connections. */
private connectionHighlights = new WeakMap<RenderedConnection, SVGElement>();
/** Locations of connection highlights. */
private highlightOffsets = new WeakMap<RenderedConnection, Coordinate>();
/**
* @param root The root SVG element.
* @param style The style object to use for colouring.
* @param constants The renderer's constants.
*/
constructor(
root: SVGElement,
style: BlockStyle,
constants: ConstantProvider,
) {
this.constants = constants;
this.style = style;
this.svgRoot = root;
/** The primary path of the block. */
this.svgPath = dom.createSvgElement(
Svg.PATH,
{
'class': 'blocklyPath',
// The page-wide unique ID of this Block used for focusing.
'id': idGenerator.getNextUniqueId(),
},
this.svgRoot,
);
// TODO: Move this to a <def>?
this.clip = dom.createSvgElement(
Svg.CLIPPATH,
{'id': `${this.svgPath.id}_clip`},
this.svgRoot,
);
this.clipPath = dom.createSvgElement(Svg.PATH, {}, this.clip);
this.svgPath.setAttribute('clip-path', `url(#${this.clip.id})`);
this.setClass_('blocklyBlock', true);
}
/**
* Set the path generated by the renderer onto the respective SVG element.
*
* @param pathString The path.
*/
setPath(pathString: string) {
this.svgPath.setAttribute('d', pathString);
this.clipPath.setAttribute('d', pathString);
}
/**
* Flip the SVG paths in RTL.
*/
flipRTL() {
// Mirror the block's path.
this.svgPath.setAttribute('transform', 'scale(-1 1)');
}
/**
* Apply the stored colours to the block's path, taking into account whether
* the paths belong to a shadow block.
*
* @param block The source block.
*/
applyColour(block: BlockSvg) {
this.svgPath.setAttribute('stroke', this.style.colourTertiary);
this.svgPath.setAttribute('stroke-width', '2px');
this.svgPath.setAttribute('fill', this.style.colourPrimary);
this.updateShadow_(block.isShadow());
this.updateDisabled_(!block.isEnabled() || block.getInheritedDisabled());
}
/**
* Set the style.
*
* @param blockStyle The block style to use.
*/
setStyle(blockStyle: BlockStyle) {
this.style = blockStyle;
}
/**
* Add or remove the given CSS class on the path object's root SVG element.
*
* @param className The name of the class to add or remove
* @param add True if the class should be added. False if it should be
* removed.
*/
protected setClass_(className: string, add: boolean) {
if (!className) {
return;
}
if (add) {
dom.addClass(this.svgRoot, className);
} else {
dom.removeClass(this.svgRoot, className);
}
}
/**
* Set whether the block shows a highlight or not. Block highlighting is
* often used to visually mark blocks currently being executed.
*
* @param enable True if highlighted.
*/
updateHighlighted(enable: boolean) {
if (enable) {
this.setClass_('blocklyHighlighted', true);
} else {
this.setClass_('blocklyHighlighted', false);
}
}
/**
* Updates the look of the block to reflect a shadow state.
*
* @param shadow True if the block is a shadow block.
*/
protected updateShadow_(shadow: boolean) {
if (shadow) {
this.setClass_('blocklyShadow', true);
this.svgPath.setAttribute('stroke', 'none');
this.svgPath.setAttribute('fill', this.style.colourSecondary);
} else {
this.setClass_('blocklyShadow', false);
}
}
/**
* Updates the look of the block to reflect a disabled state.
*
* @param disabled True if disabled.
*/
protected updateDisabled_(disabled: boolean) {
this.setClass_('blocklyDisabled', disabled);
this.setClass_('blocklyDisabledPattern', disabled);
}
/**
* Add or remove styling showing that a block is selected.
*
* @param enable True if selection is enabled, false otherwise.
*/
updateSelected(enable: boolean) {
this.setClass_('blocklySelected', enable);
}
/**
* Add or remove styling showing that a block is dragged over a delete area.
*
* @param enable True if the block is being dragged over a delete area, false
* otherwise.
*/
updateDraggingDelete(enable: boolean) {
this.setClass_('blocklyDraggingDelete', enable);
}
/**
* Add or remove styling showing that a block is an insertion marker.
*
* @param enable True if the block is an insertion marker, false otherwise.
*/
updateInsertionMarker(enable: boolean) {
this.setClass_('blocklyInsertionMarker', enable);
}
/**
* Add or remove styling showing that a block is movable.
*
* @param enable True if the block is movable, false otherwise.
*/
updateMovable(enable: boolean) {
this.setClass_('blocklyDraggable', enable);
}
/**
* Add or remove styling that shows that if the dragging block is dropped,
* this block will be replaced. If a shadow block, it will disappear.
* Otherwise it will bump.
*
* @param enable True if styling should be added.
*/
updateReplacementFade(enable: boolean) {
this.setClass_('blocklyReplaceable', enable);
}
/**
* Add or remove styling that shows that if the dragging block is dropped,
* this block will be connected to the input.
*
* @param _conn The connection on the input to highlight.
* @param _enable True if styling should be added.
*/
updateShapeForInputHighlight(_conn: Connection, _enable: boolean) {
// NOOP
}
/** Adds the given path as a connection highlight for the given connection. */
addConnectionHighlight(
connection: RenderedConnection,
connectionPath: string,
offset: Coordinate,
rtl: boolean,
): SVGElement {
const transformation =
`translate(${offset.x}, ${offset.y})` + (rtl ? ' scale(-1 1)' : '');
const previousHighlight = this.connectionHighlights.get(connection);
if (previousHighlight) {
// Since a connection already exists, make sure that its path and
// transform are correct.
previousHighlight.setAttribute('d', connectionPath);
previousHighlight.setAttribute('transform', transformation);
return previousHighlight;
}
const highlight = dom.createSvgElement(
Svg.PATH,
{
'id': connection.id,
'class': 'blocklyHighlightedConnectionPath',
'style': 'display: none;',
'd': connectionPath,
'transform': transformation,
},
this.svgRoot,
);
this.connectionHighlights.set(connection, highlight);
return highlight;
}
/**
* Removes any highlight associated with the given connection, if it exists.
*/
removeConnectionHighlight(connection: RenderedConnection) {
const highlight = this.connectionHighlights.get(connection);
if (!highlight) return;
dom.removeNode(highlight);
this.connectionHighlights.delete(connection);
}
}