Skip to content

Commit f989f6c

Browse files
SimeonCSimeonC
SimeonC
authored and
SimeonC
committed
fix(taBind.paste): Fix some paste isuses.
1 parent 3969898 commit f989f6c

File tree

1 file changed

+31
-37
lines changed

1 file changed

+31
-37
lines changed

lib/taBind.js

+31-37
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
2626
};
2727
};
2828
}])
29-
.directive('taBind', ['taSanitize', '$timeout', '$window', '$document', 'taFixChrome', 'taBrowserTag', 'taSelection', 'taSelectableElements', 'taApplyCustomRenderers', 'taOptions', '_taBlankTest',
30-
function(taSanitize, $timeout, $window, $document, taFixChrome, taBrowserTag, taSelection, taSelectableElements, taApplyCustomRenderers, taOptions, _taBlankTest){
29+
.directive('taBind', [
30+
'taSanitize', '$timeout', '$window', '$document', 'taFixChrome', 'taBrowserTag',
31+
'taSelection', 'taSelectableElements', 'taApplyCustomRenderers', 'taOptions',
32+
'_taBlankTest', '$parse', 'taDOM',
33+
function(
34+
taSanitize, $timeout, $window, $document, taFixChrome, taBrowserTag,
35+
taSelection, taSelectableElements, taApplyCustomRenderers, taOptions,
36+
_taBlankTest, $parse, taDOM){
3137
// Uses for this are textarea or input with ng-model and ta-bind='text'
3238
// OR any non-form element with contenteditable="contenteditable" ta-bind="html|text" ng-model
3339
return {
@@ -247,31 +253,11 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
247253
});
248254
}else{
249255
// all the code specific to contenteditable divs
250-
var waitforpastedata = function(savedcontent, _savedSelection, cb) {
251-
if (element[0].childNodes && element[0].childNodes.length > 0) {
252-
cb(savedcontent, _savedSelection);
253-
} else {
254-
that = {
255-
s: savedcontent,
256-
_: _savedSelection,
257-
cb: cb
258-
};
259-
that.callself = function () {
260-
waitforpastedata(that.s, that._, that.cb);
261-
};
262-
setTimeout(that.callself, 5);
263-
}
264-
};
265256
var _processingPaste = false;
266257
/* istanbul ignore next: phantom js cannot test this for some reason */
267-
var processpaste = function(savedcontent, _savedSelection) {
268-
text = element[0].innerHTML;
269-
element[0].innerHTML = savedcontent;
270-
271-
// restore selection
272-
$window.rangy.restoreSelection(_savedSelection);
258+
var processpaste = function(text) {
273259
/* istanbul ignore else: don't care if nothing pasted */
274-
if(text.trim().length){
260+
if(text && text.trim().length){
275261
// test paste from word/microsoft product
276262
if(text.match(/class=["']*Mso(Normal|List)/i)){
277263
var textFragment = text.match(/<!--StartFragment-->([\s\S]*?)<!--EndFragment-->/i);
@@ -373,7 +359,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
373359
if(text.match(/<[^>]*?(text-angular)[^>]*?>/)){
374360
var _el = angular.element("<div>" + text + "</div>");
375361
_el.find('textarea').remove();
376-
var binds = getByAttribute(_el, 'ta-bind');
362+
var binds = taDOM.getByAttribute(_el, 'ta-bind');
377363
for(var _b = 0; _b < binds.length; _b++){
378364
var _target = binds[_b][0].parentNode.parentNode;
379365
for(var _c = 0; _c < binds[_b][0].childNodes.length; _c++){
@@ -387,7 +373,8 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
387373
// in case of pasting only a span - chrome paste, remove them. THis is just some wierd formatting
388374
text = text.replace(/<(|\/)span[^>]*?>/ig, '');
389375
}
390-
text = text.replace(/<br class="Apple-interchange-newline"[^>]*?>/ig, '');
376+
// Webkit on Apple tags
377+
text = text.replace(/<br class="Apple-interchange-newline"[^>]*?>/ig, '').replace(/<span class="Apple-converted-space">( |&nbsp;)<\/span>/ig, '&nbsp;');
391378
}
392379

393380
text = taSanitize(text, '', _disableSanitizer);
@@ -414,11 +401,11 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
414401
e.preventDefault();
415402
return false;
416403
}
404+
417405
// Code adapted from http://stackoverflow.com/questions/2176861/javascript-get-clipboard-data-on-paste-event-cross-browser/6804718#6804718
418-
var _savedSelection = $window.rangy.saveSelection();
419406
_processingPaste = true;
420407
element.addClass('processing-paste');
421-
var savedcontent = element[0].innerHTML;
408+
var pastedContent;
422409
var clipboardData = (e.originalEvent || e).clipboardData;
423410
if (clipboardData && clipboardData.getData) {// Webkit - get data from clipboard, put into editdiv, cleanup, then cancel event
424411
var _types = "";
@@ -427,20 +414,27 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
427414
}
428415
/* istanbul ignore next: browser tests */
429416
if (/text\/html/i.test(_types)) {
430-
element[0].innerHTML = clipboardData.getData('text/html');
417+
pastedContent = clipboardData.getData('text/html');
431418
} else if (/text\/plain/i.test(_types)) {
432-
element[0].innerHTML = clipboardData.getData('text/plain');
433-
} else {
434-
element[0].innerHTML = "";
419+
pastedContent = clipboardData.getData('text/plain');
435420
}
436-
waitforpastedata(savedcontent, _savedSelection, processpaste);
421+
422+
processpaste(pastedContent);
437423
e.stopPropagation();
438424
e.preventDefault();
439425
return false;
440426
} else {// Everything else - empty editdiv and allow browser to paste content into it, then cleanup
441-
element[0].innerHTML = "";
442-
waitforpastedata(savedcontent, _savedSelection, processpaste);
443-
return true;
427+
var _savedSelection = $window.rangy.saveSelection(),
428+
_tempDiv = angular.element('<div class="ta-hidden-input" contenteditable="true"></div>');
429+
$document.find('body').append(_tempDiv);
430+
_tempDiv[0].focus();
431+
$timeout(function(){
432+
// restore selection
433+
$window.rangy.restoreSelection(_savedSelection);
434+
processpaste(_tempDiv[0].innerHTML);
435+
_tempDiv.remove();
436+
element[0].focus();
437+
}, 0);
444438
}
445439
});
446440
element.on('cut', scope.events.cut = function(e){
@@ -456,7 +450,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
456450
if(eventData) angular.extend(event, eventData);
457451
/* istanbul ignore else: readonly check */
458452
if(!_isReadonly){
459-
if(event.metaKey || event.ctrlKey){
453+
if(!event.altKey && event.metaKey || event.ctrlKey){
460454
// covers ctrl/command + z
461455
if((event.keyCode === 90 && !event.shiftKey)){
462456
_undo();

0 commit comments

Comments
 (0)