Skip to content

Commit 0937da9

Browse files
committed
Make it so the App and the Editor use the same GeneratorContext.
Make GeneratorContext methods addClassMethodName and getClassMethodName only store the method name if it is different than the name field value.
1 parent 37d18a2 commit 0937da9

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/App.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ const App: React.FC = () => {
253253
const [deleteTooltip, setDeleteTooltip] = useState('Delete');
254254
const blocklyComponent = useRef<BlocklyComponentType | null>(null);
255255
const [triggerPythonRegeneration, setTriggerPythonRegeneration] = useState(0);
256-
const generatorContext = useRef(createGeneratorContext());
256+
const generatorContext = useRef<GeneratorContext | null>(null);
257257
const blocksEditor = useRef<editor.Editor | null>(null);
258258
const [generatedCode, setGeneratedCode] = useState('');
259259
const [newProjectNameModalPurpose, setNewProjectNameModalPurpose] = useState('');
@@ -534,7 +534,8 @@ const App: React.FC = () => {
534534
blocklyWorkspace.addChangeListener(mutatorOpenListener);
535535
blocklyWorkspace.addChangeListener(handleBlocksChanged);
536536
}
537-
blocksEditor.current = new editor.Editor(blocklyWorkspace, storage);
537+
generatorContext.current = createGeneratorContext();
538+
blocksEditor.current = new editor.Editor(blocklyWorkspace, generatorContext.current, storage);
538539
}, [blocklyComponent, storage]);
539540

540541
const handleBlocksChanged = (event: Blockly.Events.Abstract) => {

src/editor/editor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import * as Blockly from 'blockly/core';
2323

2424
import { extendedPythonGenerator } from './extended_python_generator';
25-
import { createGeneratorContext, GeneratorContext } from './generator_context';
25+
import { GeneratorContext } from './generator_context';
2626
import * as commonStorage from '../storage/common_storage';
2727
import { getToolboxJSON } from '../toolbox/toolbox';
2828

@@ -33,8 +33,8 @@ const EMPTY_TOOLBOX: Blockly.utils.toolbox.ToolboxDefinition = {
3333
};
3434

3535
export class Editor {
36-
private generatorContext = createGeneratorContext();
3736
private blocklyWorkspace: Blockly.WorkspaceSvg;
37+
private generatorContext: GeneratorContext;
3838
private storage: commonStorage.Storage;
3939
private currentModule: commonStorage.Module | null = null;
4040
private modulePath: string = '';
@@ -44,8 +44,9 @@ export class Editor {
4444
private bindedOnChange: any = null;
4545
private toolbox: Blockly.utils.toolbox.ToolboxDefinition = EMPTY_TOOLBOX;
4646

47-
constructor(blocklyWorkspace: Blockly.WorkspaceSvg, storage: commonStorage.Storage) {
47+
constructor(blocklyWorkspace: Blockly.WorkspaceSvg, generatorContext: GeneratorContext, storage: commonStorage.Storage) {
4848
this.blocklyWorkspace = blocklyWorkspace;
49+
this.generatorContext = generatorContext;
4950
this.storage = storage;
5051
}
5152

src/editor/generator_context.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@ export class GeneratorContext {
9797
}
9898

9999
addClassMethodName(nameFieldValue: string, methodName: string) {
100-
this.classMethodNames[nameFieldValue] = methodName;
100+
if (nameFieldValue !== methodName) {
101+
this.classMethodNames[nameFieldValue] = methodName;
102+
}
101103
}
102104

103105
getClassMethodName(nameFieldValue: string): string | null {
104-
if (nameFieldValue in this.classMethodNames) {
106+
if (this.classMethodNames[nameFieldValue]) {
105107
return this.classMethodNames[nameFieldValue];
106108
}
107-
return null;
109+
return nameFieldValue;
108110
}
109111
}

0 commit comments

Comments
 (0)