Disabled keyboard shortcut for missing data attributes

refs https://github.com/TryGhost/Team/issues/1665

- in case no data-ghost-search attributes on a site, the keyboard shortcut to enable search is disabled
- allows site owners to completely disable search on their site
This commit is contained in:
Rishabh 2022-07-08 10:47:36 +02:00
parent f722cab8b5
commit 604cde2633

View File

@ -73,14 +73,19 @@ export default class App extends React.Component {
showPopup: true
});
};
const customTriggerSelector = '[data-ghost-search]';
this.customTriggerButtons = document.querySelectorAll(customTriggerSelector) || [];
this.customTriggerButtons = this.getCustomTriggerButtons();
this.customTriggerButtons.forEach((customTriggerButton) => {
customTriggerButton.removeEventListener('click', this.clickHandler);
customTriggerButton.addEventListener('click', this.clickHandler);
});
}
getCustomTriggerButtons() {
const customTriggerSelector = '[data-ghost-search]';
return document.querySelectorAll(customTriggerSelector) || [];
}
handleSearchUrl() {
const [path] = window.location.hash.substr(1).split('?');
if (path === '/search' || path === '/search/') {
@ -92,6 +97,10 @@ export default class App extends React.Component {
}
addKeyboardShortcuts() {
const customTriggerButtons = this.getCustomTriggerButtons();
if (!customTriggerButtons?.length) {
return;
}
this.handleKeyDown = (e) => {
if (e.key === 'k' && e.metaKey) {
this.setState({