|
11 | 11 | /** underscoreThrottle()
|
12 | 12 | * From underscore http://underscorejs.org/docs/underscore.html
|
13 | 13 | */
|
14 |
| - var underscoreThrottle = function(func, wait) { |
15 |
| - var context, args, timeout, result; |
| 14 | + var underscoreThrottle = function(func, wait, options) { |
| 15 | + var context, args, result; |
| 16 | + var timeout = null; |
16 | 17 | var previous = 0;
|
| 18 | + if (!options) { |
| 19 | + options = {}; |
| 20 | + } |
17 | 21 | var later = function() {
|
18 |
| - previous = new Date(); |
| 22 | + previous = options.leading === false ? 0 : $.now(); |
19 | 23 | timeout = null;
|
20 | 24 | result = func.apply(context, args);
|
| 25 | + if (!timeout) { |
| 26 | + context = args = null; |
| 27 | + } |
21 | 28 | };
|
22 | 29 | return function() {
|
23 |
| - var now = new Date(); |
| 30 | + var now = $.now(); |
| 31 | + if (!previous && options.leading === false) { |
| 32 | + previous = now; |
| 33 | + } |
24 | 34 | var remaining = wait - (now - previous);
|
25 | 35 | context = this;
|
26 | 36 | args = arguments;
|
27 |
| - if (remaining <= 0) { |
28 |
| - clearTimeout(timeout); |
29 |
| - timeout = null; |
| 37 | + if (remaining <= 0 || remaining > wait) { |
| 38 | + if (timeout) { |
| 39 | + clearTimeout(timeout); |
| 40 | + timeout = null; |
| 41 | + } |
30 | 42 | previous = now;
|
31 | 43 | result = func.apply(context, args);
|
32 |
| - } else if (!timeout) { |
| 44 | + if (!timeout) { |
| 45 | + context = args = null; |
| 46 | + } |
| 47 | + } |
| 48 | + else if (!timeout && options.trailing !== false) { |
33 | 49 | timeout = setTimeout(later, remaining);
|
34 | 50 | }
|
35 | 51 | return result;
|
|
73 | 89 | };
|
74 | 90 | $.fn.wysiwyg = function (userOptions) {
|
75 | 91 | var editor = this,
|
76 |
| - wrapper = $(editor).parent(), |
77 | 92 | selectedRange,
|
78 | 93 | options,
|
79 | 94 | toolbarBtnSelector,
|
80 | 95 | updateToolbar = function () {
|
81 | 96 | if (options.activeToolbarClass) {
|
82 |
| - $(options.toolbarSelector,wrapper).find(toolbarBtnSelector).each(underscoreThrottle(function () { |
| 97 | + $(options.toolbarSelector).find(toolbarBtnSelector).each(function () { |
83 | 98 | var commandArr = $(this).data(options.commandRole).split(' '),
|
84 | 99 | command = commandArr[0];
|
85 | 100 |
|
|
93 | 108 | } else {
|
94 | 109 | $(this).removeClass(options.activeToolbarClass);
|
95 | 110 | }
|
96 |
| - }, options.keypressTimeout)); |
| 111 | + }); |
97 | 112 | }
|
98 | 113 | },
|
99 | 114 | execCommand = function (commandWithArgs, valueArg) {
|
|
104 | 119 | var parts = commandWithArgs.split('-');
|
105 | 120 |
|
106 | 121 | if ( parts.length === 1 ) {
|
107 |
| - document.execCommand(command, 0, args); |
| 122 | + underscoreThrottle(document.execCommand(command, false, args), options.keypressTimeout); |
108 | 123 | }
|
109 | 124 | else if ( parts[0] === 'format' && parts.length === 2 ) {
|
110 |
| - document.execCommand('formatBlock', false, parts[1] ); |
| 125 | + underscoreThrottle(document.execCommand('formatBlock', false, parts[1] ), options.keypressTimeout); |
111 | 126 | }
|
112 | 127 |
|
113 | 128 | editor.trigger('change');
|
|
119 | 134 | if (editor.attr('contenteditable') && editor.is(':visible')) {
|
120 | 135 | e.preventDefault();
|
121 | 136 | e.stopPropagation();
|
122 |
| - execCommand(command); |
| 137 | + underscoreThrottle(execCommand(command), options.keypressTimeout); |
123 | 138 | }
|
124 | 139 | }).keyup(hotkey, function (e) {
|
125 | 140 | if (editor.attr('contenteditable') && editor.is(':visible')) {
|
|
190 | 205 | $.each(files, function (idx, fileInfo) {
|
191 | 206 | if (/^image\//.test(fileInfo.type)) {
|
192 | 207 | $.when(readFileIntoDataUrl(fileInfo)).done(function (dataUrl) {
|
193 |
| - execCommand('insertimage', dataUrl); |
| 208 | + underscoreThrottle(execCommand('insertimage', dataUrl), options.keypressTimeout); |
194 | 209 | editor.trigger('image-inserted');
|
195 | 210 | }).fail(function (e) {
|
196 | 211 | options.fileUploadError("file-reader", e);
|
|
203 | 218 | markSelection = function (input, color) {
|
204 | 219 | restoreSelection();
|
205 | 220 | if (document.queryCommandSupported('hiliteColor')) {
|
206 |
| - document.execCommand('hiliteColor', 0, color || 'transparent'); |
| 221 | + underscoreThrottle(document.execCommand('hiliteColor', false, color || 'transparent'), options.keypressTimeout); |
207 | 222 | }
|
208 | 223 | saveSelection();
|
209 | 224 | input.data(options.selectionMarker, color);
|
210 | 225 | },
|
211 | 226 | bindToolbar = function (toolbar, options) {
|
212 |
| - toolbar.find(toolbarBtnSelector, wrapper).click(function () { |
| 227 | + toolbar.find(toolbarBtnSelector).click(function () { |
213 | 228 | restoreSelection();
|
214 | 229 | editor.focus();
|
215 | 230 |
|
216 | 231 | if ($(this).data(options.commandRole) === 'html') {
|
217 | 232 | toggleHtmlEdit();
|
218 | 233 | }
|
219 | 234 | else {
|
220 |
| - execCommand($(this).data(options.commandRole)); |
| 235 | + underscoreThrottle(execCommand($(this).data(options.commandRole)), options.keypressTimeout); |
221 | 236 | }
|
222 | 237 | saveSelection();
|
223 | 238 | });
|
|
229 | 244 | restoreSelection();
|
230 | 245 | if (newValue) {
|
231 | 246 | editor.focus();
|
232 |
| - execCommand($(this).data(options.commandRole), newValue); |
| 247 | + underscoreThrottle(execCommand($(this).data(options.commandRole), newValue), options.keypressTimeout); |
233 | 248 | }
|
234 | 249 | saveSelection();
|
235 | 250 | }).on('focus', function () {
|
|
0 commit comments