forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatch.js
163 lines (145 loc) · 4.43 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
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 { exportS3 } from "./s3.js";
import { global_state } from "./global_state.js";
import { saveGame } from "./saveGame.js"
import { view as viewMobile } from "./mobile/view.js";
import { mute } from "./engine/playTune.js";
import { strip } from 'ansicolor';
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) {
if (state.uploadState === "uploading") return;
state.uploadLogs = "";
upload(state.codemirror.state.doc.toString());
},
SAVE(args, state) {
saveGame(state);
state.stale = false;
dispatch("RENDER");
},
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);
},
TOGGLE_MUTE() {
mute.current = !mute.current;
dispatch("RENDER");
},
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 })
});
},
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;
},
SET_NAME: setName,
LOAD_FROM_DATA({ data }, state) {
console.log(data);
},
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);
}
}
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;
}
}