Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block output #10

Merged
merged 7 commits into from
Mar 9, 2024
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: 14 additions & 0 deletions src/blocks/essence.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,20 @@ export const blocks = Blockly.common.createBlockDefinitionsFromJsonArray([
"tooltip": "",
"helpUrl": ""
},
{
"type": "output",
"message0": "%1",
"args0": [
{
"type": "field_label_serializable",
"name": "SOLUTION",
"text": "test"
}
],
"colour": 0,
"tooltip": "",
"helpUrl": ""
}

])

6 changes: 6 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pre, code {
min-width: 600px;
}

#blocklyDiv2 {
flex-basis: 100%;
height: 100%;
min-width: 600px;
}

#outputPane {
display: flex;
flex-direction: column;
Expand Down
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<div id="output"></div>
</div>
<div id="blocklyDiv"></div>
<div id="blocklyDiv2"></div>
</div>
</body>
</html>
40 changes: 39 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {essenceGenerator} from './generators/essence';
import {save, load} from './serialization';
import {toolbox} from './toolbox';
import './index.css';
import { variables } from 'blockly/blocks';

// Register the blocks and generator with Blockly
Blockly.common.defineBlocks(blocks);
Expand All @@ -25,6 +26,7 @@ const codeDiv = document.getElementById('generatedCode').firstChild;
const outputDiv = document.getElementById('output');
const blocklyDiv = document.getElementById('blocklyDiv');
const ws = Blockly.inject(blocklyDiv, {toolbox});
const blockOut = Blockly.inject(document.getElementById('blocklyDiv2'), {});

//variable category using https://www.npmjs.com/package/@blockly/plugin-typed-variable-modal.
// much of the code below is from the usage instructions
Expand Down Expand Up @@ -178,6 +180,43 @@ async function getSolution() {
solution = await get(currentJobid);
}
solutionText.innerHTML = JSON.stringify(solution, undefined, 2);

if (solution.status == "ok"){
for (let sol of solution.solution){
for (let v in sol){
blockOut.createVariable(v);
let varBlock = blockOut.newBlock('variables_set');
varBlock.setFieldValue(blockOut.getVariable(v).getId(), 'VAR');
//console.log(typeof(sol[v]));
let valueBlock;
switch (typeof(sol[v])){
case("bigint"):
case("number"): {
valueBlock = blockOut.newBlock('math_number');
valueBlock.setFieldValue(sol[v], "NUM");
break;
}
case("string"): {
console.log("enum");
valueBlock = blockOut.newBlock('text');
valueBlock.setFieldValue(sol[v], "TEXT");
break;
}
default:{
console.log("idk");
valueBlock = null;
break;
}

};
varBlock.getInput("VALUE").connection.connect(valueBlock.outputConnection);
let addVarBlock = new Blockly.Events.BlockCreate(varBlock);
addVarBlock.run(true);

}
}

}
}

// generate essence file from generated code
Expand All @@ -188,7 +227,6 @@ function downloadEssenceCode() {
let filename = prompt("Please enter essence file name", "test");
filename = filename + ".essence"
let code = essenceGenerator.workspaceToCode(ws);
console.log(code)
let file = new File([code], filename);
let url = URL.createObjectURL(file);
const a = document.createElement("a");
Expand Down