-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
type-sim.js
73 lines (60 loc) · 2.15 KB
/
type-sim.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
(function () {
'use strict';
// typesim macro, by chapel; for sugarcube 2
// version 2.0.0
function typeSim (content, $target, callback) {
if (!content || typeof content !== 'string') {
return;
}
if ($target && !($target instanceof $)) {
$target = $($target);
}
var i = 0;
var arrayify = content.split('');
var message = [];
var $textarea = $(document.createElement('textarea'))
.addClass('type-sim')
.on('input.type-sim', function () {
var $self = $(this);
i = message.push(arrayify[i]);
$self.val(message.join(''));
if (content.length === message.length) {
$self
.off('input.type-sim')
.ariaDisabled(true);
if (callback && typeof callback === 'function') {
callback(message.join(''));
}
$(document).trigger({
type : ':type-sim-end',
message : message.join('')
});
}
});
if ($target && $target[0]) {
$target.append($textarea);
}
return $textarea;
}
Macro.add('typesim', {
tags : null,
handler : function () {
if (!this.args.length || !this.args[0] || typeof this.args[0] !== 'string') {
return this.error('no text to type out was provided');
}
var $wrapper = $(document.createElement('span')).addClass('macro' + this.name);
var $callbackOutput = $(document.createElement('div'));
var wiki;
if (this.payload[0].contents && this.payload[0].contents.trim()) {
wiki = this.payload[0].contents;
}
function callback () {
$callbackOutput.wiki(wiki);
}
typeSim(this.args[0], $wrapper, callback);
$wrapper
.append($callbackOutput)
.appendTo($(this.output));
}
});
})();