-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathicon.ts
More file actions
222 lines (189 loc) · 6.38 KB
/
Copy pathicon.ts
File metadata and controls
222 lines (189 loc) · 6.38 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
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type {Block} from '../block.js';
import type {BlockSvg} from '../block_svg.js';
import * as browserEvents from '../browser_events.js';
import {getFocusManager} from '../focus_manager.js';
import type {IContextMenu} from '../interfaces/i_contextmenu.js';
import type {IFocusableTree} from '../interfaces/i_focusable_tree.js';
import {hasBubble} from '../interfaces/i_has_bubble.js';
import type {IIcon} from '../interfaces/i_icon.js';
import * as renderManagement from '../render_management.js';
import * as tooltip from '../tooltip.js';
import {Coordinate} from '../utils/coordinate.js';
import * as dom from '../utils/dom.js';
import * as idGenerator from '../utils/idgenerator.js';
import {Rect} from '../utils/rect.js';
import {Size} from '../utils/size.js';
import {Svg} from '../utils/svg.js';
import type {WorkspaceSvg} from '../workspace_svg.js';
import type {IconType} from './icon_types.js';
/**
* The abstract icon class. Icons are visual elements that live in the top-start
* corner of the block. Usually they provide more "meta" information about a
* block (such as warnings or comments) as opposed to fields, which provide
* "actual" information, related to how a block functions.
*/
export abstract class Icon implements IIcon, IContextMenu {
/**
* The position of this icon relative to its blocks top-start,
* in workspace units.
*/
protected offsetInBlock: Coordinate = new Coordinate(0, 0);
/** The position of this icon in workspace coordinates. */
protected workspaceLocation: Coordinate = new Coordinate(0, 0);
/** The root svg element visually representing this icon. */
protected svgRoot: SVGGElement | null = null;
/** The tooltip for this icon. */
protected tooltip: tooltip.TipInfo;
/** The unique ID of this icon. */
private id: string;
constructor(protected sourceBlock: Block) {
this.tooltip = sourceBlock;
this.id = idGenerator.getNextUniqueId();
}
getType(): IconType<IIcon> {
throw new Error('Icons must implement getType');
}
initView(pointerdownListener: (e: PointerEvent) => void): void {
if (this.svgRoot) return; // The icon has already been initialized.
const svgBlock = this.sourceBlock as BlockSvg;
this.svgRoot = dom.createSvgElement(Svg.G, {
'class': 'blocklyIconGroup',
'id': this.id,
});
svgBlock.getSvgRoot().appendChild(this.svgRoot);
this.updateSvgRootOffset();
browserEvents.conditionalBind(
this.svgRoot,
'pointerdown',
this,
pointerdownListener,
);
(this.svgRoot as any).tooltip = this;
tooltip.bindMouseEvents(this.svgRoot);
}
dispose(): void {
tooltip.unbindMouseEvents(this.svgRoot);
dom.removeNode(this.svgRoot);
}
getWeight(): number {
return -1;
}
getSize(): Size {
return new Size(0, 0);
}
/**
* Sets the tooltip for this icon to the given value. Null to show the
* tooltip of the block.
*/
setTooltip(tip: tooltip.TipInfo | null) {
this.tooltip = tip ?? this.sourceBlock;
}
/** Returns the tooltip for this icon. */
getTooltip(): tooltip.TipInfo {
return this.tooltip;
}
applyColour(): void {}
updateEditable(): void {}
updateCollapsed(): void {
if (!this.svgRoot) return;
if (this.sourceBlock.isCollapsed()) {
this.svgRoot.style.display = 'none';
} else {
this.svgRoot.style.display = 'block';
}
if (hasBubble(this)) {
this.setBubbleVisible(false);
}
}
hideForInsertionMarker(): void {
if (!this.svgRoot) return;
this.svgRoot.style.display = 'none';
}
isShownWhenCollapsed(): boolean {
return false;
}
setOffsetInBlock(offset: Coordinate): void {
this.offsetInBlock = offset;
this.updateSvgRootOffset();
}
private updateSvgRootOffset(): void {
this.svgRoot?.setAttribute(
'transform',
`translate(${this.offsetInBlock.x}, ${this.offsetInBlock.y})`,
);
}
onLocationChange(blockOrigin: Coordinate): void {
this.workspaceLocation = Coordinate.sum(blockOrigin, this.offsetInBlock);
}
onClick(): void {}
/**
* Check whether the icon should be clickable while the block is in a flyout.
* The default is that icons are clickable in all flyouts (auto-closing or not).
* Subclasses may override this function to change this behavior.
*
* @param autoClosingFlyout true if the containing flyout is an auto-closing one.
* @returns Whether the icon should be clickable while the block is in a flyout.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
isClickableInFlyout(autoClosingFlyout: boolean): boolean {
return true;
}
/** See IFocusableNode.getFocusableElement. */
getFocusableElement(): HTMLElement | SVGElement {
const svgRoot = this.svgRoot;
if (!svgRoot) throw new Error('Attempting to focus uninitialized icon.');
return svgRoot;
}
/** See IFocusableNode.getFocusableTree. */
getFocusableTree(): IFocusableTree {
return this.sourceBlock.workspace as WorkspaceSvg;
}
/** See IFocusableNode.onNodeFocus. */
onNodeFocus(): void {
const blockBounds = (this.sourceBlock as BlockSvg).getBoundingRectangle();
const bounds = new Rect(
blockBounds.top + this.offsetInBlock.y,
blockBounds.top + this.offsetInBlock.y + this.getSize().height,
blockBounds.left + this.offsetInBlock.x,
blockBounds.left + this.offsetInBlock.x + this.getSize().width,
);
(this.sourceBlock as BlockSvg).workspace.scrollBoundsIntoView(bounds);
}
/** See IFocusableNode.onNodeBlur. */
onNodeBlur(): void {}
/** See IFocusableNode.canBeFocused. */
canBeFocused(): boolean {
return true;
}
/**
* Handles the user acting on this icon via keyboard navigation.
* Performs the same action as a click would, and focuses this icon's bubble
* if it has one.
*/
performAction() {
this.onClick();
renderManagement.finishQueuedRenders().then(() => {
if (hasBubble(this) && this.bubbleIsVisible()) {
const bubble = this.getBubble();
if (!bubble) return;
getFocusManager().focusNode(bubble);
}
});
}
/**
* Returns the block that this icon is attached to.
*
* @returns The block this icon is attached to.
*/
getSourceBlock(): Block {
return this.sourceBlock;
}
showContextMenu(e: PointerEvent) {
(this.getSourceBlock() as BlockSvg).showContextMenu(e);
}
}