import { Editor, FileHelpers, TLClipboardWriteInfo, TLExternalContentSource, VecLike, activeElementShouldCaptureKeys, assert, compact, getGlobalDocument, isDefined, preventDefault, uniq, useEditor, useMaybeEditor, useValue, } from '@tldraw/editor' import lz from 'lz-string' import { useCallback, useEffect } from 'react' import { defaultHandleExternalTextContent } from '../../defaultExternalContentHandlers' import { TLDRAW_CUSTOM_PNG_MIME_TYPE, getCanonicalClipboardReadType } from '../../utils/clipboard' import { TLUiEventSource, useUiEvents } from '../context/events' import { pasteFiles } from './clipboard/pasteFiles' import { pasteUrl } from './clipboard/pasteUrl' /** * Resolves paste modifier keys into plain-text and position behavior. * Alt/Option inverts the paste-at-cursor user preference. * * @param isShift - Whether the Shift key is pressed (indicates plain text paste) * @param isAlt - Whether the Alt/Option key is pressed (inverts paste position preference) * @param pasteAtCursorPref - The user's preference for pasting at the cursor (true) or center (false) * * @internal */ export function resolvePasteModifiers( isShift: boolean, isAlt: boolean, pasteAtCursorPref: boolean ) { return { isPlainText: isShift, pasteAtCursor: isAlt ? !pasteAtCursorPref : pasteAtCursorPref, } } // Expected paste mime types. The earlier in this array they appear, the higher preference we give // them. For example, we prefer the `web image/png+tldraw` type to plain `image/png` as it does not // strip some of the extra metadata we write into it. const expectedPasteFileMimeTypes = [ TLDRAW_CUSTOM_PNG_MIME_TYPE, 'image/png', 'image/jpeg', 'image/webp', 'image/svg+xml', ] satisfies string[] /** * Strip HTML tags from a string. * @param html - The HTML to strip. * @internal */ function stripHtml(html: string) { // See const doc = getGlobalDocument().implementation.createHTMLDocument('') doc.documentElement.innerHTML = html.trim() return doc.body.textContent || doc.body.innerText || '' } /** * Extract iframe src and dimensions from an HTML string containing an iframe element. * Tries width/height HTML attributes first, then falls back to pixel values in the * style attribute, then to sensible defaults. * Returns null if no valid iframe is found. * @internal */ export function extractIframeFromHtml( html: string ): { src: string; width: number; height: number } | null { if (!html.includes(' { try { const u = new URL(url) return u.protocol === 'http:' || u.protocol === 'https:' } catch { return false } } /** @public */ const getValidHttpURLList = (url: string) => { const urls = url.split(/[\n\s]/) for (const url of urls) { try { const u = new URL(url) if (!(u.protocol === 'http:' || u.protocol === 'https:')) { return } } catch { return } } return uniq(urls) } /** @public */ const isSvgText = (text: string) => { return /^ { const validUrlList = getValidHttpURLList(data) if (validUrlList) { for (const url of validUrlList) { pasteUrl(editor, url, point, sources, clipboardPasteSource) } } else if (isValidHttpURL(data)) { pasteUrl(editor, data, point, sources, clipboardPasteSource) } else if (isSvgText(data)) { editor.markHistoryStoppingPoint('paste') putPastedExternalContent( editor, { type: 'svg-text', text: data, point, sources, }, { source: clipboardPasteSource, point } ) } else { editor.markHistoryStoppingPoint('paste') putPastedExternalContent( editor, { type: 'text', text: data, point, sources, }, { source: clipboardPasteSource, point } ) } } /** * Something found on the clipboard, either through the event's clipboard data or the browser's clipboard API. * @internal */ type ClipboardThing = | { type: 'file' source: Promise } | { type: 'blob' source: Promise } | { type: 'url' source: Promise } | { type: 'html' source: Promise } | { type: 'text' source: Promise } | { type: string source: Promise } /** * Handle a paste using event clipboard data. This is the "original" * paste method that uses the clipboard data from the paste event. * https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent/clipboardData * * @param editor - The editor * @param clipboardData - The clipboard data * @param point - The point to paste at * @internal */ const handlePasteFromEventClipboardData = async ( editor: Editor, clipboardData: DataTransfer, point?: VecLike ) => { // Do not paste while in any editing state if (editor.getEditingShapeId() !== null) return if (!clipboardData) { throw Error('No clipboard data') } const things: ClipboardThing[] = [] for (const item of Object.values(clipboardData.items)) { switch (item.kind) { case 'file': { // files are always blobs things.push({ type: 'file', source: new Promise((r) => r(item.getAsFile())) as Promise, }) break } case 'string': { // strings can be text or html if (item.type === 'text/html') { things.push({ type: 'html', source: new Promise((r) => item.getAsString(r)) as Promise, }) } else if (item.type === 'text/plain') { things.push({ type: 'text', source: new Promise((r) => item.getAsString(r)) as Promise, }) } else { things.push({ type: item.type, source: new Promise((r) => item.getAsString(r)) }) } break } } } handleClipboardThings(editor, things, point, 'native-event') } /** * Handle a paste using items retrieved from the Clipboard API. * https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem * * @param editor - The editor * @param clipboardItems - The clipboard items to handle * @param point - The point to paste at * @internal */ const handlePasteFromClipboardApi = async ({ editor, clipboardItems, point, fallbackFiles, clipboardPasteSource, }: { editor: Editor clipboardItems: ClipboardItem[] point?: VecLike fallbackFiles?: File[] clipboardPasteSource: 'native-event' | 'clipboard-read' }) => { // We need to populate the array of clipboard things // based on the ClipboardItems from the Clipboard API. // This is done in a different way than when using // the clipboard data from the paste event. const things: ClipboardThing[] = [] for (const item of clipboardItems) { const matchingTypes = expectedPasteFileMimeTypes.filter((t) => item.types.includes(t)) if (matchingTypes.length > 0) { things.push({ type: 'blob', source: (async () => { for (const type of matchingTypes) { const blob = await item.getType(type) // Chrome 147 stable regression: web custom-format blobs come back as // 0 bytes when clipboard.read() runs inside a paste event. Fixed in // Chrome Canary; expected to ship to stable in a later release. Until // then, skip empty payloads and fall back to the next preferred type // (usually image/png, which means Cmd+V paste of a tldraw-copied PNG // loses the pHYs DPI chunk and pastes at 2x size on affected Chrome // stable versions). Right-click Paste continues to work at 1x because // that path calls clipboard.read() from a click handler, not a paste // event. Remove this workaround when the fix ships to stable. // https://issues.chromium.org/issues/505045934 if (blob.size === 0) continue return FileHelpers.rewriteMimeType(blob, getCanonicalClipboardReadType(type)) } return null })(), }) } if (item.types.includes('text/html')) { things.push({ type: 'html', source: (async () => { const blob = await item.getType('text/html') return await FileHelpers.blobToText(blob) })(), }) } if (item.types.includes('text/uri-list')) { things.push({ type: 'url', source: (async () => { const blob = await item.getType('text/uri-list') return await FileHelpers.blobToText(blob) })(), }) } if (item.types.includes('text/plain')) { things.push({ type: 'text', source: (async () => { const blob = await item.getType('text/plain') return await FileHelpers.blobToText(blob) })(), }) } } if (fallbackFiles?.length && things.length === 1 && things[0].type === 'text') { things.pop() things.push( ...fallbackFiles.map((f): ClipboardThing => ({ type: 'file', source: Promise.resolve(f) })) ) } else if (fallbackFiles?.length && things.length === 0) { // Files pasted in Safari from your computer don't have types, so we need to use the fallback files directly // if they're available. This only works if pasted keyboard shortcuts. Pasting from the menu in Safari seems to never // let you access files that are copied from your computer. things.push( ...fallbackFiles.map((f): ClipboardThing => ({ type: 'file', source: Promise.resolve(f) })) ) } return await handleClipboardThings(editor, things, point, clipboardPasteSource) } async function handleClipboardThings( editor: Editor, things: ClipboardThing[], point: VecLike | undefined, clipboardPasteSource: 'native-event' | 'clipboard-read' ) { // 1. Handle files // // We need to handle files separately because if we want them to // be placed next to each other, we need to create them all at once. const files = things.filter( (t) => (t.type === 'file' || t.type === 'blob') && t.source !== null ) as Extract[] // Just paste the files, nothing else if (files.length) { if (files.length > editor.options.maxFilesAtOnce) { throw Error('Too many files') } const fileBlobs = compact(await Promise.all(files.map((t) => t.source))) return await pasteFiles(editor, fileBlobs, point, undefined, clipboardPasteSource) } // 2. Generate clipboard results for non-file things // // Getting the source from the items is async, however they must be accessed syncronously; // we can't await them in a loop. So we'll map them to promises and await them all at once, // then make decisions based on what we find. const results = await Promise.all( things .filter((t) => t.type !== 'file') .map( (t) => new Promise((r) => { const thing = t as Exclude if (thing.type === 'file') { r({ type: 'error', data: null, reason: 'unexpected file' }) return } thing.source.then((text) => { // first, see if we can find tldraw content, which is JSON inside of an html comment const tldrawHtmlComment = text.match(/
]*>(.*)<\/div>/)?.[1] if (tldrawHtmlComment) { try { // First try parsing as plain JSON (version 2/3 formats) let json try { json = JSON.parse(tldrawHtmlComment) } catch { // Fall back to LZ decompression (legacy format) const jsonComment = lz.decompressFromBase64(tldrawHtmlComment) if (jsonComment === null) { r({ type: 'error', data: null, reason: `found tldraw data comment but could not parse`, }) return } json = JSON.parse(jsonComment) } if (json.type !== 'application/tldraw') { r({ type: 'error', data: json, reason: `found tldraw data comment but JSON was of a different type: ${json.type}`, }) return } // Handle versioned clipboard format if (json.version === 3) { // Version 3: Assets are plain, decompress only other data try { const otherData = JSON.parse( lz.decompressFromBase64(json.data.otherCompressed) || '{}' ) const reconstructedData = { assets: json.data.assets || [], ...otherData, } r({ type: 'tldraw', data: reconstructedData }) return } catch (error) { r({ type: 'error', data: json, reason: `failed to decompress version 2 clipboard data: ${error}`, }) return } } if (json.version === 2) { // Version 2: Everything is plain, this had issues with encoding... :-/ // TODO: nix this support after some time. r({ type: 'tldraw', data: json.data }) } else { // Version 1 or no version: Legacy format if (typeof json.data === 'string') { r({ type: 'error', data: json, reason: 'found tldraw json but data was a string instead of a TLClipboardModel object', }) return } r({ type: 'tldraw', data: json.data }) return } } catch { r({ type: 'error', data: tldrawHtmlComment, reason: 'found tldraw json but data was a string instead of a TLClipboardModel object', }) return } } else { if (thing.type === 'html') { r({ type: 'text', data: text, subtype: 'html' }) return } if (thing.type === 'url') { r({ type: 'text', data: text, subtype: 'url' }) return } // if we have not found a tldraw comment, Otherwise, try to parse the text as JSON directly. try { const json = JSON.parse(text) if (json.type === 'excalidraw/clipboard') { // If the clipboard contains content copied from excalidraw, then paste that r({ type: 'excalidraw', data: json }) return } else { r({ type: 'text', data: text, subtype: 'json' }) return } } catch { // If we could not parse the text as JSON, then it's just text r({ type: 'text', data: text, subtype: 'text' }) return } } r({ type: 'error', data: text, reason: 'unhandled case' }) }) }) ) ) // 3. // // Now that we know what kind of stuff we're dealing with, we can actual create some content. // There are priorities here, so order matters: we've already handled images and files, which // take first priority; then we want to handle tldraw content, then excalidraw content, then // html content, then links, and finally text content. // Try to paste tldraw content for (const result of results) { if (result.type === 'tldraw') { editor.markHistoryStoppingPoint('paste') putPastedExternalContent( editor, { type: 'tldraw', content: result.data, point }, { source: clipboardPasteSource, point } ) return } } // Try to paste excalidraw content for (const result of results) { if (result.type === 'excalidraw') { editor.markHistoryStoppingPoint('paste') putPastedExternalContent( editor, { type: 'excalidraw', content: result.data, point }, { source: clipboardPasteSource, point } ) return } } // Try to paste html content for (const result of results) { if (result.type === 'text' && result.subtype === 'html') { const rootNode = new DOMParser().parseFromString(result.data, 'text/html') const bodyNode = rootNode.querySelector('body') // Check for iframe embeds in HTML before stripping content const iframeInfo = extractIframeFromHtml(result.data) if (iframeInfo) { editor.markHistoryStoppingPoint('paste') editor.putExternalContent({ type: 'embed', url: iframeInfo.src, point, embed: { width: iframeInfo.width, height: iframeInfo.height, doesResize: true, }, }) return } // try to find a link // Edge on Windows 11 home appears to paste a link as a single in // the HTML document. If we're pasting a single like tag we'll just // assume the user meant to paste the URL. const isHtmlSingleLink = bodyNode && Array.from(bodyNode.children).filter((el) => el.nodeType === 1).length === 1 && bodyNode.firstElementChild && bodyNode.firstElementChild.tagName === 'A' && bodyNode.firstElementChild.hasAttribute('href') && bodyNode.firstElementChild.getAttribute('href') !== '' if (isHtmlSingleLink) { const href = bodyNode.firstElementChild.getAttribute('href')! handleText(editor, href, point, results, clipboardPasteSource) return } // If the html is NOT a link, and we have NO OTHER texty content, then paste the html as text if (!results.some((r) => r.type === 'text' && r.subtype !== 'html') && result.data.trim()) { const html = stripHtml(result.data) ?? '' if (html) { handleText(editor, stripHtml(result.data), point, results, clipboardPasteSource) return } } // If the html is NOT a link, and we have other texty content, then paste the html as a text shape if (results.some((r) => r.type === 'text' && r.subtype !== 'html')) { const html = stripHtml(result.data) ?? '' if (html) { editor.markHistoryStoppingPoint('paste') putPastedExternalContent( editor, { type: 'text', text: html, html: result.data, point, sources: results, }, { source: clipboardPasteSource, point } ) return } } } // Allow pasting any