@@ -655,7 +655,9 @@ function($window, $document, taDOM){
655
655
656
656
range . selectNodeContents ( el ) ;
657
657
range . collapse ( false ) ;
658
-
658
+ if ( el . childNodes && el . childNodes [ el . childNodes . length - 1 ] && el . childNodes [ el . childNodes . length - 1 ] . nodeName === 'br' ) {
659
+ range . startOffset = range . endOffset = range . startOffset - 1 ;
660
+ }
659
661
rangy . getSelection ( ) . setSingleRange ( range ) ;
660
662
} ,
661
663
// from http://stackoverflow.com/questions/6690752/insert-html-at-caret-in-a-contenteditable-div
@@ -760,6 +762,7 @@ function($window, $document, taDOM){
760
762
range . deleteContents ( ) ;
761
763
}
762
764
}
765
+
763
766
range . insertNode ( frag ) ;
764
767
if ( lastNode ) {
765
768
api . setSelectionToElementEnd ( lastNode ) ;
@@ -933,6 +936,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
933
936
var _isInputFriendly = _isContentEditable || element [ 0 ] . tagName . toLowerCase ( ) === 'textarea' || element [ 0 ] . tagName . toLowerCase ( ) === 'input' ;
934
937
var _isReadonly = false ;
935
938
var _focussed = false ;
939
+ var _skipRender = false ;
936
940
var _disableSanitizer = attrs . taUnsafeSanitizer || taOptions . disableSanitizer ;
937
941
var _lastKey ;
938
942
var BLOCKED_KEYS = / ^ ( 9 | 1 9 | 2 0 | 2 7 | 3 3 | 3 4 | 3 5 | 3 6 | 3 7 | 3 8 | 3 9 | 4 0 | 4 5 | 1 1 2 | 1 1 3 | 1 1 4 | 1 1 5 | 1 1 6 | 1 1 7 | 1 1 8 | 1 1 9 | 1 2 0 | 1 2 1 | 1 2 2 | 1 2 3 | 1 4 4 | 1 4 5 ) $ / i;
@@ -1039,6 +1043,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
1039
1043
} ;
1040
1044
1041
1045
var _setViewValue = function ( _val , triggerUndo ) {
1046
+ _skipRender = true ;
1042
1047
if ( typeof triggerUndo === "undefined" || triggerUndo === null ) triggerUndo = true && _isContentEditable ; // if not contentEditable then the native undo/redo is fine
1043
1048
if ( typeof _val === "undefined" || _val === null ) _val = _compileHtml ( ) ;
1044
1049
if ( _blankTest ( _val ) ) {
@@ -1419,6 +1424,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
1419
1424
if ( ! _isReadonly ) {
1420
1425
_setViewValue ( ) ;
1421
1426
}
1427
+ _skipRender = true ; // don't redo the whole thing, just check the placeholder logic
1422
1428
ngModel . $render ( ) ;
1423
1429
} ) ;
1424
1430
@@ -1435,7 +1441,7 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
1435
1441
1436
1442
element . on ( 'focus' , scope . events . focus = function ( ) {
1437
1443
_focussed = true ;
1438
- ngModel . $render ( ) ;
1444
+ element . removeClass ( 'placeholder-text' ) ;
1439
1445
} ) ;
1440
1446
1441
1447
element . on ( 'mouseup' , scope . events . mouseup = function ( ) {
@@ -1523,20 +1529,27 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
1523
1529
ngModel . $render = function ( ) {
1524
1530
// catch model being null or undefined
1525
1531
var val = ngModel . $viewValue || '' ;
1532
+
1526
1533
// if the editor isn't focused it needs to be updated, otherwise it's receiving user input
1527
- if ( $document [ 0 ] . activeElement !== element [ 0 ] ) {
1528
- // Not focussed
1534
+ if ( ! _skipRender ) {
1535
+ /* istanbul ignore else: in other cases we don't care */
1536
+ if ( _isContentEditable && _focussed ) {
1537
+ // element is focussed, test for placeholder
1538
+ element . removeClass ( 'placeholder-text' ) ;
1539
+ element [ 0 ] . blur ( ) ;
1540
+ $timeout ( function ( ) {
1541
+ element [ 0 ] . focus ( ) ;
1542
+ taSelection . setSelectionToElementEnd ( element . children ( ) [ element . children ( ) . length - 1 ] ) ;
1543
+ } , 1 ) ;
1544
+ }
1529
1545
if ( _isContentEditable ) {
1530
1546
// WYSIWYG Mode
1531
1547
if ( attrs . placeholder ) {
1532
1548
if ( val === '' ) {
1533
1549
// blank
1534
- if ( _focussed ) element . removeClass ( 'placeholder-text' ) ;
1535
- else element . addClass ( 'placeholder-text' ) ;
1536
1550
_setInnerHTML ( _defaultVal ) ;
1537
1551
} else {
1538
1552
// not-blank
1539
- element . removeClass ( 'placeholder-text' ) ;
1540
1553
_setInnerHTML ( val ) ;
1541
1554
}
1542
1555
} else {
@@ -1556,13 +1569,16 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
1556
1569
// only for input and textarea inputs
1557
1570
element . val ( val ) ;
1558
1571
}
1559
- } else {
1560
- /* istanbul ignore else: in other cases we don't care */
1561
- if ( _isContentEditable ) {
1562
- // element is focussed, test for placeholder
1572
+ }
1573
+ if ( _isContentEditable && attrs . placeholder ) {
1574
+ if ( val === '' ) {
1575
+ if ( _focussed ) element . removeClass ( 'placeholder-text' ) ;
1576
+ else element . addClass ( 'placeholder-text' ) ;
1577
+ } else {
1563
1578
element . removeClass ( 'placeholder-text' ) ;
1564
1579
}
1565
1580
}
1581
+ _skipRender = false ;
1566
1582
} ;
1567
1583
1568
1584
if ( attrs . taReadonly ) {
@@ -2109,7 +2125,7 @@ textAngular.directive("textAngular", [
2109
2125
scope . displayElements . forminput . val ( ngModel . $viewValue ) ;
2110
2126
// if the editors aren't focused they need to be updated, otherwise they are doing the updating
2111
2127
/* istanbul ignore else: don't care */
2112
- if ( ! scope . _elementSelectTriggered && $document [ 0 ] . activeElement !== scope . displayElements . html [ 0 ] && $document [ 0 ] . activeElement !== scope . displayElements . text [ 0 ] ) {
2128
+ if ( ! scope . _elementSelectTriggered ) {
2113
2129
// catch model being null or undefined
2114
2130
scope . html = ngModel . $viewValue || '' ;
2115
2131
}
0 commit comments