-
Notifications
You must be signed in to change notification settings - Fork 26
/
autosize.js
86 lines (74 loc) · 1.71 KB
/
autosize.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
'use strict';
var crossvent = require('crossvent');
var dom = require('./dom');
var text = require('./text');
var props = [
'fontFamily',
'fontSize',
'fontWeight',
'fontStyle',
'letterSpacing',
'textTransform',
'wordSpacing',
'textIndent',
'webkitBoxSizing',
'mozBoxSizing',
'boxSizing',
'padding',
'border'
];
var offset = 20;
module.exports = function factory (el) {
var mirror = dom('span');
document.body.appendChild(mirror);
remap();
bind();
return {
remap: remap,
refresh: refresh,
destroy: destroy
};
function remap () {
var c = computed();
var value;
var i;
for (i = 0; i < props.length; i++) {
value = c[props[i]];
if (value !== void 0 && value !== null) { // otherwise IE blows up
mirror.style[props[i]] = value;
}
}
mirror.disabled = 'disabled';
mirror.style.whiteSpace = 'pre';
mirror.style.position = 'absolute';
mirror.style.top = mirror.style.left = '-9999em';
}
function refresh () {
var value = el.value;
if (value === mirror.value) {
return;
}
text(mirror, value);
var width = mirror.offsetWidth + offset;
el.style.width = width + 'px';
}
function bind (remove) {
var op = remove ? 'remove' : 'add';
crossvent[op](el, 'keydown', refresh);
crossvent[op](el, 'keyup', refresh);
crossvent[op](el, 'input', refresh);
crossvent[op](el, 'paste', refresh);
crossvent[op](el, 'change', refresh);
}
function destroy () {
bind(true);
mirror.parentElement.removeChild(mirror);
el.style.width = '';
}
function computed () {
if (window.getComputedStyle) {
return window.getComputedStyle(el);
}
return el.currentStyle;
}
};