-
Notifications
You must be signed in to change notification settings - Fork 2k
/
useClipboardEvents.ts
698 lines (628 loc) · 18.5 KB
/
useClipboardEvents.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
import {
Editor,
FileHelpers,
TLArrowShape,
TLBookmarkShape,
TLEmbedShape,
TLExternalContentSource,
TLGeoShape,
TLTextShape,
VecLike,
isNonNull,
preventDefault,
stopEventPropagation,
uniq,
useEditor,
useValue,
} from '@tldraw/editor'
import lz from 'lz-string'
import { useCallback, useEffect } from 'react'
import { TLUiEventSource, useUiEvents } from '../context/events'
import { pasteExcalidrawContent } from './clipboard/pasteExcalidrawContent'
import { pasteFiles } from './clipboard/pasteFiles'
import { pasteTldrawContent } from './clipboard/pasteTldrawContent'
import { pasteUrl } from './clipboard/pasteUrl'
/**
* Strip HTML tags from a string.
* @param html - The HTML to strip.
* @internal
*/
function stripHtml(html: string) {
// See <https://github.com/developit/preact-markup/blob/4788b8d61b4e24f83688710746ee36e7464f7bbc/src/parse-markup.js#L60-L69>
const doc = document.implementation.createHTMLDocument('')
doc.documentElement.innerHTML = html.trim()
return doc.body.textContent || doc.body.innerText || ''
}
/** @public */
export const isValidHttpURL = (url: string) => {
try {
const u = new URL(url)
return u.protocol === 'http:' || u.protocol === 'https:'
} catch (e) {
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 (e) {
return
}
}
return uniq(urls)
}
/** @public */
const isSvgText = (text: string) => {
return /^<svg/.test(text)
}
const INPUTS = ['input', 'select', 'textarea']
/**
* Get whether to disallow clipboard events.
*
* @param editor - The editor instance.
* @internal
*/
function disallowClipboardEvents(editor: Editor) {
const { activeElement } = document
return (
editor.getIsMenuOpen() ||
(activeElement &&
(activeElement.getAttribute('contenteditable') ||
INPUTS.indexOf(activeElement.tagName.toLowerCase()) > -1))
)
}
/**
* Whether a ClipboardItem is a file.
* @param item - The ClipboardItem to check.
* @internal
*/
const isFile = (item: ClipboardItem) => {
return item.types.find((i) => i.match(/^image\//))
}
/**
* Handle text pasted into the editor.
* @param editor - The editor instance.
* @param data - The text to paste.
* @param point - The point at which to paste the text.
* @internal
*/
const handleText = (
editor: Editor,
data: string,
point?: VecLike,
sources?: TLExternalContentSource[]
) => {
const validUrlList = getValidHttpURLList(data)
if (validUrlList) {
for (const url of validUrlList) {
pasteUrl(editor, url, point)
}
} else if (isValidHttpURL(data)) {
pasteUrl(editor, data, point)
} else if (isSvgText(data)) {
editor.mark('paste')
editor.putExternalContent({
type: 'svg-text',
text: data,
point,
sources,
})
} else {
editor.mark('paste')
editor.putExternalContent({
type: 'text',
text: data,
point,
sources,
})
}
}
/**
* 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<File | null>
}
| {
type: 'blob'
source: Promise<Blob | null>
}
| {
type: 'url'
source: Promise<string>
}
| {
type: 'html'
source: Promise<string>
}
| {
type: 'text'
source: Promise<string>
}
| {
type: string
source: Promise<string>
}
/**
* 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<File | null>,
})
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<string>,
})
} else if (item.type === 'text/plain') {
things.push({
type: 'text',
source: new Promise((r) => item.getAsString(r)) as Promise<string>,
})
} else {
things.push({ type: item.type, source: new Promise((r) => item.getAsString(r)) })
}
break
}
}
}
handleClipboardThings(editor, things, point)
}
/**
* 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: Editor,
clipboardItems: ClipboardItem[],
point?: VecLike
) => {
// 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) {
if (isFile(item)) {
for (const type of item.types) {
if (type.match(/^image\//)) {
things.push({ type: 'blob', source: item.getType(type) })
}
}
}
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)
})(),
})
}
}
return await handleClipboardThings(editor, things, point)
}
async function handleClipboardThings(editor: Editor, things: ClipboardThing[], point?: VecLike) {
// 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<ClipboardThing, { type: 'file' } | { type: 'blob' }>[]
// Just paste the files, nothing else
if (files.length) {
const fileBlobs = await Promise.all(files.map((t) => t.source!))
const urls = (fileBlobs.filter(Boolean) as (File | Blob)[]).map((blob) =>
URL.createObjectURL(blob)
)
return await pasteFiles(editor, urls, point)
}
// 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<TLExternalContentSource>(
things
.filter((t) => t.type !== 'file')
.map(
(t) =>
new Promise((r) => {
const thing = t as Exclude<ClipboardThing, { type: 'file' } | { type: 'blob' }>
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(/<tldraw[^>]*>(.*)<\/tldraw>/)?.[1]
if (tldrawHtmlComment) {
try {
// If we've found tldraw content in the html string, use that as JSON
const jsonComment = lz.decompressFromBase64(tldrawHtmlComment)
if (jsonComment === null) {
r({
type: 'error',
data: jsonComment,
reason: `found tldraw data comment but could not parse base64`,
})
return
} else {
const 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}`,
})
}
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 (e: any) {
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 (e) {
// 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') {
pasteTldrawContent(editor, result.data, point)
return
}
}
// Try to paste excalidraw content
for (const result of results) {
if (result.type === 'excalidraw') {
pasteExcalidrawContent(editor, result.data, point)
return
}
}
// Try to paste html content
for (const result of results) {
if (result.type === 'text' && result.subtype === 'html') {
// try to find a link
const rootNode = new DOMParser().parseFromString(result.data, 'text/html')
const bodyNode = rootNode.querySelector('body')
// Edge on Windows 11 home appears to paste a link as a single <a/> 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)
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()) {
handleText(editor, stripHtml(result.data), point, results)
return
}
}
}
// Try to paste a link
for (const result of results) {
if (result.type === 'text' && result.subtype === 'url') {
pasteUrl(editor, result.data, point, results)
return
}
}
// Finally, if we haven't bailed on anything yet, we can paste text content
for (const result of results) {
if (result.type === 'text' && result.subtype === 'text' && result.data.trim()) {
// The clipboard may include multiple text items, but we only want to paste the first one
handleText(editor, result.data, point, results)
return
}
}
}
/**
* When the user copies, write the contents to local storage and to the clipboard
*
* @param editor - The editor instance.
* @public
*/
const handleNativeOrMenuCopy = (editor: Editor) => {
const content = editor.getContentFromCurrentPage(editor.getSelectedShapeIds())
if (!content) {
if (navigator && navigator.clipboard) {
navigator.clipboard.writeText('')
}
return
}
const stringifiedClipboard = lz.compressToBase64(
JSON.stringify({
type: 'application/tldraw',
kind: 'content',
data: content,
})
)
if (typeof navigator === 'undefined') {
return
} else {
// Extract the text from the clipboard
const textItems = content.shapes
.map((shape) => {
if (
editor.isShapeOfType<TLTextShape>(shape, 'text') ||
editor.isShapeOfType<TLGeoShape>(shape, 'geo') ||
editor.isShapeOfType<TLArrowShape>(shape, 'arrow')
) {
return shape.props.text
}
if (
editor.isShapeOfType<TLBookmarkShape>(shape, 'bookmark') ||
editor.isShapeOfType<TLEmbedShape>(shape, 'embed')
) {
return shape.props.url
}
return null
})
.filter(isNonNull)
if (navigator.clipboard?.write) {
const htmlBlob = new Blob([`<tldraw>${stringifiedClipboard}</tldraw>`], {
type: 'text/html',
})
let textContent = textItems.join(' ')
// This is a bug in chrome android where it won't paste content if
// the text/plain content is "" so we need to always add an empty
// space 🤬
if (textContent === '') {
textContent = ' '
}
navigator.clipboard.write([
new ClipboardItem({
'text/html': htmlBlob,
// What is this second blob used for?
'text/plain': new Blob([textContent], { type: 'text/plain' }),
}),
])
} else if (navigator.clipboard.writeText) {
navigator.clipboard.writeText(`<tldraw>${stringifiedClipboard}</tldraw>`)
}
}
}
/** @public */
export function useMenuClipboardEvents() {
const editor = useEditor()
const trackEvent = useUiEvents()
const copy = useCallback(
function onCopy(source: TLUiEventSource) {
if (editor.getSelectedShapeIds().length === 0) return
handleNativeOrMenuCopy(editor)
trackEvent('copy', { source })
},
[editor, trackEvent]
)
const cut = useCallback(
function onCut(source: TLUiEventSource) {
if (editor.getSelectedShapeIds().length === 0) return
handleNativeOrMenuCopy(editor)
editor.deleteShapes(editor.getSelectedShapeIds())
trackEvent('cut', { source })
},
[editor, trackEvent]
)
const paste = useCallback(
async function onPaste(
data: DataTransfer | ClipboardItem[],
source: TLUiEventSource,
point?: VecLike
) {
// If we're editing a shape, or we are focusing an editable input, then
// we would want the user's paste interaction to go to that element or
// input instead; e.g. when pasting text into a text shape's content
if (editor.getEditingShapeId() !== null) return
if (Array.isArray(data) && data[0] instanceof ClipboardItem) {
handlePasteFromClipboardApi(editor, data, point)
trackEvent('paste', { source: 'menu' })
} else {
// Read it first and then recurse, kind of weird
navigator.clipboard.read().then((clipboardItems) => {
paste(clipboardItems, source, point)
})
}
},
[editor, trackEvent]
)
return {
copy,
cut,
paste,
}
}
/** @public */
export function useNativeClipboardEvents() {
const editor = useEditor()
const trackEvent = useUiEvents()
const appIsFocused = useValue('editor.isFocused', () => editor.getInstanceState().isFocused, [
editor,
])
useEffect(() => {
if (!appIsFocused) return
const copy = (e: ClipboardEvent) => {
if (
editor.getSelectedShapeIds().length === 0 ||
editor.getEditingShapeId() !== null ||
disallowClipboardEvents(editor)
) {
return
}
preventDefault(e)
handleNativeOrMenuCopy(editor)
trackEvent('copy', { source: 'kbd' })
}
function cut(e: ClipboardEvent) {
if (
editor.getSelectedShapeIds().length === 0 ||
editor.getEditingShapeId() !== null ||
disallowClipboardEvents(editor)
) {
return
}
preventDefault(e)
handleNativeOrMenuCopy(editor)
editor.deleteShapes(editor.getSelectedShapeIds())
trackEvent('cut', { source: 'kbd' })
}
let disablingMiddleClickPaste = false
const pointerUpHandler = (e: PointerEvent) => {
if (e.button === 1) {
// middle mouse button
disablingMiddleClickPaste = true
requestAnimationFrame(() => {
disablingMiddleClickPaste = false
})
}
}
const paste = (e: ClipboardEvent) => {
if (disablingMiddleClickPaste) {
stopEventPropagation(e)
return
}
// If we're editing a shape, or we are focusing an editable input, then
// we would want the user's paste interaction to go to that element or
// input instead; e.g. when pasting text into a text shape's content
if (editor.getEditingShapeId() !== null || disallowClipboardEvents(editor)) return
// First try to use the clipboard data on the event
if (e.clipboardData && !editor.inputs.shiftKey) {
handlePasteFromEventClipboardData(editor, e.clipboardData)
} else {
// Or else use the clipboard API
navigator.clipboard.read().then((clipboardItems) => {
if (Array.isArray(clipboardItems) && clipboardItems[0] instanceof ClipboardItem) {
handlePasteFromClipboardApi(editor, clipboardItems, editor.inputs.currentPagePoint)
}
})
}
preventDefault(e)
trackEvent('paste', { source: 'kbd' })
}
document.addEventListener('copy', copy)
document.addEventListener('cut', cut)
document.addEventListener('paste', paste)
document.addEventListener('pointerup', pointerUpHandler)
return () => {
document.removeEventListener('copy', copy)
document.removeEventListener('cut', cut)
document.removeEventListener('paste', paste)
document.removeEventListener('pointerup', pointerUpHandler)
}
}, [editor, trackEvent, appIsFocused])
}