Skip to content

Commit addaad8

Browse files
committed
Add eventId to extra state of mrc_event block.
Fixed indentation in onBlockChanged and getEvent methods.
1 parent 61405a2 commit addaad8

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

src/blocks/mrc_event.ts

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,22 @@ type Parameter = {
4141
};
4242

4343
type EventExtraState = {
44+
eventId?: string,
4445
params?: Parameter[],
4546
}
4647

4748
export type EventBlock = Blockly.Block & EventMixin & Blockly.BlockSvg;
4849

4950
interface EventMixin extends EventMixinType {
51+
mrcEventId: string,
5052
mrcParameters: Parameter[],
5153
}
5254
type EventMixinType = typeof EVENT;
5355

5456
const EVENT = {
5557
/**
56-
* Block initialization.
57-
*/
58+
* Block initialization.
59+
*/
5860
init: function (this: EventBlock): void {
5961
this.setStyle(MRC_STYLE_EVENTS);
6062
this.appendDummyInput("TITLE")
@@ -66,10 +68,11 @@ const EVENT = {
6668
},
6769

6870
/**
69-
* Returns the state of this block as a JSON serializable object.
70-
*/
71+
* Returns the state of this block as a JSON serializable object.
72+
*/
7173
saveExtraState: function (this: EventBlock): EventExtraState {
7274
const extraState: EventExtraState = {
75+
eventId: this.mrcEventId,
7376
};
7477
extraState.params = [];
7578
if (this.mrcParameters) {
@@ -83,9 +86,10 @@ const EVENT = {
8386
return extraState;
8487
},
8588
/**
86-
* Applies the given state to this block.
87-
*/
89+
* Applies the given state to this block.
90+
*/
8891
loadExtraState: function (this: EventBlock, extraState: EventExtraState): void {
92+
this.mrcEventId = extraState.eventId ? extraState.eventId : this.id;
8993
this.mrcParameters = [];
9094

9195
if (extraState.params) {
@@ -100,8 +104,8 @@ const EVENT = {
100104
this.updateBlock_();
101105
},
102106
/**
103-
* Update the block to reflect the newly loaded extra state.
104-
*/
107+
* Update the block to reflect the newly loaded extra state.
108+
*/
105109
updateBlock_: function (this: EventBlock): void {
106110
const name = this.getFieldValue(FIELD_EVENT_NAME);
107111
const input = this.getInput('TITLE');
@@ -136,7 +140,7 @@ const EVENT = {
136140
paramBlock.nextConnection && paramBlock.nextConnection.targetBlock();
137141
}
138142
this.mrcUpdateParams();
139-
mutateMethodCallers(this.workspace, this.id, this.getEvent());
143+
mutateMethodCallers(this.workspace, this.mrcEventId, this.getEvent());
140144
},
141145
decompose: function (this: EventBlock, workspace: Blockly.Workspace) {
142146
// This is a special sub-block that only gets created in the mutator UI.
@@ -189,42 +193,50 @@ const EVENT = {
189193
const oldName = nameField.getValue();
190194
if (oldName && oldName !== name && oldName !== legalName) {
191195
// Rename any callers.
192-
renameMethodCallers(this.workspace, this.id, legalName);
196+
renameMethodCallers(this.workspace, this.mrcEventId, legalName);
193197
}
194198
return legalName;
195199
},
196200
onBlockChanged(block: Blockly.BlockSvg, blockEvent: Blockly.Events.BlockBase): void {
197-
const blockBlock = block as Blockly.Block;
201+
const blockBlock = block as Blockly.Block;
198202

199-
if (blockEvent.type === Blockly.Events.BLOCK_MOVE) {
200-
const parent = ChangeFramework.getParentOfType(block, MRC_MECHANISM_COMPONENT_HOLDER);
203+
if (blockEvent.type === Blockly.Events.BLOCK_MOVE) {
204+
const parent = ChangeFramework.getParentOfType(block, MRC_MECHANISM_COMPONENT_HOLDER);
201205

202-
if (parent) {
203-
// If it is, we allow it to stay.
204-
blockBlock.setWarningText(null);
205-
return;
206-
}
207-
// If we end up here it shouldn't be allowed
208-
block.unplug(true);
209-
blockBlock.setWarningText('Events can only go in the events section of the robot or mechanism');
210-
blockBlock.getIcon(Blockly.icons.IconType.WARNING)!.setBubbleVisible(true);
206+
if (parent) {
207+
// If it is, we allow it to stay.
208+
blockBlock.setWarningText(null);
209+
return;
211210
}
212-
},
213-
getEvent: function (this: EventBlock): storageModuleContent.Event {
214-
const event: storageModuleContent.Event = {
215-
blockId: this.id,
216-
name: this.getFieldValue(FIELD_EVENT_NAME),
217-
args: [],
218-
};
219-
this.mrcParameters.forEach(param => {
220-
event.args.push({
221-
name: param.name,
222-
type: param.type ? param.type : '',
223-
});
211+
// If we end up here it shouldn't be allowed
212+
block.unplug(true);
213+
blockBlock.setWarningText('Events can only go in the events section of the robot or mechanism');
214+
blockBlock.getIcon(Blockly.icons.IconType.WARNING)!.setBubbleVisible(true);
215+
}
216+
},
217+
getEvent: function (this: EventBlock): storageModuleContent.Event {
218+
const event: storageModuleContent.Event = {
219+
eventId: this.mrcEventId,
220+
name: this.getFieldValue(FIELD_EVENT_NAME),
221+
args: [],
222+
};
223+
this.mrcParameters.forEach(param => {
224+
event.args.push({
225+
name: param.name,
226+
type: param.type ? param.type : '',
224227
});
225-
return event;
226-
},
227-
}
228+
});
229+
return event;
230+
},
231+
/**
232+
* mrcChangeIds is called when a module is copied so that the copy has different ids than the original.
233+
*/
234+
mrcChangeIds: function (this: EventBlock, oldIdToNewId: { [oldId: string]: string }): void {
235+
if (this.mrcEventId in oldIdToNewId) {
236+
this.mrcEventId = oldIdToNewId[this.mrcEventId];
237+
}
238+
},
239+
};
228240

229241
export const setup = function () {
230242
Blockly.Blocks[BLOCK_NAME] = EVENT;

0 commit comments

Comments
 (0)