-
Notifications
You must be signed in to change notification settings - Fork 5
/
click-to-proceed.js
156 lines (147 loc) · 4.63 KB
/
click-to-proceed.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
(() => {
"use strict";
$(document).on(":passageinit", () => {
CTP.Logs.forEach((_, id) => {
if (!CTP.Repository.get(id)?.persist) CTP.Logs.delete(id);
});
CTP.Repository.forEach(({ persist }, id) => {
if (!persist) CTP.Repository.delete(id);
});
});
var CTP = class _CTP {
constructor(id, persist = false) {
this.stack = [];
this.clears = [];
this.options = {};
if (!id?.trim()) throw new Error(`No ID specified!`);
this.id = id;
this.persist = persist;
_CTP.Repository.set(id, this);
}
static get Repository() {
if (!setup["@CTP/Repository"]) setup["@CTP/Repository"] = new Map();
return setup["@CTP/Repository"];
}
static get Logs() {
if (!variables()["@CTP/Logs"]) variables()["@CTP/Logs"] = new Map();
return variables()["@CTP/Logs"];
}
get log() {
if (!_CTP.Logs.get(this.id)) _CTP.Logs.set(this.id, { lastClear: -1, index: -1, seen: -1 });
return _CTP.Logs.get(this.id);
}
static getCTP(id) {
return _CTP.Repository.get(id);
}
add(content, options = {}) {
if (options.clear) this.clears.push(this.stack.length);
this.stack.push({
options,
content,
index: this.stack.length,
element: $()
});
return this;
}
print(index) {
const { content, options: iOpts } = this.stack[index];
const options = {
...this.options,
...iOpts
};
const element = $(document.createElement(options.element || "span")).addClass("--macro-ctp-hidden").attr({
"data-macro-ctp-id": this.id,
"data-macro-ctp-index": index
}).on("update-internal.macro-ctp", (event, firstTime) => {
if ($(event.target).is(element)) {
if (index === this.log.index) {
if (firstTime) {
if (typeof content === "string") element.wiki(content);
else element.append(content);
element.addClass(options.transition ? "--macro-ctp-t8n" : "");
}
element.removeClass("--macro-ctp-hidden");
} else {
if (index < this.log.seen) element.removeClass("--macro-ctp-t8n");
element.toggleClass("--macro-ctp-hidden", index > this.log.index || index < this.log.lastClear);
}
}
});
this.stack[index].element = element;
return element;
}
output() {
const wrapper = document.createDocumentFragment();
for (let i = 0; i < this.stack.length; i++) {
this.print(i).appendTo(wrapper);
}
return wrapper;
}
advance() {
if (this.log.index < this.stack.length - 1) {
this.log.index++;
const firstTime = this.log.index > this.log.seen;
this.log.seen = Math.max(this.log.seen, this.log.index);
this.log.lastClear = this.clears.slice().reverse().find((el) => el <= this.log.index) ?? -1;
$(document).trigger("update.macro-ctp", ["advance", this.id, this.log.index]);
this.stack.forEach(({ element }) => element.trigger("update-internal.macro-ctp", [firstTime, "advance", this.id, this.log.index]));
}
return this;
}
back() {
if (this.log.index > 0) {
this.log.index--;
this.log.lastClear = this.clears.slice().reverse().find((el) => el <= this.log.index) ?? -1;
$(document).trigger("update.macro-ctp", ["back", this.id, this.log.index]);
this.stack.forEach(({ element }) => element.trigger("update-internal.macro-ctp", [false, "back", this.id, this.log.index]));
}
return this;
}
};
window.CTP = CTP;
Macro.add("ctp", {
tags: ["ctpNext"],
handler() {
const id = this.args[0];
const persist = this.args.slice(1).includes("persist");
const ctp = new CTP(id, persist);
const _passage = passage();
this.payload.forEach(({ args, name, contents }) => {
const options = {};
if (args.includes("clear")) options.clear = true;
if (args.includesAny("t8n", "transition")) options.transition = true;
const elementArg = args.find((el) => el.startsWith("element:")) ?? "";
if (elementArg) options.element = elementArg.replace("element:", "");
if (name === "ctp") ctp.options = { ...options };
ctp.add(contents, options);
});
$(this.output).append(ctp.output());
if (_passage === passage()) {
const i = Math.max(ctp.log.index, 0);
ctp.log.index = -1;
ctp.log.seen = -1;
while (ctp.log.index < i) ctp.advance();
}
}
});
Macro.add("ctpAdvance", {
handler() {
const id = this.args[0];
if (id) {
const ctp = CTP.getCTP(id);
if (ctp) ctp.advance();
else throw new Error(`No CTP with ID '${id}' found!`);
} else throw new Error(`No ID specified!`);
}
});
Macro.add("ctpBack", {
handler() {
const id = this.args[0];
if (id) {
const ctp = CTP.getCTP(id);
if (ctp) ctp.back();
else throw new Error(`No CTP with ID '${id}' found!`);
} else throw new Error(`No ID specified!`);
}
});
})();