Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import * as editor from './editor/editor';
import { extendedPythonGenerator } from './editor/extended_python_generator';

import * as commonStorage from './storage/common_storage';
import * as storageModule from './storage/module';
import * as storageProject from './storage/project';
import * as clientSideStorage from './storage/client_side_storage';

import * as CustomBlocks from './blocks/setup_custom_blocks';
Expand Down Expand Up @@ -129,7 +131,7 @@ const App: React.FC = (): React.JSX.Element => {
* App wrapper that manages project state and provides it to UserSettingsProvider.
*/
const AppWithUserSettings: React.FC<{ storage: commonStorage.Storage }> = ({ storage }) => {
const [project, setProject] = React.useState<commonStorage.Project | null>(null);
const [project, setProject] = React.useState<storageProject.Project | null>(null);

return (
<UserSettingsProvider
Expand All @@ -145,16 +147,16 @@ const AppWithUserSettings: React.FC<{ storage: commonStorage.Storage }> = ({ sto
* Inner application content component that has access to UserSettings context.
*/
interface AppContentProps {
project: commonStorage.Project | null;
setProject: React.Dispatch<React.SetStateAction<commonStorage.Project | null>>;
project: storageProject.Project | null;
setProject: React.Dispatch<React.SetStateAction<storageProject.Project | null>>;
}

const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.JSX.Element => {
const { t, i18n } = useTranslation();
const { settings, updateLanguage, updateTheme, storage, isLoading } = useUserSettings();

const [alertErrorMessage, setAlertErrorMessage] = React.useState('');
const [currentModule, setCurrentModule] = React.useState<commonStorage.Module | null>(null);
const [currentModule, setCurrentModule] = React.useState<storageModule.Module | null>(null);
const [messageApi, contextHolder] = Antd.message.useMessage();
const [generatedCode, setGeneratedCode] = React.useState<string>('');
const [toolboxSettingsModalIsOpen, setToolboxSettingsModalIsOpen] = React.useState(false);
Expand Down Expand Up @@ -340,7 +342,7 @@ const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.J
};

/** Changes current module with automatic saving if modified. */
const changeModule = async (module: commonStorage.Module | null): Promise<void> => {
const changeModule = async (module: storageModule.Module | null): Promise<void> => {
if (currentModule && areBlocksModified()) {
await saveBlocks();
}
Expand All @@ -359,7 +361,7 @@ const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.J
};

/** Creates tab items from project data. */
const createTabItemsFromProject = (projectData: commonStorage.Project): Tabs.TabItem[] => {
const createTabItemsFromProject = (projectData: storageProject.Project): Tabs.TabItem[] => {
const tabs: Tabs.TabItem[] = [
{
key: projectData.robot.modulePath,
Expand Down
69 changes: 35 additions & 34 deletions src/blocks/mrc_call_python_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import { createFieldDropdown } from '../fields/FieldDropdown';
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
import { MRC_STYLE_FUNCTIONS } from '../themes/styles'
import * as toolboxItems from '../toolbox/items';
import * as commonStorage from '../storage/common_storage';
import * as storageModule from '../storage/module';
import * as storageModuleContent from '../storage/module_content';


// A block to call a python function.
Expand Down Expand Up @@ -496,11 +497,11 @@ const CALL_PYTHON_FUNCTION = {
},
mutateMethodCaller: function(
this: CallPythonFunctionBlock,
methodOrEvent: commonStorage.Method | commonStorage.Event
methodOrEvent: storageModuleContent.Method | storageModuleContent.Event
): void {
// mutateMethodCaller is called when the method or event definition block in the same module is modified.
if (this.mrcFunctionKind == FunctionKind.EVENT) {
const event = methodOrEvent as commonStorage.Event;
const event = methodOrEvent as storageModuleContent.Event;
this.mrcArgs = [];
event.args.forEach((arg) => {
this.mrcArgs.push({
Expand All @@ -509,7 +510,7 @@ const CALL_PYTHON_FUNCTION = {
});
});
} else if (this.mrcFunctionKind == FunctionKind.INSTANCE_WITHIN) {
const method = methodOrEvent as commonStorage.Method;
const method = methodOrEvent as storageModuleContent.Method;
this.mrcReturnType = method.returnType;
this.mrcArgs = [];
// We don't include the arg for the self argument because we don't need a socket for it.
Expand All @@ -522,9 +523,9 @@ const CALL_PYTHON_FUNCTION = {
}
this.updateBlock_();
},
getComponentsFromRobot: function(this: CallPythonFunctionBlock): commonStorage.Component[] {
getComponentsFromRobot: function(this: CallPythonFunctionBlock): storageModuleContent.Component[] {
// Get the list of components whose type matches this.mrcComponentClassName.
const components: commonStorage.Component[] = [];
const components: storageModuleContent.Component[] = [];
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace);
if (editor) {
editor.getComponentsFromRobot().forEach(component => {
Expand All @@ -550,9 +551,9 @@ const CALL_PYTHON_FUNCTION = {
// visible warning on it.
if (this.mrcFunctionKind === FunctionKind.INSTANCE_COMPONENT) {
let foundComponent = false;
const componentsInScope: commonStorage.Component[] = [];
const componentsInScope: storageModuleContent.Component[] = [];
componentsInScope.push(...this.getComponentsFromRobot());
if (editor.getCurrentModuleType() === commonStorage.MODULE_TYPE_MECHANISM) {
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
componentsInScope.push(...editor.getComponentsFromWorkspace());
}
for (const component of componentsInScope) {
Expand Down Expand Up @@ -599,7 +600,7 @@ const CALL_PYTHON_FUNCTION = {
// If the robot method has changed, update the block if possible or put a
// visible warning on it.
if (this.mrcFunctionKind === FunctionKind.INSTANCE_ROBOT) {
if (editor.getCurrentModuleType() === commonStorage.MODULE_TYPE_MECHANISM) {
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
warnings.push('This block is not allowed to be used inside a mechanism.');
} else {
let foundRobotMethod = false;
Expand Down Expand Up @@ -645,7 +646,7 @@ const CALL_PYTHON_FUNCTION = {
// If the method has changed, update the block if possible or put a
// visible warning on it.
if (this.mrcFunctionKind === FunctionKind.INSTANCE_MECHANISM) {
if (editor.getCurrentModuleType() === commonStorage.MODULE_TYPE_MECHANISM) {
if (editor.getCurrentModuleType() === storageModule.MODULE_TYPE_MECHANISM) {
warnings.push('This block is not allowed to be used inside a mechanism.');
} else {
let foundMechanism = false;
Expand All @@ -661,7 +662,7 @@ const CALL_PYTHON_FUNCTION = {

let foundMechanismMethod = false;
const mechanism = editor.getMechanism(mechanismInRobot);
const mechanismMethods: commonStorage.Method[] = mechanism
const mechanismMethods: storageModuleContent.Method[] = mechanism
? editor.getMethodsFromMechanism(mechanism) : [];
for (const mechanismMethod of mechanismMethods) {
if (mechanismMethod.blockId === this.mrcOtherBlockId) {
Expand Down Expand Up @@ -790,11 +791,11 @@ export const pythonFromBlock = function(
: block.getFieldValue(FIELD_FUNCTION_NAME);
// Generate the correct code depending on the module type.
switch (generator.getModuleType()) {
case commonStorage.MODULE_TYPE_ROBOT:
case commonStorage.MODULE_TYPE_MECHANISM:
case storageModule.MODULE_TYPE_ROBOT:
case storageModule.MODULE_TYPE_MECHANISM:
code = 'self.';
break;
case commonStorage.MODULE_TYPE_OPMODE:
case storageModule.MODULE_TYPE_OPMODE:
code = 'self.robot.';
break;
}
Expand All @@ -815,13 +816,13 @@ export const pythonFromBlock = function(
: block.getFieldValue(FIELD_FUNCTION_NAME);
// Generate the correct code depending on the module type.
switch (generator.getModuleType()) {
case commonStorage.MODULE_TYPE_ROBOT:
case storageModule.MODULE_TYPE_ROBOT:
code = 'self.' + mechanismName;
break;
case commonStorage.MODULE_TYPE_OPMODE:
case storageModule.MODULE_TYPE_OPMODE:
code = 'self.robot.' + mechanismName;
break;
case commonStorage.MODULE_TYPE_MECHANISM:
case storageModule.MODULE_TYPE_MECHANISM:
// The INSTANCE_MECHANISM version should not be used in a mechanism.
// TODO(lizlooney): What if the user copies a block from an robot or opmode and pastes
// it into a mechanism?
Expand Down Expand Up @@ -882,7 +883,7 @@ export function renameMethodCallers(workspace: Blockly.Workspace, otherBlockId:
}

export function mutateMethodCallers(
workspace: Blockly.Workspace, otherBlockId: string, methodOrEvent: commonStorage.Method | commonStorage.Event) {
workspace: Blockly.Workspace, otherBlockId: string, methodOrEvent: storageModuleContent.Method | storageModuleContent.Event) {
const oldRecordUndo = Blockly.Events.getRecordUndo();

getMethodCallers(workspace, otherBlockId).forEach(block => {
Expand Down Expand Up @@ -1070,14 +1071,14 @@ function createInstanceMethodBlock(
}

export function addInstanceWithinBlocks(
methods: commonStorage.Method[],
methods: storageModuleContent.Method[],
contents: toolboxItems.ContentsType[]) {
methods.forEach(method => {
contents.push(createInstanceWithinBlock(method));
});
}

function createInstanceWithinBlock(method: commonStorage.Method): toolboxItems.Block {
function createInstanceWithinBlock(method: storageModuleContent.Method): toolboxItems.Block {
const extraState: CallPythonFunctionExtraState = {
functionKind: FunctionKind.INSTANCE_WITHIN,
returnType: method.returnType,
Expand All @@ -1088,7 +1089,7 @@ function createInstanceWithinBlock(method: commonStorage.Method): toolboxItems.B
const fields: {[key: string]: any} = {};
fields[FIELD_FUNCTION_NAME] = method.visibleName;
const inputs: {[key: string]: any} = {};
// Convert method.args from commonStorage.MethodArg[] to ArgData[].
// Convert method.args from storageModuleContent.MethodArg[] to ArgData[].
const args: ArgData[] = [];
// We don't include the arg for the self argument because we don't need a socket for it.
for (let i = 1; i < method.args.length; i++) {
Expand All @@ -1103,7 +1104,7 @@ function createInstanceWithinBlock(method: commonStorage.Method): toolboxItems.B
}

export function getInstanceComponentBlocks(
component: commonStorage.Component): toolboxItems.ContentsType[] {
component: storageModuleContent.Component): toolboxItems.ContentsType[] {
const contents: toolboxItems.ContentsType[] = [];

const classData = getClassData(component.className);
Expand Down Expand Up @@ -1131,7 +1132,7 @@ export function getInstanceComponentBlocks(
}

function createInstanceComponentBlock(
component: commonStorage.Component, functionData: FunctionData): toolboxItems.Block {
component: storageModuleContent.Component, functionData: FunctionData): toolboxItems.Block {
const extraState: CallPythonFunctionExtraState = {
functionKind: FunctionKind.INSTANCE_COMPONENT,
returnType: functionData.returnType,
Expand All @@ -1155,14 +1156,14 @@ function createInstanceComponentBlock(
}

export function addInstanceRobotBlocks(
methods: commonStorage.Method[],
methods: storageModuleContent.Method[],
contents: toolboxItems.ContentsType[]) {
methods.forEach(method => {
contents.push(createInstanceRobotBlock(method));
});
}

function createInstanceRobotBlock(method: commonStorage.Method): toolboxItems.Block {
function createInstanceRobotBlock(method: storageModuleContent.Method): toolboxItems.Block {
const extraState: CallPythonFunctionExtraState = {
functionKind: FunctionKind.INSTANCE_ROBOT,
returnType: method.returnType,
Expand All @@ -1173,7 +1174,7 @@ function createInstanceRobotBlock(method: commonStorage.Method): toolboxItems.Bl
const fields: {[key: string]: any} = {};
fields[FIELD_FUNCTION_NAME] = method.visibleName;
const inputs: {[key: string]: any} = {};
// Convert method.args from commonStorage.MethodArg[] to ArgData[].
// Convert method.args from storageModuleContent.MethodArg[] to ArgData[].
const args: ArgData[] = [];
// We don't include the arg for the self argument because we don't need a socket for it.
for (let i = 1; i < method.args.length; i++) {
Expand All @@ -1188,17 +1189,17 @@ function createInstanceRobotBlock(method: commonStorage.Method): toolboxItems.Bl
}

export function addInstanceMechanismBlocks(
mechanismInRobot: commonStorage.MechanismInRobot,
methods: commonStorage.Method[],
mechanismInRobot: storageModuleContent.MechanismInRobot,
methods: storageModuleContent.Method[],
contents: toolboxItems.ContentsType[]) {
methods.forEach(method => {
contents.push(createInstanceMechanismBlock(mechanismInRobot, method));
});
}

function createInstanceMechanismBlock(
mechanismInRobot: commonStorage.MechanismInRobot,
method: commonStorage.Method): toolboxItems.Block {
mechanismInRobot: storageModuleContent.MechanismInRobot,
method: storageModuleContent.Method): toolboxItems.Block {
const extraState: CallPythonFunctionExtraState = {
functionKind: FunctionKind.INSTANCE_MECHANISM,
returnType: method.returnType,
Expand All @@ -1212,7 +1213,7 @@ function createInstanceMechanismBlock(
fields[FIELD_MECHANISM_NAME] = mechanismInRobot.name;
fields[FIELD_FUNCTION_NAME] = method.visibleName;
const inputs: {[key: string]: any} = {};
// Convert method.args from commonStorage.MethodArg[] to ArgData[].
// Convert method.args from storageModuleContent.MethodArg[] to ArgData[].
const args: ArgData[] = [];
// For INSTANCE_MECHANISM functions, the 0 argument is 'self', but
// self is represented by the FIELD_MECHANISM_NAME field.
Expand All @@ -1229,14 +1230,14 @@ function createInstanceMechanismBlock(
}

export function addFireEventBlocks(
events: commonStorage.Event[],
events: storageModuleContent.Event[],
contents: toolboxItems.ContentsType[]) {
events.forEach(event => {
contents.push(createFireEventBlock(event));
});
}

function createFireEventBlock(event: commonStorage.Event): toolboxItems.Block {
function createFireEventBlock(event: storageModuleContent.Event): toolboxItems.Block {
const extraState: CallPythonFunctionExtraState = {
functionKind: FunctionKind.EVENT,
returnType: RETURN_TYPE_NONE,
Expand All @@ -1246,7 +1247,7 @@ function createFireEventBlock(event: commonStorage.Event): toolboxItems.Block {
const fields: {[key: string]: any} = {};
fields[FIELD_EVENT_NAME] = event.name;
const inputs: {[key: string]: any} = {};
// Convert event.args from commonStorage.MethodArg[] to ArgData[].
// Convert event.args from storageModuleContent.MethodArg[] to ArgData[].
const args: ArgData[] = [];
event.args.forEach(methodArg => {
args.push({
Expand Down
19 changes: 10 additions & 9 deletions src/blocks/mrc_class_method_def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import { createFieldNonEditableText } from '../fields/FieldNonEditableText'
import { createFieldFlydown } from '../fields/field_flydown';
import { Order } from 'blockly/python';
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
import * as commonStorage from '../storage/common_storage';
import * as storageModule from '../storage/module';
import * as storageModuleContent from '../storage/module_content';
import { renameMethodCallers, mutateMethodCallers } from './mrc_call_python_function'
import * as toolboxItems from '../toolbox/items';
import { getClassData } from './utils/python';
Expand Down Expand Up @@ -266,8 +267,8 @@ const CLASS_METHOD_DEF = {
}
return legalName;
},
getMethod: function (this: ClassMethodDefBlock): commonStorage.Method | null {
const method: commonStorage.Method = {
getMethod: function (this: ClassMethodDefBlock): storageModuleContent.Method | null {
const method: storageModuleContent.Method = {
blockId: this.id,
visibleName: this.getFieldValue(FIELD_METHOD_NAME),
pythonName: this.mrcFuncName ? this.mrcFuncName : '',
Expand All @@ -288,13 +289,13 @@ const CLASS_METHOD_DEF = {
});
return method;
},
getMethodForWithin: function (this: ClassMethodDefBlock): commonStorage.Method | null {
getMethodForWithin: function (this: ClassMethodDefBlock): storageModuleContent.Method | null {
if (this.mrcCanBeCalledWithinClass) {
return this.getMethod();
}
return null;
},
getMethodForOutside: function (this: ClassMethodDefBlock): commonStorage.Method | null {
getMethodForOutside: function (this: ClassMethodDefBlock): storageModuleContent.Method | null {
if (this.mrcCanBeCalledOutsideClass) {
return this.getMethod();
}
Expand Down Expand Up @@ -424,7 +425,7 @@ export const pythonFromBlock = function (

let params = block.mrcParameters;
let paramString = "self";
if (generator.getModuleType() == commonStorage.MODULE_TYPE_MECHANISM && block.mrcPythonMethodName == '__init__') {
if (generator.getModuleType() == storageModule.MODULE_TYPE_MECHANISM && block.mrcPythonMethodName == '__init__') {
const ports: string[] = generator.getComponentPortParameters();
if (ports.length) {
paramString += ', ' + ports.join(', ');
Expand Down Expand Up @@ -452,7 +453,7 @@ export const pythonFromBlock = function (
code = generator.scrub_(block, code);
generator.addClassMethodDefinition(funcName, code);

// Save the name of the function we just generated so we can use it to create the commonStorage.Method.
// Save the name of the function we just generated so we can use it to create the storageModuleContent.Method.
// in the getMethod function.
block.mrcFuncName = funcName;

Expand Down Expand Up @@ -518,7 +519,7 @@ function createClassMethodDefBlock(

export function getMethodsForWithin(
workspace: Blockly.Workspace,
methods: commonStorage.Method[]): void {
methods: storageModuleContent.Method[]): void {
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
const method = (block as ClassMethodDefBlock).getMethodForWithin();
if (method) {
Expand All @@ -529,7 +530,7 @@ export function getMethodsForWithin(

export function getMethodsForOutside(
workspace: Blockly.Workspace,
methods: commonStorage.Method[]): void {
methods: storageModuleContent.Method[]): void {
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
const method = (block as ClassMethodDefBlock).getMethodForOutside();
if (method) {
Expand Down
Loading