-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypomatic.es.js
80 lines (69 loc) · 1.79 KB
/
typomatic.es.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
var Typomatic = function Typomatic(element, options) {
this.element = element;
this.options = Typomatic.customise(options);
this.element.classList.add('typomatic');
this.cursor = document.createElement('span');
this.cursor.className = 'cursor';
this.element.appendChild(this.cursor);
this.queue = [];
};
Typomatic.customise = function customise (options) {
var source = {
speed: 90
};
for (var i in options) {
source[i] = options[i];
}
return source;
};
Typomatic.prototype.add = function add (event) {
this.queue.push(event);
};
Typomatic.prototype.next = function next () {
var event = this.queue.shift();
if (typeof event === 'function') {
event(this.next.bind(this));
}
};
Typomatic.prototype.type = function type (text, speed) {
var this$1 = this;
var event = function (done) {
var typing = document.createElement('span');
typing.className = 'typing';
this$1.cursor.classList.remove('blink');
this$1.element.insertBefore(typing, this$1.cursor);
var tag, segment;
var i = 0;
var animate = function () {
if (segment === text) {
this$1.cursor.classList.add('blink');
return done();
}
segment = text.slice(0, ++i);
typing.innerHTML = segment;
var char = segment.slice(-1);
if (char === '<') { tag = true; }
if (char === '>') { tag = false; }
if (tag) { return animate(); }
window.setTimeout(
animate,
speed || this$1.options.speed
);
};
animate();
};
this.add(event);
return this;
};
Typomatic.prototype.exec = function exec (code) {
var event = function (done) {
code();
return done();
};
this.add(event);
return this;
};
Typomatic.prototype.done = function done () {
return this.next();
};
export default Typomatic;