mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 23:40:35 -05:00
Fix Cmd-left/right bug in Firefox on Mac OS X.
Firefox incorrectly goes back/forward in history instead of moving the cursor to the beginning/end of the line when you press cmd-left/right on a mac. We now override this to do the right thing.
This commit is contained in:
parent
03818bd3e8
commit
87c0f3fbe1
4 changed files with 73 additions and 25 deletions
|
@ -22,13 +22,18 @@ var win = doc.defaultView;
|
||||||
var body = doc.body;
|
var body = doc.body;
|
||||||
|
|
||||||
var ua = navigator.userAgent;
|
var ua = navigator.userAgent;
|
||||||
|
|
||||||
|
var isIOS = /iP(?:ad|hone|od)/.test( ua );
|
||||||
|
var isMac = /Mac OS X/.test( ua );
|
||||||
|
|
||||||
var isGecko = /Gecko\//.test( ua );
|
var isGecko = /Gecko\//.test( ua );
|
||||||
var isIE = /Trident\//.test( ua );
|
var isIE = /Trident\//.test( ua );
|
||||||
var isIE8 = ( win.ie === 8 );
|
var isIE8 = ( win.ie === 8 );
|
||||||
var isIOS = /iP(?:ad|hone|od)/.test( ua );
|
|
||||||
var isOpera = !!win.opera;
|
var isOpera = !!win.opera;
|
||||||
var isWebKit = /WebKit\//.test( ua );
|
var isWebKit = /WebKit\//.test( ua );
|
||||||
|
|
||||||
|
var ctrlKey = isMac ? 'meta-' : 'ctrl-';
|
||||||
|
|
||||||
var useTextFixer = isIE || isOpera;
|
var useTextFixer = isIE || isOpera;
|
||||||
var cantFocusEmptyTextNodes = isIE || isWebKit;
|
var cantFocusEmptyTextNodes = isIE || isWebKit;
|
||||||
var losesSelectionOnBlur = isIE;
|
var losesSelectionOnBlur = isIE;
|
||||||
|
@ -1088,11 +1093,13 @@ RangePrototype.expandToBlockBoundaries = function () {
|
||||||
doc,
|
doc,
|
||||||
win,
|
win,
|
||||||
body,
|
body,
|
||||||
|
isIOS,
|
||||||
|
isMac,
|
||||||
isGecko,
|
isGecko,
|
||||||
isIE,
|
isIE,
|
||||||
isIE8,
|
isIE8,
|
||||||
isIOS,
|
|
||||||
isOpera,
|
isOpera,
|
||||||
|
ctrlKey,
|
||||||
useTextFixer,
|
useTextFixer,
|
||||||
cantFocusEmptyTextNodes,
|
cantFocusEmptyTextNodes,
|
||||||
losesSelectionOnBlur,
|
losesSelectionOnBlur,
|
||||||
|
@ -2547,6 +2554,8 @@ var keys = {
|
||||||
9: 'tab',
|
9: 'tab',
|
||||||
13: 'enter',
|
13: 'enter',
|
||||||
32: 'space',
|
32: 'space',
|
||||||
|
37: 'left',
|
||||||
|
39: 'right',
|
||||||
46: 'delete'
|
46: 'delete'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2883,19 +2892,33 @@ var keyHandlers = {
|
||||||
addLinks( range.startContainer );
|
addLinks( range.startContainer );
|
||||||
getRangeAndRemoveBookmark( range );
|
getRangeAndRemoveBookmark( range );
|
||||||
setSelection( range );
|
setSelection( range );
|
||||||
},
|
}
|
||||||
'ctrl-b': mapKeyToFormat( 'B' ),
|
|
||||||
'ctrl-i': mapKeyToFormat( 'I' ),
|
|
||||||
'ctrl-u': mapKeyToFormat( 'U' ),
|
|
||||||
'ctrl-y': mapKeyTo( redo ),
|
|
||||||
'ctrl-z': mapKeyTo( undo ),
|
|
||||||
'ctrl-shift-z': mapKeyTo( redo )
|
|
||||||
};
|
};
|
||||||
|
// Firefox incorrectly handles Cmd-left/Cmd-right on Mac:
|
||||||
|
// it goes back/forward in history! Override to do the right
|
||||||
|
// thing.
|
||||||
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=289384
|
||||||
|
if ( isMac && isGecko && sel.modify ) {
|
||||||
|
keyHandlers[ 'meta-left' ] = function ( event ) {
|
||||||
|
event.preventDefault();
|
||||||
|
sel.modify( 'move', 'backward', 'lineboundary' );
|
||||||
|
};
|
||||||
|
keyHandlers[ 'meta-right' ] = function ( event ) {
|
||||||
|
event.preventDefault();
|
||||||
|
sel.modify( 'move', 'forward', 'lineboundary' );
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
keyHandlers[ ctrlKey + 'b' ] = mapKeyToFormat( 'B' );
|
||||||
|
keyHandlers[ ctrlKey + 'i' ] = mapKeyToFormat( 'I' );
|
||||||
|
keyHandlers[ ctrlKey + 'u' ] = mapKeyToFormat( 'U' );
|
||||||
|
keyHandlers[ ctrlKey + 'y' ] = mapKeyTo( redo );
|
||||||
|
keyHandlers[ ctrlKey + 'z' ] = mapKeyTo( undo );
|
||||||
|
keyHandlers[ ctrlKey + 'shift-z' ] = mapKeyTo( redo );
|
||||||
|
|
||||||
// Ref: http://unixpapa.com/js/key.html
|
// Ref: http://unixpapa.com/js/key.html
|
||||||
// Opera does not fire keydown repeatedly.
|
// Opera does not fire keydown repeatedly.
|
||||||
addEventListener( isOpera ? 'keypress' : 'keydown',
|
addEventListener( isOpera ? 'keypress' : 'keydown', function ( event ) {
|
||||||
function ( event ) {
|
|
||||||
var code = event.keyCode,
|
var code = event.keyCode,
|
||||||
key = keys[ code ] || String.fromCharCode( code ).toLowerCase(),
|
key = keys[ code ] || String.fromCharCode( code ).toLowerCase(),
|
||||||
modifiers = '';
|
modifiers = '';
|
||||||
|
@ -2912,7 +2935,8 @@ addEventListener( isOpera ? 'keypress' : 'keydown',
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( event.altKey ) { modifiers += 'alt-'; }
|
if ( event.altKey ) { modifiers += 'alt-'; }
|
||||||
if ( event.ctrlKey || event.metaKey ) { modifiers += 'ctrl-'; }
|
if ( event.ctrlKey ) { modifiers += 'ctrl-'; }
|
||||||
|
if ( event.metaKey ) { modifiers += 'meta-'; }
|
||||||
if ( event.shiftKey ) { modifiers += 'shift-'; }
|
if ( event.shiftKey ) { modifiers += 'shift-'; }
|
||||||
|
|
||||||
key = modifiers + key;
|
key = modifiers + key;
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -17,13 +17,18 @@ var win = doc.defaultView;
|
||||||
var body = doc.body;
|
var body = doc.body;
|
||||||
|
|
||||||
var ua = navigator.userAgent;
|
var ua = navigator.userAgent;
|
||||||
|
|
||||||
|
var isIOS = /iP(?:ad|hone|od)/.test( ua );
|
||||||
|
var isMac = /Mac OS X/.test( ua );
|
||||||
|
|
||||||
var isGecko = /Gecko\//.test( ua );
|
var isGecko = /Gecko\//.test( ua );
|
||||||
var isIE = /Trident\//.test( ua );
|
var isIE = /Trident\//.test( ua );
|
||||||
var isIE8 = ( win.ie === 8 );
|
var isIE8 = ( win.ie === 8 );
|
||||||
var isIOS = /iP(?:ad|hone|od)/.test( ua );
|
|
||||||
var isOpera = !!win.opera;
|
var isOpera = !!win.opera;
|
||||||
var isWebKit = /WebKit\//.test( ua );
|
var isWebKit = /WebKit\//.test( ua );
|
||||||
|
|
||||||
|
var ctrlKey = isMac ? 'meta-' : 'ctrl-';
|
||||||
|
|
||||||
var useTextFixer = isIE || isOpera;
|
var useTextFixer = isIE || isOpera;
|
||||||
var cantFocusEmptyTextNodes = isIE || isWebKit;
|
var cantFocusEmptyTextNodes = isIE || isWebKit;
|
||||||
var losesSelectionOnBlur = isIE;
|
var losesSelectionOnBlur = isIE;
|
||||||
|
|
|
@ -9,11 +9,13 @@
|
||||||
doc,
|
doc,
|
||||||
win,
|
win,
|
||||||
body,
|
body,
|
||||||
|
isIOS,
|
||||||
|
isMac,
|
||||||
isGecko,
|
isGecko,
|
||||||
isIE,
|
isIE,
|
||||||
isIE8,
|
isIE8,
|
||||||
isIOS,
|
|
||||||
isOpera,
|
isOpera,
|
||||||
|
ctrlKey,
|
||||||
useTextFixer,
|
useTextFixer,
|
||||||
cantFocusEmptyTextNodes,
|
cantFocusEmptyTextNodes,
|
||||||
losesSelectionOnBlur,
|
losesSelectionOnBlur,
|
||||||
|
@ -1468,6 +1470,8 @@ var keys = {
|
||||||
9: 'tab',
|
9: 'tab',
|
||||||
13: 'enter',
|
13: 'enter',
|
||||||
32: 'space',
|
32: 'space',
|
||||||
|
37: 'left',
|
||||||
|
39: 'right',
|
||||||
46: 'delete'
|
46: 'delete'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1804,19 +1808,33 @@ var keyHandlers = {
|
||||||
addLinks( range.startContainer );
|
addLinks( range.startContainer );
|
||||||
getRangeAndRemoveBookmark( range );
|
getRangeAndRemoveBookmark( range );
|
||||||
setSelection( range );
|
setSelection( range );
|
||||||
},
|
}
|
||||||
'ctrl-b': mapKeyToFormat( 'B' ),
|
|
||||||
'ctrl-i': mapKeyToFormat( 'I' ),
|
|
||||||
'ctrl-u': mapKeyToFormat( 'U' ),
|
|
||||||
'ctrl-y': mapKeyTo( redo ),
|
|
||||||
'ctrl-z': mapKeyTo( undo ),
|
|
||||||
'ctrl-shift-z': mapKeyTo( redo )
|
|
||||||
};
|
};
|
||||||
|
// Firefox incorrectly handles Cmd-left/Cmd-right on Mac:
|
||||||
|
// it goes back/forward in history! Override to do the right
|
||||||
|
// thing.
|
||||||
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=289384
|
||||||
|
if ( isMac && isGecko && sel.modify ) {
|
||||||
|
keyHandlers[ 'meta-left' ] = function ( event ) {
|
||||||
|
event.preventDefault();
|
||||||
|
sel.modify( 'move', 'backward', 'lineboundary' );
|
||||||
|
};
|
||||||
|
keyHandlers[ 'meta-right' ] = function ( event ) {
|
||||||
|
event.preventDefault();
|
||||||
|
sel.modify( 'move', 'forward', 'lineboundary' );
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
keyHandlers[ ctrlKey + 'b' ] = mapKeyToFormat( 'B' );
|
||||||
|
keyHandlers[ ctrlKey + 'i' ] = mapKeyToFormat( 'I' );
|
||||||
|
keyHandlers[ ctrlKey + 'u' ] = mapKeyToFormat( 'U' );
|
||||||
|
keyHandlers[ ctrlKey + 'y' ] = mapKeyTo( redo );
|
||||||
|
keyHandlers[ ctrlKey + 'z' ] = mapKeyTo( undo );
|
||||||
|
keyHandlers[ ctrlKey + 'shift-z' ] = mapKeyTo( redo );
|
||||||
|
|
||||||
// Ref: http://unixpapa.com/js/key.html
|
// Ref: http://unixpapa.com/js/key.html
|
||||||
// Opera does not fire keydown repeatedly.
|
// Opera does not fire keydown repeatedly.
|
||||||
addEventListener( isOpera ? 'keypress' : 'keydown',
|
addEventListener( isOpera ? 'keypress' : 'keydown', function ( event ) {
|
||||||
function ( event ) {
|
|
||||||
var code = event.keyCode,
|
var code = event.keyCode,
|
||||||
key = keys[ code ] || String.fromCharCode( code ).toLowerCase(),
|
key = keys[ code ] || String.fromCharCode( code ).toLowerCase(),
|
||||||
modifiers = '';
|
modifiers = '';
|
||||||
|
@ -1833,7 +1851,8 @@ addEventListener( isOpera ? 'keypress' : 'keydown',
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( event.altKey ) { modifiers += 'alt-'; }
|
if ( event.altKey ) { modifiers += 'alt-'; }
|
||||||
if ( event.ctrlKey || event.metaKey ) { modifiers += 'ctrl-'; }
|
if ( event.ctrlKey ) { modifiers += 'ctrl-'; }
|
||||||
|
if ( event.metaKey ) { modifiers += 'meta-'; }
|
||||||
if ( event.shiftKey ) { modifiers += 'shift-'; }
|
if ( event.shiftKey ) { modifiers += 'shift-'; }
|
||||||
|
|
||||||
key = modifiers + key;
|
key = modifiers + key;
|
||||||
|
|
Loading…
Reference in a new issue