From 604cde263395b6e441d5efb2d2e2dd26b9aafadf Mon Sep 17 00:00:00 2001 From: Rishabh Date: Fri, 8 Jul 2022 10:47:36 +0200 Subject: [PATCH] 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 --- ghost/sodo-search/src/App.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ghost/sodo-search/src/App.js b/ghost/sodo-search/src/App.js index ea860f3564..9194a69098 100644 --- a/ghost/sodo-search/src/App.js +++ b/ghost/sodo-search/src/App.js @@ -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({