I'm trying to create a tool where you can create HTML pages using Blockly blocks. I already have a page that shows my workspace and my self-created block. Now I want to write a script that gets the code from the workspace. Normally there is a workspaceToCode method in the Blockly library. Unfortunately I can't access any Blockly methods or really anything Blockly-related in my index.html.
I've looked up similar projects and can't seem to find any differences. I'm loading blockly_compressed.js, blocks_compressed.js and javascript_compressed.js. And because it shows me a workspace with "functioning" blocks I'm pretty sure that the paths are correct.
See below what I tried and thanks in advance for your help:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<style>
...
</style>
<script src="node_modules/blockly/blockly_compressed.js"></script>
<script src="node_modules/blockly/blocks_compressed.js"></script>
<script src="node_modules/blockly/msg/en.js"></script>
<script src="node_modules/blockly/javascript_compressed.js"></script>
</head>
<body>
...
<script src="html_blocks.js"></script>
<script src="html_generator.js"></script>
<script src="main.js"></script>
<script>
function update(event) {
var code = HtmlGenerator.workspaceToCode(workspace);
document.getElementById('HTMLCodeDiv').innerText = code;
}
workspace.addChangeListener(update);
</script>
</body>
</html>
The error it is giving is "unresolved function or method" for the workspaceToCode method as well as the addChangeListener method.
Since you haven't shown all of your code, I can't provide a precise answer to explain exactly what's gone wrong for you here, but I can say that Blockly in a classical (non-module) script tag adds itself to the
windowas follows:where
rootiswindow(by way ofthis) andfactory()is the entire Blockly code. All Blockly functions are namespaced inside of thewindow.Blocklyobject, so there is no suchwindow.workspacevariable that would be created unless one of your other scripts (not shown) created this and attached it to the window.If you open your browser console, you can type
Blockly.and see the list of available properties that were imported by the scripts. The other Blockly scripts simply attach more properties to the globalBlocklyobject that was created by the first script tag.Blockly.WorkspaceandBlockly.workspaceToCodeare some of these properties, and you can callBlockly.injectto create a workspace.For example,
It strikes me as an antipattern to use
node_modulesin script tags like this, even though some of the Blockly examples do this. Usually, you'd use a bundler of some sort (webpack, parcel, vite, browserify, rollup, etc) to allow you to dynamically import the code using modules (example below). Or else keep your build without any local dependencies and use a CDN and take advantage of client caching (as shown above). Usingnode_modulesdirectly seems like the worst of both worlds, especially without a minification build.For example, you can use parcel to build your app for the web. A bundler makes it easy to use
node_moduleswithout specifying the paths. You can develop using modules rather than legacy UMD script tags, which help you organize the project into chunks and avoid polluting the window with shared data.The example below is contrived for clarity, but hopefully you can extrapolate the approach (or something similar) to your project.
index.html:src/index.js:src/generate-code.js:package.json:Building and running:
Now, navigate to (by default) http://localhost:1234 (or whatever
parceltells you on the console) and begin developing.