🐛 Fixed Ctrl+h and Ctrl+d breaking the editor when used on card boundaries in macOS

closes https://github.com/TryGhost/Ghost/issues/10240

- intercept known macOS content-modifying keyboard shortcuts and simulate the "normal" keyboard events that they map to
  - `ctrl+h`: `Backspace`
  - `ctrl+d`: `Delete`
This commit is contained in:
Kevin Ansfield 2020-03-07 21:33:12 +00:00
parent 98e0f66c59
commit 0df2a5da55

View File

@ -3,6 +3,7 @@
* https://github.com/bustle/ember-mobiledoc-editor
*/
import Browser from 'mobiledoc-kit/utils/browser';
import Component from '@ember/component';
import Editor from 'mobiledoc-kit/editor/editor';
import EmberObject, {computed, get} from '@ember/object';
@ -810,6 +811,10 @@ export default Component.extend({
// handlers manually instead.
//
// Does not work on Linux but it's easier to have keymaps without dead keys there
//
// Secondarily, we also use this handler to deal with known default key combos
// that perform actions like DELETE, BACKSPACE, etc which can break mobiledoc
// if not intercepted and handled like the "normal" key events
handleKeydown(event) {
let key = Key.fromEvent(event);
this._updateModifiersFromKey(key, {isDown: true});
@ -824,6 +829,32 @@ export default Component.extend({
if (event.key === '`' && event.code === 'Space') {
this._triggerTextHandlers();
}
// intercept and simulate keyboard events to be picked up by
// mobiledoc-kit's event manager
// https://github.com/TryGhost/Ghost/issues/10240
let {editor} = this;
if (Browser.isMac() && editor && editor.cursor && editor.cursor.isAddressable(event.target)) {
// ctrl+h = BACKSPACE
if (event.key === 'h' && event.ctrlKey) {
event.preventDefault();
let simEvent = new KeyboardEvent('keydown', {
key: 'Backspace',
keyCode: 8
});
event.target.dispatchEvent(simEvent);
}
// ctrl+d = DELETE
if (event.key === 'd' && event.ctrlKey) {
event.preventDefault();
let simEvent = new KeyboardEvent('keydown', {
key: 'Delete',
keyCode: 46
});
event.target.dispatchEvent(simEvent);
}
}
},
handleKeyup(event) {