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

View File

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