import { Notice } from 'obsidian' ; export function copyImageToClipboard ( svg ) { const canvas = createCanvas ( svg ) ; const img = generateImage ( svg , canvas , ( ) => { canvas . toBlob ( ( blob ) => { const item = new ClipboardItem ( { "image/png" : blob } ) ; navigator . clipboard . write ( [ item ] ) ; new Notice ( 'Screenshot copied to the clipboard.' ) ; } ) ; } ) ; } function createCanvas ( svg ) { const canvas = document . createElement ( "canvas" ) ; canvas . width = svg . clientWidth ; canvas . height = svg . clientHeight ; return canvas ; } function generateImage ( svg , canvas , callback ) { var ctx = canvas . getContext ( "2d" ) ; return drawInlineSVG ( ctx , svg , callback ) ; } function drawInlineSVG ( ctx , svg , callback ) { const xml = new XMLSerializer ( ) . serializeToString ( svg ) ; const svg64 = btoa ( unescape ( encodeURIComponent ( xml ) ) ) ; const b64Start = 'data:image/svg+xml;base64,' ; const image64 = b64Start + svg64 ; const img = new Image ( ) ; img . onload = function ( ) { ctx . drawImage ( img , 0 , 0 ) ; callback ( ) ; } ; img . src = image64 ; return img ; }