forked from dwachss/bililiteRange
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbililiteRange.fancytextasync.js
More file actions
106 lines (100 loc) · 4.21 KB
/
Copy pathbililiteRange.fancytextasync.js
File metadata and controls
106 lines (100 loc) · 4.21 KB
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
// Turn a textarea element into a pre element that can use a highlighter
// Designed for use with Prism (prismjs.com)
// usage: editor = bililiteRange.fancytext(element, function(e, cb) {
// Prism.highlightElement(e, true, cb);
// }, threshold);
// the element should have the appropriate class=language-* for Prism.
// Version: 1.0
// Documentation: http://bililite.com/blog/2013/12/16/simple-syntax-highlighting-editor-with-prism/
// Copyright (c) 2013 Daniel Wachsstock
// depends: bililiteRange.js, bililiteRange.utils.js (the latter is only for autoindenting; if that's not used it is not necessary)
// MIT license:
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
bililiteRange.fancyTextAsync = function(editor, highlighter, threshold){
var highlightQueue = [];
if (editor.tagName.toLowerCase() == 'textarea'){
// turn the editor into an editable <pre>, since that is what Prism works on
var replacement = document.createElement('pre');
replacement.setAttribute ('contenteditable', true);
[].forEach.call(editor.attributes, function(attr){
replacement.setAttribute(attr.name, attr.value);
});
replacement.textContent = editor.value;
editor.parentNode.replaceChild(replacement, editor);
editor = replacement;
}
// for large texts, it can be too slow to run the highlighter on every input event.
// use the code from http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
// to limit it to once every threshold milliseconds
function debounce (func, threshold){
if (!threshold) return func; // no debouncing
var timeout;
return function(){
var self = this, args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function(){
func.apply(self, args);
}, threshold);
};
}
var rng = bililiteRange(editor);
function highlight(){
rng.bounds('selection');
// handle what Lea Verou calls "Dirty fix to #2"--seems to be Chrome issue with missing newlines
// from https://github.com/LeaVerou/dabblet/issues/2
if (!/\n$/.test(editor.textContent)) editor.textContent += '\n';
highlighter(editor, function() {
rng.select();
highlightQueue.shift();
if (highlightQueue.length > 0) {
// process next highlight request in queue
highlightQueue[0]();
}
});
}
function highlightAsync() {
highlightQueue.push(highlight);
if (highlightQueue.length == 1) {
highlightQueue[0]();
} // else highlight is already running
}
if (highlighter){
highlightAsync();
rng.listen('input', debounce(highlightAsync, threshold));
}
rng.listen('paste', function(evt){
if (!evt.defaultPrevented) {
// Firefox changes newlines to br's on paste!
// Chrome pastes cr's! Nothing is easy.
rng.bounds('selection').
text(evt.clipboardData.getData("text/plain").replace(/\r/g,''), 'end').
select();
evt.preventDefault();
}
});
rng.listen('keydown', function(evt){
// avoid the fancy element-creation with newlines
if (evt.keyCode == 13 && evt.defaultPrevented){
rng.bounds('selection').text('\n','end', rng.data().autoindent).select();
evt.preventDefault();
}
});
return editor;
};