-
Notifications
You must be signed in to change notification settings - Fork 8
/
tailormade.js
176 lines (152 loc) · 4.03 KB
/
tailormade.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
'use strict';
var sell = require('sell');
var crossvent = require('crossvent');
var seleccion = require('seleccion');
var throttle = require('./throttle');
var getSelection = seleccion.get;
var props = [
'direction',
'boxSizing',
'width',
'height',
'overflowX',
'overflowY',
'borderTopWidth',
'borderRightWidth',
'borderBottomWidth',
'borderLeftWidth',
'paddingTop',
'paddingRight',
'paddingBottom',
'paddingLeft',
'fontStyle',
'fontVariant',
'fontWeight',
'fontStretch',
'fontSize',
'fontSizeAdjust',
'lineHeight',
'fontFamily',
'textAlign',
'textTransform',
'textIndent',
'textDecoration',
'letterSpacing',
'wordSpacing'
];
var win = global;
var doc = document;
var ff = win.mozInnerScreenX !== null && win.mozInnerScreenX !== void 0;
function tailormade (el, options) {
var textInput = el.tagName === 'INPUT' || el.tagName === 'TEXTAREA';
var throttledRefresh = throttle(refresh, 30);
var o = options || {};
bind();
return {
read: readPosition,
refresh: throttledRefresh,
destroy: destroy
};
function noop () {}
function readPosition () { return (textInput ? coordsText : coordsHTML)(); }
function refresh () {
if (o.sleeping) {
return;
}
return (o.update || noop)(readPosition());
}
function coordsText () {
var p = sell(el);
var context = prepare();
var readings = readTextCoords(context, p.start);
doc.body.removeChild(context.mirror);
return readings;
}
function coordsHTML () {
var sel = getSelection();
if (sel.rangeCount) {
var range = sel.getRangeAt(0);
var needsToWorkAroundNewlineBug = range.startContainer.nodeName === 'P' && range.startOffset === 0;
if (needsToWorkAroundNewlineBug) {
return {
x: range.startContainer.offsetLeft,
y: range.startContainer.offsetTop,
absolute: true
};
}
if (range.getClientRects) {
var rects = range.getClientRects();
if (rects.length > 0) {
return {
x: rects[0].left,
y: rects[0].top,
absolute: true
};
}
}
}
return { x: 0, y: 0 };
}
function readTextCoords (context, p) {
var rest = doc.createElement('span');
var mirror = context.mirror;
var computed = context.computed;
write(mirror, read(el).substring(0, p));
if (el.tagName === 'INPUT') {
mirror.textContent = mirror.textContent.replace(/\s/g, '\u00a0');
}
write(rest, read(el).substring(p) || '.');
mirror.appendChild(rest);
return {
x: rest.offsetLeft + parseInt(computed['borderLeftWidth']),
y: rest.offsetTop + parseInt(computed['borderTopWidth'])
};
}
function read (el) {
return textInput ? el.value : el.innerHTML;
}
function prepare () {
var computed = win.getComputedStyle ? getComputedStyle(el) : el.currentStyle;
var mirror = doc.createElement('div');
var style = mirror.style;
doc.body.appendChild(mirror);
if (el.tagName !== 'INPUT') {
style.wordWrap = 'break-word';
}
style.whiteSpace = 'pre-wrap';
style.position = 'absolute';
style.visibility = 'hidden';
props.forEach(copy);
if (ff) {
style.width = parseInt(computed.width) - 2 + 'px';
if (el.scrollHeight > parseInt(computed.height)) {
style.overflowY = 'scroll';
}
} else {
style.overflow = 'hidden';
}
return { mirror: mirror, computed: computed };
function copy (prop) {
style[prop] = computed[prop];
}
}
function write (el, value) {
if (textInput) {
el.textContent = value;
} else {
el.innerHTML = value;
}
}
function bind (remove) {
var op = remove ? 'remove' : 'add';
crossvent[op](el, 'keydown', throttledRefresh);
crossvent[op](el, 'keyup', throttledRefresh);
crossvent[op](el, 'input', throttledRefresh);
crossvent[op](el, 'paste', throttledRefresh);
crossvent[op](el, 'change', throttledRefresh);
}
function destroy () {
bind(true);
}
}
module.exports = tailormade;