Skip to content

Commit

Permalink
feat(ta-paste): Add attribute that allows intercetping of the pasted …
Browse files Browse the repository at this point in the history
…content.
  • Loading branch information
SimeonC authored and SimeonC committed Feb 5, 2015
1 parent 07eda5a commit 3969898
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ textAngular.run([function(){
}]);

textAngular.directive("textAngular", [
'$compile', '$timeout', 'taOptions', 'taSelection', 'taExecCommand', 'textAngularManager', '$window', '$document', '$animate', '$log', '$q',
function($compile, $timeout, taOptions, taSelection, taExecCommand, textAngularManager, $window, $document, $animate, $log, $q){
'$compile', '$timeout', 'taOptions', 'taSelection', 'taExecCommand',
'textAngularManager', '$window', '$document', '$animate', '$log', '$q', '$parse',
function($compile, $timeout, taOptions, taSelection, taExecCommand,
textAngularManager, $window, $document, $animate, $log, $q, $parse){
return {
require: '?ngModel',
scope: {},
Expand Down Expand Up @@ -341,7 +343,14 @@ textAngular.directive("textAngular", [
}
});
}


if(attrs.taPaste){
scope._pasteHandler = function(_html){
return $parse(attrs.taPaste)(scope.$parent, {$html: _html});
};
scope.displayElements.text.attr('ta-paste', '_pasteHandler($html)');
}

// compile the scope with the text and html elements only - if we do this with the main element it causes a compile loop
$compile(scope.displayElements.scrollWindow)(scope);
$compile(scope.displayElements.html)(scope);
Expand Down Expand Up @@ -401,7 +410,11 @@ textAngular.directive("textAngular", [
};
scope.displayElements.html.on('blur', _focusout);
scope.displayElements.text.on('blur', _focusout);


scope.displayElements.text.on('paste', function(event){
element.triggerHandler('paste', event);
});

// Setup the default toolbar tools, this way allows the user to add new tools like plugins.
// This is on the editor for future proofing if we find a better way to do this.
scope.queryFormatBlockState = function(command){
Expand Down
6 changes: 6 additions & 0 deletions lib/taBind.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
var _lastKey;
var BLOCKED_KEYS = /^(9|19|20|27|33|34|35|36|37|38|39|40|45|112|113|114|115|116|117|118|119|120|121|122|123|144|145)$/i;
var UNDO_TRIGGER_KEYS = /^(8|13|32|46|59|61|107|109|186|187|188|189|190|191|192|219|220|221|222)$/i; // spaces, enter, delete, backspace, all punctuation
var _pasteHandler;

// defaults to the paragraph element, but we need the line-break or it doesn't allow you to type into the empty element
// non IE is '<p><br/></p>', ie is '<p></p>' as for once IE gets it correct...
Expand All @@ -67,6 +68,8 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'

var _blankTest = _taBlankTest(_defaultTest);

if(attrs.taPaste) _pasteHandler = $parse(attrs.taPaste);

element.addClass('ta-bind');

var _undoKeyupTimeout;
Expand Down Expand Up @@ -388,6 +391,9 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
}

text = taSanitize(text, '', _disableSanitizer);

if(_pasteHandler) text = _pasteHandler(scope, {$html: text}) || text;

taSelection.insertHtml(text, element[0]);
$timeout(function(){
ngModel.$setViewValue(_compileHtml());
Expand Down

0 comments on commit 3969898

Please sign in to comment.