Skip to content

Commit 810a7cb

Browse files
authored
Changed blocks to not assume they are BlockSvg (#337)
* Changed blocks (except the ones that are used in mutators) to not assume they are BlockSvg. * Added comment.
1 parent c5f15ff commit 810a7cb

File tree

8 files changed

+41
-19
lines changed

8 files changed

+41
-19
lines changed

src/blocks/mrc_call_python_function.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type FunctionArg = {
7070

7171
const WARNING_ID_FUNCTION_CHANGED = 'function changed';
7272

73-
export type CallPythonFunctionBlock = Blockly.Block & CallPythonFunctionMixin & Blockly.BlockSvg;
73+
export type CallPythonFunctionBlock = Blockly.Block & CallPythonFunctionMixin;
7474
interface CallPythonFunctionMixin extends CallPythonFunctionMixinType {
7575
mrcFunctionKind: FunctionKind,
7676
mrcReturnType: string,
@@ -871,7 +871,9 @@ const CALL_PYTHON_FUNCTION = {
871871
if (icon) {
872872
icon.setBubbleVisible(true);
873873
}
874-
this.bringToFront();
874+
if (this.rendered) {
875+
(this as unknown as Blockly.BlockSvg).bringToFront();
876+
}
875877
} else {
876878
// Clear the existing warning on the block.
877879
this.setWarningText(null, WARNING_ID_FUNCTION_CHANGED);

src/blocks/mrc_class_method_def.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export interface Parameter {
5858
type?: string;
5959
}
6060

61-
export type ClassMethodDefBlock = Blockly.Block & ClassMethodDefMixin & Blockly.BlockSvg;
61+
export type ClassMethodDefBlock = Blockly.Block & ClassMethodDefMixin;
6262
interface ClassMethodDefMixin extends ClassMethodDefMixinType {
6363
mrcMethodId: string,
6464
mrcCanChangeSignature: boolean,
@@ -182,12 +182,18 @@ const CLASS_METHOD_DEF = {
182182
if (this.mrcCanChangeSignature) {
183183
const nameField = new Blockly.FieldTextInput(name);
184184
input.insertFieldAt(0, nameField, FIELD_METHOD_NAME);
185-
this.setMutator(paramContainer.getMutatorIcon(this));
185+
if (this.rendered) {
186+
this.setMutator(paramContainer.getMutatorIcon(this as unknown as Blockly.BlockSvg));
187+
}
186188
nameField.setValidator(this.mrcNameFieldValidator.bind(this, nameField));
187189
} else {
188190
input.insertFieldAt(0, createFieldNonEditableText(name), FIELD_METHOD_NAME);
189-
// Case because a current bug in blockly where it won't allow passing null to Blockly.Block.setMutator makes it necessary.
190-
(this as Blockly.BlockSvg).setMutator(null);
191+
// Block.setMutator is defined as setMutator(_mutator: MutatorIcon) and BlockSvg.setMutator
192+
// is defined as setMutator(mutator: MutatorIcon | null).
193+
// Therefore, to call setMutator(null), this must be casted to BlockSvg.
194+
if (this.rendered) {
195+
(this as unknown as Blockly.BlockSvg).setMutator(null);
196+
}
191197
}
192198
this.mrcUpdateParams();
193199
this.mrcUpdateReturnInput();
@@ -231,7 +237,9 @@ const CLASS_METHOD_DEF = {
231237
* mrcOnMutatorOpen is called when the mutator on a ClassMethodDefBlock is opened.
232238
*/
233239
mrcOnMutatorOpen: function(this: ClassMethodDefBlock): void {
234-
paramContainer.onMutatorOpen(this);
240+
if (this.rendered) {
241+
paramContainer.onMutatorOpen(this as unknown as Blockly.BlockSvg);
242+
}
235243
},
236244
mrcRenameParameter: function (this: ClassMethodDefBlock, oldName: string, newName: string) {
237245
const nextBlock = this.getInputTargetBlock(INPUT_STACK);

src/blocks/mrc_event.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type EventExtraState = {
5050
params?: Parameter[],
5151
}
5252

53-
export type EventBlock = Blockly.Block & EventMixin & Blockly.BlockSvg;
53+
export type EventBlock = Blockly.Block & EventMixin;
5454

5555
interface EventMixin extends EventMixinType {
5656
mrcEventId: string,
@@ -130,7 +130,9 @@ const EVENT = {
130130

131131
const nameField = new Blockly.FieldTextInput(name);
132132
input.insertFieldAt(0, nameField, FIELD_EVENT_NAME);
133-
this.setMutator(paramContainer.getMutatorIcon(this));
133+
if (this.rendered) {
134+
this.setMutator(paramContainer.getMutatorIcon(this as unknown as Blockly.BlockSvg));
135+
}
134136
nameField.setValidator(this.mrcNameFieldValidator.bind(this, nameField));
135137

136138
this.mrcUpdateParams();
@@ -225,7 +227,9 @@ const EVENT = {
225227
* mrcOnMutatorOpen is called when the mutator on an EventBlock is opened.
226228
*/
227229
mrcOnMutatorOpen: function(this: EventBlock): void {
228-
paramContainer.onMutatorOpen(this);
230+
if (this.rendered) {
231+
paramContainer.onMutatorOpen(this as unknown as Blockly.BlockSvg);
232+
}
229233
},
230234
checkBlockIsInHolder: function(this: EventBlock): void {
231235
const rootBlock: Blockly.Block | null = this.getRootBlock();

src/blocks/mrc_event_handler.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export enum SenderType {
5151
const SENDER_VALUE_ROBOT = 'robot';
5252
const WARNING_ID_EVENT_CHANGED = 'event changed';
5353

54-
export type EventHandlerBlock = Blockly.Block & EventHandlerMixin & Blockly.BlockSvg;
54+
export type EventHandlerBlock = Blockly.Block & EventHandlerMixin;
5555

5656
interface EventHandlerMixin extends EventHandlerMixinType {
5757
mrcSenderType: SenderType;
@@ -292,7 +292,9 @@ const EVENT_HANDLER = {
292292
if (icon) {
293293
icon.setBubbleVisible(true);
294294
}
295-
this.bringToFront();
295+
if (this.rendered) {
296+
(this as unknown as Blockly.BlockSvg).bringToFront();
297+
}
296298
} else {
297299
// Clear the existing warning on the block.
298300
this.setWarningText(null, WARNING_ID_EVENT_CHANGED);

src/blocks/mrc_get_parameter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const FIELD_PARAMETER_NAME = 'PARAMETER_NAME';
3939
const WARNING_ID_NOT_IN_METHOD = 'not in method';
4040

4141

42-
type GetParameterBlock = Blockly.Block & Blockly.BlockSvg & GetParameterMixin;
42+
type GetParameterBlock = Blockly.Block & GetParameterMixin;
4343

4444
interface GetParameterMixin extends GetParameterMixinType {
4545
// TODO(lizlooney): currently mrcParameterType is never set to anything other than '' because

src/blocks/mrc_jump_to_step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const FIELD_STEP_NAME = 'STEP_NAME';
3434
const WARNING_ID_NOT_IN_STEP = 'not in step';
3535

3636

37-
type JumpToStepBlock = Blockly.Block & Blockly.BlockSvg & JumpToStepMixin;
37+
type JumpToStepBlock = Blockly.Block & JumpToStepMixin;
3838

3939
interface JumpToStepMixin extends JumpToStepMixinType {
4040
mrcHasWarning: boolean,

src/blocks/mrc_mechanism.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type MechanismExtraState = {
6363
const WARNING_ID_NOT_IN_HOLDER = 'not in holder';
6464
const WARNING_ID_MECHANISM_CHANGED = 'mechanism changed';
6565

66-
export type MechanismBlock = Blockly.Block & MechanismMixin & Blockly.BlockSvg;
66+
export type MechanismBlock = Blockly.Block & MechanismMixin;
6767
interface MechanismMixin extends MechanismMixinType {
6868
mrcMechanismModuleId: string
6969
mrcMechanismId: string,
@@ -402,7 +402,9 @@ const MECHANISM = {
402402
if (icon) {
403403
icon.setBubbleVisible(true);
404404
}
405-
this.bringToFront();
405+
if (this.rendered) {
406+
(this as unknown as Blockly.BlockSvg).bringToFront();
407+
}
406408
} else {
407409
// Clear the existing warning on the block.
408410
this.setWarningText(null, WARNING_ID_MECHANISM_CHANGED);

src/blocks/mrc_steps.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type StepsExtraState = {
4646
stepNames: string[],
4747
};
4848

49-
export type StepsBlock = Blockly.Block & StepsMixin & Blockly.BlockSvg;
49+
export type StepsBlock = Blockly.Block & StepsMixin;
5050
interface StepsMixin extends StepsMixinType {
5151
mrcStepNames: string[];
5252
}
@@ -62,7 +62,9 @@ const STEPS = {
6262
.appendField(Blockly.Msg.STEPS);
6363
this.setInputsInline(false);
6464
this.setStyle(MRC_STYLE_STEPS);
65-
this.setMutator(stepContainer.getMutatorIcon(this));
65+
if (this.rendered) {
66+
this.setMutator(stepContainer.getMutatorIcon(this as unknown as Blockly.BlockSvg));
67+
}
6668
},
6769
...NONCOPYABLE_BLOCK,
6870
saveExtraState: function (this: StepsBlock): StepsExtraState {
@@ -155,7 +157,9 @@ const STEPS = {
155157
* mrcOnMutatorOpen is called when the mutator on an StepsBlock is opened.
156158
*/
157159
mrcOnMutatorOpen: function (this: StepsBlock): void {
158-
stepContainer.onMutatorOpen(this);
160+
if (this.rendered) {
161+
stepContainer.onMutatorOpen(this as unknown as Blockly.BlockSvg);
162+
}
159163
},
160164
mrcUpdateStepName: function (this: StepsBlock, step: number, newName: string): string {
161165
const oldName = this.mrcStepNames[step];

0 commit comments

Comments
 (0)