An Open Source Implementation of Svelte Components for Web Development using Google Blockly
This project provides Svelte components for visual programming using Google Blockly. It allows developers to create block-based programming interfaces within Svelte applications, making it easier to implement visual programming features in your web projects.
npm install svelte-web-blocks
This package uses XML files for Blockly toolbox definitions. If you're using Vite and encounter errors about "No loader configured for .xml files", add the following plugin to your vite.config.js/ts:
// vite-plugin-xml.js
import fs from 'fs';
import path from 'path';
export default function xmlPlugin() {
return {
name: 'vite-plugin-xml',
transform(_code: string, id: string) {
if (id.endsWith('.xml?raw')) {
const filePath = id.replace('?raw', '');
const content = fs.readFileSync(filePath, 'utf-8');
return {
code: `export default ${JSON.stringify(content)};`,
map: null
};
}
}
};
}
Then update your vite.config.js/ts:
// vite.config.js/ts
import { defineConfig } from 'vite';
import { sveltekit } from '@sveltejs/kit/vite';
import xmlPlugin from './path/to/vite-plugin-xml.js';
export default defineConfig({
plugins: [
sveltekit(),
xmlPlugin()
]
});
<script>
import { BlocklyEditor } from 'svelte-web-blocks';
let editor;
let jsonOutput = '';
function handleEditorChange(event) {
if (event.detail && event.detail.json) {
jsonOutput = event.detail.json;
}
}
function clearWorkspace() {
if (editor) {
editor.clearWorkspace();
}
}
</script>
<div class="editor-container" style="height: 600px;">
<BlocklyEditor
bind:this={editor}
showCodeView={true}
showJsonView={true}
on:change={handleEditorChange}
/>
</div>
<div class="controls">
<button on:click={clearWorkspace}>Clear Workspace</button>
</div>
<script>
import { BlocklyEditor } from 'svelte-web-blocks';
let editor;
let jsonInput = `[
{
"type": "document",
"properties": {
"title": "My Web Page"
},
"children": [
{
"type": "heading",
"properties": {
"text": "Hello World"
}
}
]
}
]`;
function loadJson() {
if (editor && jsonInput) {
editor.loadFromJson(jsonInput);
}
}
</script>
<div class="editor-container" style="height: 600px;">
<BlocklyEditor bind:this={editor} />
</div>
<div class="json-input">
<textarea bind:value={jsonInput} rows="10"></textarea>
<button on:click={loadJson}>Load from JSON</button>
</div>
createBlocksFromJson
: Convert JSON to Blockly blocksconvertHTMLToPug
: Transform HTML to PUG formatinitializeBlocks
: Load all block definitions and generatorsThis package supports various workflows:
Basic HTML Generation
<script>
import { BasicBlockly } from 'svelte-web-blocks';
</script>
<BasicBlockly />
JSON Generation
<script>
import { BlocklyToJson } from 'svelte-web-blocks';
</script>
<BlocklyToJson />
JSON to Blocks Conversion
<script>
import { JsonToBlocks } from 'svelte-web-blocks';
</script>
<JsonToBlocks />
HTML to PUG Conversion
<script>
import { HtmlToPug } from 'svelte-web-blocks';
</script>
<HtmlToPug />
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)This project is licensed under the MIT License - see the LICENSE file for details.