-
Notifications
You must be signed in to change notification settings - Fork 0
/
equaColor.js
316 lines (282 loc) · 8.21 KB
/
equaColor.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
function fillFrameWithFormule(systeme, t) {
var frame = generateFrame();
systeme.setVariable('t', t);
for (let z = 0; z < 8; z++) {
systeme.setVariable('z', z);
for (let y = 0; y < 8; y++) {
systeme.setVariable('y', y);
for (let x = 0; x < 8; x++) {
systeme.setVariable('x', x);
var color = systeme.getValue();
if (isNaN(color) || typeof color !== 'number') {
console.log('Equation non valide', { x, y, z, t, color, systeme });
throw new Error(`L'équation n'est pas valide : ${color}`);
}
frame[x][y][z] = color;
}
}
}
return frame;
}
function generateFramesWithFormule() {
const tMax = 1;
/** @type {number[][][][]} */
var frames = new Array(tMax);
const formule = this.getFormule();
var startEqua = Date.now();
const systeme = JigMath.getSystem(formule);
var dureeEqua = Date.now() - startEqua;
console.log(`Système généré en ${dureeEqua} ms`, systeme);
const start = Date.now();
var deltaLog = 0;
for (let t = 0; t < tMax; t++) {
frames[t] = fillFrameWithFormule(systeme, t);
if (Date.now() - deltaLog > 500) {
console.info(`Génération de la frame ${t}/${tMax}`);
deltaLog = Date.now();
}
}
console.info(`Frames générées en ${Date.now() - start} ms`);
return frames;
}
class EquaColor {
container;
input;
backdrop;
/**
* Create an equation area from a container
* Events are sent by the container
* @param {HTMLElement} container
*/
constructor(container) {
this.container = container;
this.input = document.createElement('textarea');
this.input.setAttribute('spellcheck', false);
this.input.classList.add('formule_input');
this.backdrop = document.createElement('div');
this.backdrop.classList.add('formule_backdrop');
container.appendChild(this.input);
container.appendChild(this.backdrop);
container.classList.add('formule_container');
// Colored TextArea https://stackoverflow.com/a/56087599/12908345
this.input.addEventListener("scroll", () => this.backdrop.scrollTop = this.input.scrollTop);
this.input.addEventListener("input", () => this.onFormuleChanged());
this.input.addEventListener("mousedown", () => setTimeout(() => this.onFormuleCursorMoved(), 1));
this.input.addEventListener("keydown", () => setTimeout(() => this.onFormuleCursorMoved(), 1));
this.input.addEventListener('focusout', () => this.highlightBlob(null));
}
setFormule(formule) {
this.input.value = formule;
this.onFormuleChanged();
}
formulePrefix = '';
getFormule() {
/**
* @type {string}
*/
var formule = this.input.value;
this.formulePrefix = formule.match(/^f\([\w,]+\)\s*=\s*/)?.[0] || '';
if (this.formulePrefix)
formule = formule.replace(this.formulePrefix, '');
return formule;
}
getSystem() {
const formule = this.getFormule();
var debutSysteme = Date.now();
const systeme = JigMath.getSystem(formule);
var dureeSysteme = Date.now() - debutSysteme;
console.log(`Système généré en ${dureeSysteme} ms`, systeme);
return systeme;
}
onFormuleChanged() {
const formule = this.getFormule();
var system;
var error;
this.backdrop.innerHTML = '';
try {
system = new JigMath.System(formule);
this.formuleReady = true;
}
catch (err) {
this.formuleReady = false;
if (err.constructor === JigMath.EquaError) {
system = err.system;
error = err;
}
else {
console.warn('Formule error', err, err.system);
this.backdrop.appendChild(this.createSpanForItem(err.message || err));
this.sendEvent("jigmath_error", err);
return;
}
}
if (this.formulePrefix) {
const spanPrefix = this.createSpanForItem(this.formulePrefix);
spanPrefix.removeAttribute('JigMath');
this.backdrop.appendChild(spanPrefix);
}
this.backdrop.appendChild(this.createSpanForItem(system));
if (formule.endsWith('\n')) this.backdrop.appendChild(this.createSpanForItem(' '));
if (error?.item) {
var errorItem;
if (error.data?.blobEnd) {
const blobEndIndex = error.data.blobEnd.iS;
const childs = Array.from(error.item?.span?.children || []);
errorItem = childs[blobEndIndex];
}
if (!errorItem) {
errorItem = error.item?.span;
}
errorItem?.classList.add('equa_error');
}
this.onFormuleCursorMoved();
system.variables.forEach(v => v.value = 0);
const valueAt0 = system.getValue();
if (typeof valueAt0 !== 'number') this.formuleReady = false;
if (this.formuleReady) {
this.sendEvent("jigmath_ready", system);
}
}
onFormuleCursorMoved() {
const cursorIndexStart = this.input.selectionStart;
const cursorIndexEnd = this.input.selectionEnd;
if (cursorIndexStart !== cursorIndexEnd) {
this.highlightBlob(null);
return;
}
const cursorIndex = cursorIndexStart;
const selectedItemBefore = this.getEquaItemAtOriginalIndex(this.backdrop, cursorIndex - 1);
const selectedItemAfter = this.getEquaItemAtOriginalIndex(this.backdrop, cursorIndex);
var blobHightlight;
if (selectedItemBefore) {
blobHightlight = this.getFirstBlobParent(selectedItemBefore);
}
if (selectedItemAfter && (!blobHightlight || selectedItemAfter.getAttribute('JigMath') === 'EquaBlobLimit')) {
blobHightlight = this.getFirstBlobParent(selectedItemAfter);
}
this.highlightBlob(blobHightlight);
}
/**
* @param {Item} item
*/
createSpanForItem(item) {
const span = document.createElement('span');
if (typeof item === 'string') {
this.fillSpanWithText(span, item);
if (item.match(/^\s*$/)) {
span.setAttribute('JigMath', 'spaces');
}
else {
span.setAttribute('JigMath', 'string');
}
if (item === ')' || item === '(') {
span.classList.add('equa_error');
}
}
else {
item.span = span;
span.item = item;
span.setAttribute('JigMath', item.constructor.name);
const subItems = item.getSubItems();
if (subItems) {
for (const subItem of subItems) {
span.appendChild(this.createSpanForItem(subItem));
}
}
else {
this.fillSpanWithText(span, item.getOriginal());
}
this.applyEquaDecoration(item, span);
}
return span;
}
fillSpanWithText(span, text) {
if (text.includes('\n')) {
var spanCount = 0;
for (const s of text.split('\n')) {
if (spanCount) span.appendChild(document.createElement('br'));
const subSpan = document.createElement('span');
subSpan.innerText = s;
span.appendChild(subSpan);
spanCount++;
}
}
else {
span.innerText = text;
}
}
/**
* Colors the equation
* @param {Item} item
* @param {HTMLSpanElement} span
*/
applyEquaDecoration(item, span) {
if (item.constructor === JigMath.EquaVariable) {
if (!['x', 'y', 'z', 't'].includes(item.name)) {
span.classList.add('equa_warning');
this.formuleReady = false;
}
}
else if (item.constructor === JigMath.EquaFunction) {
if (!item.function) {
span.classList.add('equa_warning');
this.formuleReady = false;
}
}
}
/**
* @param {HTMLSpanElement} span
* @param {number} index
* @param {number} originalOffset
* @return {HTMLElement}
*/
getEquaItemAtOriginalIndex(span, index, originalOffset = 0) {
/**
* @type {HTMLSpanElement[]}
*/
const subItems = span.children && Array.from(span.children);
if (!subItems) return span;
for (const subItem of subItems) {
if (subItem.constructor !== HTMLSpanElement)
continue;
const original = subItem.innerText;
const originalLength = original.length;
if (originalOffset + originalLength > index)
return this.getEquaItemAtOriginalIndex(subItem, index, originalOffset);
originalOffset += originalLength;
}
return span;
}
/** @param {HTMLSpanElement} span */
getFirstBlobParent(span) {
while (span && span.getAttribute('JigMath') !== 'EquaBlob') {
span = span.parentElement;
}
return span;
}
/** @type {HTMLSpanElement} */
previousBlobHighlighed;
/** @param {HTMLSpanElement} blob */
highlightBlob(blob) {
if (blob === this.previousBlobHighlighed) return;
if (this.previousBlobHighlighed) {
this.previousBlobHighlighed.removeAttribute('selected');
}
if (blob) {
blob.setAttribute('selected', '');
this.previousBlobHighlighed = blob;
}
else {
this.previousBlobHighlighed = undefined;
}
}
sendEvent(name, detail) {
const myEvent = new CustomEvent(name, {
detail,
bubbles: true,
cancelable: false,
composed: false,
});
this.container.dispatchEvent(myEvent);
}
}