Skip to content

Commit 9ee1246

Browse files
committed
Added moduleId to Module and ModuleContent.
Updated mrc_mechanism to find the mechanism by its module id.
1 parent fa92a90 commit 9ee1246

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/blocks/mrc_mechanism.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Parameter = {
4646
};
4747

4848
type MechanismExtraState = {
49+
mechanismModuleId?: string,
4950
importModule?: string,
5051
parameters?: Parameter[],
5152
}
@@ -54,6 +55,7 @@ const WARNING_ID_MECHANISM_CHANGED = 'mechanism changed';
5455

5556
export type MechanismBlock = Blockly.Block & MechanismMixin & Blockly.BlockSvg;
5657
interface MechanismMixin extends MechanismMixinType {
58+
mrcMechanismModuleId: string
5759
mrcImportModule: string,
5860
mrcParameters: Parameter[],
5961
}
@@ -80,6 +82,7 @@ const MECHANISM = {
8082
*/
8183
saveExtraState: function (this: MechanismBlock): MechanismExtraState {
8284
const extraState: MechanismExtraState = {
85+
mechanismModuleId: this.mrcMechanismModuleId,
8386
};
8487
extraState.parameters = [];
8588
this.mrcParameters.forEach((arg) => {
@@ -97,6 +100,7 @@ const MECHANISM = {
97100
* Applies the given state to this block.
98101
*/
99102
loadExtraState: function (this: MechanismBlock, extraState: MechanismExtraState): void {
103+
this.mrcMechanismModuleId = extraState.mechanismModuleId ? extraState.mechanismModuleId : '';
100104
this.mrcImportModule = extraState.importModule ? extraState.importModule : '';
101105
this.mrcParameters = [];
102106
if (extraState.parameters) {
@@ -157,6 +161,7 @@ const MECHANISM = {
157161
const mechanismName = this.getFieldValue(FIELD_NAME);
158162
const mechanismType = this.mrcImportModule + '.' + this.getFieldValue(FIELD_TYPE);
159163
return {
164+
moduleId: this.mrcMechanismModuleId,
160165
blockId: this.id,
161166
name: mechanismName,
162167
className: mechanismType,
@@ -169,14 +174,10 @@ const MECHANISM = {
169174
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace);
170175
if (editor) {
171176
// Find the mechanism.
172-
// TODO(lizlooney): The user can rename the mechanism. We need to store a UUID in
173-
// each module JSON file so we can track mechanisms, etc, even if the name changes.
174-
// Then here, we'd look for the mechanism with the marching UUID, and we'd update the
175-
// FIELD_TYPE value if the mechanism's class name had changed.
176177
let foundMechanism: storageModule.Mechanism | null = null;
177178
const components: storageModuleContent.Component[] = []
178179
for (const mechanism of editor.getMechanisms()) {
179-
if (mechanism.className === this.getFieldValue(FIELD_TYPE)) {
180+
if (mechanism.moduleId === this.mrcMechanismModuleId) {
180181
foundMechanism = mechanism;
181182
components.push(...editor.getComponentsFromMechanism(mechanism));
182183
break;
@@ -251,6 +252,7 @@ export function createMechanismBlock(
251252
const snakeCaseName = storageNames.pascalCaseToSnakeCase(mechanism.className);
252253
const mechanismName = 'my_' + snakeCaseName;
253254
const extraState: MechanismExtraState = {
255+
mechanismModuleId: mechanism.moduleId,
254256
importModule: snakeCaseName,
255257
parameters: [],
256258
};

src/storage/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export const MODULE_TYPE_MECHANISM = 'mechanism';
2828
export const MODULE_TYPE_OPMODE = 'opmode';
2929

3030
export type Module = {
31-
// TODO(lizlooney): Add a uuid so we can keep track of mechanisms in the robot even if the user renames the mechamism
3231
modulePath: string,
3332
moduleType: string,
33+
moduleId: string,
3434
projectName: string, // For example, WackyWheelerRobot
3535
className: string, // For example, GamePieceShooter.
3636
dateModifiedMillis: number,

src/storage/module_content.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @author [email protected] (Liz Looney)
2020
*/
2121

22+
import * as Blockly from 'blockly/core';
2223
import * as storageModule from './module';
2324
import * as storageNames from './names';
2425
import startingOpModeBlocks from '../modules/opmode_start.json';
@@ -39,6 +40,7 @@ export type Method = {
3940
};
4041

4142
export type MechanismInRobot = {
43+
moduleId: string // ID of the mechanism module.
4244
blockId: string, // ID of the mrc_mechanism block that adds the mechanism to the robot.
4345
name: string,
4446
className: string, // Includes the module name, for example 'game_piece_shooter.GamePieceShooter'.
@@ -79,6 +81,7 @@ export function newRobotContent(projectName: string): string {
7981
const module: storageModule.Robot = {
8082
modulePath: storageNames.makeRobotPath(projectName),
8183
moduleType: storageModule.MODULE_TYPE_ROBOT,
84+
moduleId: Blockly.utils.idGenerator.genUid(),
8285
projectName: projectName,
8386
className: storageNames.CLASS_NAME_ROBOT,
8487
dateModifiedMillis: 0,
@@ -94,6 +97,7 @@ export function newMechanismContent(projectName: string, mechanismClassName: str
9497
const module: storageModule.Mechanism = {
9598
modulePath: storageNames.makeModulePath(projectName, mechanismClassName),
9699
moduleType: storageModule.MODULE_TYPE_MECHANISM,
100+
moduleId: Blockly.utils.idGenerator.genUid(),
97101
projectName: projectName,
98102
className: mechanismClassName,
99103
dateModifiedMillis: 0,
@@ -109,6 +113,7 @@ export function newOpModeContent(projectName: string, opModeClassName: string):
109113
const module: storageModule.OpMode = {
110114
modulePath: storageNames.makeModulePath(projectName, opModeClassName),
111115
moduleType: storageModule.MODULE_TYPE_OPMODE,
116+
moduleId: Blockly.utils.idGenerator.genUid(),
112117
projectName: projectName,
113118
className: opModeClassName,
114119
dateModifiedMillis: 0,
@@ -129,6 +134,7 @@ export function makeModuleContentText(
129134
methods: Method[]): string {
130135
const moduleContent = new ModuleContent(
131136
module.moduleType,
137+
module.moduleId,
132138
blocks,
133139
mechanisms,
134140
components,
@@ -139,8 +145,12 @@ export function makeModuleContentText(
139145

140146
export function parseModuleContentText(moduleContentText: string): ModuleContent {
141147
const parsedContent = JSON.parse(moduleContentText);
148+
if (!parsedContent.moduleId) {
149+
parsedContent.moduleId = Blockly.utils.idGenerator.genUid();
150+
}
142151
return new ModuleContent(
143152
parsedContent.moduleType,
153+
parsedContent.moduleId,
144154
parsedContent.blocks,
145155
parsedContent.mechanisms,
146156
parsedContent.components,
@@ -151,6 +161,7 @@ export function parseModuleContentText(moduleContentText: string): ModuleContent
151161
export class ModuleContent {
152162
constructor(
153163
private moduleType: string,
164+
private moduleId: string,
154165
private blocks : { [key: string]: any },
155166
private mechanisms: MechanismInRobot[],
156167
private components: Component[],
@@ -166,6 +177,10 @@ export class ModuleContent {
166177
return this.moduleType;
167178
}
168179

180+
getModuleId(): string {
181+
return this.moduleId;
182+
}
183+
169184
getBlocks(): { [key: string]: any } {
170185
return this.blocks;
171186
}

src/storage/project.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export async function listProjects(storage: commonStorage.Storage): Promise<Proj
5151
const module: storageModule.Module = {
5252
modulePath: modulePath,
5353
moduleType: moduleType,
54+
moduleId: moduleContent.getModuleId(),
5455
projectName: storageNames.getProjectName(modulePath),
5556
className: storageNames.getClassName(modulePath),
5657
dateModifiedMillis: dateModifiedMillis,

0 commit comments

Comments
 (0)