-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteIt.js
637 lines (579 loc) · 23.2 KB
/
WriteIt.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
/**
* @license MIT
* @author Khushit Shah
*/
class WriteIt {
/**
* Creates new WriteItJS class handles starting of animation of nodes and SEO.
*/
constructor() {
this.WRITEIT_ANIMATE = "writeit-animate";
this.WRITEIT_AUTO_START = "writeit-autostart";
this.WRITEIT_CHAR = "writeit-char";
this.WRITEIT_REPLACE_NEXT = "writeit-replace-next";
this.WRITEIT_SPEED = "writeit-speed";
this.WRITEIT_SPEED_FIXED = "writeit-speed-fixed";
this.WRITEIT_HAS_PREV = "writeit-has-prev";
this.WRITEIT_START_DELAY = "writeit-start-delay";
this.WRITEIT_LOOP = "writeit-loop";
this.WRITEIT_NEXT = "writeit-next";
this.WRITEIT_HIDDEN = "writeit-hidden";
this.WRITEIT_NO_BLINKING_WRITEIT = "writeit-no-blink";
this.WRITEIT_LOOP_REVERSE = "writeit-loop-reverse";
this.WRITEIT_WAIT_IN_REVERSE = "writeit-wait-in-reverse";
this.WRITEIT_WRITE_ALL_IN_REVERSE = "writeit-write-all-in-reverse";
this.count = 0;
/**
* @type HTMLElement[]
*/
this.nodes = [];
this.init();
/** }
* @type WriteItNode[]
*/
this.writeitNodes = [];
}
/**
* Starts animating whole page.
*/
startAnimation() {
this.nodes.forEach(element => {
let c = new WriteItNode(element, false, this.count++);
this.writeitNodes.push(c);
c.startAnimation();
});
}
/**
* Pauses the animation of node, if it is currently animating.
* @param {string} node
*/
pauseAnimation(node) {
node = this.findNode(node);
if (node) node.pauseAnimation();
}
/**
* Return a WriteItNode that maches the selector and already parsed by writeit.
* @param {string} node
*/
findNode(node) {
node = document.querySelector(node);
let writeItNode = null;
this.writeitNodes.forEach(_node => {
if (_node.node == node) {
writeItNode = _node;
return false;
}
return true;
});
return writeItNode;
}
/**
* Resumes the animation of code if it's currently paused.
* @param {string} node
*/
resumeAnimation(node) {
let toResumeNode = this.findNode(node);
if (toResumeNode) toResumeNode.resumeAnimation();
}
/**
* Starts a animation of a specific node.
* If the node is already animating stops it and starts new animation.
* @param node String representing ID of HTML element to Animate.
*/
startAnimationOfNode(node, fromAnotherNode) {
let writeitNode = this.findNode(node);
if (writeitNode) {
writeitNode.stopAnimation();
writeitNode.init();
} else {
writeitNode = new WriteItNode(node, false, this.count++);
this.writeitNodes.push(writeitNode);
}
if (fromAnotherNode)
writeitNode.fromAnotherNode = true;
writeitNode.startAnimation();
}
init() {
this.nodes = Array.from(
document.querySelectorAll("*[" + this.WRITEIT_ANIMATE + "]")
);
this.seo();
}
/**
* Adds a Cloned Hidden Node to html for SEO optimization, as
* The original node text is going to change.
*/
seo() {
this.nodes.forEach(element => {
const newELement = element.cloneNode(true);
newELement.id = "seo_" + element.id;
newELement.style.display = "none";
element.parentNode.appendChild(newELement);
});
}
}
class WriteItNode {
/**
* Creates A Node to Animate.
* @param {HTMLElement} node
* @param {boolean} _fromAnotherNode
*/
constructor(node, _fromAnotherNode, _id) {
/**
* @type RegExp
* Looks for a comma after any character.
*/
this.running = false;
this.commaSepReg = /,(?!\\)/;
if (node == undefined || node == null) {
throw new Error("Node must be a valid HTML tag");
}
this.node = node;
this.fromAnotherNode = _fromAnotherNode;
this.waitIndex = [];
this.writeAllTextAtOnceIndex = [];
this.waitIndex["default"] = [];
this.writeAllTextAtOnceIndex["default"] = [];
this.wait = false;
this.id = _id;
this.timeout = 0;
this.interval = 0;
this.innerHTML = this.node.innerHTML;
this.texts = [];
this.textsIndex = -1;
this.init();
}
init() {
if (this.node.hasAttribute(WriteItJS.WRITEIT_HIDDEN)) {
this.pdisp = this.node.style.display;
this.node.style.display = "none";
}
if (this.node.hasAttribute(WriteItJS.WRITEIT_CHAR)) {
this.writeitChar = this.node.getAttribute(WriteItJS.WRITEIT_CHAR);
} else {
this.writeitChar = "|";
}
if (this.node.hasAttribute(WriteItJS.WRITEIT_REPLACE_NEXT)) {
this.originalTexts = this.node
.getAttribute(WriteItJS.WRITEIT_REPLACE_NEXT)
// remove redudant spaces.
.replace(/[\s]+/g, " ")
.split("")
.reverse()
.join("")
.split(this.commaSepReg)
.map(s =>
s
.split("")
.reverse()
.join("")
.replace("\\,", ",")
)
.reverse();
// remove redudant spaces
this.text = this.node.innerHTML.replace(/[\s]+/g, " ");
this.textsIndex = -1;
} else {
// remove redudant spaces
this.text = this.node.innerHTML.replace(/[\s]+/g, " ");
}
this.text = this.text.trim();
// parse text in order.
let temp = 0;
let i;
while (this.text.indexOf('${', temp) != -1 || this.text.indexOf('$`', temp) != -1) {
let waitTempIndex = this.text.indexOf('${', temp);
let writeTempIndex = this.text.indexOf('$`', temp);
if (waitTempIndex > -1 && (waitTempIndex < writeTempIndex || writeTempIndex == -1)) {
// set wait.
// TODO: Check that matching closing bracket is present!
i = waitTempIndex;
let secToWait = this.text.substring(i + 2, this.text.indexOf('}', i));
this.waitIndex["default"][i] = Number(secToWait);
// this might return -1
this.text = this.text.replace(this.text.substring(i, this.text.indexOf("}", i) + 1), "");
temp += 1;
} else if (writeTempIndex > -1 && (writeTempIndex < waitTempIndex || waitTempIndex == -1)) {
// set write.
i = writeTempIndex;
// Check that matching quote(`) is present!
let endIndex = this.text.indexOf("`", i + 2);
if (endIndex != -1) {
let length = endIndex - i - 2;
this.writeAllTextAtOnceIndex["default"][i] = endIndex - 1;
if (this.node.hasAttribute(WriteItJS.WRITEIT_WRITE_ALL_IN_REVERSE)) {
this.writeAllTextAtOnceIndex["default"][i + length] = i;
}
this.text = this.text.replace(this.text.substring(i, endIndex + 1), this.text.substring(i + 2, endIndex));
temp += endIndex;
}
}
}
if (this.node.hasAttribute(WriteItJS.WRITEIT_REPLACE_NEXT)) {
// Loop for all texts in WRITEIT_REPLACE_NEXT and also parse them.
for (let iterator = 0; iterator < this.originalTexts.length; iterator++) {
let tempText = this.originalTexts[iterator];
this.waitIndex[iterator] = [];
this.writeAllTextAtOnceIndex[iterator] = [];
let temp = 0;
let i;
while (tempText.indexOf('${', temp) != -1 || tempText.indexOf('$`', temp) != -1) {
let waitTempIndex = tempText.indexOf('${', temp);
let writeTempIndex = tempText.indexOf('$`', temp);
if (waitTempIndex > -1 && (waitTempIndex < writeTempIndex || writeTempIndex == -1)) {
// set wait.
i = waitTempIndex;
let secToWait = tempText.substring(i + 2, tempText.indexOf('}', i));
this.waitIndex[iterator][i] = secToWait;
tempText = tempText.replace(tempText.substring(i, tempText.indexOf("}", i) + 1), " ");
temp += 2;
} else if (writeTempIndex > -1 && (writeTempIndex < waitTempIndex || waitTempIndex == -1)) {
// set write.
i = writeTempIndex;
let endIndex = tempText.indexOf("`", i + 2);
this.writeAllTextAtOnceIndex[iterator][i + 1] = endIndex - 1;
if (this.node.hasAttribute(WriteItJS.WRITEIT_WRITE_ALL_IN_REVERSE)) {
this.writeAllTextAtOnceIndex[iterator][endIndex] = i + 1;
}
tempText = tempText.replace(tempText.substring(i, endIndex + 1), tempText.substring(i + 2, endIndex));
temp += endIndex + 1;
}
}
this.texts[iterator] = tempText + " "; // add space as wait time can be at last.
}
}
this.speed = 0;
this.index = 0;
this.reverse = false;
this.setSpeed();
}
/**
* Sets a speed for the animation.
* If writeit-speed-* is present than it uses that,
* otherwise uses default speed of "6.5±1" letters/second.
*/
setSpeed() {
this.speed = 1000 / (Math.random() * (7.5 - 5.5) + 5.5); // Time in millis to write one letter.
if (this.node.hasAttribute(WriteItJS.WRITEIT_SPEED_FIXED)) {
this.speed =
1000 / parseFloat(this.node.getAttribute(WriteItJS.WRITEIT_SPEED_FIXED));
} else if (this.node.hasAttribute(WriteItJS.WRITEIT_SPEED)) {
this.speed =
1000 /
(Math.random() * 3 +
(parseFloat(this.node.getAttribute(WriteItJS.WRITEIT_SPEED)) - 1));
}
}
/**
* Starts Animation according to factors like delay, before element etc.
*/
startAnimation() {
let delay = 0;
if (
this.fromAnotherNode ||
!this.node.hasAttribute(WriteItJS.WRITEIT_HAS_PREV)
) {
this.node.innerHTML = " ";
if (this.node.hasAttribute(WriteItJS.WRITEIT_START_DELAY)) {
delay =
1000 *
parseFloat(this.node.getAttribute(WriteItJS.WRITEIT_START_DELAY));
}
} else {
// Element has "writeit-has-prev" attribute So, Don't start animating.
return;
}
// check if the node is hidden?
if (this.node.hasAttribute(WriteItJS.WRITEIT_HIDDEN)) {
this.node.style.display = this.pdisp;
}
if (this.node.hasAttribute(WriteItJS.WRITEIT_NEXT)) {
let nodes = this.node.getAttribute(WriteItJS.WRITEIT_NEXT).split(",");
nodes.forEach(node => {
let curNode = WriteItJS.findNode(node);
if (curNode && curNode.running) {
curNode.stopAnimation();
curNode.init();
}
});
}
this.blinkCursor(delay, () => {
this.animate();
this.running = true;
})
}
stopAnimation() {
clearTimeout(this.timeout);
clearInterval(this.interval);
this.timeout = -1; // Stop executing animation() now;
this.node.innerHTML = this.innerHTML;
this.running = false;
}
/**
* Pauses animation.
*/
pauseAnimation() {
clearTimeout(this.timeout);
this.timeout = -1;
}
/**
* Resumes animation.
*/
resumeAnimation() {
this.setSpeed();
this.timeout = this.setTimeout(this.animate, this.speed);
}
/**
* Adds/Removes a letter from "html";
*/
animate() {
// return if animation is paused or stopped.
if (this.timeout == -1) {
return;
}
// Wait if we need to wait at these position.
if (this.waitIndex[this.textsIndex < 0 ? "default" : this.textsIndex][this.index] != undefined && !this.wait) {
let secsToWait = this.waitIndex[this.textsIndex < 0 ? "default" : this.textsIndex][this.index];
this.node.innerHTML = this.text.substring(0, this.index) + " ";
if (this.reverse && this.node.hasAttribute(WriteItJS.WRITEIT_WAIT_IN_REVERSE)) {
secsToWait = secsToWait * 1000;
this.index--;
} else if (!this.reverse) {
secsToWait = secsToWait * 1000;
this.index++;
} else {
secsToWait = 0;
this.index--;
}
if (this.index <= 0 || this.index >= this.text.length) {
this.handleIterationEnd();
return;
} else {
}
// this.timeout = this.setTimeout(() => {
// this.wait = false;
// this.animate();
// }, secsToWait);
this.blinkCursor(secsToWait, ()=> {
this.animate();
})
return;
}
// return if already waiting!
if (this.wait) return;
// Write all text at once.
if (this.writeAllTextAtOnceIndex[this.textsIndex < 0 ? "default" : this.textsIndex][this.index] != undefined) {
let destinationIndex = this.writeAllTextAtOnceIndex[this.textsIndex < 0 ? "default" : this.textsIndex][this.index];
if (this.reverse && destinationIndex < this.index) {
this.node.innerHTML = this.text.substring(0, destinationIndex);
this.index = destinationIndex - 1;
} else if (!this.reverse && destinationIndex > this.index) {
this.node.innerHTML = this.text.substring(0, destinationIndex);
this.index = destinationIndex + 1;
}
if (this.index <= 0 || this.index >= this.text.length) {
this.handleIterationEnd();
return;
} else {
this.node.innerHTML += this.writeitChar;
}
this.nextIteration();
return;
}
// Browser may have added ending tag so ignore it.
let str = this.text.substring(0, this.index);
// Add HTML without writeit-char.
this.node.innerHTML = (str.trim().length != 0) ? str : " ";
// If reverse remove a character from last.
if (this.reverse) {
// We are at start of the string.
if (str.length <= 0) {
this.handleIterationEnd();
return;
}
let chrToRemove = str.substr(str.length - 1, 1);
if (chrToRemove == ">") {
// Go back until next < is found.
chrToRemove = this.text.substring(
str.length,
str.lastIndexOf("<", str.length)
);
str = str.substr(0, str.length - chrToRemove.length);
this.index -= chrToRemove.length;
} else if (chrToRemove == ";") {
// Go back until next & is found.
chrToRemove = this.text.substring(
str.length,
str.lastIndexOf("&", str.length)
);
str = str.substr(0, str.length - chrToRemove.length);
this.index -= chrToRemove.length;
} else {
str = str.substring(0, str.length - 1);
this.index--;
}
} else {
if (str.length >= this.text.length) {
// We are at End in the string.
this.handleIterationEnd();
return;
}
// Add a letter to the last.
let chrToAdd = this.text.substr(str.length, 1);
if (chrToAdd == "<") {
// Take everything until ">".
chrToAdd = this.text.substring(
str.length,
this.text.indexOf(">", str.length) + 1
);
} else if (chrToAdd == "&") {
// handle &...;
chrToAdd = this.text.substring(
str.length,
this.text.indexOf(";", str.length) + 1
);
}
this.index += chrToAdd.length;
str = str + chrToAdd;
}
// Add writeit at the end.
str += this.writeitChar;
// Update the InnerHTML
this.node.innerHTML = str;
this.nextIteration();
}
/**
* Gets Executed after animation is over,
* It decides whether to rerun or reverse or call animation of another element.
*/
handleIterationEnd() {
if (this.reverse) {
// Check if it has WRITEIT_REPLACE_NEXT, then replace text with next text.
if (this.node.hasAttribute(WriteItJS.WRITEIT_REPLACE_NEXT)) {
// If we are already at the last position.
if (this.textsIndex + 1 >= this.texts.length) {
// Start from first if it has writeit-loop-reverse.
if (this.node.hasAttribute(WriteItJS.WRITEIT_LOOP_REVERSE)) {
// Start animation from the start!
this.textsIndex = 0;
this.text = this.texts[this.textsIndex];
this.trigerNextAnimation(false, 0, true);
}
} else {
// Just increment the textsIndex.
this.text = this.texts[++this.textsIndex];
this.trigerNextAnimation(false, 0, true);
}
} else if (this.node.hasAttribute(WriteItJS.WRITEIT_LOOP_REVERSE)) {
this.trigerNextAnimation(false, 0, true);
}
} else {
if (this.node.hasAttribute(WriteItJS.WRITEIT_REPLACE_NEXT)) {
// Current Text has been writtern, Start reverse.
// if this is last text and node don't conatains writeit-loop then stop.
if (
this.textsIndex >= this.texts.length - 1 &&
!(this.node.hasAttribute(WriteItJS.WRITEIT_LOOP) || this.node.hasAttribute(WriteItJS.WRITEIT_LOOP_REVERSE))
) {
this.animationEnd();
return;
} else if (this.textsIndex >= this.texts.length - 1 && this.node.hasAttribute(WriteItJS.WRITEIT_LOOP)) {
// Start animation from the start!
this.textsIndex = 0;
this.text = this.texts[this.textsIndex];
this.trigerNextAnimation(false, 0, true);
} else {
this.trigerNextAnimation(true);
}
} else if (this.node.hasAttribute(WriteItJS.WRITEIT_LOOP)) {
this.trigerNextAnimation(false, 0, true);
} else if (this.node.hasAttribute(WriteItJS.WRITEIT_LOOP_REVERSE)) {
this.node.innerHTML += this.writeitChar;
this.trigerNextAnimation(true);
} else {
// Add Simple Blinking writeit animation.
this.animationEnd();
}
}
}
/**
* Handles end of the animation of element, starts last blinking writeit or trigers animation of another elements.
*/
animationEnd() {
if (this.node.hasAttribute(WriteItJS.WRITEIT_NEXT)) {
// Go through all of them and start them.
let nodes = this.node.getAttribute(WriteItJS.WRITEIT_NEXT).split(",");
nodes.forEach(node => {
WriteItJS.startAnimation(node, true);
});
} else {
if (!this.node.hasAttribute(WriteItJS.WRITEIT_NO_BLINKING_WRITEIT))
this.lastWriteItBlinkAnimation();
}
}
/**
* @param {boolean} reverse wether animation is reverse or forward.
* @param {integer} _index (optional) decides wether to set index.
* @param {boolean} _reInitNode (optional) decides wether to set node.innerHTML with writeitChar.
*/
trigerNextAnimation(reverse, _index, _reInitNode) {
this.reverse = reverse;
this.index = _index != undefined ? _index : this.index;
this.node.innerHTML = _reInitNode ? this.writeitChar : this.node.innerHTML;
this.timeout = this.setTimeout(this.animate, this.speed);
}
/**
* Keeps blinking writeit for infinity.
*/
lastWriteItBlinkAnimation() {
this.interval = this.setInterval(() => {
if (this.node.innerHTML.replace(/'/g, "\"") != (this.text + this.writeitChar).replace(/'/g, "\"")) {
this.node.innerHTML = this.text + this.writeitChar;
} else {
this.node.innerHTML = this.text + ("\u00A0".repeat(this.writeitChar.length));
}
}, 500);
}
blinkCursor(time, callback) {
this.curText = this.node.innerHTML;
this.blinkInterval = this.setInterval(() => {
if (this.node.innerHTML.replace(/'/g, "\"") != (this.curText + this.writeitChar).replace(/'/g, "\"")) {
this.node.innerHTML = this.curText + this.writeitChar;
} else {
this.node.innerHTML = this.curText + ("\u00A0".repeat(this.writeitChar.length));
}
}, 500);
this.setTimeout(() => {
clearInterval(this.blinkInterval);
callback();
}, time);
}
/**
* Resets the speed and calls the animate function as per delay
*/
nextIteration() {
this.setSpeed();
this.timeout = this.setTimeout(this.animate, this.speed);
}
setInterval(method, delay) {
return setInterval(method.bind(this), delay);
}
setTimeout(method, delay) {
return setTimeout(method.bind(this), delay);
}
}
const WriteItJS = new WriteIt();
// Automatically start Animation if writeit-auto-start attribute is present,
// this is not related with the animation of node
// but, it starts parsing each node which has "writeit-animate"
if (document.querySelector("*[" + WriteItJS.WRITEIT_AUTO_START + "]")) {
WriteItJS.startAnimation();
}
// add other WriteItJS function as an array object so they don't get ignored by Closure ADVANCE settings.
window["WriteItJS"] = WriteItJS;
window["WriteItJS"]["startAnimation"] = WriteItJS.startAnimation;
window["WriteItJS"]["pauseAnimation"] = WriteItJS.pauseAnimation;
window["WriteItJS"]["resumeAnimation"] = WriteItJS.resumeAnimation;
window["WriteItJS"]["startAnimationOfNode"] = WriteItJS.startAnimationOfNode;
// other functions are not necessary for users[programmers].
console.log("WriteIt.js v2.0 loaded!, visit https://github.com/khushit-shah/WriteIt.js");