File tree Expand file tree Collapse file tree 1 file changed +9
-16
lines changed Expand file tree Collapse file tree 1 file changed +9
-16
lines changed Original file line number Diff line number Diff line change 1+ document . addEventListener ( 'copy' , ( e ) => {
2+ const selectedText = window . getSelection ( ) ?. toString ( ) ?? ''
3+ const cleanedText = selectedText ?. replace ( / \n + $ / , '\n' )
4+ e . clipboardData ?. setData ( 'text/plain' , cleanedText )
5+
6+ e . preventDefault ( )
7+ } )
8+
19export function copyToClip ( text : string ) {
2- return new Promise ( ( resolve , reject ) => {
3- try {
4- const input : HTMLTextAreaElement = document . createElement ( 'textarea' )
5- input . setAttribute ( 'readonly' , 'readonly' )
6- input . value = text
7- document . body . appendChild ( input )
8- input . select ( )
9- if ( document . execCommand ( 'copy' ) )
10- document . execCommand ( 'copy' )
11- document . body . removeChild ( input )
12- resolve ( text )
13- }
14- catch ( error ) {
15- reject ( error )
16- }
17- } )
10+ return navigator . clipboard . writeText ( text )
1811}
You can’t perform that action at this time.
0 commit comments