-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
312 lines (278 loc) · 7.8 KB
/
code.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { toast_alert, toast_ok } from "./toast.js";
import { marked } from "./marked.esm.js";
import DOMPurify from "./purify.es.mjs";
import { supported_actions } from "./common.js";
import { replace_variables, get_text_from_section } from "./utils.js";
var story = {};
const viewer_states = Object.freeze({
MENU: "MENU",
PLAYING: "PLAYING",
});
const hot_keys = {
b: {
description: "back",
action: one_step_back,
},
f: {
description: "toggle full screen",
action: () => {
if (document.fullscreenElement) {
document.exitFullscreen();
return;
}
const element = document.body;
const requestMethod =
element.requestFullScreen ||
element.webkitRequestFullScreen ||
element.mozRequestFullScreen ||
element.msRequestFullScreen;
if (requestMethod) {
// Native full screen.
requestMethod.call(element);
}
},
},
h: {
description: "toggle hide text",
action: () => {
if (story_container.classList.contains("d-none")) {
story_container.classList.remove("d-none");
} else {
story_container.classList.add("d-none");
}
},
},
};
let current_viewer_state = viewer_states.MENU;
const menu_container = document.getElementById("menu_container");
const story_container = document.getElementById("story_container");
const story_text = document.getElementById("story_text");
const choices_row = document.getElementById("choices_row");
const background_image = document.getElementById("background_image");
const spinner = document.getElementById("spinner");
function one_step_back() {
if (!story?.state?.history || story.state.history.length < 1) {
return;
}
load_section(story.state.history.pop(), false);
}
function load_file(content_handler, read_as_data) {
var input = document.createElement("input");
input.type = "file";
input.onchange = (e) => {
var file = e.target.files[0];
var reader = new FileReader();
if (read_as_data) {
reader.readAsDataURL(file);
} else {
reader.readAsText(file, "UTF-8");
}
reader.onload = (readerEvent) => {
const content = readerEvent.target.result;
//console.log(content);
content_handler(content);
};
};
input.click();
}
function load_graph_from_file() {
load_file((content) => {
try {
story = JSON.parse(content);
} catch (error) {
toast_alert("Not a valid json");
}
start_playing();
});
}
function load_graph_from_url(url) {
toast_ok("Loading story from " + url);
show_spinner();
fetch(url)
.then((response) => {
if (response.ok) {
return response.json();
}
})
.then((json) => {
story = json;
start_playing();
})
.catch((error) => {
toast_alert("Error loading story from " + url);
console.error("error loading url:", url, error);
})
.finally(hide_spinner);
}
function start_playing() {
if (!story?.sections) {
toast_alert("No Story loaded");
current_viewer_state = viewer_states.MENU;
show_ui_components_according_to_state();
return;
}
if (!story?.state) {
story.state = {};
}
if (
!story?.state?.current_section ||
!story?.sections?.[story.state.current_section]
) {
if (!Object.keys(story.sections)) {
toast_alert("This story has no sections. Please load a different one.");
return;
}
story.state.current_section = Object.keys(story.sections)[0];
}
load_section(story.state.current_section);
toast_ok("Story Adventure Loaded");
current_viewer_state = viewer_states.PLAYING;
show_ui_components_according_to_state();
}
function execute_actions(script) {
for (const action of script) {
if (!(action.action in supported_actions)) {
console.error("No such action", action.action);
return;
}
supported_actions[action.action].action(story, action.parameters);
}
}
function load_section(id, add_current_section_to_history = true) {
if (!story.state) {
story.state = {};
}
if (!story.state.history) {
story.state.history = [];
}
if (add_current_section_to_history) {
story.state.history.push(story.state.current_section);
}
story.state.current_section = id;
if (!story?.sections?.[id]) {
toast_alert(`Section ${id} is missing from the story`);
return;
}
const section = story.sections[id];
if (section.script) {
execute_actions(section.script);
}
const text = get_text_from_section(section, story.state?.variables);
if (!text) {
toast_alert("This section has no text");
}
story_text.innerHTML = DOMPurify.sanitize(marked.parse(text));
if (section?.media?.type === "image" && section?.media?.src) {
background_image.src = section.media.src;
if (!background_image.style) {
background_image.style = {};
}
background_image.classList.remove("d-none");
}
choices_row.innerHTML = "";
if (section?.next) {
for (const choice of section.next) {
const col = choices_row.appendChild(document.createElement("div"));
col.className = "col";
const button = col.appendChild(document.createElement("button"));
button.className = "btn btn-primary";
button.type = "button";
if (choice?.text) {
button.appendChild(
document.createTextNode(
replace_variables(choice.text, story.state?.variables)
)
);
} else {
button.innerHTML = '<i class="bi bi-arrow-right-circle-fill"></i>';
}
button.addEventListener("click", (event) => {
load_section(choice.next);
event.stopPropagation();
});
}
}
}
function show_ui_components_according_to_state() {
if (current_viewer_state == viewer_states.MENU) {
if (!story_container.classList.contains("d-none")) {
story_container.classList.add("d-none");
}
menu_container.classList.remove("d-none");
return;
}
if (current_viewer_state == viewer_states.PLAYING) {
if (!menu_container.classList.contains("d-none")) {
menu_container.classList.add("d-none");
}
story_container.classList.remove("d-none");
return;
}
}
function read_query_params() {
let params = new URL(document.location.toString())?.searchParams;
let load = params?.get("load");
if (load) {
load_graph_from_url(load);
}
}
function handle_global_click() {
if (
document.activeElement.nodeName === "INPUT" ||
document.activeElement.nodeName === "BUTTON" ||
!story?.state?.current_section
) {
return;
}
const section = story.sections[story.state.current_section];
if (!section?.next) {
return;
}
if (section.next.length != 1) {
return;
}
load_section(section.next[0].next);
}
function handle_global_key_down(event) {
if (
document.activeElement.nodeName === "INPUT" ||
document.activeElement.nodeName === "TEXTAREA"
) {
return;
}
for (const key of Object.keys(hot_keys)) {
if (event.key === key) {
hot_keys[key].action();
event.stopPropagation();
}
}
}
function show_spinner() {
spinner.classList.remove("d-none");
}
function hide_spinner() {
if (!spinner.classList.contains("d-none")) {
spinner.classList.add("d-none");
}
}
function overwrite_actions() {
supported_actions["INPUT"].action = (st, parameters) => {
if (!parameters || parameters.length < 2) {
console.error("Need to parameters to ask for input", action);
return;
}
const user_input = prompt(parameters[1]);
supported_actions["SET"].action(st, [parameters[0], user_input]);
};
}
function on_load() {
overwrite_actions();
show_ui_components_according_to_state();
read_query_params();
}
document
.getElementById("load_button")
.addEventListener("click", load_graph_from_file);
document.addEventListener("click", handle_global_click);
document.addEventListener("keydown", handle_global_key_down);
on_load();