Skip to content

Commit 4cfdfe4

Browse files
committed
Create the block for the init method when creating a new OpMode.
1 parent ddd3f5e commit 4cfdfe4

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

src/App.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ import * as commonStorage from './storage/common_storage';
5757
import * as ChangeFramework from './blocks/utils/change_framework'
5858
import {mutatorOpenListener} from './blocks/mrc_class_method_def'
5959

60-
import {create as createOpMode} from './modules/mrc_module_opmode'
61-
6260

6361
type NewWorkspaceNameModalProps = {
6462
isOpen: boolean;
@@ -552,9 +550,9 @@ const App: React.FC = () => {
552550
};
553551

554552
const handleNewWorkspaceNameOk = (newWorkspaceName: string) => {
555-
const newWorkspacePath = commonStorage.makeModulePath(newWorkspaceName, newWorkspaceName);
553+
const newWorkspacePath = commonStorage.makeWorkspacePath(newWorkspaceName);
556554
if (newWorkspaceNameModalPurpose === 'NewWorkspace') {
557-
const workspaceContent = commonStorage.newModuleContent(commonStorage.MODULE_TYPE_WORKSPACE);
555+
const workspaceContent = commonStorage.newWorkspaceContent(newWorkspaceName);
558556
storage.createModule(
559557
commonStorage.MODULE_TYPE_WORKSPACE, newWorkspacePath, workspaceContent,
560558
(success: boolean, errorMessage: string) => {
@@ -631,7 +629,7 @@ const App: React.FC = () => {
631629
const handleNewOpModeNameOk = (workspaceName: string, newOpModeName: string) => {
632630
const newOpModePath = commonStorage.makeModulePath(workspaceName, newOpModeName);
633631
if (newOpModeNameModalPurpose === 'NewOpMode') {
634-
const opModeContent = commonStorage.newModuleContent(commonStorage.MODULE_TYPE_OPMODE);
632+
const opModeContent = commonStorage.newOpModeContent(workspaceName, newOpModeName);
635633
storage.createModule(
636634
commonStorage.MODULE_TYPE_OPMODE, newOpModePath, opModeContent,
637635
(success: boolean, errorMessage: string) => {
@@ -645,13 +643,6 @@ const App: React.FC = () => {
645643
setAlertErrorVisible(true);
646644
}
647645
});
648-
// TODO: This needs to be changed based off the type
649-
if (blocklyComponent.current){
650-
const blocklyWorkspace = blocklyComponent.current.getBlocklyWorkspace();
651-
if(blocklyComponent){
652-
createOpMode(blocklyWorkspace);
653-
}
654-
}
655646
} else if (newOpModeNameModalPurpose === 'RenameOpMode') {
656647
const workspaceName = commonStorage.getWorkspaceName(currentModulePath);
657648
const oldOpModeName = commonStorage.getModuleName(currentModulePath);

src/editor/editor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ export class Editor {
104104

105105
public loadModuleBlocks(currentModule: commonStorage.Module | null) {
106106
this.currentModule = currentModule;
107-
extendedPythonGenerator.setCurrentModule(currentModule);
108107
if (currentModule) {
109108
this.modulePath = currentModule.modulePath;
110109
this.workspacePath = commonStorage.makeWorkspacePath(currentModule.workspaceName);
@@ -214,6 +213,7 @@ export class Editor {
214213
}
215214

216215
private getModuleContent(): string {
216+
extendedPythonGenerator.setCurrentModule(this.currentModule);
217217
const pythonCode = extendedPythonGenerator.workspaceToCode(this.blocklyWorkspace);
218218
const exportedBlocks = JSON.stringify(extendedPythonGenerator.getExportedBlocks(this.blocklyWorkspace));
219219
const blocksContent = JSON.stringify(Blockly.serialization.workspaces.save(this.blocklyWorkspace));

src/modules/mrc_module_opmode.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Blockly from 'blockly';
22
import { BLOCK_NAME as MRC_CLASS_METHOD_DEF } from '../blocks/mrc_class_method_def'
33

4-
export function create(workspace : Blockly.Workspace){
4+
export function create(workspace : Blockly.Workspace, render: boolean) {
55
let initBlock = workspace.newBlock(MRC_CLASS_METHOD_DEF);
66
initBlock.setFieldValue('init', 'NAME');
77
const extra = {
@@ -14,6 +14,8 @@ export function create(workspace : Blockly.Workspace){
1414
if (initBlock.loadExtraState){
1515
initBlock.loadExtraState(extra);
1616
}
17-
(initBlock as Blockly.BlockSvg).initSvg();
18-
(initBlock as Blockly.BlockSvg).render();
19-
}
17+
if (render) {
18+
(initBlock as Blockly.BlockSvg).initSvg();
19+
(initBlock as Blockly.BlockSvg).render();
20+
}
21+
}

src/storage/common_storage.ts

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

22+
import * as Blockly from 'blockly/core';
23+
2224
import {Block} from "../toolbox/items";
25+
import {create as createOpMode} from '../modules/mrc_module_opmode'
26+
import {extendedPythonGenerator} from '../editor/extended_python_generator';
2327

2428
// Types, constants, and functions related to modules, regardless of where the modules are stored.
2529

@@ -129,34 +133,36 @@ export function makeUploadWorkspaceName(uploadFileName: string): string {
129133
}
130134

131135
/**
132-
* Returns the module content for a new module.
136+
* Returns the module content for a new Workspace.
133137
*/
134-
export function newModuleContent(moduleType: string): string {
135-
let pythonCode;
136-
let blocksContent;
137-
let exportedBlocks;
138-
139-
switch (moduleType) {
140-
case MODULE_TYPE_WORKSPACE:
141-
pythonCode = '';
142-
blocksContent = '{}';
143-
exportedBlocks = '[]';
144-
break;
145-
case MODULE_TYPE_OPMODE:
146-
// TODO: Update the python code and blocks content here to be an OpMode.
147-
pythonCode = '';
148-
blocksContent = '{}';
149-
exportedBlocks = '[]';
150-
break;
151-
case MODULE_TYPE_MECHANISM:
152-
pythonCode = '';
153-
blocksContent = '{}';
154-
exportedBlocks = '[]';
155-
break;
156-
default:
157-
throw new Error('Unexpected module type: ' + moduleType);
158-
}
138+
export function newWorkspaceContent(workspaceName: string): string {
139+
const pythonCode = '';
140+
const exportedBlocks = '[]';
141+
const blocksContent = '{}';
142+
return makeModuleContent(pythonCode, exportedBlocks, blocksContent);
143+
}
144+
145+
/**
146+
* Returns the module content for a new OpMode.
147+
*/
148+
export function newOpModeContent(workspaceName: string, opModeName: string): string {
149+
const module: Module = {
150+
modulePath: makeModulePath(workspaceName, opModeName),
151+
moduleType: MODULE_TYPE_OPMODE,
152+
workspaceName: workspaceName,
153+
moduleName: opModeName,
154+
dateModifiedMillis: 0,
155+
};
156+
157+
// Create a headless blockly workspace.
158+
const headlessBlocklyWorkspace = new Blockly.Workspace();
159+
headlessBlocklyWorkspace.options.oneBasedIndex = false;
160+
createOpMode(headlessBlocklyWorkspace, false);
159161

162+
extendedPythonGenerator.setCurrentModule(module);
163+
const pythonCode = extendedPythonGenerator.workspaceToCode(headlessBlocklyWorkspace);
164+
const exportedBlocks = JSON.stringify(extendedPythonGenerator.getExportedBlocks(headlessBlocklyWorkspace));
165+
const blocksContent = JSON.stringify(Blockly.serialization.workspaces.save(headlessBlocklyWorkspace));
160166
return makeModuleContent(pythonCode, exportedBlocks, blocksContent);
161167
}
162168

0 commit comments

Comments
 (0)