Skip to content

Commit

Permalink
fix(taBind.paste): Fix some paste isuses.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonC authored and SimeonC committed Feb 5, 2015
1 parent 3969898 commit f989f6c
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions lib/taBind.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
};
};
}])
.directive('taBind', ['taSanitize', '$timeout', '$window', '$document', 'taFixChrome', 'taBrowserTag', 'taSelection', 'taSelectableElements', 'taApplyCustomRenderers', 'taOptions', '_taBlankTest',
function(taSanitize, $timeout, $window, $document, taFixChrome, taBrowserTag, taSelection, taSelectableElements, taApplyCustomRenderers, taOptions, _taBlankTest){
.directive('taBind', [
'taSanitize', '$timeout', '$window', '$document', 'taFixChrome', 'taBrowserTag',
'taSelection', 'taSelectableElements', 'taApplyCustomRenderers', 'taOptions',
'_taBlankTest', '$parse', 'taDOM',
function(
taSanitize, $timeout, $window, $document, taFixChrome, taBrowserTag,
taSelection, taSelectableElements, taApplyCustomRenderers, taOptions,
_taBlankTest, $parse, taDOM){
// Uses for this are textarea or input with ng-model and ta-bind='text'
// OR any non-form element with contenteditable="contenteditable" ta-bind="html|text" ng-model
return {
Expand Down Expand Up @@ -247,31 +253,11 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
});
}else{
// all the code specific to contenteditable divs
var waitforpastedata = function(savedcontent, _savedSelection, cb) {
if (element[0].childNodes && element[0].childNodes.length > 0) {
cb(savedcontent, _savedSelection);
} else {
that = {
s: savedcontent,
_: _savedSelection,
cb: cb
};
that.callself = function () {
waitforpastedata(that.s, that._, that.cb);
};
setTimeout(that.callself, 5);
}
};
var _processingPaste = false;
/* istanbul ignore next: phantom js cannot test this for some reason */
var processpaste = function(savedcontent, _savedSelection) {
text = element[0].innerHTML;
element[0].innerHTML = savedcontent;

// restore selection
$window.rangy.restoreSelection(_savedSelection);
var processpaste = function(text) {
/* istanbul ignore else: don't care if nothing pasted */
if(text.trim().length){
if(text && text.trim().length){
// test paste from word/microsoft product
if(text.match(/class=["']*Mso(Normal|List)/i)){
var textFragment = text.match(/<!--StartFragment-->([\s\S]*?)<!--EndFragment-->/i);
Expand Down Expand Up @@ -373,7 +359,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
if(text.match(/<[^>]*?(text-angular)[^>]*?>/)){
var _el = angular.element("<div>" + text + "</div>");
_el.find('textarea').remove();
var binds = getByAttribute(_el, 'ta-bind');
var binds = taDOM.getByAttribute(_el, 'ta-bind');
for(var _b = 0; _b < binds.length; _b++){
var _target = binds[_b][0].parentNode.parentNode;
for(var _c = 0; _c < binds[_b][0].childNodes.length; _c++){
Expand All @@ -387,7 +373,8 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
// in case of pasting only a span - chrome paste, remove them. THis is just some wierd formatting
text = text.replace(/<(|\/)span[^>]*?>/ig, '');
}
text = text.replace(/<br class="Apple-interchange-newline"[^>]*?>/ig, '');
// Webkit on Apple tags
text = text.replace(/<br class="Apple-interchange-newline"[^>]*?>/ig, '').replace(/<span class="Apple-converted-space">( |&nbsp;)<\/span>/ig, '&nbsp;');
}

text = taSanitize(text, '', _disableSanitizer);
Expand All @@ -414,11 +401,11 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
e.preventDefault();
return false;
}

// Code adapted from http://stackoverflow.com/questions/2176861/javascript-get-clipboard-data-on-paste-event-cross-browser/6804718#6804718
var _savedSelection = $window.rangy.saveSelection();
_processingPaste = true;
element.addClass('processing-paste');
var savedcontent = element[0].innerHTML;
var pastedContent;
var clipboardData = (e.originalEvent || e).clipboardData;
if (clipboardData && clipboardData.getData) {// Webkit - get data from clipboard, put into editdiv, cleanup, then cancel event
var _types = "";
Expand All @@ -427,20 +414,27 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
}
/* istanbul ignore next: browser tests */
if (/text\/html/i.test(_types)) {
element[0].innerHTML = clipboardData.getData('text/html');
pastedContent = clipboardData.getData('text/html');
} else if (/text\/plain/i.test(_types)) {
element[0].innerHTML = clipboardData.getData('text/plain');
} else {
element[0].innerHTML = "";
pastedContent = clipboardData.getData('text/plain');
}
waitforpastedata(savedcontent, _savedSelection, processpaste);

processpaste(pastedContent);
e.stopPropagation();
e.preventDefault();
return false;
} else {// Everything else - empty editdiv and allow browser to paste content into it, then cleanup
element[0].innerHTML = "";
waitforpastedata(savedcontent, _savedSelection, processpaste);
return true;
var _savedSelection = $window.rangy.saveSelection(),
_tempDiv = angular.element('<div class="ta-hidden-input" contenteditable="true"></div>');
$document.find('body').append(_tempDiv);
_tempDiv[0].focus();
$timeout(function(){
// restore selection
$window.rangy.restoreSelection(_savedSelection);
processpaste(_tempDiv[0].innerHTML);
_tempDiv.remove();
element[0].focus();
}, 0);
}
});
element.on('cut', scope.events.cut = function(e){
Expand All @@ -456,7 +450,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
if(eventData) angular.extend(event, eventData);
/* istanbul ignore else: readonly check */
if(!_isReadonly){
if(event.metaKey || event.ctrlKey){
if(!event.altKey && event.metaKey || event.ctrlKey){
// covers ctrl/command + z
if((event.keyCode === 90 && !event.shiftKey)){
_undo();
Expand Down

0 comments on commit f989f6c

Please sign in to comment.