I am integrating blockly workspace into my react application. I'm following the same approach from the blocklyfactory demo codebase (
https://github.com/google/blockly/blob/b6282096429c9a4ef9c53228097cd744457f1065/demos/blockfactory/factory.js#L164) for updating preview workspace. But whenever the previewworkspace gets disposed and injected again, I'm losing focus from the factory workspace which interrups the user flow.
For eg. I've created a value block in the factory workspace now that block gets updated in the preview workspace and when I start to type NAME or any value, the focus gets removed half the way itself.
createPreviewBlock(blockType, code) {
const jsonCode = JSON.parse(code);
this.createNewBlock(blockType, jsonCode);
const previewBlock = this.previewWorkspace.newBlock(blockType);
previewBlock.initSvg();
previewBlock.render();
previewBlock.setMovable(false);
previewBlock.setDeletable(false);
previewBlock.moveBy(15, 10);
this.previewWorkspace.clearUndo();
}
updateFactoryPreview() {
try {
if (this.previewWorkspace) {
this.previewWorkspace.dispose();
}
const blocklyConfig = {
media: '/blockly/media/',
scrollbars: true,
theme: DarkTheme,
grid: {
spacing: 20,
length: 3,
colour: 'rgba(255, 255, 255, 0.1)',
snap: true,
},
};
this.previewWorkspace = BlocklyFactory.inject(
this.manager.containers.blocklyPreviewContainer,
blocklyConfig,
);
this.previewWorkspace.clear();
const { blockType, code } = this.getCodeFromBlocks();
if (code) {
this.createPreviewBlock(blockType, code);
}
} catch (error) {
console.error(error);
}
}
In the above code, If I didn't assign the newly created workspace to this.previewWorkspace, the focus is still there (ofcourse the new blocks won't be created). and also If I comment the "this.createPreviewBlock(blockType, code)" line, the focus is still there...
What can I do to make the focus to stay in the factory workspace itself like the one in the hosted demo (
https://blockly-demo.appspot.com/static/demos/blockfactory/index.html)