Skip to content

Commit f294ad1

Browse files
committed
Moved code that register a custom toolbox categories into the function that returns the category.
Pass the editor to getToolboxJSON and other toolbox functions that need the editor or the blockly workspace or the current module type.
1 parent 84d5c1b commit f294ad1

File tree

7 files changed

+276
-294
lines changed

7 files changed

+276
-294
lines changed

src/editor/editor.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ import * as eventHandler from '../blocks/mrc_event_handler';
3232
import * as classMethodDef from '../blocks/mrc_class_method_def';
3333
import * as mechanismComponentHolder from '../blocks/mrc_mechanism_component_holder';
3434
//import { testAllBlocksInToolbox } from '../toolbox/toolbox_tests';
35-
import { registerCategory as registerMethodsCategory } from '../toolbox/methods_category';
36-
import { registerCategory as registerEventsCategory } from '../toolbox/event_category';
37-
import {
38-
registerRobotEventHandlersCategory,
39-
registerMechanismEventHandlersCategory
40-
} from '../toolbox/event_handlers_category';
4135
import { getToolboxJSON } from '../toolbox/toolbox';
4236

4337
const EMPTY_TOOLBOX: Blockly.utils.toolbox.ToolboxDefinition = {
@@ -67,10 +61,6 @@ export class Editor {
6761
this.blocklyWorkspace = blocklyWorkspace;
6862
this.generatorContext = generatorContext;
6963
this.storage = storage;
70-
// Register the custom toolbox categories.
71-
registerMethodsCategory(blocklyWorkspace);
72-
registerEventsCategory(blocklyWorkspace);
73-
registerRobotEventHandlersCategory(blocklyWorkspace);
7464
}
7565

7666
private onChangeWhileLoading(event: Blockly.Events.Abstract) {
@@ -177,10 +167,6 @@ export class Editor {
177167
? this.moduleContentText
178168
: modulePathToContentText[mechanism.modulePath]);
179169
}
180-
// Register the custom toolbox categories for the mechanisms in the robot.
181-
this.robotContent.getMechanisms().forEach(mechanismInRobot => {
182-
registerMechanismEventHandlersCategory(this.blocklyWorkspace, mechanismInRobot);
183-
});
184170
this.loadBlocksIntoBlocklyWorkspace();
185171
}
186172
}
@@ -220,9 +206,7 @@ export class Editor {
220206
}, 50);
221207
return;
222208
}
223-
this.setToolbox(
224-
getToolboxJSON(
225-
shownPythonToolboxCategories, this.currentModule));
209+
this.setToolbox(getToolboxJSON(shownPythonToolboxCategories, this));
226210
}
227211
}
228212

@@ -239,6 +223,10 @@ export class Editor {
239223
return this.getModuleContentText() !== this.moduleContentText;
240224
}
241225

226+
public getBlocklyWorkspace(): Blockly.WorkspaceSvg {
227+
return this.blocklyWorkspace;
228+
}
229+
242230
public getCurrentModuleType(): string {
243231
if (this.currentModule) {
244232
return this.currentModule.moduleType;
@@ -252,6 +240,7 @@ export class Editor {
252240
}
253241

254242
// Generate python because some parts of components, events, and methods are affected.
243+
extendedPythonGenerator.init(this.blocklyWorkspace);
255244
extendedPythonGenerator.mrcWorkspaceToCode(this.blocklyWorkspace, this.generatorContext);
256245

257246
const blocks = Blockly.serialization.workspaces.save(this.blocklyWorkspace);

src/toolbox/event_category.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ import { Editor } from '../editor/editor';
3030

3131
const CUSTOM_CATEGORY_EVENTS = 'MRC_EVENTS';
3232

33-
export function registerCategory(blocklyWorkspace: Blockly.WorkspaceSvg): void {
34-
new EventsCategory(blocklyWorkspace);
35-
}
33+
export function getCategory(editor: Editor): toolboxItems.Category {
34+
const blocklyWorkspace = editor.getBlocklyWorkspace();
3635

37-
export function getCategory(): toolboxItems.Category {
36+
// If this category hasn't been register yet, do it now.
37+
if (!blocklyWorkspace.getToolboxCategoryCallback(CUSTOM_CATEGORY_EVENTS)) {
38+
const category = new EventsCategory();
39+
blocklyWorkspace.registerToolboxCategoryCallback(CUSTOM_CATEGORY_EVENTS, category.eventsFlyout.bind(category));
40+
}
3841
return {
3942
kind: 'category',
4043
categorystyle: MRC_CATEGORY_STYLE_METHODS,
@@ -44,33 +47,31 @@ export function getCategory(): toolboxItems.Category {
4447
}
4548

4649
class EventsCategory {
47-
constructor(blocklyWorkspace: Blockly.WorkspaceSvg) {
48-
blocklyWorkspace.registerToolboxCategoryCallback(CUSTOM_CATEGORY_EVENTS, this.eventsFlyout.bind(this));
49-
}
50-
5150
public eventsFlyout(workspace: Blockly.WorkspaceSvg) {
51+
const editor = Editor.getEditorForBlocklyWorkspace(workspace);
52+
if (!editor) {
53+
throw new Error('No editor for blockly workspace');
54+
}
55+
5256
const contents: toolboxItems.ContentsType[] = [];
5357

54-
const editor = Editor.getEditorForBlocklyWorkspace(workspace);
55-
if (editor) {
56-
const eventsFromWorkspace = editor.getEventsFromWorkspace();
57-
const eventNames: string[] = [];
58-
eventsFromWorkspace.forEach(event => {
59-
eventNames.push(event.name);
60-
});
58+
const eventsFromWorkspace = editor.getEventsFromWorkspace();
59+
const eventNames: string[] = [];
60+
eventsFromWorkspace.forEach(event => {
61+
eventNames.push(event.name);
62+
});
6163

62-
// Add a block that lets the user define a new event.
63-
contents.push(
64-
{
65-
kind: 'label',
66-
text: 'Custom Events',
67-
},
68-
createCustomEventBlock(storageNames.makeUniqueName('my_event', eventNames))
69-
);
64+
// Add a block that lets the user define a new event.
65+
contents.push(
66+
{
67+
kind: 'label',
68+
text: 'Custom Events',
69+
},
70+
createCustomEventBlock(storageNames.makeUniqueName('my_event', eventNames))
71+
);
7072

71-
// Get blocks for firing methods defined in the current workspace.
72-
addFireEventBlocks(eventsFromWorkspace, contents);
73-
}
73+
// Get blocks for firing events defined in the current workspace.
74+
addFireEventBlocks(eventsFromWorkspace, contents);
7475

7576
const toolboxInfo = {
7677
contents: contents,

src/toolbox/event_handlers_category.ts

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -41,73 +41,72 @@ function getCustomValue(mechanismInRobot: storageModuleContent.MechanismInRobot
4141
// If the event is defined in the robot, mechanismInRobot is null.
4242
return (mechanismInRobot === null)
4343
? CUSTOM_CATEGORY_EVENT_HANDLERS_ROBOT
44-
: CUSTOM_CATEGORY_EVENT_HANDLERS_MECHANISM_PREFIX + mechanismInRobot.name;
44+
: CUSTOM_CATEGORY_EVENT_HANDLERS_MECHANISM_PREFIX + mechanismInRobot.mechanismId;
4545
}
4646

47-
export function registerRobotEventHandlersCategory(blocklyWorkspace: Blockly.WorkspaceSvg): void {
48-
new EventHandlersCategory(blocklyWorkspace, null);
49-
}
47+
export function getRobotEventHandlersCategory(editor: Editor): toolboxItems.Category {
48+
const blocklyWorkspace = editor.getBlocklyWorkspace();
5049

51-
export function getRobotEventHandlersCategory(): toolboxItems.Category {
50+
// If this category hasn't been register yet, do it now.
51+
const customValue = getCustomValue(null);
52+
if (!blocklyWorkspace.getToolboxCategoryCallback(customValue)) {
53+
const category = new EventHandlersCategory(null);
54+
blocklyWorkspace.registerToolboxCategoryCallback(customValue, category.robotEventHandlersFlyout.bind(category));
55+
}
5256
return {
5357
kind: 'category',
5458
name: Blockly.Msg['MRC_CATEGORY_EVENTS'],
55-
custom: getCustomValue(null),
59+
custom: customValue,
5660
};
5761
}
5862

59-
export function registerMechanismEventHandlersCategory(
60-
blocklyWorkspace: Blockly.WorkspaceSvg, mechanismInRobot: storageModuleContent.MechanismInRobot): void {
61-
new EventHandlersCategory(blocklyWorkspace, mechanismInRobot);
62-
}
63-
6463
export function getMechanismEventHandlersCategory(
64+
editor: Editor,
6565
mechanismInRobot: storageModuleContent.MechanismInRobot): toolboxItems.Category {
66+
const blocklyWorkspace = editor.getBlocklyWorkspace();
67+
68+
// If this category hasn't been register yet, do it now.
69+
const customValue = getCustomValue(mechanismInRobot);
70+
if (!blocklyWorkspace.getToolboxCategoryCallback(customValue)) {
71+
const category = new EventHandlersCategory(mechanismInRobot);
72+
blocklyWorkspace.registerToolboxCategoryCallback(customValue, category.mechanismEventHandlersFlyout.bind(category));
73+
}
6674
return {
6775
kind: 'category',
6876
name: Blockly.Msg['MRC_CATEGORY_EVENTS'],
69-
custom: getCustomValue(mechanismInRobot),
77+
custom: customValue,
7078
};
7179
}
7280

7381
class EventHandlersCategory {
7482
// If the event is defined in the robot, mechanismInRobot is null.
7583
mechanismInRobot: storageModuleContent.MechanismInRobot | null;
7684

77-
constructor(
78-
blocklyWorkspace: Blockly.WorkspaceSvg,
79-
mechanismInRobot: storageModuleContent.MechanismInRobot | null) {
85+
constructor(mechanismInRobot: storageModuleContent.MechanismInRobot | null) {
8086
this.mechanismInRobot = mechanismInRobot;
81-
if (mechanismInRobot === null) {
82-
blocklyWorkspace.registerToolboxCategoryCallback(
83-
getCustomValue(mechanismInRobot),
84-
this.robotEventHandlersFlyout.bind(this));
85-
} else {
86-
blocklyWorkspace.registerToolboxCategoryCallback(
87-
getCustomValue(mechanismInRobot),
88-
this.mechanismEventHandlersFlyout.bind(this));
89-
}
9087
}
9188

9289
public robotEventHandlersFlyout(workspace: Blockly.WorkspaceSvg) {
93-
const contents: toolboxItems.ContentsType[] = [];
94-
9590
const editor = Editor.getEditorForBlocklyWorkspace(workspace);
96-
if (editor) {
97-
// Get the list of events from the robot.
98-
const eventsFromRobot = editor.getEventsFromRobot();
99-
// Remove events if there is already a corresponding handler in the workspace.
100-
const eventHandlerBlocks = editor.getRobotEventHandlersAlreadyInWorkspace();
101-
const eventIds: string[] = [];
102-
eventHandlerBlocks.forEach(eventHandlerBlock => {
103-
eventIds.push(eventHandlerBlock.getEventId());
104-
});
105-
const eventsToShow = eventsFromRobot.filter(event => {
106-
return !eventIds.includes(event.eventId);
107-
});
108-
addRobotEventHandlerBlocks(eventsToShow, contents);
91+
if (!editor) {
92+
throw new Error('No editor for blockly workspace');
10993
}
11094

95+
const contents: toolboxItems.ContentsType[] = [];
96+
97+
// Get the list of events from the robot.
98+
const eventsFromRobot = editor.getEventsFromRobot();
99+
// Remove events if there is already a corresponding handler in the workspace.
100+
const eventHandlerBlocks = editor.getRobotEventHandlersAlreadyInWorkspace();
101+
const eventIds: string[] = [];
102+
eventHandlerBlocks.forEach(eventHandlerBlock => {
103+
eventIds.push(eventHandlerBlock.getEventId());
104+
});
105+
const eventsToShow = eventsFromRobot.filter(event => {
106+
return !eventIds.includes(event.eventId);
107+
});
108+
addRobotEventHandlerBlocks(eventsToShow, contents);
109+
111110
const toolboxInfo = {
112111
contents: contents,
113112
};
@@ -116,29 +115,34 @@ class EventHandlersCategory {
116115
}
117116

118117
public mechanismEventHandlersFlyout(workspace: Blockly.WorkspaceSvg) {
118+
const editor = Editor.getEditorForBlocklyWorkspace(workspace);
119+
if (!editor) {
120+
throw new Error('No editor for blockly workspace');
121+
}
122+
if (!this.mechanismInRobot) {
123+
throw new Error('mechanismInRobot is null');
124+
}
125+
119126
const contents: toolboxItems.ContentsType[] = [];
120127

121-
const editor = Editor.getEditorForBlocklyWorkspace(workspace);
122-
if (editor && this.mechanismInRobot) {
123-
// Get the list of events from the mechanism.
124-
const mechanism = editor.getMechanism(this.mechanismInRobot);
125-
if (mechanism) {
126-
const eventsFromMechanism = editor.getEventsFromMechanism(mechanism);
127-
// Remove events if there is already a corresponding handler in the workspace.
128-
const eventHandlerBlocks = editor.getMechanismEventHandlersAlreadyInWorkspace(
129-
this.mechanismInRobot);
130-
const eventIds: string[] = [];
131-
eventHandlerBlocks.forEach(eventHandlerBlock => {
132-
eventIds.push(eventHandlerBlock.getEventId());
133-
});
134-
const eventsToShow = eventsFromMechanism.filter(event => {
135-
return !eventIds.includes(event.eventId);
136-
});
137-
addMechanismEventHandlerBlocks(this.mechanismInRobot, eventsToShow, contents);
138-
if (contents.length === 0) {
139-
const label : toolboxItems.Label = new toolboxItems.Label(Blockly.Msg['NO_MECHANISM_CONTENTS']);
140-
contents.push(label);
141-
}
128+
// Get the list of events from the mechanism.
129+
const mechanism = editor.getMechanism(this.mechanismInRobot);
130+
if (mechanism) {
131+
const eventsFromMechanism = editor.getEventsFromMechanism(mechanism);
132+
// Remove events if there is already a corresponding handler in the workspace.
133+
const eventHandlerBlocks = editor.getMechanismEventHandlersAlreadyInWorkspace(
134+
this.mechanismInRobot);
135+
const eventIds: string[] = [];
136+
eventHandlerBlocks.forEach(eventHandlerBlock => {
137+
eventIds.push(eventHandlerBlock.getEventId());
138+
});
139+
const eventsToShow = eventsFromMechanism.filter(event => {
140+
return !eventIds.includes(event.eventId);
141+
});
142+
addMechanismEventHandlerBlocks(this.mechanismInRobot, eventsToShow, contents);
143+
if (contents.length === 0) {
144+
const label : toolboxItems.Label = new toolboxItems.Label(Blockly.Msg['NO_MECHANISM_CONTENTS']);
145+
contents.push(label);
142146
}
143147
}
144148

0 commit comments

Comments
 (0)