🐛 Fixed pasting into the post tags input not working (#1739)

closes https://github.com/tryghost/ghost/issues/12294
refs https://github.com/TryGhost/Admin/pull/1707

- cache registered shortcuts and check KeyboardEvent before dispatching event to the root
This commit is contained in:
Kukhyeon Heo 2022-08-03 19:14:12 +09:00 committed by GitHub
parent 08b31ebadb
commit 3780e80b14
3 changed files with 54 additions and 4 deletions

View File

@ -1,6 +1,7 @@
/* global key */
import Component from '@glimmer/component';
import Ember from 'ember';
import shortcuts from '../utils/shortcuts';
import {A, isArray} from '@ember/array';
import {action, get} from '@ember/object';
import {
@ -91,11 +92,14 @@ export default class GhTokenInput extends Component {
// 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.
// Only dispatch KeyboardEvent to the root when it's a registered shortcut.
if (shortcuts.includes(event)) {
const copy = new KeyboardEvent(event.type, event);
document.getElementsByClassName('gh-app')[0].dispatchEvent(copy);
event.preventDefault(); // don't show the save dialog.
return false;
return false;
}
}
// fallback to default

View File

@ -3,6 +3,8 @@ import Mixin from '@ember/object/mixin';
import {run} from '@ember/runloop';
import {typeOf} from '@ember/utils';
import shortcutsCache from '../utils/shortcuts';
// Configure KeyMaster to respond to all shortcuts,
// even inside of
// input, textarea, and select.
@ -58,6 +60,8 @@ export default Mixin.create({
action = action.action;
}
shortcutsCache.register(shortcut);
key(shortcut, scope, (event) => {
// stop things like ctrl+s from actually opening a save dialog
event.preventDefault();
@ -73,6 +77,7 @@ export default Mixin.create({
Object.keys(shortcuts).forEach((shortcut) => {
let scope = shortcuts[shortcut].scope || 'default';
shortcutsCache.unregister(shortcut);
key.unbind(shortcut, scope);
});
},

View File

@ -0,0 +1,41 @@
const cache = {};
export function includes(event) {
const keys = [];
let ctrlPressed = false;
if (event.ctrlKey) {
keys.push('ctrl');
ctrlPressed = true;
}
if (event.shiftKey) {
keys.push('shift');
}
if (event.altKey) {
keys.push('alt');
}
keys.push(event.key);
const exists = cache[keys.join('+')];
if (!exists && ctrlPressed) { // Test things like cmd+s
return cache[keys.join('+').replace('ctrl', 'cmd')];
}
return exists;
}
export function register(shortcut) {
cache[shortcut.toLowerCase()] = true;
}
export function unregister(shortcut) {
delete cache[shortcut];
}
export function getAll() {
return Object.assign({}, cache);
}