-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathimageFileHandler.spec.ts
More file actions
90 lines (76 loc) 路 2.67 KB
/
Copy pathimageFileHandler.spec.ts
File metadata and controls
90 lines (76 loc) 路 2.67 KB
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
import { Editor } from '@tiptap/core'
import Image from '@tiptap/extension-image'
import StarterKit from '@tiptap/starter-kit'
import { describe, expect, it, vi } from 'vitest'
import { imageFileHandlerExtension } from '../../../../src/editor/extensions/imageFileHandler'
function createEditor(): Editor {
return new Editor({
extensions: [
StarterKit,
Image.configure({ inline: false, allowBase64: true }),
imageFileHandlerExtension()
]
})
}
function createClipboardEvent({ files = [], html = '' }: { files?: File[]; html?: string }) {
const event = new Event('paste', { bubbles: true, cancelable: true }) as ClipboardEvent
Object.defineProperty(event, 'clipboardData', {
value: {
files,
getData: (type: string) => (type === 'text/html' ? html : '')
}
})
return event
}
function getImageSources(editor: Editor) {
const sources: string[] = []
editor.state.doc.descendants((node) => {
if (node.type.name === 'image') {
sources.push(node.attrs.src)
}
})
return sources
}
describe('image file handler extension', () => {
it('embeds a pasted image file as data url instead of the html image source', async () => {
const editor = createEditor()
const file = new File(['image'], 'image.png', { type: 'image/png' })
const event = createClipboardEvent({
files: [file],
html: '<img src="blob:https://localhost/2c3c4f5e">'
})
try {
editor.view.dom.dispatchEvent(event)
expect(event.defaultPrevented).toBe(true)
await vi.waitFor(() => expect(getImageSources(editor)).toHaveLength(1))
expect(getImageSources(editor)[0]).toMatch(/^data:image\/png;base64,/)
} finally {
editor.destroy()
}
})
it('leaves clipboard payloads without image files to the default paste handling', () => {
const editor = createEditor()
const file = new File(['pdf'], 'document.pdf', { type: 'application/pdf' })
const event = createClipboardEvent({ files: [file], html: '<p>document.pdf</p>' })
try {
editor.view.dom.dispatchEvent(event)
expect(getImageSources(editor)).toHaveLength(0)
expect(editor.state.doc.textContent).toBe('document.pdf')
} finally {
editor.destroy()
}
})
it('does not paste images into read-only editors', async () => {
const editor = createEditor()
editor.setEditable(false)
const file = new File(['image'], 'image.png', { type: 'image/png' })
const event = createClipboardEvent({ files: [file] })
try {
editor.view.dom.dispatchEvent(event)
await new Promise((resolve) => setTimeout(resolve))
expect(getImageSources(editor)).toHaveLength(0)
} finally {
editor.destroy()
}
})
})