Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
🐛 Fixed Ctrl/Cmd+S triggering browser save when tags or authors input…
Browse files Browse the repository at this point in the history
… has focus (#1707)

closes TryGhost/Ghost#11786

`GhTokenInput` uses `PowerSelect` component of `ember-power-select` internally in `app/components/gh-token-input/select-multiple`. 

When you open that component, [you can find](https://github.com/cibernox/ember-power-select/blob/d36f38f39eba6a0afad6fd23821d20c06a9a42ef/addon/components/power-select.ts#L262-L278) that it calls `stopImmediatePropagation` when ctrl/cmd or meta key is down.

```js
  handleTriggerKeydown(e: KeyboardEvent) {
    if (this.args.onKeydown && this.args.onKeydown(this.storedAPI, e) === false) {
      e.stopImmediatePropagation();
      return;
    }
    if (e.ctrlKey || e.metaKey) {
      e.stopImmediatePropagation();
      return;
    }
    if ((e.keyCode >= 48 && e.keyCode <= 90) || isNumpadKeyEvent(e)) { // Keys 0-9, a-z or numpad keys
      (this.triggerTypingTask as unknown as Performable).perform(e);
    } else if (e.keyCode === 32) {  // Space
      this._handleKeySpace(this.storedAPI, e);
    } else {
      return this._routeKeydown(this.storedAPI, e);
    }
  }
```

Because of that, I had to dispatch event directly to the root of the Ghost admin app.
  • Loading branch information
sainthkh committed Sep 21, 2020
1 parent e179638 commit d6bca3d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/components/gh-token-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ class GhTokenInput extends Component {
}
}

// https://github.com/TryGhost/Ghost/issues/11786
// ember-power-select stops propagation of events when ctrl/CMD or meta key is down.
// So, we're dispatching KeyboardEvent directly to the root of ghost app.
if (event.ctrlKey || event.metaKey) {
const copy = new KeyboardEvent(event.type, event);
document.getElementsByClassName('gh-app')[0].dispatchEvent(copy);
event.preventDefault(); // don't show the save dialog.

return false;
}

// fallback to default
return true;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/acceptance/editor-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Mirage from 'ember-cli-mirage';
import ctrlOrCmd from 'ghost-admin/utils/ctrl-or-cmd';
import moment from 'moment';
import sinon from 'sinon';
import {authenticateSession, invalidateSession} from 'ember-simple-auth/test-support';
Expand Down Expand Up @@ -829,5 +830,27 @@ describe('Acceptance: Editor', function () {
'facebook title not present after closing subview'
).to.equal(0);
});

// https://github.com/TryGhost/Ghost/issues/11786
it('save shortcut works when tags/authors field is focused', async function () {
let post = this.server.create('post', {authors: [author]});

await visit(`/editor/post/${post.id}`);
await fillIn('[data-test-editor-title-input]', 'CMD-S Test');

await click('[data-test-psm-trigger]');
await click('[data-test-token-input]');

await triggerEvent('[data-test-token-input]', 'keydown', {
keyCode: 83, // s
metaKey: ctrlOrCmd === 'command',
ctrlKey: ctrlOrCmd === 'ctrl'
});

// Check if save request has been sent correctly.
let [lastRequest] = this.server.pretender.handledRequests.slice(-1);
let body = JSON.parse(lastRequest.requestBody);
expect(body.posts[0].title).to.equal('CMD-S Test');
});
});
});

0 comments on commit d6bca3d

Please sign in to comment.