diff --git a/blocks/IDE/block_design.json b/blocks/IDE/block_design.json new file mode 100644 index 0000000..3c538d2 --- /dev/null +++ b/blocks/IDE/block_design.json @@ -0,0 +1,17 @@ +{ + "ide_addcode": { + "message0": "Add Code %1", + "args0": [ + { + "type": "input_value", + "name": "CODE", + "text": "print('hello world!')" + } + ], + "previousStatement": null, + "nextStatement": null, + "colour": 150, + "tooltip": "Add Code for some code is not in IDE", + "helpUrl": "" + } +} diff --git a/blocks/IDE/generator.js b/blocks/IDE/generator.js new file mode 100644 index 0000000..b5050a5 --- /dev/null +++ b/blocks/IDE/generator.js @@ -0,0 +1,15 @@ +const { luaGenerator } = require('blockly/lua'); + +// Check if luaGenerator.forBlock is defined and initialize if necessary- +if (!luaGenerator.forBlock) { + luaGenerator.forBlock = {}; +} + +// Define your custom block handler +luaGenerator.forBlock['ide_addcode'] = function(block, generator) { + var codefromuser = generator.valueToCode(block, 'CODE', generator.ORDER_ATOMIC); + + // Remove all occurrences of the matched characters + const cleanedStr = codefromuser.replace(/[']/g, ''); + return cleanedStr; +}; diff --git a/blocks/IDE/index.json b/blocks/IDE/index.json new file mode 100644 index 0000000..6f4e377 --- /dev/null +++ b/blocks/IDE/index.json @@ -0,0 +1,14 @@ +{ + "name": "IDE", + "author": "DPSoftware Foundation", + "description": "Block for IDE", + "version": "1.0.0", + "category": "Blockly Extensions", + "keyword": "test", + "license": "GPL-3.0-or-later", + "peripherals": false, + "library": false, + "turtle": false, + "require_network": false, + "dependencies": {} +} \ No newline at end of file diff --git a/blocks/IDE/toolbox.xml b/blocks/IDE/toolbox.xml new file mode 100644 index 0000000..8055198 --- /dev/null +++ b/blocks/IDE/toolbox.xml @@ -0,0 +1,6 @@ + \ No newline at end of file diff --git a/blocks/test/index.json b/blocks/test/index.json index 4923508..cf8a6e2 100644 --- a/blocks/test/index.json +++ b/blocks/test/index.json @@ -9,6 +9,6 @@ "peripherals": false, "library": false, "turtle": false, - "system": true, - "require_network": false + "require_network": false, + "dependencies": {} } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 959f720..52bf04b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,23 +1,49 @@ { "name": "ccide", - "version": "1.0.0", + "version": "1.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "ccide", - "version": "1.0.0", + "version": "1.0.2", "license": "GPL-3.0-or-later", "dependencies": { + "@blockly/field-multilineinput": "^5.0.5", + "@blockly/theme-dark": "^7.0.3", "@electron/remote": "^2.1.2", "blockly": "^11.1.1", "bootstrap": "^5.3.3", - "electron-prompt": "^1.7.0" + "electron-prompt": "^1.7.0", + "mergexml": "^1.2.4", + "xmldom": "^0.6.0" }, "devDependencies": { "electron": "^31.1.0" } }, + "node_modules/@blockly/field-multilineinput": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@blockly/field-multilineinput/-/field-multilineinput-5.0.5.tgz", + "integrity": "sha512-fCh+fkEeNCUwPyO6O45rH68e27RjMFWAvfUu/fYybGSIvt92uZfhZqq0veLMqAhwfYR7qQ7HIhUqvHQzAe4t6g==", + "engines": { + "node": ">=8.0.0" + }, + "peerDependencies": { + "blockly": "^11.0.0" + } + }, + "node_modules/@blockly/theme-dark": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/@blockly/theme-dark/-/theme-dark-7.0.3.tgz", + "integrity": "sha512-lRV9V5vjijbJAlUdZqtEhUt81OjHjZK8vagxir1cUqTrZ/+MNS2m7wkhIVPba4RWInJ1LY/nHEoQn6demZqRRQ==", + "engines": { + "node": ">=8.17.0" + }, + "peerDependencies": { + "blockly": "^11.0.0" + } + }, "node_modules/@electron/get": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/@electron/get/-/get-2.0.3.tgz", @@ -127,6 +153,14 @@ "@types/node": "*" } }, + "node_modules/@xmldom/xmldom": { + "version": "0.7.13", + "resolved": "https://registry.npmjs.org/@xmldom/xmldom/-/xmldom-0.7.13.tgz", + "integrity": "sha512-lm2GW5PkosIzccsaZIz7tp8cPADSIlIHWDFTR1N0SzfinhhYgeIQjFMz4rYzanCScr3DqQLeomUDArp6MWKm+g==", + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/agent-base": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.1.tgz", @@ -138,6 +172,11 @@ "node": ">= 14" } }, + "node_modules/asap": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", + "integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==" + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -358,6 +397,15 @@ "integrity": "sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==", "optional": true }, + "node_modules/dezalgo": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.4.tgz", + "integrity": "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig==", + "dependencies": { + "asap": "^2.0.0", + "wrappy": "1" + } + }, "node_modules/electron": { "version": "31.1.0", "resolved": "https://registry.npmjs.org/electron/-/electron-31.1.0.tgz", @@ -486,6 +534,19 @@ "node": ">= 6" } }, + "node_modules/formidable": { + "version": "3.5.1", + "resolved": "https://registry.npmjs.org/formidable/-/formidable-3.5.1.tgz", + "integrity": "sha512-WJWKelbRHN41m5dumb0/k8TeAx7Id/y3a+Z7QfhxP/htI9Js5zYaEDtG8uMgG0vM0lOlqnmjE99/kfpOYi/0Og==", + "dependencies": { + "dezalgo": "^1.0.4", + "hexoid": "^1.0.0", + "once": "^1.4.0" + }, + "funding": { + "url": "https://ko-fi.com/tunnckoCore/commissions" + } + }, "node_modules/fs-extra": { "version": "8.1.0", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", @@ -675,6 +736,14 @@ "node": ">= 0.4" } }, + "node_modules/hexoid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/hexoid/-/hexoid-1.0.0.tgz", + "integrity": "sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==", + "engines": { + "node": ">=8" + } + }, "node_modules/html-encoding-sniffer": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-4.0.0.tgz", @@ -829,6 +898,16 @@ "node": ">=10" } }, + "node_modules/mergexml": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/mergexml/-/mergexml-1.2.4.tgz", + "integrity": "sha512-yiOlDqcVCz7AG1eSboonc18FTlfqDEKYfGoAV3Lul98u6YRV/s0kjtf4bjk47t0hLTFJR0BSYMd6BpmX3xDjNQ==", + "dependencies": { + "@xmldom/xmldom": "^0.7.0", + "formidable": "^3.5.1", + "xpath": "0.0.27" + } + }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", @@ -1229,6 +1308,22 @@ "resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz", "integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==" }, + "node_modules/xmldom": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.6.0.tgz", + "integrity": "sha512-iAcin401y58LckRZ0TkI4k0VSM1Qg0KGSc3i8rU+xrxe19A/BN1zHyVSJY7uoutVlaTSzYyk/v5AmkewAP7jtg==", + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/xpath": { + "version": "0.0.27", + "resolved": "https://registry.npmjs.org/xpath/-/xpath-0.0.27.tgz", + "integrity": "sha512-fg03WRxtkCV6ohClePNAECYsmpKKTv5L8y/X3Dn1hQrec3POx2jHZ/0P2qQ6HvsrU1BmeqXcof3NGGueG6LxwQ==", + "engines": { + "node": ">=0.6.0" + } + }, "node_modules/yauzl": { "version": "2.10.0", "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", @@ -1240,6 +1335,18 @@ } }, "dependencies": { + "@blockly/field-multilineinput": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/@blockly/field-multilineinput/-/field-multilineinput-5.0.5.tgz", + "integrity": "sha512-fCh+fkEeNCUwPyO6O45rH68e27RjMFWAvfUu/fYybGSIvt92uZfhZqq0veLMqAhwfYR7qQ7HIhUqvHQzAe4t6g==", + "requires": {} + }, + "@blockly/theme-dark": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/@blockly/theme-dark/-/theme-dark-7.0.3.tgz", + "integrity": "sha512-lRV9V5vjijbJAlUdZqtEhUt81OjHjZK8vagxir1cUqTrZ/+MNS2m7wkhIVPba4RWInJ1LY/nHEoQn6demZqRRQ==", + "requires": {} + }, "@electron/get": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/@electron/get/-/get-2.0.3.tgz", @@ -1329,6 +1436,11 @@ "@types/node": "*" } }, + "@xmldom/xmldom": { + "version": "0.7.13", + "resolved": "https://registry.npmjs.org/@xmldom/xmldom/-/xmldom-0.7.13.tgz", + "integrity": "sha512-lm2GW5PkosIzccsaZIz7tp8cPADSIlIHWDFTR1N0SzfinhhYgeIQjFMz4rYzanCScr3DqQLeomUDArp6MWKm+g==" + }, "agent-base": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.1.tgz", @@ -1337,6 +1449,11 @@ "debug": "^4.3.4" } }, + "asap": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", + "integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==" + }, "asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -1485,6 +1602,15 @@ "integrity": "sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==", "optional": true }, + "dezalgo": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.4.tgz", + "integrity": "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig==", + "requires": { + "asap": "^2.0.0", + "wrappy": "1" + } + }, "electron": { "version": "31.1.0", "resolved": "https://registry.npmjs.org/electron/-/electron-31.1.0.tgz", @@ -1574,6 +1700,16 @@ "mime-types": "^2.1.12" } }, + "formidable": { + "version": "3.5.1", + "resolved": "https://registry.npmjs.org/formidable/-/formidable-3.5.1.tgz", + "integrity": "sha512-WJWKelbRHN41m5dumb0/k8TeAx7Id/y3a+Z7QfhxP/htI9Js5zYaEDtG8uMgG0vM0lOlqnmjE99/kfpOYi/0Og==", + "requires": { + "dezalgo": "^1.0.4", + "hexoid": "^1.0.0", + "once": "^1.4.0" + } + }, "fs-extra": { "version": "8.1.0", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", @@ -1705,6 +1841,11 @@ "function-bind": "^1.1.2" } }, + "hexoid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/hexoid/-/hexoid-1.0.0.tgz", + "integrity": "sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==" + }, "html-encoding-sniffer": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-4.0.0.tgz", @@ -1827,6 +1968,16 @@ "escape-string-regexp": "^4.0.0" } }, + "mergexml": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/mergexml/-/mergexml-1.2.4.tgz", + "integrity": "sha512-yiOlDqcVCz7AG1eSboonc18FTlfqDEKYfGoAV3Lul98u6YRV/s0kjtf4bjk47t0hLTFJR0BSYMd6BpmX3xDjNQ==", + "requires": { + "@xmldom/xmldom": "^0.7.0", + "formidable": "^3.5.1", + "xpath": "0.0.27" + } + }, "mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", @@ -2122,6 +2273,16 @@ "resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz", "integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==" }, + "xmldom": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.6.0.tgz", + "integrity": "sha512-iAcin401y58LckRZ0TkI4k0VSM1Qg0KGSc3i8rU+xrxe19A/BN1zHyVSJY7uoutVlaTSzYyk/v5AmkewAP7jtg==" + }, + "xpath": { + "version": "0.0.27", + "resolved": "https://registry.npmjs.org/xpath/-/xpath-0.0.27.tgz", + "integrity": "sha512-fg03WRxtkCV6ohClePNAECYsmpKKTv5L8y/X3Dn1hQrec3POx2jHZ/0P2qQ6HvsrU1BmeqXcof3NGGueG6LxwQ==" + }, "yauzl": { "version": "2.10.0", "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", diff --git a/package.json b/package.json index 967338d..78c96f0 100644 --- a/package.json +++ b/package.json @@ -13,9 +13,13 @@ "electron": "^31.1.0" }, "dependencies": { + "@blockly/field-multilineinput": "^5.0.5", + "@blockly/theme-dark": "^7.0.3", "@electron/remote": "^2.1.2", "blockly": "^11.1.1", "bootstrap": "^5.3.3", - "electron-prompt": "^1.7.0" + "electron-prompt": "^1.7.0", + "mergexml": "^1.2.4", + "xmldom": "^0.6.0" } } diff --git a/src/blocksmanager.js b/src/blocksmanager.js index 626fc69..0769661 100644 --- a/src/blocksmanager.js +++ b/src/blocksmanager.js @@ -1,25 +1,27 @@ const fs = require('fs'); const path = require('path'); +const { DOMParser, XMLSerializer } = require('xmldom'); const peripheralsfolder = path.join(__dirname, "../blocks"); -function mergeXml(originalXml, appendXml) { - // Remove from appendXml - const cleanedAppendXml = appendXml.replace(/^]*>|<\/xml>$/g, '').trim(); +function mergeXml(xml1, xml2) { + const parser = new DOMParser(); + const serializer = new XMLSerializer(); - // Find the closing tag in originalXml - const closingTag = ''; - const index = originalXml.lastIndexOf(closingTag); - - if (index === -1) { - console.error('Closing tag not found in original XML.'); - return originalXml; // return original XML as is + const doc1 = parser.parseFromString(xml1, 'text/xml'); + const doc2 = parser.parseFromString(xml2, 'text/xml'); + + const root1 = doc1.documentElement; + const children2 = doc2.documentElement.childNodes; + + // Iterate through children2 and append each node to root1 + for (let i = 0; i < children2.length; i++) { + root1.appendChild(children2[i].cloneNode(true)); } - // Prepare the modified XML with the insertion of cleanedAppendXml - const modifiedXml = originalXml.slice(0, index) + cleanedAppendXml + originalXml.slice(index + closingTag.length); - - return modifiedXml; + const mergedXml = serializer.serializeToString(doc1); + console.log(mergedXml) + return mergedXml; } function loadperipheral(workspace, currenttoolbar, peripherals) { diff --git a/src/ccRemote.js b/src/ccRemote.js new file mode 100644 index 0000000..14c03a2 --- /dev/null +++ b/src/ccRemote.js @@ -0,0 +1,67 @@ +const WebSocket = require('ws'); + +class CCRemote { + constructor(ip = "127.0.0.1", port = 5133) { + this.socket = new WebSocket.Server({ + port: port, + host: ip + }); + + console.log("Server is started"); + + this.socket.on('connection', (ws) => { + console.log('WebSocket connection established.'); + + ws.on('message', (message) => { + console.log(`Received message => ${message}`); + }); + }); + + this.socket.on('close', () => { + console.log('WebSocket connection closed.'); + }); + + this.socket.on('error', (error) => { + console.error('WebSocket error:', error); + }); + } + + sendCommand(command) { + // Iterate over each connected client and send the command + this.socket.clients.forEach((client) => { + if (client.readyState === WebSocket.OPEN) { + client.send(command); + } + }); + } + + sendCode(Code) { + // Iterate over each connected client and send the command + this.socket.clients.forEach((client) => { + if (client.readyState === WebSocket.OPEN) { + client.send("sendcode"); + setTimeout(() => { + console.log(Code) + client.send(Code); + return true + }, 500); + } else { + return false + } + }); + } + runCode() { + // Iterate over each connected client and send the command + this.socket.clients.forEach((client) => { + if (client.readyState === WebSocket.OPEN) { + client.send("runcode"); + return true; + } else { + return false; + } + }); + } + +} + +module.exports = { CCRemote }; diff --git a/src/codegen.js b/src/codegen.js index f77da9d..05d4d17 100644 --- a/src/codegen.js +++ b/src/codegen.js @@ -3,6 +3,9 @@ function delay(time) { } const { luaGenerator } = require('blockly/lua'); // Use require syntax for Blockly module +const { CCRemote } = require("./ccRemote") + +const ccInstance = new CCRemote('127.0.0.1', 5133); const progress = document.getElementById("progress"); const circles = document.querySelectorAll(".circle"); @@ -33,6 +36,9 @@ const uploadUpdateProgress = () => { } }; +function clientexit() { + ccInstance.sendCommand("exit") +} async function gencode() { document.getElementById('upload-popup').style.display = 'block'; upcurrentActive = 1; @@ -43,8 +49,9 @@ async function gencode() { upcurrentActive++; uploadUpdateProgress(); document.getElementById('upload-status').textContent = "Generating code"; + let code = null; try { - let code = luaGenerator.workspaceToCode(workspace); + code = luaGenerator.workspaceToCode(workspace); console.log(code); } catch (e) { uploadError = true; @@ -57,16 +64,18 @@ async function gencode() { document.getElementById('upload-status').textContent = "Uploading code to machine"; upcurrentActive++; uploadUpdateProgress(); - await delay(1000) + ccInstance.sendCode(code); + await delay(500) // execute with remote document.getElementById('upload-status').textContent = "Executing code"; upcurrentActive++; uploadUpdateProgress(); - await delay(1000) + ccInstance.runCode(); // done! document.getElementById('upload-status').textContent = "Done!"; + await delay(1000) document.getElementById('upload-popup').style.animation = 'fadeOut 0.3s ease'; // Apply fade-out animation setTimeout(function() { document.getElementById('upload-popup').style.display = 'none'; // Hide popup after animation completes diff --git a/src/index.html b/src/index.html index cb1c2ed..5478afd 100644 --- a/src/index.html +++ b/src/index.html @@ -22,7 +22,10 @@ - + diff --git a/src/index.js b/src/index.js index a4e41b3..10cfe47 100644 --- a/src/index.js +++ b/src/index.js @@ -28,6 +28,7 @@ app.whenReady().then(() => { win.loadFile('index.html') //win.openDevTools(); + win.maximize() //app.on('activate', () => { // if (BrowserWindow.getAllWindows().length === 0) { @@ -111,8 +112,8 @@ app.whenReady().then(() => { { label: 'Edit', submenu: [ - { role: 'undo', accelerator: 'CmdOrCtrl+Z' }, - { role: 'redo', accelerator: 'CmdOrCtrl+Y' }, + { role: 'undo', accelerator: 'CmdOrCtrl+Z', click: () => {win.webContents.send('request-undo-redo', false)} }, + { role: 'redo', accelerator: 'CmdOrCtrl+Y', click: () => {win.webContents.send('request-undo-redo', true)} }, { type: 'separator' }, { role: 'cut', accelerator: 'CmdOrCtrl+X' }, { role: 'copy', accelerator: 'CmdOrCtrl+C' }, @@ -225,34 +226,34 @@ app.whenReady().then(() => { ipc.on('workspace-notsave', (event) => { win.setTitle(`${currentprojectname}* | ccIDE`) - }) - - /* - win.on('close', function(e){ - win.show() - }); - - app.on("before-quit", function() { - if (currentprojectopen) { - const result = dialog.showMessageBoxSync({ - type: 'question', - buttons: ['Save', 'Don\'t Save', 'Cancel'], - defaultId: 2, - title: 'Save Changes', - message: "Your project is not saved", - }); - if (result === 1) { - win = null - } else if (result === 0) { - appexiting = true; - win.webContents.send('save-workspace-request'); - - } - } else { - win = null - } - - }) - */ + }) }) + + +app.on("close", function(e) { + console.log("Close event triggered"); + if (currentprojectopen) { + const result = dialog.showMessageBoxSync({ + type: 'question', + buttons: ['Save', 'Don\'t Save', 'Cancel'], + defaultId: 2, + title: 'Save Changes', + message: "Your project is not saved", + }); + if (result === 1) { + // Don't save, continue closing + win = null; + } else if (result === 0) { + // Save and then quit + appexiting = true; + win.webContents.send('save-workspace-request'); + } else { + // Cancel the close operation + e.preventDefault(); + } + } else { + // No unsaved changes, continue closing + win = null; + } +}); \ No newline at end of file diff --git a/src/module_block_design.json b/src/module_block_design.json index 7280f57..b7cf57b 100644 --- a/src/module_block_design.json +++ b/src/module_block_design.json @@ -1,50 +1,17 @@ { - "sys_commmands_exec": { - "message0": "Execute %1", + "sys_sleep": { + "message0": "Sleep For %1 Second", "args0": [ - { - "type": "input_value", - "name": "EXECOMMANDS", - "text": "say Hello World" - } + { + "type": "input_value", + "name": "SLEEPSEC", + "text": 1 + } ], "previousStatement": null, "nextStatement": null, "colour": 120, - "tooltip": "Execute a specific command.", - "helpUrl": "https://tweaked.cc/module/commands.html#v:exec" - }, - "sys_commmands_getblockposition": { - "message0": "GetBlockInfo at minX %1 minY %2 minZ %3 maxX %4 maxY %5 maxZ %6", - "args0": [ - { - "type": "input_value", - "name": "MINX" - }, - { - "type": "input_value", - "name": "MINY" - }, - { - "type": "input_value", - "name": "MINZ" - }, - { - "type": "input_value", - "name": "MAXX" - }, - { - "type": "input_value", - "name": "MAXY" - }, - { - "type": "input_value", - "name": "MAXZ" - } - ], - "colour": 120, - "output": "Array", - "tooltip": "Get information about a range of blocks.", - "helpUrl": "https://tweaked.cc/module/commands.html#v:getBlockInfos" + "tooltip": "Sleep command", + "helpUrl": "" } } \ No newline at end of file diff --git a/src/module_generator.js b/src/module_generator.js index e69de29..831048f 100644 --- a/src/module_generator.js +++ b/src/module_generator.js @@ -0,0 +1,14 @@ +const { luaGenerator } = require('blockly/lua'); + +// Check if luaGenerator.forBlock is defined and initialize if necessary- +if (!luaGenerator.forBlock) { + luaGenerator.forBlock = {}; +} + +// Define your custom block handler +luaGenerator.forBlock['sys_sleep'] = function(block, generator) { + var sleepfor = generator.valueToCode(block, 'SLEEPSEC', generator.ORDER_OVERRIDES); + + var code = `sleep(${sleepfor})`; + return code; +}; diff --git a/src/toolbox.xml b/src/toolbox.xml index 1ad854a..efb0f07 100644 --- a/src/toolbox.xml +++ b/src/toolbox.xml @@ -1,9 +1,16 @@ \ No newline at end of file diff --git a/src/virtualcode.js b/src/virtualcode.js index 8de9437..f8335ed 100644 --- a/src/virtualcode.js +++ b/src/virtualcode.js @@ -9,18 +9,33 @@ const path = require('path'); const { ipcRenderer } = require("electron"); const { loadperipheral } = require("./blocksmanager"); const Blockly = require('blockly'); +const { DarkTheme } = require('@blockly/theme-dark') const ipc = ipcRenderer; - let isprojectsaved = false; let usedlibinproject = [] Blockly.utils.colour.setHsvSaturation(0.9) let originaltoolbar = fs.readFileSync(path.join(__dirname, "toolbox.xml"), 'utf8'); - +const sysmodulejson = fs.readFileSync(path.join(__dirname, "module_block_design.json"), 'utf8'); + +const blocksJson = JSON.parse(sysmodulejson); +for (const blockId in blocksJson) { + if (blocksJson.hasOwnProperty(blockId)) { + Blockly.Blocks[blockId] = { + init: function() { + this.jsonInit(blocksJson[blockId]); + } + }; + } +} +require("./module_generator") + + var workspace = Blockly.inject('blocklyDiv', { toolbox: originaltoolbar, + theme: DarkTheme, trashcan: true, grid: { spacing: 20, @@ -31,6 +46,7 @@ var workspace = Blockly.inject('blocklyDiv', { }); originaltoolbar = loadperipheral(workspace, originaltoolbar, "test"); +originaltoolbar = loadperipheral(workspace, originaltoolbar, "IDE"); workspace.getToolbox().getFlyout().autoClose = false; @@ -72,7 +88,13 @@ ipc.on('workspace-saved', (event, success) => { isprojectsaved = success }); +ipc.on('request-undo-redo', (event, redo) => { + console.log(redo) + workspace.undo(redo) +}); + // Ensure Blockly container is shown after the workspace is injected document.getElementById('loadingScreen').style.visibility = 'hidden'; document.getElementById('blocklyContainer').style.visibility = 'visible'; -document.getElementById('statusMessage').textContent = "ready"; \ No newline at end of file +document.getElementById('statusMessage').textContent = "ready"; +