-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.html
More file actions
227 lines (212 loc) · 7.79 KB
/
example.html
File metadata and controls
227 lines (212 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hexput Blockly Example</title>
<!-- Load Blockly first -->
<script src="https://unpkg.com/blockly/blockly.min.js"></script>
<!-- Load our bundle after Blockly -->
<script src="./dist/hexput-blockly.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100vh;
}
.header {
padding: 10px 20px;
background-color: #4a6da7;
color: white;
display: flex;
justify-content: space-between;
align-items: center;
}
.header h1 {
margin: 0;
font-size: 1.5em;
}
.container {
display: flex;
flex: 1;
overflow: hidden;
}
#blocklyDiv {
flex: 2;
height: 100%;
}
.output-panel {
flex: 1;
display: flex;
flex-direction: column;
background-color: #f5f5f5;
border-left: 1px solid #ddd;
overflow: hidden;
min-width: 300px;
}
#output {
flex: 1;
margin: 0;
padding: 10px;
background-color: #2d2d2d;
color: #f8f8f2;
font-family: 'Courier New', Courier, monospace;
overflow: auto;
white-space: pre;
}
.toolbar {
padding: 10px;
background-color: #eee;
display: flex;
justify-content: space-between;
}
button {
padding: 8px 16px;
background-color: #4a6da7;
color: white;
border: none;
cursor: pointer;
border-radius: 4px;
}
button:hover {
background-color: #3b5998;
}
</style>
</head>
<body>
<div class="header">
<h1>Hexput Blockly Example</h1>
</div>
<div class="container">
<div id="blocklyDiv"></div>
<div class="output-panel">
<div class="toolbar">
<button onclick="generateCode()">Generate Hexput Code</button>
</div>
<pre id="output">// Generated Hexput code will appear here</pre>
</div>
</div>
<!-- Toolbox definition -->
<xml id="toolbox" style="display: none">
<category name="Variables" colour="230">
<block type="variable_declaration"></block>
<block type="variable_reference"></block>
</category>
<category name="Values" colour="160">
<block type="number_literal"></block>
<block type="string_literal"></block>
<block type="boolean_literal"></block>
<block type="any_concat"></block>
<block type="math_subtract"></block>
<block type="math_multiply"></block>
<block type="math_divide"></block>
<block type="medicine-aspirin"></block>
<block type="medicine-paracetamol"></block>
</category>
<category name="Collections" colour="290">
<block type="array_literal"></block>
<block type="array_item"></block>
<block type="object_literal"></block>
<block type="object_property"></block>
<block type="object_property_access"></block>
<block type="object_bracket_access"></block>
<block type="array_index_access"></block>
</category>
<category name="Control" colour="210">
<block type="if_statement"></block>
<block type="loop_statement"></block>
<block type="function_call"></block>
<block type="function_params"></block>
<block type="function_parameter"></block>
</category>
<category name="Logic" colour="210">
<block type="logic_and"></block>
<block type="logic_or"></block>
<block type="logic_not"></block>
<block type="comparison_eq"></block>
<block type="comparison_neq"></block>
<block type="comparison_gt"></block>
<block type="comparison_lt"></block>
<block type="comparison_gte"></block>
<block type="comparison_lte"></block>
</category>
</xml>
<script>
// Initialize when page is loaded
document.addEventListener('DOMContentLoaded', function() {
// Make sure the library is loaded
if (!hexputBlockly) {
console.error('hexputBlockly library not loaded!');
document.getElementById('output').textContent = '// Error: hexputBlockly library not loaded';
return;
}
// Initialize Blockly with our custom blocks
hexputBlockly.initBlockly(Blockly, 'blocklyDiv');
// Verify that Blockly.Hexput is defined
if (!Blockly.Hexput) {
console.error('Blockly.Hexput is not defined!');
document.getElementById('output').textContent = '// Error: Hexput generator not initialized';
return;
}
// Add custom literals dynamically
const aspirinBlockType = hexputBlockly.addCustomLiteral(
Blockly,
Blockly.Hexput,
"medicine-aspirin",
"Aspirin",
"medication.aspirin"
);
const paracetamolBlockType = hexputBlockly.addCustomLiteral(
Blockly,
Blockly.Hexput,
"medicine-paracetamol",
"Paracetamol",
"medication.paracetamol"
);
// Create some starter blocks to demonstrate functionality
createStarterBlocks();
});
// Function to generate code from blocks
function generateCode() {
if (!Blockly || !Blockly.Hexput) {
document.getElementById('output').textContent = '// Error: Hexput generator not available';
return;
}
const workspace = Blockly.getMainWorkspace();
try {
// Generate Hexput code
const code = Blockly.Hexput.workspaceToCode(workspace);
document.getElementById('output').textContent = code || '// No blocks in workspace';
} catch (e) {
document.getElementById('output').textContent = '// Error generating code: ' + e.message;
console.error(e);
}
}
// Create some starter blocks for the example
function createStarterBlocks() {
const workspace = Blockly.getMainWorkspace();
// Clear existing blocks
workspace.clear();
// Create a simple variable declaration
const varBlock = workspace.newBlock('variable_declaration');
varBlock.setFieldValue('greeting', 'VAR_NAME');
varBlock.moveBy(50, 50);
// Add a string literal
const stringBlock = workspace.newBlock('string_literal');
stringBlock.setFieldValue('Hello, Hexput!', 'TEXT');
stringBlock.moveBy(50, 100);
// Connect the string to the variable
const connection = varBlock.getInput('VALUE').connection;
connection.connect(stringBlock.outputConnection);
// Render blocks
varBlock.initSvg();
varBlock.render();
stringBlock.initSvg();
stringBlock.render();
}
</script>
</body>
</html>