-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathindex.ts
More file actions
254 lines (222 loc) · 6.79 KB
/
index.ts
File metadata and controls
254 lines (222 loc) · 6.79 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
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Grid dropdown field.
* @author kozbial@google.com (Monica Kozbial)
*/
import * as Blockly from 'blockly/core';
import {Grid} from './grid';
import type {GridItem} from './grid_item';
/**
* A config object for defining a field grid dropdown.
*/
export interface FieldGridDropdownConfig extends Blockly.FieldDropdownConfig {
columns?: string | number;
primaryColour?: string;
borderColour?: string;
}
/**
* Construct a FieldGridDropdown from a JSON arg object.
*/
export interface FieldGridDropdownFromJsonConfig
extends FieldGridDropdownConfig {
options?: Blockly.MenuGenerator;
}
type FieldGridDropdownValidator = Blockly.FieldDropdownValidator;
/**
* Grid dropdown field.
*/
export class FieldGridDropdown extends Blockly.FieldDropdown {
/**
* The number of columns in the dropdown grid. Must be an integer value
* greater than 0. Defaults to 3.
*/
private columns = 3;
private primaryColour?: string;
private borderColour?: string;
/** Object representing the grid of choices show in the dropdown. */
private grid?: Grid;
/**
* Class for an grid dropdown field.
*
* @param menuGenerator A non-empty array of options for a dropdown list,
* or a function which generates these options.
* @param validator A function that is called to validate
* changes to the field's value. Takes in a language-neutral dropdown
* option & returns a validated language-neutral dropdown option, or null
* to abort the change.
* @param config A map of options used to configure the field.
* See the [field creation documentation]{@link
* https://developers.google.com/blockly/guides/create-custom-blocks/fields/built-in-fields/dropdown#creation}
* for a list of properties this parameter supports.
* @extends {Blockly.Field}
* @constructor
* @throws {TypeError} If `menuGenerator` options are incorrectly structured.
*/
constructor(
menuGenerator: Blockly.MenuGenerator,
validator?: FieldGridDropdownValidator,
config?: FieldGridDropdownConfig,
) {
super(menuGenerator, validator, config);
if (config?.columns) {
this.setColumns(parseInt(`${config.columns}`));
}
if (config && config.primaryColour) {
this.primaryColour = config.primaryColour;
}
if (config && config.borderColour) {
this.borderColour = config.borderColour;
}
}
/**
* Constructs a FieldGridDropdown from a JSON arg object.
*
* @param config A JSON object with options.
* @returns The new field instance.
* @package
* @nocollapse
*/
static fromJson(config: FieldGridDropdownFromJsonConfig) {
if (!config.options) {
throw new Error(
'options are required for the dropdown field. The ' +
'options property must be assigned an array of ' +
'[humanReadableValue, languageNeutralValue] tuples.',
);
}
// `this` might be a subclass of FieldDropdown if that class doesn't
// override the static fromJson method.
return new this(config.options, undefined, config);
}
/**
* Sets the number of columns on the grid. Updates the styling to reflect.
*
* @param columns The number of columns. Is rounded to
* an integer value and must be greater than 0. Invalid
* values are ignored.
*/
setColumns(columns: number) {
if (!isNaN(columns) && columns >= 1) {
this.columns = columns;
// If the field is currently being shown, reload the grid.
if (
Blockly.DropDownDiv.getOwner() === this &&
Blockly.DropDownDiv.isVisible()
) {
this.grid?.dispose();
this.showEditor_();
}
}
}
/* eslint-disable @typescript-eslint/naming-convention */
/**
* Create a dropdown menu under the text.
*
* @param e Optional mouse event that triggered the field to open, or
* undefined if triggered programmatically.
*/
protected showEditor_(e?: MouseEvent) {
Blockly.DropDownDiv.clearContent();
const rtl = !!this.getSourceBlock()?.workspace.RTL;
this.grid = new Grid(
Blockly.DropDownDiv.getContentDiv(),
this.getOptions(false),
this.columns,
rtl,
(selectedItem: GridItem) => {
Blockly.DropDownDiv.hideIfOwner(this);
this.setValue(selectedItem.getValue());
},
);
Blockly.DropDownDiv.getContentDiv().classList.add(
'blocklyFieldGridContainer',
);
const colours = this.getColours();
if (colours && colours.border) {
Blockly.DropDownDiv.setColour(colours.primary, colours.border);
}
Blockly.DropDownDiv.showPositionedByField(
this,
this.dropdownDispose_.bind(this),
);
const selectedValue = this.getValue();
if (selectedValue) {
this.grid.setSelectedValue(selectedValue);
}
}
/**
* Updates the field's value to the given value.
*
* @param newValue The new value for this field.
*/
protected override doValueUpdate_(newValue: string) {
super.doValueUpdate_(newValue);
this.grid?.setSelectedValue(newValue);
}
/**
* Determine the colours for the dropdowndiv. The dropdown should match block
* colour unless other colours are specified in the config.
*
* @returns The colours to set for the dropdowndiv.
*/
private getColours() {
if (this.primaryColour && this.borderColour) {
return {
primary: this.primaryColour,
border: this.borderColour,
};
}
const sourceBlock = this.getSourceBlock();
if (!(sourceBlock instanceof Blockly.BlockSvg)) return;
const colourSource = sourceBlock.isShadow()
? sourceBlock.getParent()
: sourceBlock;
if (!colourSource) return;
return {
primary: this.primaryColour ?? colourSource.getColour(),
border: this.borderColour ?? colourSource.getColourTertiary(),
};
}
}
/** Register the field and any dependencies. */
export function registerFieldGridDropdown() {
Blockly.fieldRegistry.register('field_grid_dropdown', FieldGridDropdown);
}
/**
* CSS for grid field.
*/
Blockly.Css.register(`
.blocklyFieldGridContainer {
padding: 7px;
overflow: auto;
}
.blocklyFieldGrid {
display: grid;
grid-gap: 7px;
grid-template-columns: repeat(var(--grid-columns), min-content);
}
.blocklyFieldGrid .blocklyFieldGridItem {
border: 1px solid rgba(1, 1, 1, 0.5);
border-radius: 4px;
color: white;
min-width: auto;
background: none;
white-space: nowrap;
cursor: pointer;
padding: 6px 15px;
}
.blocklyFieldGrid .blocklyFieldGridRow {
display: contents;
}
.blocklyFieldGrid .blocklyFieldGridItem.blocklyFieldGridItemSelected {
background-color: rgba(1, 1, 1, 0.25);
}
.blocklyFieldGrid .blocklyFieldGridItem:focus {
box-shadow: 0 0 0 4px hsla(0, 0%, 100%, .2);
outline: none;
}
`);