-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
139 lines (122 loc) · 4.46 KB
/
app.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
//JSHint globals
/*global
window:true,
document:true,
*/
//--------------------------------------
(function(){
var out = window.coolText = function(el){
$(el).on("keypress paste", function(){out.inputEventsHandler.apply(out,arguments);});
};
out.rangeEndContainer = function(){
var sel = window.getSelection();
var range = sel.getRangeAt(0);
return range.endContainer;
};
//ref: http://stackoverflow.com/a/6691294/689223
out.insertNodeAtCaret = function(node) {
console.log("\n");
var sel, range;
if (window.getSelection) {
// IE9 and non-IE
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
var containerNode = $(range.endContainer).closest(".inserted");
console.log("containerNode");
console.log(containerNode[0]);
console.log(range.endOffset);
// Preserve the selection
var preserveSelection = function(){
console.log("node");
console.log(node);
if (node) {
range = range.cloneRange();
range.setStartAfter(node);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
};
//ref: https://developer.mozilla.org/en-US/docs/DOM/range
if( !containerNode.length){
range.insertNode(node);
}
else if (range.endOffset === 0 && containerNode.text().length > 0){//&& containerNode.text().length > 0){
node = $(node).insertBefore(containerNode)[0];
console.log("insert previously");
}
else{
node = $(node).insertAfter(containerNode)[0];
console.log("insert after");
}
preserveSelection();
node.scrollIntoView(true);
return node;
}
} else if (document.selection && document.selection.type != "Control") {
// IE < 9
document.selection.createRange().pasteHTML(node.outerHTML);
}
};
out.getClipboardData = function(e) {
if (window.clipboardData && window.clipboardData.getData){ // IE
return window.clipboardData.getData('Text');//ref: http://msdn.microsoft.com/es-es/library/ie/ms536436(v=vs.85).aspx
}
else if (e.clipboardData && e.clipboardData.getData){
return e.clipboardData.getData('text/html');
}
};
out.generateNode = function (innerHTML){
innerHTML = innerHTML || "";
var el = document.createElement("span");
el.innerHTML = innerHTML;
el.classList.add("inserted");
return el;
};
out.pasteHandler = function(e){
var data = this.getClipboardData(e.originalEvent);
};
out.keypressHandler = function(e){
return String.fromCharCode(e.which);
};
out.newLineHandler = function(e){
var node1 = $("<br>")[0];
node1 = this.insertNodeAtCaret( node1 );
var node2 = this.insertNodeAtCaret( $("<br>")[0] );
$(node1).remove();
e.preventDefault();
};
//ref:http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
out.isNewLine = function(e){
return e.which == 13;
};
out.isArrowKey = function(e){
return (e.charCode <= 40 && e.charCode >= 37) || e.charCode === 0/*firefox*/;
};
out.inputEventsHandler = function(e){
var c;
console.log("which key: "+e.which);
console.log(e);
if (e.metaKey || e.ctrKey) return;//in firefox keypress also captures commands like: ctr+a, ctr+c, ctr+v (cmd also included for MacOS)
if (e.type === "paste"){
c = this.pasteHandler(e);
}
else{
c = this.keypressHandler(e);
}
if (this.isNewLine(e)){
c = this.newLineHandler(e);
}
console.log("input:"+c);
if (c && c!==" " && !this.isArrowKey(e) ){
var node = out.generateNode(c);
node = this.insertNodeAtCaret( node );
e.preventDefault();
}
};
})(window);
$(function(){
window.coolText(".input");
});