Created
July 17, 2017 06:38
-
-
Save sandcastle/00aaa8a820061c899edf76c3ed3c8bac to your computer and use it in GitHub Desktop.
A copy to clipboard function (in typescript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const copyToClipboard = (url: string) => { | |
document.addEventListener('copy', (e: ClipboardEvent) => { | |
e.clipboardData.setData('text/plain', url); | |
e.preventDefault(); | |
document.removeEventListener('copy'); | |
}); | |
document.execCommand('copy'); | |
}; |
Should be:
document.removeEventListener('copy', this.e);
changed a little:
public copyToClipboard(data: string): void {
const listener = (e: ClipboardEvent) => {
e.clipboardData.setData('text/plain', data);
e.preventDefault();
document.removeEventListener('copy', listener);
};
document.addEventListener('copy', listener);
document.execCommand('copy');
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The function
removeEventListener
will pass TSLint if you pass it the correct amount of parameters:document.removeEventListener('copy', this.ClipboardEvent);