|
19 | 19 | * @author [email protected] (Liz Looney)
|
20 | 20 | */
|
21 | 21 |
|
| 22 | +import * as Blockly from 'blockly/core'; |
| 23 | + |
22 | 24 | import {Block} from "../toolbox/items";
|
| 25 | +import {create as createOpMode} from '../modules/mrc_module_opmode' |
| 26 | +import {extendedPythonGenerator} from '../editor/extended_python_generator'; |
23 | 27 |
|
24 | 28 | // Types, constants, and functions related to modules, regardless of where the modules are stored.
|
25 | 29 |
|
@@ -129,34 +133,36 @@ export function makeUploadWorkspaceName(uploadFileName: string): string {
|
129 | 133 | }
|
130 | 134 |
|
131 | 135 | /**
|
132 |
| - * Returns the module content for a new module. |
| 136 | + * Returns the module content for a new Workspace. |
133 | 137 | */
|
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); |
159 | 161 |
|
| 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)); |
160 | 166 | return makeModuleContent(pythonCode, exportedBlocks, blocksContent);
|
161 | 167 | }
|
162 | 168 |
|
|
0 commit comments