-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
1207 lines (1082 loc) · 30.1 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cytoscape from "./cytoscape.esm.min.js";
import cytoscapeKlay from "./cytoscape-klay.js";
import JSZip from "./jszip.js";
cytoscape.use(cytoscapeKlay);
import { toast_alert, toast_ok } from "./toast.js";
import { supported_actions } from "./common.js";
import {
create_element_with_classes_and_attributes,
get_text_from_section,
tools_files,
} from "./utils.js";
const data_url_regexp = /^data:image\/([a-z]*);base64,(.*)$/;
var story = {};
const current_editor_story_key = "current_editor_story";
const text_area = document.getElementById("text");
const text_label = document.getElementById("text_label");
const text_containers = document.getElementsByClassName(
"text_editor_container"
);
const img_container = document.getElementById("img_container");
const delete_button = document.getElementById("delete_button");
const add_node_button = document.getElementById("add_node_button");
const add_edge_button = document.getElementById("add_edge_button");
const load_button = document.getElementById("load_button");
const add_media_button = document.getElementById("add_media_button");
const section_select = document.getElementById("section_select");
const action_div = document.getElementById("action_div");
const variables_menu = document.getElementById("variables_menu");
const hot_keys = {
s: {
description: "Add new Section",
action: handle_add_node,
},
m: {
description: "Add Media",
action: add_or_remove_media,
},
};
var active_element = null;
text_editor_hide();
var cy = cytoscape({
container: document.getElementById("cy"),
boxSelectionEnabled: false,
autounselectify: false,
style: cytoscape
.stylesheet()
.selector("node")
.css({
padding: 10,
"border-width": 3,
"border-opacity": 0.5,
content: "data(id)",
"text-valign": "center",
"text-halign": "center",
})
.selector(".leave")
.style({
shape: "round-hexagon",
"background-color": "red",
})
.selector(".root")
.style({
shape: "diamond",
"background-color": "green",
})
.selector("edge")
.css({
"curve-style": "bezier",
width: 6,
"target-arrow-shape": "triangle",
"line-color": "#ffaaaa",
"target-arrow-color": "#ffaaaa",
}),
}); // cy init
function add_node(section) {
cy.add([{ group: "nodes", data: section }]);
const new_node = cy.getElementById(section.id);
const active_section = find_elements_section(active_element);
if (active_section) {
const active_node = cy.getElementById(active_section.id);
if (active_node) {
new_node.position({
x: active_node.position("x") + 100,
y: active_node.position("y") + 20,
});
}
}
new_node.on("tap", function (evt) {
text_editor_load(section);
});
console.log("node added", new_node);
section_select.appendChild(
create_element_with_classes_and_attributes("option", {
attributes: { value: section.id },
text: section.id,
})
);
}
function remove_edge(from, to) {
const edge_id = from + "-" + to;
cy.remove("#" + edge_id);
}
function add_edge(section, choice) {
const edge_id = section.id + "-" + choice.next;
cy.add([
{
group: "edges",
data: {
id: edge_id,
source: section.id,
target: choice.next,
},
},
]);
const new_edge = cy.getElementById(edge_id);
new_edge.on("tap", function (evt) {
text_editor_load(choice);
});
console.log("edge added", new_edge);
}
function redraw_adventure_graph() {
console.debug("drawing adventure graph", story);
cy.remove("node");
section_select.innerHTML = "";
section_select.appendChild(
create_element_with_classes_and_attributes("option", {
attributes: { value: "new_section" },
text: "New Section",
})
);
for (const id in story.sections) {
const section = story.sections[id];
section.id = id;
add_node(section);
}
for (const id in story.sections) {
const section = story.sections[id];
if (!section.next) {
continue;
}
for (const next of section.next) {
add_edge(section, next);
}
}
cy.$("node").leaves().addClass("leave");
cy.$("node").roots().addClass("root");
const layouts = {
breadthfirst: {
name: "breadthfirst",
directed: true,
spacingFactor: 1.3,
animate: true,
},
cose: {
name: "cose",
animate: true,
},
klay: {
name: "klay",
animate: true,
klay: {
aspectRatio: 10, // The aimed aspect ratio of the drawing, that is the quotient of width by height
direction: "RIGHT", // Overall direction of edges: horizontal (right / left) or vertical (down / up)
/* UNDEFINED, RIGHT, LEFT, DOWN, UP */
thoroughness: 14, // How much effort should be spent to produce a nice layout..
},
},
};
const layout = cy.layout(layouts["klay"]);
layout.run();
cy.fit();
}
function edit_variable(variable) {
let new_value = prompt(
`Set variable ${variable} with current value '${story?.state?.variables?.[variable]}'`
);
if (new_value) {
story.state.variables[variable] = new_value;
}
}
function add_variable() {
let new_var = prompt("Name of the new variable:");
if (!story.state) {
story.state = {};
}
if (!story.state.variables) {
story.state.variables = {};
}
story.state.variables[new_var] = "";
load_variables_menu();
}
function add_menu_item(menu, text, on_click) {
menu.appendChild(document.createElement("li")).appendChild(
create_element_with_classes_and_attributes("a", {
class_list: ["dropdown-item"],
attributes: {
href: "#",
},
event_listener: {
click: on_click,
},
text: text,
})
);
}
function load_variables_menu() {
variables_menu.innerHTML = "";
if (story?.state?.variables) {
for (const variable of Object.keys(story?.state?.variables)) {
add_menu_item(variables_menu, variable, () => edit_variable(variable));
}
}
add_menu_item(variables_menu, "Add Variable", add_variable);
}
function add_action_select_to(col, current_value, apply_new_action) {
const select = col.appendChild(document.createElement("select"));
select.classList.add("form-select");
for (const supported_action of Object.keys(supported_actions)) {
select.appendChild(
create_element_with_classes_and_attributes("option", {
class_list: ["form-select"],
attributes: {
value: supported_action,
},
text: supported_action,
})
);
}
select.value = current_value;
select.onchange = () => {
apply_new_action(select.options[select.selectedIndex].value);
text_editor_load(active_element);
};
}
function add_parameter(
col,
action,
parameter_index,
action_type,
parameter_type_index,
row
) {
const parameter_type =
supported_actions[action_type].parameters[parameter_type_index];
if (parameter_type == "STRING") {
const input = col.appendChild(document.createElement("input"));
input.type = "text";
input.classList.add("form-control");
input.value = action.parameters[parameter_index];
input.addEventListener("change", () => {
action.parameters[parameter_index] = input.value;
console.debug("changed action", action);
});
}
if (parameter_type == "VARIABLE") {
const select = col.appendChild(document.createElement("select"));
select.classList.add("form-select");
if (!story?.state?.variables) {
return;
}
for (const variable of Object.keys(story.state.variables)) {
const option = select.appendChild(document.createElement("option"));
select.classList.add("form-select");
option.text = variable;
option.value = variable;
}
select.value = action.parameters[parameter_index];
select.onchange = () => {
action.parameters[parameter_index] =
select.options[select.selectedIndex].value;
};
}
if (parameter_type == "SECTION") {
const select = col.appendChild(document.createElement("select"));
select.classList.add("form-select");
for (const section_key of Object.keys(story.sections)) {
const option = select.appendChild(document.createElement("option"));
select.classList.add("form-select");
option.text = story.sections[section_key].id;
option.value = story.sections[section_key].id;
}
select.value = action.parameters[parameter_index];
select.onchange = () => {
action.parameters[parameter_index] =
select.options[select.selectedIndex].value;
};
}
if (parameter_type == "ACTION") {
col.remove();
add_action_and_parameter_inputs_to_row(
row,
action,
(new_action) => {
action.parameters[parameter_index] = new_action;
action.parameters.splice(parameter_index + 1);
for (const parameter_type of supported_actions[new_action].parameters) {
action.parameters.push("");
}
},
action.parameters[parameter_index],
parameter_index + 1
);
}
}
function add_action_and_parameter_inputs_to_row(
row,
action,
apply_new_action,
action_type,
start_in_current_action_params
) {
const first_col = row.appendChild(document.createElement("div"));
first_col.classList.add("col");
add_action_select_to(first_col, action_type, apply_new_action);
if (!supported_actions[action_type]) {
return;
}
for (let i = 0; i < supported_actions[action_type].parameters.length; i++) {
const para_col = row.appendChild(document.createElement("div"));
para_col.classList.add("col");
add_parameter(
para_col,
action,
start_in_current_action_params + i,
action_type,
i,
row
);
}
}
function load_actions(section) {
action_div.innerHTML = `
<div class="row">
<div class="col">
<button type="button" id="add_action_button" class="btn btn-primary">Add Action</button>
</div>
</div>
`;
document
.getElementById("add_action_button")
.addEventListener("click", add_action);
if (!section?.script) {
console.debug("No scripts for section", section.id);
return;
}
for (const action of section.script) {
const row = action_div.appendChild(document.createElement("div"));
row.classList.add("row");
add_action_and_parameter_inputs_to_row(
row,
action,
(new_action) => {
action.action = new_action;
action.parameters = [];
for (const parameter_type of supported_actions[action.action]
.parameters) {
action.parameters.push("");
}
},
action.action,
0
);
}
}
function text_editor_load(element) {
if (!element) {
element = active_element;
}
if (!element) {
console.error("Can not load empty element into text editor");
return;
}
const elements_section = find_elements_section(element);
if (!elements_section) {
console.error("No (parent) section for ", element);
return;
}
text_label.innerText = "Story for Section " + elements_section.id;
if (element.next && typeof element.next === "string") {
// active element is an edge
text_label.innerText =
"Choice going from Section " +
elements_section.id +
" to Section " +
element.next;
if (!action_div.classList.contains("d-none")) {
action_div.classList.add("d-none");
}
} else {
// active element is a node
cy.$("node").unselect();
cy.getElementById(elements_section.id).select();
action_div.classList.remove("d-none");
load_actions(element);
}
for (const text_container of text_containers) {
text_container.classList.remove("d-none");
}
text_area.value = "";
active_element = element;
if (element?.text_lines) {
text_area.value = element.text_lines.join("\n");
}
if (element?.text) {
text_area.value = element.text;
}
if (element?.media?.type === "image") {
img_container.style.display = "block";
const img = img_container?.getElementsByTagName("img")?.[0];
if (img) {
img.src = element?.media?.src;
}
add_media_button.innerHTML = "Remove Media";
} else {
img_container.style.display = "none";
add_media_button.innerHTML = "Add Media";
}
}
function text_editor_hide() {
for (const text_container of text_containers) {
if (!text_container.classList.contains("d-none")) {
text_container.classList.add("d-none");
}
}
}
function handle_text_change(event) {
if (!active_element) {
return;
}
if (active_element?.text_lines) {
active_element.text_lines = text_area.value.split("\n");
} else {
active_element.text = text_area.value;
}
}
function find_elements_section(element) {
if (story?.sections?.[element?.id]) {
return story?.sections?.[element?.id];
}
for (const id in story?.sections) {
const section = story.sections[id];
if (!section?.next) {
continue;
}
if (section.next.includes(element)) {
return section;
}
}
console.log("no section for element");
return null;
}
function handle_delete() {
if (!active_element) {
return;
}
const parent_section = find_elements_section(active_element);
var deleted_section_id = null;
if (story?.sections?.[active_element?.id]) {
delete story.sections[active_element.id];
console.debug("deleted section", active_element);
deleted_section_id = active_element.id;
}
for (const id in story?.sections) {
const section = story.sections[id];
if (!section?.next) {
continue;
}
if (section.next.includes(active_element)) {
section.next.splice(section.next.indexOf(active_element), 1);
console.debug("deleted link", active_element);
break;
}
for (var i = 0; i < section.next.length; i++) {
if (section.next[i].next === deleted_section_id) {
section.next.splice(i, 1);
i--;
}
}
}
text_editor_hide();
if (deleted_section_id) {
cy.remove("#" + deleted_section_id);
} else {
remove_edge(parent_section?.id, active_element?.next);
}
//redraw_adventure_graph();
}
function handle_add_node() {
if (!story) {
story = {};
}
let next_id = 1;
if (!story.sections) {
story.sections = {};
} else {
next_id = Math.max(...Object.keys(story.sections)) + 1;
}
story.sections[next_id] = {
id: next_id,
text_lines: [""],
};
add_node(story.sections[next_id]);
//redraw_adventure_graph();
text_editor_load(story.sections[next_id]);
return next_id;
}
function handle_add_edge() {
const elements_section = find_elements_section(active_element);
if (!elements_section) {
toast_alert("Please select the starting node, than add edge.");
return;
}
let targetId = section_select.options[section_select.selectedIndex].value;
if (!targetId) {
return;
}
if (targetId == "new_section") {
targetId = handle_add_node();
}
if (!elements_section?.next) {
elements_section.next = [];
}
elements_section.next.push({
text: "",
next: targetId,
});
add_edge(
elements_section,
elements_section.next[elements_section.next.length - 1]
);
}
async function download_media_in_section(
current_index,
section_ids,
final_callback
) {
console.debug("current_index", current_index);
if (current_index >= section_ids.length) {
final_callback();
return;
}
const section = story.sections[section_ids[current_index]];
if (section?.media?.src && !section.media.src.startsWith?.("data")) {
console.debug(`Embedding ${section.media.src}`);
return fetch(section.media.src)
.then((response) => {
if (response.status === 200) {
return response.blob();
} else {
console.log(
`Error ${response.status} fetching pic ${section.media.src}`
);
}
})
.then((imageBlob) => {
read_blob_and_handle(
imageBlob,
(content) => {
section.media.src = content;
download_media_in_section(
current_index + 1,
section_ids,
final_callback
);
},
true
);
});
} else {
return download_media_in_section(
current_index + 1,
section_ids,
final_callback
);
}
}
async function download_as_is() {
toast_ok("Generating JSON for download...");
var dataStr =
"data:text/json;charset=utf-8," +
encodeURIComponent(JSON.stringify(story, null, 2));
return trigger_data_dl(dataStr);
}
async function download_graph_in_one() {
toast_ok("Downloading all external picture references...");
const section_ids = Object.keys(story.sections);
download_media_in_section(0, section_ids, () => {
toast_ok("All pictures embedded. Generating json for download...");
download_as_is();
});
}
async function trigger_data_dl(dataStr, file_name) {
if (!file_name) {
file_name = get_file_safe_title() + ".json";
}
var p = create_element_with_classes_and_attributes("p");
var dlAnchorElem = p.appendChild(
create_element_with_classes_and_attributes("a", {
attributes: {
href: dataStr,
download: file_name,
},
class_list: ["link-warning"],
text: file_name,
})
);
toast_ok(
"Your file is ready",
p,
create_element_with_classes_and_attributes("p", {
innerHTML: "Your download should start shortly...",
})
);
dlAnchorElem.click();
}
async function download_graph_split() {
toast_ok("Extracting images into separate files");
const story_deep_copy = JSON.parse(JSON.stringify(story));
var zip = new JSZip();
var folder = zip.folder(get_file_safe_title());
for (const section_id in story.sections) {
const section = story.sections[section_id];
if (!section?.media?.src) {
continue;
}
const match = section.media.src.match(data_url_regexp);
if (match) {
const type = match[1];
const data = match[2];
console.log("Adding image for section", section, "to zip");
const file_name = section_id + "." + type;
story_deep_copy.sections[section.id].media.src =
"../" + get_file_safe_title() + "/" + file_name;
folder.file(file_name, data, { base64: true });
} else {
console.debug("section media src did not match data url regexp", section);
}
}
toast_ok("Saving Story");
folder.file(get_file_safe_title() + ".json", JSON.stringify(story_deep_copy));
add_stroy_adventure_files(zip).then(() => {
toast_ok("Generating Zip");
zip.generateAsync({ type: "base64" }).then(function (content) {
trigger_data_dl(
"data:application/zip;base64," + content,
get_file_safe_title() + ".zip"
);
});
});
}
async function add_to_zip(zip, folder, global_path = "../") {
const wait_for_all = [];
if (folder?.files) {
for (const file_name of folder.files) {
wait_for_all.push(
fetch(global_path + file_name)
.then((response) => response.blob())
.then((blob) => {
editor_folder.file(file_name, blob);
})
);
}
if (folder?.folders) {
for (const sub_folder of folder.folders) {
const zip_sub_folder = folder.folder(sub_folder);
wait_for_all.push(
add_to_zip(
zip_sub_folder,
folder.folders[sub_folder],
global_path + "/" + sub_folder
)
);
}
}
}
return Promise.all(wait_for_all);
}
async function add_stroy_adventure_files(zip) {
toast_ok("Adding tools to archive");
return add_to_zip(zip, tools_files).then(() => {
const story_name = get_file_safe_title();
zip.file(
"index.html",
`<!DOCTYPE html>
<html>
<head>
<title>Loading Adventure...</title>
</head>
<body>
<script>
window.location.href="./viewer/?load=../${story_name}/${story_name}.json";
</script>
</body>
</html>
`
);
});
}
function get_file_safe_title() {
if (!story?.meta?.title) {
return "story_adventure";
}
return story.meta.title.replaceAll(/[^a-z0-9-_]/gi, "_");
}
function load_file(content_handler, read_as_data) {
var input = document.createElement("input");
input.type = "file";
input.onchange = (e) => {
const file = e.target.files[0];
read_blob_and_handle(file, content_handler, read_as_data);
};
input.click();
}
function read_blob_and_handle(blob, content_handler, read_as_data) {
const reader = new FileReader();
if (read_as_data) {
reader.readAsDataURL(blob);
} else {
reader.readAsText(blob, "UTF-8");
}
reader.onload = (readerEvent) => {
const content = readerEvent.target.result;
//console.log(content);
content_handler(content);
};
}
function load_graph() {
load_file((content) => {
story = JSON.parse(content);
redraw_adventure_graph();
load_variables_menu();
});
}
function add_or_remove_media() {
if (active_element?.media?.type) {
active_element.media = {};
text_editor_load(active_element);
} else {
load_file((content) => {
if (!active_element || !story?.sections?.[active_element.id]) {
alert("Please select section to add media to.");
return;
}
active_element.media = {
type: "image",
src: content,
};
text_editor_load(active_element);
}, true);
}
}
function new_story() {
story = {
sections: {
1: {
text_lines: [""],
id: 1,
},
},
};
text_editor_load(story.sections["1"]);
redraw_adventure_graph();
load_variables_menu();
}
function paste_image(event) {
let clipboardData = event.clipboardData || window.clipboardData;
let item = clipboardData?.items?.[0];
if (item?.type?.indexOf("image") === 0) {
// Get the blob of the image
var blob = item.getAsFile();
// Create a file reader
var reader = new FileReader();
// Set the onload event handler
reader.onload = function (loadEvent) {
// Get the data URL of the image
let content = loadEvent.target.result;
active_element.media = {
type: "image",
src: content,
};
text_editor_load(active_element);
};
// Read the blob as a data URL
reader.readAsDataURL(blob);
}
}
function get_parent_section_of_active_element() {
var parent_section = null;
var sibbling_index = null;
for (const section_id of Object.keys(story.sections)) {
const section = story.sections[section_id];
if (!section?.next) {
continue;
}
for (var i = 0; i < section.next.length; i++) {
const choice = section.next[i];
if (choice.next === active_element.id) {
parent_section = section;
sibbling_index = i;
break;
}
}
if (parent_section) {
break;
}
}
return { parent_section, sibbling_index };
}
function handle_global_key_down(event) {
//console.debug("keydown", 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();
}
}
if (!active_element) {
return;
}
const active_section = find_elements_section(active_element);
if (!active_section) {
return;
}
if (event.key === "ArrowDown") {
event.stopPropagation();
if (!active_section?.next || active_section.next.length < 1) {
return;
}
active_element = story.sections[active_section.next[0].next];
text_editor_load(active_element);
return;
}
if (!story?.sections) {
return;
}
const { parent_section, sibbling_index } =
get_parent_section_of_active_element();
if (!parent_section) {
return;
}
if (event.key === "ArrowUp") {
event.stopPropagation();
text_editor_load(parent_section);
return;
}
if (event.key === "ArrowLeft") {
event.stopPropagation();
if (sibbling_index > 0) {
text_editor_load(
story.sections[parent_section.next[sibbling_index - 1].next]
);
return;
}
text_editor_load(
story.sections[parent_section.next[parent_section.next.length - 1].next]
);
return;
}
if (event.key === "ArrowRight") {
event.stopPropagation();
if (sibbling_index < parent_section.next.length - 1) {
text_editor_load(
story.sections[parent_section.next[sibbling_index + 1].next]
);
return;
}
text_editor_load(story.sections[parent_section.next[0].next]);
return;
}
}
function add_action() {
const active_section = find_elements_section(active_element);
if (!active_section) {
return;
}
if (!active_section?.script) {
active_section.script = [];
}
active_section.script.push({
action: "NONE",
parameters: [],
});
console.debug("added action to section", active_section.id);
text_editor_load(active_section);
}
text_area.addEventListener("change", handle_text_change);