forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatch.js
186 lines (160 loc) · 5.48 KB
/
dispatch.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
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
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 { saveToFile } from "./dispatches/export/saveToFile.js";
import { exportS3 } from "./s3.js";
import { global_state } from "./global_state.js";
import { view as viewMobile } from "./mobile/view.js";
import { mute } from "./engine/playTune.js";
import { strip } from "ansicolor";
const ACTIONS = {
INIT: init,
RUN: run,
SET_BITMAPS({ bitmaps }, state) {
state.bitmaps = bitmaps;
},
TOGGLE_MUTE() {
mute.current = !mute.current;
dispatch("RENDER");
},
LOG_ERROR: logError,
SET_EDIT_RANGE({ range }, state) {
state.editRange = range;
},
SET_ASSET_EDITOR({ type, text }, state) {
state.editor = type;
dispatch("RUN", { headless: true });
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;
},
RENDER_MOBILE({ text }, state) {
// render(document.querySelector(".root"), view(state));
render(document.querySelector(".root"), viewMobile(text));
// screen.orientation.lock('landscape');
},
RENDER(args, state) {
render(document.querySelector(".root"), view(state));
},
DOC_OPEN(args, state) {
const setPerc = n => {
document.documentElement.style.setProperty("--docs-percentage", `${n}%`);
localStorage.setItem("docs-percentage", `${n}%`);
};
const perc = getComputedStyle(document.documentElement)
.getPropertyValue("--docs-percentage");
setPerc((perc.trim() === "0%") ? 75 : 0);
document.querySelector(".docs").classList.toggle("docs-expanded");
dispatch("RENDER");
},
SET_UPLOAD_STATE(args, state) {
state.uploadState = args;
dispatch("RENDER");
},
UPLOAD_LOG(args, state) {
args = strip(args);
console.log(args);
state.uploadLogs += state.uploadLogs ? '\n' + args : args;
dispatch("RENDER");
const container = document.querySelector(".logs-container");
container.scrollTo(0, container.scrollHeight);
},
UPLOAD(args, state) {
if (state.uploadState === "uploading") return;
state.uploadLogs = "";
upload(state.codemirror.state.doc.toString());
},
// File operations
LOAD_NEW_GAME({ code }, state) {
state.newDocument = true;
const cur = state.codemirror.state.doc.toString();
dispatch("SET_EDITOR_TEXT", { text: "", range: [0, cur.length] });
dispatch("RUN");
state.newDocument = true;
dispatch("SET_EDITOR_TEXT", { text: code, range: [0, 0] });
const titleMatch = code.match(/@title:\s+([^\n]+)/);
const name = (titleMatch !== null) ? titleMatch[1] : "UNTITLED";
state.prevName = name;
state.staleRun = true;
dispatch("RENDER");
// fold all tagged template literals
setTimeout(() => {
const fullText = state.codemirror.state.doc.toString();
const matches = [ ...fullText.matchAll(/(map|bitmap|tune)`[\s\S]*?`/g) ];
state.codemirror.collapseRanges(matches.map((match) => [ match.index, match.index + 1]));
}, 100);
},
SAVE_TO_STORAGE(args, state) {
if (state.newDocument) {
state.newDocument = false;
return;
}
const code = state.codemirror.state.doc.toString();
const titleMatch = code.match(/@title:\s+([^\n]+)/);
const name = (titleMatch !== null) ? titleMatch[1] : "UNTITLED";
const newSave = [ name, code ];
const currentGames = state.savedGames.filter(x => x[0] !== name && x[0] !== state.prevName);
state.savedGames = [ newSave, ...currentGames ];
state.prevName = name;
window.localStorage.setItem("puzzle-lab", JSON.stringify(state.savedGames));
window.localStorage.setItem("last-game", name);
},
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] : "UNTITLED";
saveToFile(`${name}.js`, string);
},
async GET_URL(args, state) {
if (state.shareLinkState !== "idle") return console.warn("Share already in progress");
state.shareLinkState = "loading";
dispatch("RENDER");
const string = state.codemirror.state.doc.toString();
const link = await exportS3(string);
console.log(`Got link: ${link}`);
const input = document.createElement("input");
input.value = link;
input.select();
document.execCommand("copy");
input.remove();
navigator.clipboard.writeText(link)
state.shareLinkState = "copied";
dispatch("RENDER");
setTimeout(() => {
state.shareLinkState = "idle";
dispatch("RENDER");
}, 3000);
fetch("https://misguided.enterprises/clubscraps/cabal", {
method: "POST",
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ link })
});
},
}
export function dispatch(action, args = {}) {
const trigger = ACTIONS[action];
if (trigger) return trigger(args, global_state);
else {
console.log("Action not recognized:", action);
return null;
}
}