-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (56 loc) · 1.97 KB
/
index.js
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
// https://github.com/google/blockly-samples/tree/master/examples/custom-generator-codelab <3
import * as Blockly from 'blockly';
import {setup} from './blocks/setup';
import {script} from './blocks/script';
import {kbAndMouse} from './blocks/kbm';
import {hotkeys} from './blocks/hotkeys';
import {win} from './blocks/window';
import {text} from './blocks/text';
import {math} from './blocks/math';
import {gui} from './blocks/gui';
import {builtInVars} from './blocks/builtInVars';
import {ahkGen} from './codeGen/generator';
import {toolbox} from './toolbox';
import './index.css';
// TODO: Dynamic tooltips
// TODO (Once majority blocks done): Beautify json (Tabs Size 4)
Blockly.common.defineBlocks(setup);
Blockly.common.defineBlocks(script);
Blockly.common.defineBlocks(kbAndMouse);
Blockly.common.defineBlocks(hotkeys);
Blockly.common.defineBlocks(win);
Blockly.common.defineBlocks(text);
Blockly.common.defineBlocks(math);
Blockly.common.defineBlocks(gui);
Blockly.common.defineBlocks(builtInVars);
const codeDiv = document.getElementById('generatedCode').firstChild;
const blocklyDiv = document.getElementById('blocklyDiv');
const workspace = Blockly.inject(blocklyDiv, {toolbox});
const storageKey = 'ahk_v'
function save(workspace) {
const data = Blockly.serialization.workspaces.save(workspace);
window.localStorage?.setItem(storageKey, JSON.stringify(data));
};
function load(workspace) {
const data = window.localStorage?.getItem(storageKey);
if (!data) return;
Blockly.Events.disable();
Blockly.serialization.workspaces.load(JSON.parse(data), workspace, false);
Blockly.Events.enable();
};
const runCode = () => {
const code = ahkGen.workspaceToCode(workspace);
codeDiv.innerText = code;
};
load(workspace);
runCode();
workspace.addChangeListener((event) => {
if (event.isUiEvent) return;
save(workspace);
});
workspace.addChangeListener((event) => {
if (event.isUiEvent || event.type == Blockly.Events.FINISHED_LOADING || workspace.isDragging()) {
return
}
runCode();
});