-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathindex.ts
More file actions
301 lines (272 loc) · 8.41 KB
/
index.ts
File metadata and controls
301 lines (272 loc) · 8.41 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview A plugin that highlights the content on the workspace.
*/
import * as Blockly from 'blockly/core';
/**
* List of events that cause a change in content area size.
*/
const contentChangeEvents = [
Blockly.Events.VIEWPORT_CHANGE,
Blockly.Events.BLOCK_MOVE,
Blockly.Events.BLOCK_DELETE,
Blockly.Events.COMMENT_MOVE,
Blockly.Events.COMMENT_RESIZE,
Blockly.Events.COMMENT_CREATE,
Blockly.Events.COMMENT_DELETE,
];
/**
* The default padding to use for the content highlight in workspace units.
*/
const defaultPadding = 10;
/**
* Length of opacity change transition in seconds.
*/
const animationTime = 0.25;
/**
* A plugin that highlights the area where content exists on the workspace.
*/
export class ContentHighlight {
/**
* The width of the highlight rectangle in workspace units.
*/
private width = 0;
/**
* The height of the highlight rectangle in workspace units.
*/
private height = 0;
/**
* The top offset of the highlight rectangle in pixels.
*/
private top = 0;
/**
* The left offset of the highlight rectangle in pixels.
*/
private left = 0;
/**
* The last scale value applied on the content highlight.
*/
private lastScale = 1;
/**
* The cached content metrics for the workspace in workspace units.
*/
private cachedContentMetrics?: Blockly.MetricsManager.ContainerRegion;
/**
* The padding to use around the content area.
*/
private padding = defaultPadding;
private svgGroup?: SVGGElement;
private rect?: SVGRectElement;
private background?: SVGRectElement;
private onChangeWrapper?: (event: Blockly.Events.Abstract) => void;
/**
* Constructor for the content highlight plugin.
*
* @param workspace The workspace that the plugin will be added to.
*/
constructor(protected workspace: Blockly.WorkspaceSvg) {}
/**
* Initializes the plugin.
*
* @param padding The padding to use for the content area highlight
* rectangle, in workspace units.
*/
init(padding?: number) {
this.padding = padding || defaultPadding;
/** @type {SVGElement} */
this.svgGroup = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.G,
{class: 'contentAreaHighlight'},
null,
);
const rnd = String(Math.random()).substring(2);
const mask = Blockly.utils.dom.createSvgElement(
new Blockly.utils.Svg('mask'),
{
id: 'contentAreaHighlightMask' + rnd,
},
this.svgGroup,
);
Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.RECT,
{
x: 0,
y: 0,
width: '100%',
height: '100%',
fill: 'white',
},
mask,
);
this.rect = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.RECT,
{
x: 0,
y: 0,
rx: Blockly.bubbles.Bubble.BORDER_WIDTH,
ry: Blockly.bubbles.Bubble.BORDER_WIDTH,
fill: 'black',
},
mask,
);
this.background = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.RECT,
{
x: 0,
y: 0,
width: '100%',
height: '100%',
mask: `url(#contentAreaHighlightMask${rnd})`,
},
this.svgGroup,
);
this.applyColor();
const metricsManager = this.workspace.getMetricsManager();
this.cachedContentMetrics = metricsManager.getContentMetrics(true);
this.resize(this.cachedContentMetrics);
const absoluteMetrics = metricsManager.getAbsoluteMetrics();
this.position(this.cachedContentMetrics, absoluteMetrics);
// Apply transition animation for opacity changes.
this.svgGroup.style.transition = `opacity ${animationTime}s`;
const parentSvg = this.workspace.getParentSvg();
if (parentSvg.firstChild) {
parentSvg.insertBefore(this.svgGroup, parentSvg.firstChild);
} else {
parentSvg.appendChild(this.svgGroup);
}
this.onChangeWrapper = this.onChange.bind(this);
this.workspace.addChangeListener(this.onChangeWrapper);
}
/**
* Disposes of content highlight.
* Unlinks from all DOM elements and remove all event listeners
* to prevent memory leaks.
*/
dispose() {
if (this.svgGroup) {
Blockly.utils.dom.removeNode(this.svgGroup);
}
if (this.onChangeWrapper) {
this.workspace.removeChangeListener(this.onChangeWrapper);
}
}
/**
* Handles events triggered on the workspace.
*
* @param event The event.
*/
private onChange(event: Blockly.Events.Abstract) {
if (event.type === Blockly.Events.THEME_CHANGE) {
this.applyColor();
} else if (contentChangeEvents.indexOf(event.type) !== -1) {
const metricsManager = this.workspace.getMetricsManager();
if (event.type !== Blockly.Events.VIEWPORT_CHANGE) {
// The content metrics change when it's not a viewport change event.
this.cachedContentMetrics = metricsManager.getContentMetrics(true);
this.resize(this.cachedContentMetrics);
}
const absoluteMetrics = metricsManager.getAbsoluteMetrics();
if (this.cachedContentMetrics) {
this.position(this.cachedContentMetrics, absoluteMetrics);
}
} else if (event.type === Blockly.Events.BLOCK_DRAG) {
this.handleDragEvent(event as Blockly.Events.BlockDrag);
} else if (event.type === Blockly.Events.COMMENT_DRAG) {
this.handleDragEvent(event as Blockly.Events.CommentDrag);
} else if (event.type === Blockly.Events.BLOCK_CHANGE) {
// Resizes the content highlight when it is a block change event
const metricsManager = this.workspace.getMetricsManager();
this.cachedContentMetrics = metricsManager.getContentMetrics(true);
this.resize(this.cachedContentMetrics);
}
}
/**
* Changes opacity of the highlight based on what kind of block drag event
* is passed.
*
* @param event The BlockDrag event.
*/
private handleDragEvent(
event: Blockly.Events.BlockDrag | Blockly.Events.CommentDrag,
) {
const opacity = event.isStart ? '0' : '1';
this.svgGroup?.setAttribute('opacity', opacity);
}
/**
* Applies the color fill for the highlight based on the current theme.
*/
private applyColor() {
const theme = this.workspace.getTheme();
const bgColor =
theme.getComponentStyle('workspaceBackgroundColour') || '#ffffff';
const colorDarkened = Blockly.utils.colour.blend('#000', bgColor, 0.1);
const colorLightened = Blockly.utils.colour.blend('#fff', bgColor, 0.1);
const color =
bgColor === '#ffffff' || bgColor === '#fff'
? colorDarkened
: colorLightened;
if (!color) return;
this.background?.setAttribute('fill', color);
}
/**
* Resizes the content highlight.
*
* @param contentMetrics The content metrics for the workspace in workspace
* coordinates.
*/
private resize(contentMetrics: Blockly.MetricsManager.ContainerRegion) {
const width = contentMetrics.width
? contentMetrics.width + 2 * this.padding
: 0;
const height = contentMetrics.height
? contentMetrics.height + 2 * this.padding
: 0;
if (width !== this.width) {
this.width = width;
this.rect?.setAttribute('width', `${width}`);
}
if (height !== this.height) {
this.height = height;
this.rect?.setAttribute('height', `${height}`);
}
}
/**
* Positions the highlight on the workspace based on the workspace metrics.
*
* @param contentMetrics The content metrics for the workspace in workspace
* coordinates.
* @param absoluteMetrics The absolute metrics for the workspace.
*/
private position(
contentMetrics: Blockly.MetricsManager.ContainerRegion,
absoluteMetrics: Blockly.MetricsManager.AbsoluteMetrics,
) {
// Compute top/left manually to avoid unnecessary extra computation.
const viewTop = -this.workspace.scrollY;
const viewLeft = -this.workspace.scrollX;
const scale = this.workspace.scale;
const top =
absoluteMetrics.top +
contentMetrics.top * scale -
viewTop -
this.padding * scale;
const left =
absoluteMetrics.left +
contentMetrics.left * scale -
viewLeft -
this.padding * scale;
if (top !== this.top || left !== this.left || this.lastScale !== scale) {
this.top = top;
this.left = left;
this.lastScale = scale;
this.rect?.setAttribute(
'transform',
`translate(${left}, ${top}) scale(${scale})`,
);
}
}
}