Skip to content

Commit 3969898

Browse files
SimeonCSimeonC
SimeonC
authored and
SimeonC
committed
feat(ta-paste): Add attribute that allows intercetping of the pasted content.
1 parent 07eda5a commit 3969898

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

lib/main.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ textAngular.run([function(){
8080
}]);
8181

8282
textAngular.directive("textAngular", [
83-
'$compile', '$timeout', 'taOptions', 'taSelection', 'taExecCommand', 'textAngularManager', '$window', '$document', '$animate', '$log', '$q',
84-
function($compile, $timeout, taOptions, taSelection, taExecCommand, textAngularManager, $window, $document, $animate, $log, $q){
83+
'$compile', '$timeout', 'taOptions', 'taSelection', 'taExecCommand',
84+
'textAngularManager', '$window', '$document', '$animate', '$log', '$q', '$parse',
85+
function($compile, $timeout, taOptions, taSelection, taExecCommand,
86+
textAngularManager, $window, $document, $animate, $log, $q, $parse){
8587
return {
8688
require: '?ngModel',
8789
scope: {},
@@ -341,7 +343,14 @@ textAngular.directive("textAngular", [
341343
}
342344
});
343345
}
344-
346+
347+
if(attrs.taPaste){
348+
scope._pasteHandler = function(_html){
349+
return $parse(attrs.taPaste)(scope.$parent, {$html: _html});
350+
};
351+
scope.displayElements.text.attr('ta-paste', '_pasteHandler($html)');
352+
}
353+
345354
// compile the scope with the text and html elements only - if we do this with the main element it causes a compile loop
346355
$compile(scope.displayElements.scrollWindow)(scope);
347356
$compile(scope.displayElements.html)(scope);
@@ -401,7 +410,11 @@ textAngular.directive("textAngular", [
401410
};
402411
scope.displayElements.html.on('blur', _focusout);
403412
scope.displayElements.text.on('blur', _focusout);
404-
413+
414+
scope.displayElements.text.on('paste', function(event){
415+
element.triggerHandler('paste', event);
416+
});
417+
405418
// Setup the default toolbar tools, this way allows the user to add new tools like plugins.
406419
// This is on the editor for future proofing if we find a better way to do this.
407420
scope.queryFormatBlockState = function(command){

lib/taBind.js

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
4242
var _lastKey;
4343
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;
4444
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
45+
var _pasteHandler;
4546

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

6869
var _blankTest = _taBlankTest(_defaultTest);
6970

71+
if(attrs.taPaste) _pasteHandler = $parse(attrs.taPaste);
72+
7073
element.addClass('ta-bind');
7174

7275
var _undoKeyupTimeout;
@@ -388,6 +391,9 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
388391
}
389392

390393
text = taSanitize(text, '', _disableSanitizer);
394+
395+
if(_pasteHandler) text = _pasteHandler(scope, {$html: text}) || text;
396+
391397
taSelection.insertHtml(text, element[0]);
392398
$timeout(function(){
393399
ngModel.$setViewValue(_compileHtml());

0 commit comments

Comments
 (0)