Skip to content

Commit cd9de24

Browse files
committed
fix(taBind): Corrected issue where Apple ignores Shift+Enter on Safari
1 parent 89974c3 commit cd9de24

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/taBind.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -864,17 +864,17 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
864864
} else {
865865
// if enter - insert new taDefaultWrap, if shift+enter insert <br/>
866866
if(_defaultVal !== '' && _defaultVal !== '<BR><BR>' && event.keyCode === _ENTER_KEYCODE && !event.ctrlKey && !event.metaKey && !event.altKey){
867+
var selection = taSelection.getSelectionElement();
868+
while(!selection.nodeName.match(VALIDELEMENTS) && selection !== element[0]){
869+
selection = selection.parentNode;
870+
}
867871
if(!event.shiftKey){
868872
// new paragraph, br should be caught correctly
869-
var selection = taSelection.getSelectionElement();
870873
// shifted to nodeName here from tagName since it is more widely supported see: http://stackoverflow.com/questions/4878484/difference-between-tagname-and-nodename
871-
while(!selection.nodeName.match(VALIDELEMENTS) && selection !== element[0]){
872-
selection = selection.parentNode;
873-
}
874-
874+
//console.log('Enter', selection.nodeName, attrs.taDefaultWrap, selection.innerHTML.trim());
875875
if(selection.tagName.toLowerCase() !==
876876
attrs.taDefaultWrap &&
877-
selection.tagName.toLowerCase() !== 'li' &&
877+
selection.nodeName.toLowerCase() !== 'li' &&
878878
(selection.innerHTML.trim() === '' || selection.innerHTML.trim() === '<br>')
879879
) {
880880
// Chrome starts with a <div><br></div> after an EnterKey
@@ -883,6 +883,29 @@ angular.module('textAngular.taBind', ['textAngular.factories', 'textAngular.DOM'
883883
angular.element(selection).replaceWith(_new);
884884
taSelection.setSelectionToElementStart(_new[0]);
885885
}
886+
} else {
887+
// shift + Enter
888+
var tagName = selection.tagName.toLowerCase();
889+
//console.log('Shift+Enter', selection.tagName, attrs.taDefaultWrap, selection.innerHTML.trim());
890+
// For an LI: We see: LI p ....<br><br>
891+
// For a P: We see: P p ....<br><br>
892+
// on Safari, the browser ignores the Shift+Enter and acts just as an Enter Key
893+
// For an LI: We see: LI p <br>
894+
// For a P: We see: P p <br>
895+
if((tagName === attrs.taDefaultWrap ||
896+
tagName === 'li' ||
897+
tagName === 'pre' ||
898+
tagName === 'div') &&
899+
!/.+<br><br>/.test(selection.innerHTML.trim())) {
900+
var ps = selection.previousSibling;
901+
//console.log('wrong....', ps);
902+
// we need to remove this selection and fix the previousSibling up...
903+
if (ps) {
904+
ps.innerHTML = ps.innerHTML + '<br><br>';
905+
angular.element(selection).remove();
906+
taSelection.setSelectionToElementEnd(ps);
907+
}
908+
}
886909
}
887910
}
888911
var val = _compileHtml();

0 commit comments

Comments
 (0)