Make the share event work on any answer

...not just the initially loaded set
This commit is contained in:
Andreas Nedbal 2021-12-26 23:01:16 +01:00
parent 08a0f2e1ca
commit 21c4e0662f
2 changed files with 9 additions and 16 deletions

View File

@ -1,17 +1,10 @@
import registerEvents from 'utilities/registerEvents'; import { on } from 'retrospring/utilities/on';
import { createShareEvent } from './share'; import { shareEventHandler } from './share';
export default (): void => { export default (): void => {
if ('share' in navigator) { if ('share' in navigator) {
document.body.classList.add('cap-web-share'); document.body.classList.add('cap-web-share');
const entries: NodeList = document.querySelectorAll('.answerbox:not(.js-initialized)');
entries.forEach((element: HTMLElement) => { on('click', '[name=ab-share]', shareEventHandler);
registerEvents([
{ type: 'click', target: element.querySelector('[name=ab-share]'), handler: createShareEvent(element) }
]);
element.classList.add('js-initialized');
});
} }
} }

View File

@ -1,11 +1,11 @@
import noop from 'utilities/noop'; import noop from 'utilities/noop';
export function createShareEvent(answerbox: HTMLElement): () => void { export function shareEventHandler(event: Event): void {
return function (): void { const answerbox = (event.target as HTMLElement).closest('.answerbox');
navigator.share({ navigator.share({
url: answerbox.querySelector<HTMLAnchorElement>('.answerbox__answer-date > a, a.answerbox__permalink').href url: answerbox.querySelector<HTMLAnchorElement>('.answerbox__answer-date > a, a.answerbox__permalink').href
}) })
.then(noop) .then(noop)
.catch(noop) .catch(noop)
}
} }