Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions ui/analyse/src/study/studyShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ export function ctrl(
};
}

async function writePgnClipboard(url: string): Promise<void> {
// Firefox does not support `ClipboardItem`
if (typeof ClipboardItem === 'undefined') {
const pgn = await xhrText(url);
return navigator.clipboard.writeText(pgn);
} else {
const clipboardItem = new ClipboardItem({
'text/plain': xhrText(url).then(pgn => new Blob([pgn])),
});
return navigator.clipboard.write([clipboardItem]);
}
}

export function view(ctrl: StudyShareCtrl): VNode {
const studyId = ctrl.studyId,
chapter = ctrl.chapter();
Expand Down Expand Up @@ -150,10 +163,14 @@ export function view(ctrl: StudyShareCtrl): VNode {
title: ctrl.trans.noarg('copyChapterPgnDescription'),
},
hook: bind('click', async event => {
const pgn = await xhrText(`/study/${studyId}/${ctrl.chapter().id}.pgn`);
await navigator.clipboard.writeText(pgn);
(event.target as HTMLElement).setAttribute('data-icon', '');
setTimeout(() => (event.target as HTMLElement).setAttribute('data-icon', ''), 1000);
const iconFeedback = (success: boolean) => {
(event.target as HTMLElement).setAttribute('data-icon', success ? '' : '');
setTimeout(() => (event.target as HTMLElement).setAttribute('data-icon', ''), 1000);
};
writePgnClipboard(`/study/${studyId}/${ctrl.chapter().id}.pgn`).then(
() => iconFeedback(true),
() => iconFeedback(false)
);
}),
},
ctrl.trans.noarg('copyChapterPgn')
Expand Down