forked from hackclub/puzzlelab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatch.js
More file actions
90 lines (82 loc) · 2.31 KB
/
dispatch.js
File metadata and controls
90 lines (82 loc) · 2.31 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
import { render } from "./libs/uhtml.js";
import { view } from "./view.js";
import { upload } from "./upload.js";
import { run } from "./dispatches/run.js";
import { init } from "./dispatches/init.js";
import { logError } from "./dispatches/logError.js";
import { setName } from "./dispatches/setName.js";
import { saveToFile } from "./dispatches/export/saveToFile.js";
// import "./dispatches/fetchAndBundle/fetchAndBundle.js";
import { exportS3 } from "./s3.js";
import { global_state } from "./global_state.js";
function getURLPath(extension) {
return (
window.location.protocol +
"//" +
window.location.host +
window.location.pathname +
extension
);
}
const ACTIONS = {
INIT: init,
RUN: run,
SET_BITMAPS({ bitmaps }, state) {
state.bitmaps = bitmaps;
},
UPLOAD(args, state) {
upload(state.codemirror.state.doc.toString());
},
SAVE_TO_FILE(args, state) {
const string = state.codemirror.state.doc.toString();
const match = string.match(/@title:\s+([^\n]+)/);
const name = (match !== null) ? match[1] : "DRAFT";
saveToFile(`${name}.js`, string);
},
async GET_URL(args, state) {
const string = state.codemirror.state.doc.toString();
const link = await exportS3(string);
console.log(link);
},
LOG_ERROR: logError,
SET_EDIT_RANGE({ range }, state) {
state.editRange = range;
},
SET_ASSET_EDITOR({ type, text }, state) {
state.editor = type;
dispatch("RENDER");
if (type === null) return;
const el = document.getElementById("asset-editor");
el.loadInitValue && el.loadInitValue({
text,
bitmaps: state.bitmaps
});
},
SET_EDITOR_TEXT({ text, range }, state) {
const [ from, to ] = range;
const changes = {
from,
to,
insert: text
};
state.codemirror.dispatch({ changes })
dispatch("RENDER");
if (state.editRange === null) return;
state.editRange[1] = state.editRange[0] + text.length;
},
SET_NAME: setName,
LOAD_FROM_DATA({ data }, state) {
console.log(data);
},
RENDER(args, state) {
render(document.querySelector(".root"), view(state));
},
}
export function dispatch(action, args = {}) {
const trigger = ACTIONS[action];
if (trigger) return trigger(args, global_state);
else {
console.log("Action not recongnized:", action);
return null;
}
}