-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (157 loc) · 3.28 KB
/
index.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
/**
* Caret
*
* Listen to and manipulate the text caret.
*
* Copyright (c) 2013 by Hsiaoming Yang.
*/
var event = require('event');
var emitter = require('emitter');
module.exports = Caret;
function Caret(element) {
this.element = element;
bindMouse(this);
if (element) {
var caret = this;
event.bind(element, 'keyup', function() {
caret.emit('change');
});
}
}
emitter(Caret.prototype);
/**
* Get the current selection
*/
Caret.prototype.selection = function() {
var sel = document.getSelection();
if (!this.element) {
return sel;
}
var el = this.element;
// only when the selection in element
if (isChildOf(sel.anchorNode, el) && isChildOf(sel.focusNode, el)) {
return sel;
}
return null;
};
/**
* Get the current selection range
*/
Caret.prototype.range = function() {
var sel = this.selection();
if (!sel) {
return null;
}
if (sel.rangeCount) {
return sel.getRangeAt(0);
}
return null;
};
/**
* The nearest parent node
*/
Caret.prototype.parent = function() {
var range = this.range();
if (!range) {
return null;
}
var node = range.startContainer;
if (node.nodeType === document.ELEMENT_NODE) {
return node;
}
return node.parentElement || node.parentNode;
};
/**
* Find the block level parent node of caret
*/
Caret.prototype.blockParent = function() {
var parent = this.parent();
if (!parent) {
return null;
}
return getBlockElement(parent, this.element);
};
/**
* Save caret position
*/
Caret.prototype.save = function(range) {
range = range || this.range();
if (range) {
this._range = range;
}
};
/**
* Restore caret position
*/
Caret.prototype.restore = function(range) {
var r = document.createRange();
var sel = document.getSelection();
range = range || this._range;
if (range) {
r.setStart(range.startContainer, range.startOffset);
r.setEnd(range.endContainer, range.endOffset);
sel.removeAllRanges();
sel.addRange(r);
} else {
sel.selectAllChildren(this.element);
sel.collapseToEnd();
}
};
/**
* Bind mouse and emit select event
*/
function bindMouse(caret) {
var body = document.body;
var timer;
event.bind(body, 'mouseup', function(e) {
// caret has changed
caret.emit('change');
timer = setTimeout(function() {
var sel = document.getSelection();
if (sel && sel.toString().trim()) {
caret.emit('select', e, sel);
}
}, 50);
});
event.bind(body, 'mousedown', function() {
clearTimeout(timer);
});
}
/**
* Find the block level parent node
*/
function getBlockElement(el, parent) {
if (parent && el === parent) {
return null;
}
var style;
if (window.getComputedStyle) {
style = window.getComputedStyle(el);
} else {
style = el.currentStyle;
}
var display = style.display;
if (display === 'block' || display === 'table') {
return el;
}
return getBlockElement(el.parentElement || el.parentNode);
}
/**
* Check if the element is the child of the node
*/
function isChildOf(el, parent) {
if (!el) {
return false;
}
while (el = el.parentNode) {
if (el === document.body) {
return false;
}
if (el === parent) {
return true;
}
}
return false;
}
Caret.isChildOf = isChildOf;
Caret.getBlockElement = getBlockElement;