diff --git a/src/views/explore.ts b/src/views/explore.ts index a8ee0cc..3dd0902 100644 --- a/src/views/explore.ts +++ b/src/views/explore.ts @@ -41,6 +41,6 @@ export function explorePage( `, - options, + { ...options, enableSearchShortcut: true }, ); } diff --git a/src/views/home.ts b/src/views/home.ts index 177e596..d1b29de 100644 --- a/src/views/home.ts +++ b/src/views/home.ts @@ -47,6 +47,6 @@ export function homePage( `, - options, + { ...options, enableSearchShortcut: true }, ); } diff --git a/src/views/layout.ts b/src/views/layout.ts index fa2d0d2..c067a64 100644 --- a/src/views/layout.ts +++ b/src/views/layout.ts @@ -31,6 +31,7 @@ export interface LayoutOptions { shareHtml?: string; ogTitle?: string; ogUrl?: string; + enableSearchShortcut?: boolean; } function renderSearchButton(locale: Locale = "en"): string { @@ -207,13 +208,14 @@ ${extraScripts}
- + +
@@ -227,22 +229,20 @@ ${extraScripts} if (el && !el.contains(e.target)) document.getElementById(menuId)?.classList.add('hidden'); } }); - document.addEventListener('keydown', (e) => { + ${ + options?.enableSearchShortcut + ? `document.addEventListener('keydown', (e) => { const tag = document.activeElement?.tagName; - const inInput = tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT'; + if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return; + const isSearchShortcut = (e.key === '/' && !e.ctrlKey && !e.metaKey) || ((e.ctrlKey || e.metaKey) && e.key === 'k'); + if (!isSearchShortcut) return; + e.preventDefault(); const inlineSearch = document.getElementById('wiki-search'); - if ((e.ctrlKey || e.metaKey) && e.key === 'k') { - e.preventDefault(); - if (inlineSearch) { inlineSearch.focus(); inlineSearch.select(); } - else { const d = document.getElementById('search-modal'); if (d.open) { d.close(); } else { d.showModal(); document.getElementById('search-input').focus(); } } - } - if (e.key === '/' && !inInput && !e.ctrlKey && !e.metaKey) { - e.preventDefault(); - if (inlineSearch) { inlineSearch.focus(); inlineSearch.select(); } - else { document.getElementById('search-modal').showModal(); document.getElementById('search-input').focus(); } - } - if (e.key === 'Escape') { document.getElementById('search-modal')?.close(); } - }); + if (inlineSearch) { inlineSearch.focus(); inlineSearch.select(); } + else { document.getElementById('search-modal').showModal(); document.getElementById('search-input').focus(); } + });` + : "" + } document.getElementById('search-results')?.addEventListener('click', (e) => { const link = e.target.closest('a'); if (link) document.getElementById('search-modal').close(); diff --git a/src/views/note.ts b/src/views/note.ts index 56cd4df..e6c671c 100644 --- a/src/views/note.ts +++ b/src/views/note.ts @@ -16,6 +16,12 @@ export function notePage( ${renderedHtml} `, - { ...options, wikiName, wikiSlug, pageTitle: noteTitle }, + { + ...options, + wikiName, + wikiSlug, + pageTitle: noteTitle, + enableSearchShortcut: true, + }, ); } diff --git a/src/views/wiki.ts b/src/views/wiki.ts index 804d4a8..2b3e686 100644 --- a/src/views/wiki.ts +++ b/src/views/wiki.ts @@ -43,6 +43,6 @@ export function wikiPage( ${noteList || `
  • ${msg.wiki.noNotesYet}
  • `} `, - { ...options, wikiName, wikiSlug }, + { ...options, wikiName, wikiSlug, enableSearchShortcut: true }, ); }