Skip to content

Commit 337f176

Browse files
authored
Change the name of a component in the toolbox when the name field in a Component block is changed (wpilibsuite#331)
* Moved updateToolboxAfterDelay from MechanismComponentHolderBlock to Editor. Added editor parameter to mrcOnDescendantDisconnect and mrcOnMove methods in several blocks. Added editor parameter to mrcDescendantsMayHaveChanged in mrc_mechanism_component_holder. Removed handleToolboxUpdateRequest from App.tsx. * Modify ComponentBlock.mrcNameFieldValidator to call editor.updateToolboxAfterDelay to update the component name in the toolbox. * Removed editor import.
1 parent 24ff688 commit 337f176

File tree

8 files changed

+37
-59
lines changed

8 files changed

+37
-59
lines changed

src/App.tsx

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import ToolboxSettingsModal from './reactComponents/ToolboxSettings';
3131
import * as Tabs from './reactComponents/Tabs';
3232
import { TabType } from './types/TabType';
3333

34-
import * as editor from './editor/editor';
3534
import { extendedPythonGenerator } from './editor/extended_python_generator';
3635

3736
import * as commonStorage from './storage/common_storage';
@@ -42,7 +41,6 @@ import * as clientSideStorage from './storage/client_side_storage';
4241
import * as CustomBlocks from './blocks/setup_custom_blocks';
4342

4443
import { initialize as initializePythonBlocks } from './blocks/utils/python';
45-
import { TOOLBOX_UPDATE_EVENT } from './blocks/mrc_mechanism_component_holder';
4644
import { antdThemeFromString } from './reactComponents/ThemeModal';
4745
import { useTranslation } from 'react-i18next';
4846
import { UserSettingsProvider } from './reactComponents/UserSettingsProvider';
@@ -261,24 +259,6 @@ const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.J
261259
handleToolboxSettingsOk(updatedShownCategories);
262260
};
263261

264-
/** Handles toolbox update requests from blocks */
265-
const handleToolboxUpdateRequest = React.useCallback((e: Event) => {
266-
const workspaceId = (e as CustomEvent).detail.workspaceId;
267-
const correspondingEditor = editor.Editor.getEditorForBlocklyWorkspaceId(workspaceId);
268-
if (correspondingEditor) {
269-
correspondingEditor.updateToolbox(shownPythonToolboxCategories);
270-
}
271-
}, [shownPythonToolboxCategories, i18n.language]);
272-
273-
// Add event listener for toolbox updates
274-
React.useEffect(() => {
275-
window.addEventListener(TOOLBOX_UPDATE_EVENT, handleToolboxUpdateRequest);
276-
277-
return () => {
278-
window.removeEventListener(TOOLBOX_UPDATE_EVENT, handleToolboxUpdateRequest);
279-
};
280-
}, [handleToolboxUpdateRequest]);
281-
282262
// Initialize blocks when app loads
283263
React.useEffect(() => {
284264
initializeBlocks();

src/blocks/mrc_component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ const COMPONENT = {
172172
if (oldName && oldName !== name && oldName !== legalName) {
173173
// Rename any callers.
174174
renameMethodCallers(this.workspace, this.mrcComponentId, legalName);
175+
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace);
176+
if (editor) {
177+
editor.updateToolboxAfterDelay();
178+
}
175179
}
176180
return legalName;
177181
},
@@ -212,15 +216,15 @@ const COMPONENT = {
212216
/**
213217
* mrcOnMove is called when a ComponentBlock is moved.
214218
*/
215-
mrcOnMove: function(this: ComponentBlock, reason: string[]): void {
219+
mrcOnMove: function(this: ComponentBlock, editor: Editor, reason: string[]): void {
216220
this.checkBlockIsInHolder();
217221
if (reason.includes('connect')) {
218222
const rootBlock: Blockly.Block | null = this.getRootBlock();
219223
if (rootBlock && rootBlock.type === MRC_MECHANISM_COMPONENT_HOLDER) {
220224
(rootBlock as MechanismComponentHolderBlock).setNameOfChildBlock(this);
221225
}
222226
}
223-
mrcDescendantsMayHaveChanged(this.workspace);
227+
mrcDescendantsMayHaveChanged(this.workspace, editor);
224228
},
225229
checkBlockIsInHolder: function(this: ComponentBlock): void {
226230
const rootBlock: Blockly.Block | null = this.getRootBlock();

src/blocks/mrc_event.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@ const EVENT = {
211211
/**
212212
* mrcOnMove is called when an EventBlock is moved.
213213
*/
214-
mrcOnMove: function(this: EventBlock, reason: string[]): void {
214+
mrcOnMove: function(this: EventBlock, editor: Editor, reason: string[]): void {
215215
this.checkBlockIsInHolder();
216216
if (reason.includes('connect')) {
217217
const rootBlock: Blockly.Block | null = this.getRootBlock();
218218
if (rootBlock && rootBlock.type === MRC_MECHANISM_COMPONENT_HOLDER) {
219219
(rootBlock as MechanismComponentHolderBlock).setNameOfChildBlock(this);
220220
}
221221
}
222-
mrcDescendantsMayHaveChanged(this.workspace);
222+
mrcDescendantsMayHaveChanged(this.workspace, editor);
223223
},
224224
/**
225225
* mrcOnMutatorOpen is called when the mutator on an EventBlock is opened.

src/blocks/mrc_get_parameter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import * as Blockly from 'blockly';
2424
import {Order} from 'blockly/python';
2525

26+
import { Editor } from '../editor/editor';
2627
import {ExtendedPythonGenerator} from '../editor/extended_python_generator';
2728
import {createFieldNonEditableText} from '../fields/FieldNonEditableText';
2829
import {MRC_STYLE_VARIABLES} from '../themes/styles';
@@ -82,7 +83,7 @@ const GET_PARAMETER_BLOCK = {
8283
/**
8384
* mrcOnMove is called when an EventBlock is moved.
8485
*/
85-
mrcOnMove: function(this: GetParameterBlock, _reason: string[]): void {
86+
mrcOnMove: function(this: GetParameterBlock, _editor: Editor, _reason: string[]): void {
8687
this.checkBlockPlacement();
8788
},
8889
mrcOnAncestorMove: function(this: GetParameterBlock): void {

src/blocks/mrc_jump_to_step.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
import * as Blockly from 'blockly';
2323

24+
import { Editor } from '../editor/editor';
2425
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2526
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
2627
import { MRC_STYLE_VARIABLES } from '../themes/styles';
@@ -62,7 +63,7 @@ const JUMP_TO_STEP_BLOCK = {
6263
/**
6364
* mrcOnMove is called when a JumpToStepBlock is moved.
6465
*/
65-
mrcOnMove: function (this: JumpToStepBlock, _reason: string[]): void {
66+
mrcOnMove: function (this: JumpToStepBlock, _editor: Editor, _reason: string[]): void {
6667
this.checkBlockPlacement();
6768
},
6869
mrcOnAncestorMove: function (this: JumpToStepBlock): void {

src/blocks/mrc_mechanism.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,15 @@ const MECHANISM = {
212212
/**
213213
* mrcOnMove is called when a MechanismBlock is moved.
214214
*/
215-
mrcOnMove: function(this: MechanismBlock, reason: string[]): void {
215+
mrcOnMove: function(this: MechanismBlock, editor: Editor, reason: string[]): void {
216216
this.checkBlockIsInHolder();
217217
if (reason.includes('connect')) {
218218
const rootBlock: Blockly.Block | null = this.getRootBlock();
219219
if (rootBlock && rootBlock.type === MRC_MECHANISM_COMPONENT_HOLDER) {
220220
(rootBlock as MechanismComponentHolderBlock).setNameOfChildBlock(this);
221221
}
222222
}
223-
mrcDescendantsMayHaveChanged(this.workspace);
223+
mrcDescendantsMayHaveChanged(this.workspace, editor);
224224
},
225225
checkBlockIsInHolder: function(this: MechanismBlock): void {
226226
const rootBlock: Blockly.Block | null = this.getRootBlock();

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ const INPUT_COMPONENTS = 'COMPONENTS';
4646
const INPUT_PRIVATE_COMPONENTS = 'PRIVATE_COMPONENTS';
4747
const INPUT_EVENTS = 'EVENTS';
4848

49-
export const TOOLBOX_UPDATE_EVENT = 'toolbox-update-requested';
50-
5149
type MechanismComponentHolderExtraState = {
5250
hideMechanisms?: boolean;
5351
hidePrivateComponents?: boolean;
@@ -62,7 +60,6 @@ interface MechanismComponentHolderMixin extends MechanismComponentHolderMixinTyp
6260
mrcComponentBlockIds: string,
6361
mrcPrivateComponentBlockIds: string,
6462
mrcEventBlockIds: string,
65-
mrcToolboxUpdateTimeout: NodeJS.Timeout | null;
6663
}
6764
type MechanismComponentHolderMixinType = typeof MECHANISM_COMPONENT_HOLDER;
6865

@@ -78,7 +75,6 @@ const MECHANISM_COMPONENT_HOLDER = {
7875
this.mrcComponentBlockIds = '';
7976
this.mrcPrivateComponentBlockIds = '';
8077
this.mrcEventBlockIds = '';
81-
this.mrcToolboxUpdateTimeout = null;
8278
},
8379
...NONCOPYABLE_BLOCK,
8480
saveExtraState: function (this: MechanismComponentHolderBlock): MechanismComponentHolderExtraState {
@@ -135,21 +131,21 @@ const MECHANISM_COMPONENT_HOLDER = {
135131
* mrcOnLoad is called for each MechanismComponentHolderBlock when the blocks are loaded in the blockly
136132
* workspace.
137133
*/
138-
mrcOnLoad: function(this: MechanismComponentHolderBlock, _editor: Editor): void {
139-
this.collectDescendants(false);
134+
mrcOnLoad: function(this: MechanismComponentHolderBlock, editor: Editor): void {
135+
this.collectDescendants(editor, false);
140136
},
141137
/**
142138
* mrcOnDescendantDisconnect is called for each MechanismComponentHolderBlock when any descendant is
143139
* disconnected.
144140
*/
145-
mrcOnDescendantDisconnect: function(this: MechanismComponentHolderBlock): void {
146-
this.collectDescendants(true);
141+
mrcOnDescendantDisconnect: function(this: MechanismComponentHolderBlock, editor: Editor): void {
142+
this.collectDescendants(editor, true);
147143
},
148-
mrcDescendantsMayHaveChanged: function (this: MechanismComponentHolderBlock): void {
149-
this.collectDescendants(true);
144+
mrcDescendantsMayHaveChanged: function (this: MechanismComponentHolderBlock, editor: Editor): void {
145+
this.collectDescendants(editor, true);
150146
},
151147
collectDescendants: function (
152-
this: MechanismComponentHolderBlock, updateToolboxIfDescendantsChanged: boolean): void {
148+
this: MechanismComponentHolderBlock, editor: Editor, updateToolboxIfDescendantsChanged: boolean): void {
153149
let mechanismBlockIds = '';
154150
let componentBlockIds = '';
155151
let privateComponentBlockIds = '';
@@ -209,7 +205,7 @@ const MECHANISM_COMPONENT_HOLDER = {
209205
componentBlockIds !== this.mrcComponentBlockIds ||
210206
privateComponentBlockIds !== this.mrcPrivateComponentBlockIds ||
211207
eventBlockIds !== this.mrcEventBlockIds) {
212-
this.updateToolboxAfterDelay();
208+
editor.updateToolboxAfterDelay();
213209
}
214210
}
215211

@@ -218,21 +214,6 @@ const MECHANISM_COMPONENT_HOLDER = {
218214
this.mrcPrivateComponentBlockIds = privateComponentBlockIds;
219215
this.mrcEventBlockIds = eventBlockIds;
220216
},
221-
updateToolboxAfterDelay: function (this: MechanismComponentHolderBlock): void {
222-
if (this.mrcToolboxUpdateTimeout) {
223-
clearTimeout(this.mrcToolboxUpdateTimeout);
224-
}
225-
this.mrcToolboxUpdateTimeout = setTimeout(() => {
226-
const event = new CustomEvent(TOOLBOX_UPDATE_EVENT, {
227-
detail: {
228-
timestamp: Date.now(),
229-
workspaceId: this.workspace.id,
230-
}
231-
});
232-
window.dispatchEvent(event);
233-
this.mrcToolboxUpdateTimeout = null;
234-
}, 100);
235-
},
236217
/**
237218
* setNameOfChildBlock is called from mrc_mechanism, mrc_component, and mrc_event blocks when they
238219
* connect to this mrc_mechanism_component_holder block.
@@ -515,10 +496,10 @@ export function getEvents(
515496
});
516497
}
517498

518-
export function mrcDescendantsMayHaveChanged(workspace: Blockly.Workspace): void {
499+
export function mrcDescendantsMayHaveChanged(workspace: Blockly.Workspace, editor: Editor): void {
519500
// Get the holder block and call its mrcDescendantsMayHaveChanged method.
520501
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
521-
(block as MechanismComponentHolderBlock).mrcDescendantsMayHaveChanged();
502+
(block as MechanismComponentHolderBlock).mrcDescendantsMayHaveChanged(editor);
522503
});
523504
}
524505

src/editor/editor.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class Editor {
6767
private bindedOnChange: any = null;
6868
private shownPythonToolboxCategories: Set<string> | null = null;
6969
private toolbox: Blockly.utils.toolbox.ToolboxInfo = EMPTY_TOOLBOX;
70+
private toolboxUpdateTimeout: NodeJS.Timeout | null = null;
7071

7172
constructor(
7273
blocklyWorkspace: Blockly.WorkspaceSvg,
@@ -163,7 +164,7 @@ export class Editor {
163164
if (rootBlock) {
164165
// Call MRC_ON_DESCENDANT_DISCONNECT on the root block of the block that was disconnected.
165166
if (MRC_ON_DESCENDANT_DISCONNECT in rootBlock && typeof rootBlock[MRC_ON_DESCENDANT_DISCONNECT] === 'function') {
166-
rootBlock[MRC_ON_DESCENDANT_DISCONNECT]();
167+
rootBlock[MRC_ON_DESCENDANT_DISCONNECT](this);
167168
}
168169
}
169170
}
@@ -175,7 +176,7 @@ export class Editor {
175176
}
176177
// Call MRC_ON_MOVE on the block that was moved.
177178
if (MRC_ON_MOVE in block && typeof block[MRC_ON_MOVE] === 'function') {
178-
block[MRC_ON_MOVE](reason);
179+
block[MRC_ON_MOVE](this, reason);
179180
}
180181
// Call MRC_ON_ANCESTOR_MOVE on all descendents of the block that was moved.
181182
block.getDescendants(false).forEach(descendant => {
@@ -279,6 +280,16 @@ export class Editor {
279280
Blockly.serialization.workspaces.load(moduleContent.getBlocks(), this.blocklyWorkspace);
280281
}
281282

283+
public updateToolboxAfterDelay(): void {
284+
if (this.toolboxUpdateTimeout) {
285+
clearTimeout(this.toolboxUpdateTimeout);
286+
}
287+
this.toolboxUpdateTimeout = setTimeout(() => {
288+
this.updateToolboxImpl();
289+
this.toolboxUpdateTimeout = null;
290+
}, 100);
291+
}
292+
282293
public updateToolbox(shownPythonToolboxCategories: Set<string>): void {
283294
this.shownPythonToolboxCategories = shownPythonToolboxCategories;
284295
this.updateToolboxImpl();

0 commit comments

Comments
 (0)