forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolboxeditor.tsx
More file actions
348 lines (298 loc) · 14.5 KB
/
toolboxeditor.tsx
File metadata and controls
348 lines (298 loc) · 14.5 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import * as srceditor from "./srceditor";
import * as toolbox from "./toolbox";
import * as compiler from "./compiler";
export abstract class ToolboxEditor extends srceditor.Editor {
protected blockInfo: pxtc.BlocksInfo;
private searchSubset: pxt.Map<boolean | string>;
protected toolbox: toolbox.Toolbox;
protected extensions: pxt.Package[];
abstract getBlocksForCategory(ns: string, subns?: string): toolbox.BlockDefinition[];
protected shouldShowBlock(blockId: string, ns: string, shadow?: boolean) {
const filters = this.parent.state.editorState && this.parent.state.editorState.filters;
if (filters) {
// block-level filters should not apply to shadow blocks (nested)
const blockFilter = filters.blocks && filters.blocks[blockId];
const categoryFilter = filters.namespaces && filters.namespaces[ns];
// First try block filters
if (blockFilter != undefined && blockFilter == pxt.editor.FilterState.Hidden && !shadow) return false;
if (blockFilter != undefined) return true;
// Check if category is hidden
if (categoryFilter != undefined && categoryFilter == pxt.editor.FilterState.Hidden) return false;
if (categoryFilter != undefined) return true;
// Check default filter state
if (filters.defaultState != undefined && filters.defaultState == pxt.editor.FilterState.Hidden && !shadow) return false;
}
return true;
}
protected shouldShowCustomCategory(ns: string) {
const filters = this.parent.state.editorState && this.parent.state.editorState.filters;
if (filters) {
// These categories are special and won't have any children so we need to check the filters manually
if (ns === "variables" && (!filters.blocks ||
filters.blocks["variables_set"] ||
filters.blocks["variables_get"] ||
filters.blocks["variables_change"]) &&
(!filters.namespaces || filters.namespaces["variables"] !== pxt.editor.FilterState.Disabled)) {
return true;
} else if (ns === "functions" && (!filters.blocks ||
filters.blocks["function_definition"] ||
filters.blocks["procedures_defnoreturn"] ||
filters.blocks["procedures_callnoreturn"]) &&
(!filters.namespaces || filters.namespaces["functions"] !== pxt.editor.FilterState.Disabled)) {
return true;
} else {
return false;
}
}
return true;
}
getSearchSubset() {
if (!this.searchSubset && this.blockInfo) {
this.searchSubset = {};
const searchSubset = this.searchSubset;
// Go through all built in blocks
let blockDefinitions = pxt.blocks.blockDefinitions();
for (const id in blockDefinitions) {
const blockDef = blockDefinitions[id];
if (this.shouldShowBlock(id, blockDef.category)) {
// Add to search subset
searchSubset[id] = true;
}
}
// Go through all blocks and apply filter
this.blockInfo.blocks.forEach(fn => {
let ns = (fn.attributes.blockNamespace || fn.namespace).split('.')[0];
if (fn.attributes.debug && !pxt.options.debug) return;
if (fn.attributes.deprecated || fn.attributes.blockHidden) return;
if (this.shouldShowBlock(fn.attributes.blockId, ns)) {
// Add to search subset
searchSubset[fn.attributes.blockId] = true;
}
});
}
return this.searchSubset;
}
searchAsync(searchTerm: string): Promise<pxtc.service.SearchInfo[]> {
const searchOptions: pxtc.service.SearchOptions = {
term: searchTerm,
subset: this.getSearchSubset()
};
return compiler.apiSearchAsync(searchOptions)
.then((fns: pxtc.service.SearchInfo[]) => fns)
.then(searchResults => {
pxt.debug("searching for: " + searchTerm);
return searchResults;
});
}
protected clearCaches() {
this.searchSubset = undefined;
}
abstract getBuiltinCategory(ns: string): toolbox.ToolboxCategory;
abstract isBuiltIn(ns: string): boolean;
public getAllCategories() {
return this.getToolboxCategories(false).concat(this.getToolboxCategories(true));
}
public getToolboxCategories(isAdvanced?: boolean) {
if (!this.blockInfo) return [];
let that = this;
function filterNamespaces(namespaces: [string, pxtc.CommentAttrs][]) {
return namespaces.filter(([, md]) => !md.deprecated && (isAdvanced ? md.advanced : !md.advanced));
}
const namespaces = filterNamespaces(this.getNamespaces()
.map(ns => [ns, that.getNamespaceAttrs(ns)] as [string, pxtc.CommentAttrs]));
function createSubCategories(parent: [string, pxtc.CommentAttrs], names: string[], isAdvanced?: boolean): toolbox.ToolboxCategory[] {
return names.map(subns => {
const ns = parent[0];
const md = parent[1];
// Don't show subcategory if there are no blocks to show
const blocks = that.getBlocksForCategory(ns, subns).filter(block => that.shouldShowBlock(block.attributes.blockId, ns));
if (!blocks.length) return undefined;
const name = pxt.Util.rlf(`{id:subcategory}${subns}`);
return {
nameid: ns,
name,
subns: subns,
color: md.color,
icon: md.icon,
groups: md.groups,
groupIcons: md.groupIcons,
groupHelp: md.groupHelp,
labelLineWidth: md.labelLineWidth,
blocks: blocks,
advanced: isAdvanced
}
}).filter(subns => !!subns);
}
function createCategories(names: [string, pxtc.CommentAttrs][], isAdvanced?: boolean): toolbox.ToolboxCategory[] {
return names
.sort(([, md1], [, md2]) => {
// sort by fn weight
const w2 = (md2 ? md2.weight || 50 : 50);
const w1 = (md1 ? md1.weight || 50 : 50);
return w2 >= w1 ? 1 : -1;
}).map(([ns, md]) => {
const isBuiltIn = that.isBuiltIn(ns);
const builtInCategory = isBuiltIn ? that.getBuiltinCategory(ns) : undefined;
// We need the blocks to figure out if all blocks are hidden
const blocks = that.getBlocksForCategory(ns).filter(block => that.shouldShowBlock(block.attributes.blockId, ns));
const hasExtensionButtons = that.extensionsMap[ns];
const hasCustomClick = builtInCategory && builtInCategory.customClick;
let subcategories: toolbox.ToolboxCategory[];
if ((md.subcategories && md.subcategories.length) || that.subcategoryMap[ns]) {
subcategories = createSubCategories([ns, md], md.subcategories || Object.keys(that.subcategoryMap[ns]), isAdvanced);
}
const hasBlocks = blocks.length || hasExtensionButtons || hasCustomClick || (subcategories && subcategories.length);
// Don't show the category if there are no blocks in it
if (!hasBlocks) return undefined;
if (hasCustomClick) {
// Ensure that we need to show this custom category
if (!that.shouldShowCustomCategory(ns)) return undefined;
}
// Prepare the category
let category: toolbox.ToolboxCategory = {
nameid: ns,
name: md.block ? md.block : undefined,
color: md.color,
icon: md.icon,
groups: md.groups,
groupIcons: md.groupIcons,
groupHelp: md.groupHelp,
labelLineWidth: md.labelLineWidth,
blocks: blocks,
subcategories: subcategories,
advanced: isAdvanced
};
// Apply specific builtin customizations
if (isBuiltIn) {
category.name = builtInCategory.name;
category.icon = md.icon ? pxt.toolbox.getNamespaceIcon(md.icon)
|| md.icon : pxt.toolbox.getNamespaceIcon(ns);
category.groups = builtInCategory.groups || md.groups;
category.customClick = builtInCategory.customClick;
}
return category;
}).filter(cat => !!cat);
}
return createCategories(namespaces, isAdvanced);
}
abstract showFlyout(treeRow: toolbox.ToolboxCategory): void;
abstract hideFlyout(): void;
moveFocusToFlyout() { }
protected abstract showFlyoutHeadingLabel(ns: string, name: string, subns: string, icon: string, color: string): void;
protected abstract showFlyoutGroupLabel(group: string, groupicon: string, labelLineWidth: string, helpCallback: string): void;
protected abstract showFlyoutBlocks(ns: string, color: string, blocks: toolbox.BlockDefinition[]): void;
abstractShowFlyout(treeRow: toolbox.ToolboxCategory): boolean {
const { nameid: ns, name, subns, icon, color, groups, groupIcons, groupHelp, labelLineWidth, blocks } = treeRow;
const inTutorial = this.parent.state.tutorialOptions
&& !!this.parent.state.tutorialOptions.tutorial;
let fns = blocks;
if (!fns || !fns.length) return false;
if (!pxt.appTarget.appTheme.hideFlyoutHeadings) {
// Add the Heading label
this.showFlyoutHeadingLabel(ns, name, subns, icon, color);
}
// Organize and rearrange methods into groups
let blockGroups: pxt.Map<toolbox.BlockDefinition[]> = {}
let sortedGroups: string[] = [];
if (groups) sortedGroups = groups;
// Create a dict of group icon pairs
let groupIconsDict: { [group: string]: string } = {}
if (groups && groupIcons) {
let groupIconsList = groupIcons;
for (let i = 0; i < sortedGroups.length; i++) {
let groupIcon = groupIconsList[i];
groupIconsDict[sortedGroups[i]] = groupIcon || '';
}
}
// Create a dict of group help callback pairs
let groupHelpDict: { [group: string]: string } = {}
if (groups && groupHelp) {
let groupHelpCallbackList = groupHelp;
for (let i = 0; i < sortedGroups.length; i++) {
let helpCallback = groupHelpCallbackList[i];
groupHelpDict[sortedGroups[i]] = helpCallback || '';
}
}
// Organize the blocks into the different groups
for (let bi = 0; bi < fns.length; ++bi) {
let blk = fns[bi];
let group = blk.attributes.group || 'other';
if (!blockGroups[group]) blockGroups[group] = [];
blockGroups[group].push(blk);
}
const groupLength = Object.keys(blockGroups).length;
if (groupLength > 1) {
// Add any missing groups to the sorted groups list
Object.keys(blockGroups).sort().forEach(group => {
if (sortedGroups.indexOf(group) == -1) {
sortedGroups.push(group);
}
})
// Add labels and insert the blocks into the flyout
for (let bg = 0; bg < sortedGroups.length; ++bg) {
let group = sortedGroups[bg];
// Check if there are any blocks in that group
if (!blockGroups[group] || !blockGroups[group].length) continue;
// Add the group label
if (group != 'other' && !inTutorial) {
this.showFlyoutGroupLabel(group, groupIconsDict[group], labelLineWidth, groupHelpDict[group]);
}
// Add the blocks in that group
if (blockGroups[group]) {
this.showFlyoutBlocks(ns, color, blockGroups[group]);
}
}
} else if (groupLength == 1) {
Object.keys(blockGroups).forEach(blockGroup => {
this.showFlyoutBlocks(ns, color, blockGroups[blockGroup]);
})
}
return true;
}
protected extensionsMap: pxt.Map<pxt.PackageConfig> = {};
protected subcategoryMap: pxt.Map<pxt.Map<boolean>> = {};
protected topBlocks: toolbox.BlockDefinition[] = [];
// To be extended by editor
getNamespaceAttrs(ns: string): pxtc.CommentAttrs {
const info = this.blockInfo.apis.byQName[ns];
if (info) {
return info.attributes;
}
if (this.extensionsMap[ns]) {
const config = this.extensionsMap[ns];
return {
weight: 0,
blockId: config.name,
color: config.extension.color || '#7f8c8d',
advanced: config.extension.advanced || false,
callingConvention: ts.pxtc.ir.CallingConvention.Plain,
paramDefl: {}
}
}
return undefined;
}
// To be extended by editor
getNamespaces() {
const namespaces: string[] = [];
// Add extension namespaces if not already in
this.extensions.forEach(p => {
const config = p.config;
const name = config.name;
const namespace = config.extension.namespace || name;
if (!this.extensionsMap[namespace]) this.extensionsMap[namespace] = config;
if (!namespaces.filter(ns => ns == namespace)) {
namespaces.push(name);
}
})
return namespaces;
}
getTopBlocks(): toolbox.BlockDefinition[] {
// Order top blocks by weight
return this.topBlocks.sort((fn1, fn2) => {
// sort by fn weight
const w1 = fn1.attributes.topblockWeight || fn1.attributes.weight || 50;
const w2 = fn2.attributes.topblockWeight || fn2.attributes.weight || 50;
return w2 >= w1 ? 1 : -1;
});
}
}