Skip to content

Commit ea439cc

Browse files
committed
Removed unplug call on mechanism, component, or event blocks if they aren't in
the holder. If there are more than one of these in a stack and the user drags the whole stack out of the holder, the unplug will unplug the top one from the stack which is alarming. In mrc_mechanism_component_holder: Changed mrcMechanismIds to mrcMechanismBlockIds, mrcComponentIds to mrcComponentBlockIds, mrcPrivateComponentIds to mrcPrivateComponentBlockIds, and mrcEventIds to mrcEventBlockIds. Made them strings, which makes the comparing much more simple. Added mrcOnDescendantDisconnected, which is called when a mechanism, component, or event block is deleted. In editor, added code to handle move event for disconnect and call mrcOnDescendantDisconnect on the root block that the block was disconnected from.
1 parent 303cafc commit ea439cc

File tree

5 files changed

+48
-78
lines changed

5 files changed

+48
-78
lines changed

src/blocks/mrc_component.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,6 @@ const COMPONENT = {
167167
}
168168
return legalName;
169169
},
170-
getComponentId: function (this: ComponentBlock): string {
171-
return this.mrcComponentId;
172-
},
173170
getComponent: function (this: ComponentBlock): storageModuleContent.Component | null {
174171
const componentName = this.getFieldValue(FIELD_NAME);
175172
const componentType = this.getFieldValue(FIELD_TYPE);
@@ -220,7 +217,6 @@ const COMPONENT = {
220217
this.mrcHasNotInHolderWarning = false;
221218
} else {
222219
// Otherwise, add a warning to the block.
223-
this.unplug(true);
224220
if (!this.mrcHasNotInHolderWarning) {
225221
this.setWarningText(Blockly.Msg.WARNING_COMPONENT_NOT_IN_HOLDER, WARNING_ID_NOT_IN_HOLDER);
226222
const icon = this.getIcon(Blockly.icons.IconType.WARNING);

src/blocks/mrc_event.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ const EVENT = {
233233
this.mrcHasNotInHolderWarning = false;
234234
} else {
235235
// Otherwise, add a warning to the block.
236-
this.unplug(true);
237236
if (!this.mrcHasNotInHolderWarning) {
238237
this.setWarningText(Blockly.Msg.WARNING_EVENT_NOT_IN_HOLDER, WARNING_ID_NOT_IN_HOLDER);
239238
const icon = this.getIcon(Blockly.icons.IconType.WARNING);
@@ -244,9 +243,6 @@ const EVENT = {
244243
}
245244
}
246245
},
247-
getEventId: function (this: EventBlock): string {
248-
return this.mrcEventId;
249-
},
250246
getEvent: function (this: EventBlock): storageModuleContent.Event {
251247
const event: storageModuleContent.Event = {
252248
eventId: this.mrcEventId,

src/blocks/mrc_mechanism.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,6 @@ const MECHANISM = {
179179
}
180180
return legalName;
181181
},
182-
getMechanismId: function (this: MechanismBlock): string {
183-
return this.mrcMechanismId;
184-
},
185182
getMechanism: function (this: MechanismBlock): storageModuleContent.MechanismInRobot | null {
186183
const mechanismName = this.getFieldValue(FIELD_NAME);
187184
const mechanismType = this.mrcImportModule + '.' + this.getFieldValue(FIELD_TYPE);
@@ -229,7 +226,6 @@ const MECHANISM = {
229226
this.mrcHasNotInHolderWarning = false;
230227
} else {
231228
// Otherwise, add a warning to the block.
232-
this.unplug(true);
233229
if (!this.mrcHasNotInHolderWarning) {
234230
this.setWarningText(Blockly.Msg.WARNING_MECHANISM_NOT_IN_HOLDER, WARNING_ID_NOT_IN_HOLDER);
235231
const icon = this.getIcon(Blockly.icons.IconType.WARNING);

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ interface MechanismComponentHolderMixin extends MechanismComponentHolderMixinTyp
5555
mrcHideMechanisms: boolean;
5656
mrcHidePrivateComponents: boolean;
5757

58-
mrcMechanismIds: string[],
59-
mrcComponentIds: string[],
60-
mrcPrivateComponentIds: string[],
61-
mrcEventIds: string[],
58+
mrcMechanismBlockIds: string,
59+
mrcComponentBlockIds: string,
60+
mrcPrivateComponentBlockIds: string,
61+
mrcEventBlockIds: string,
6262
mrcToolboxUpdateTimeout: NodeJS.Timeout | null;
6363
}
6464
type MechanismComponentHolderMixinType = typeof MECHANISM_COMPONENT_HOLDER;
@@ -71,10 +71,10 @@ const MECHANISM_COMPONENT_HOLDER = {
7171
this.setInputsInline(false);
7272
this.setOutput(false);
7373
this.setStyle(MRC_STYLE_MECHANISMS);
74-
this.mrcMechanismIds = [];
75-
this.mrcComponentIds = [];
76-
this.mrcPrivateComponentIds = [];
77-
this.mrcEventIds = [];
74+
this.mrcMechanismBlockIds = '';
75+
this.mrcComponentBlockIds = '';
76+
this.mrcPrivateComponentBlockIds = '';
77+
this.mrcEventBlockIds = '';
7878
this.mrcToolboxUpdateTimeout = null;
7979
},
8080
saveExtraState: function (this: MechanismComponentHolderBlock): MechanismComponentHolderExtraState {
@@ -134,23 +134,30 @@ const MECHANISM_COMPONENT_HOLDER = {
134134
mrcOnLoad: function(this: MechanismComponentHolderBlock): void {
135135
this.collectDescendants(false);
136136
},
137+
/**
138+
* mrcOnDescendantDisconnect is called for each MechanismComponentHolderBlock when any descendant is
139+
* disconnected.
140+
*/
141+
mrcOnDescendantDisconnect: function(this: MechanismComponentHolderBlock): void {
142+
this.collectDescendants(true);
143+
},
137144
mrcDescendantsMayHaveChanged: function (this: MechanismComponentHolderBlock): void {
138145
this.collectDescendants(true);
139146
},
140147
collectDescendants: function (
141148
this: MechanismComponentHolderBlock, updateToolboxIfDescendantsChanged: boolean): void {
142-
const mechanismIds: string[] = [];
143-
const componentIds: string[] = [];
144-
const privateComponentIds: string[] = [];
145-
const eventIds: string[] = [];
149+
let mechanismBlockIds = '';
150+
let componentBlockIds = '';
151+
let privateComponentBlockIds = '';
152+
let eventBlockIds = '';
146153

147154
const mechanismsInput = this.getInput(INPUT_MECHANISMS);
148155
if (mechanismsInput && mechanismsInput.connection) {
149156
// Walk through all connected mechanism blocks.
150157
let mechanismBlock = mechanismsInput.connection.targetBlock();
151158
while (mechanismBlock) {
152159
if (mechanismBlock.type === MRC_MECHANISM_NAME) {
153-
mechanismIds.push((mechanismBlock as MechanismBlock).getMechanismId());
160+
mechanismBlockIds += mechanismBlock.id;
154161
}
155162
// Move to the next block in the stack.
156163
mechanismBlock = mechanismBlock.getNextBlock();
@@ -162,7 +169,7 @@ const MECHANISM_COMPONENT_HOLDER = {
162169
let componentBlock = componentsInput.connection.targetBlock();
163170
while (componentBlock) {
164171
if (componentBlock.type === MRC_COMPONENT_NAME) {
165-
componentIds.push((componentBlock as ComponentBlock).getComponentId());
172+
componentBlockIds += componentBlock.id;
166173
}
167174
// Move to the next block in the stack.
168175
componentBlock = componentBlock.getNextBlock();
@@ -174,7 +181,7 @@ const MECHANISM_COMPONENT_HOLDER = {
174181
let componentBlock = privateComponentsInput.connection.targetBlock();
175182
while (componentBlock) {
176183
if (componentBlock.type === MRC_COMPONENT_NAME) {
177-
privateComponentIds.push((componentBlock as ComponentBlock).getComponentId());
184+
privateComponentBlockIds += componentBlock.id;
178185
}
179186
// Move to the next block in the stack.
180187
componentBlock = componentBlock.getNextBlock();
@@ -186,65 +193,26 @@ const MECHANISM_COMPONENT_HOLDER = {
186193
let eventBlock = eventsInput.connection.targetBlock();
187194
while (eventBlock) {
188195
if (eventBlock.type === MRC_EVENT_NAME) {
189-
eventIds.push((eventBlock as EventBlock).getEventId());
196+
eventBlockIds += eventBlock.id;
190197
}
191198
// Move to the next block in the stack.
192199
eventBlock = eventBlock.getNextBlock();
193200
}
194201
}
195202

196203
if (updateToolboxIfDescendantsChanged) {
197-
let descendantsChanged = false;
198-
if (mechanismIds.length === this.mrcMechanismIds.length) {
199-
for (let i = 0; i < mechanismIds.length; i++) {
200-
if (mechanismIds[i] !== this.mrcMechanismIds[i]) {
201-
descendantsChanged = true;
202-
break;
203-
}
204-
}
205-
} else {
206-
descendantsChanged = true;
207-
}
208-
if (componentIds.length === this.mrcComponentIds.length) {
209-
for (let i = 0; i < componentIds.length; i++) {
210-
if (componentIds[i] !== this.mrcComponentIds[i]) {
211-
descendantsChanged = true;
212-
break;
213-
}
214-
}
215-
} else {
216-
descendantsChanged = true;
217-
}
218-
if (privateComponentIds.length === this.mrcPrivateComponentIds.length) {
219-
for (let i = 0; i < privateComponentIds.length; i++) {
220-
if (privateComponentIds[i] !== this.mrcPrivateComponentIds[i]) {
221-
descendantsChanged = true;
222-
break;
223-
}
224-
}
225-
} else {
226-
descendantsChanged = true;
227-
}
228-
if (eventIds.length === this.mrcEventIds.length) {
229-
for (let i = 0; i < eventIds.length; i++) {
230-
if (eventIds[i] !== this.mrcEventIds[i]) {
231-
descendantsChanged = true;
232-
break;
233-
}
234-
}
235-
} else {
236-
descendantsChanged = true;
237-
}
238-
239-
if (descendantsChanged) {
204+
if (mechanismBlockIds !== this.mrcMechanismBlockIds ||
205+
componentBlockIds !== this.mrcComponentBlockIds ||
206+
privateComponentBlockIds !== this.mrcPrivateComponentBlockIds ||
207+
eventBlockIds !== this.mrcEventBlockIds) {
240208
this.updateToolboxAfterDelay();
241209
}
242210
}
243211

244-
this.mrcMechanismIds = mechanismIds;
245-
this.mrcComponentIds = componentIds;
246-
this.mrcPrivateComponentIds = privateComponentIds;
247-
this.mrcEventIds = eventIds;
212+
this.mrcMechanismBlockIds = mechanismBlockIds;
213+
this.mrcComponentBlockIds = componentBlockIds;
214+
this.mrcPrivateComponentBlockIds = privateComponentBlockIds;
215+
this.mrcEventBlockIds = eventBlockIds;
248216
},
249217
updateToolboxAfterDelay: function (this: MechanismComponentHolderBlock): void {
250218
if (this.mrcToolboxUpdateTimeout) {

src/editor/editor.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const EMPTY_TOOLBOX: Blockly.utils.toolbox.ToolboxInfo = {
4040

4141
const MRC_ON_LOAD = 'mrcOnLoad';
4242
const MRC_ON_MOVE = 'mrcOnMove';
43+
const MRC_ON_DESCENDANT_DISCONNECT = 'mrcOnDescendantDisconnect';
4344
const MRC_ON_ANCESTOR_MOVE = 'mrcOnAncestorMove';
4445
const MRC_ON_MODULE_CURRENT = 'mrcOnModuleCurrent';
4546

@@ -126,16 +127,29 @@ export class Editor {
126127

127128
if (event.type === Blockly.Events.BLOCK_MOVE) {
128129
const blockMoveEvent = event as Blockly.Events.BlockMove;
130+
const reason: string[] = blockMoveEvent.reason ?? [];
131+
if (reason.includes('disconnect') && blockMoveEvent.oldParentId) {
132+
const oldParent = this.blocklyWorkspace.getBlockById(blockMoveEvent.oldParentId!);
133+
if (oldParent) {
134+
const rootBlock = oldParent.getRootBlock();
135+
if (rootBlock) {
136+
// Call MRC_ON_DESCENDANT_DISCONNECT on the root block of the block that was disconnected.
137+
if (MRC_ON_DESCENDANT_DISCONNECT in rootBlock && typeof rootBlock[MRC_ON_DESCENDANT_DISCONNECT] === 'function') {
138+
rootBlock[MRC_ON_DESCENDANT_DISCONNECT]();
139+
}
140+
}
141+
}
142+
}
143+
129144
const block = this.blocklyWorkspace.getBlockById(blockMoveEvent.blockId!);
130145
if (!block) {
131146
return;
132147
}
133-
// Call MRC_ON_MOVE for the block that was moved.
148+
// Call MRC_ON_MOVE on the block that was moved.
134149
if (MRC_ON_MOVE in block && typeof block[MRC_ON_MOVE] === 'function') {
135-
const reason: string[] = blockMoveEvent.reason ?? [];
136150
block[MRC_ON_MOVE](reason);
137151
}
138-
// Call MRC_ON_ANCESTOR_MOVE for all descendents of the block that was moved.
152+
// Call MRC_ON_ANCESTOR_MOVE on all descendents of the block that was moved.
139153
block.getDescendants(false).forEach(descendant => {
140154
if (MRC_ON_ANCESTOR_MOVE in descendant && typeof descendant[MRC_ON_ANCESTOR_MOVE] === 'function') {
141155
descendant[MRC_ON_ANCESTOR_MOVE]();

0 commit comments

Comments
 (0)