Skip to content

Commit

Permalink
Merge pull request #10 from N-J-Martin/blockOutput
Browse files Browse the repository at this point in the history
Block output
  • Loading branch information
ozgurakgun authored Mar 9, 2024
2 parents bbad839 + 2d9ce05 commit 59530dd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
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

0 comments on commit 59530dd

Please sign in to comment.