Skip to content

Commit

Permalink
Fixed #126
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Oct 15, 2024
1 parent f0d339e commit c5cab05
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions packages/gem-book/src/plugins/docsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const {
customElement,
attribute,
createRef,
addListener,
property,
createCSSSheet,
css,
Expand Down Expand Up @@ -302,21 +301,35 @@ class _GbpDocsearchElement extends GemBookPluginElement {
});
};

#onClick = (evt: Event) => {
const target = evt.target as Element;
if (!target.closest('.DocSearch')) return;
const link = target.closest('a');
if (
!link ||
link.target === '_blank' ||
(link.origin !== location.origin && !location.origin.includes('localhost'))
)
return;
evt.preventDefault();
this.#navigator(link.href);
};

// https://github.com/algolia/docsearch/pull/2212
#keyDown = (event: KeyboardEvent) => {
const element = event.composedPath()[0] as HTMLElement;
const tagName = element.tagName;
if (element.isContentEditable || tagName === 'INPUT' || tagName === 'SELECT' || tagName === 'TEXTAREA') {
event.stopPropagation();
}
};

@mounted()
#init = () => {
return addListener(window, 'click', (evt) => {
const target = evt.target as Element;
if (!target.closest('.DocSearch')) return;
const link = target.closest('a');
if (
!link ||
link.target === '_blank' ||
(link.origin !== location.origin && !location.origin.includes('localhost'))
)
return;
evt.preventDefault();
this.#navigator(link.href);
});
const controller = new AbortController();
addEventListener('click', this.#onClick, { signal: controller.signal });
addEventListener('keydown', this.#keyDown, { signal: controller.signal, capture: true });
return () => controller.abort();
};
}

Expand Down

0 comments on commit c5cab05

Please sign in to comment.