|
| 1 | +import { Editor } from '@tiptap/core' |
| 2 | +import Image from '@tiptap/extension-image' |
| 3 | +import StarterKit from '@tiptap/starter-kit' |
| 4 | +import { describe, expect, it, vi } from 'vitest' |
| 5 | +import { imageFileHandlerExtension } from '../../../../src/editor/extensions/imageFileHandler' |
| 6 | + |
| 7 | +function createEditor(): Editor { |
| 8 | + return new Editor({ |
| 9 | + extensions: [ |
| 10 | + StarterKit, |
| 11 | + Image.configure({ inline: false, allowBase64: true }), |
| 12 | + imageFileHandlerExtension() |
| 13 | + ] |
| 14 | + }) |
| 15 | +} |
| 16 | + |
| 17 | +function createClipboardEvent({ files = [], html = '' }: { files?: File[]; html?: string }) { |
| 18 | + const event = new Event('paste', { bubbles: true, cancelable: true }) as ClipboardEvent |
| 19 | + |
| 20 | + Object.defineProperty(event, 'clipboardData', { |
| 21 | + value: { |
| 22 | + files, |
| 23 | + getData: (type: string) => (type === 'text/html' ? html : '') |
| 24 | + } |
| 25 | + }) |
| 26 | + |
| 27 | + return event |
| 28 | +} |
| 29 | + |
| 30 | +function getImageSources(editor: Editor) { |
| 31 | + const sources: string[] = [] |
| 32 | + editor.state.doc.descendants((node) => { |
| 33 | + if (node.type.name === 'image') { |
| 34 | + sources.push(node.attrs.src) |
| 35 | + } |
| 36 | + }) |
| 37 | + return sources |
| 38 | +} |
| 39 | + |
| 40 | +describe('image file handler extension', () => { |
| 41 | + it('embeds a pasted image file as data url instead of the html image source', async () => { |
| 42 | + const editor = createEditor() |
| 43 | + const file = new File(['image'], 'image.png', { type: 'image/png' }) |
| 44 | + const event = createClipboardEvent({ |
| 45 | + files: [file], |
| 46 | + html: '<img src="blob:https://localhost/2c3c4f5e">' |
| 47 | + }) |
| 48 | + |
| 49 | + try { |
| 50 | + editor.view.dom.dispatchEvent(event) |
| 51 | + |
| 52 | + expect(event.defaultPrevented).toBe(true) |
| 53 | + await vi.waitFor(() => expect(getImageSources(editor)).toHaveLength(1)) |
| 54 | + expect(getImageSources(editor)[0]).toMatch(/^data:image\/png;base64,/) |
| 55 | + } finally { |
| 56 | + editor.destroy() |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + it('leaves clipboard payloads without image files to the default paste handling', () => { |
| 61 | + const editor = createEditor() |
| 62 | + const file = new File(['pdf'], 'document.pdf', { type: 'application/pdf' }) |
| 63 | + const event = createClipboardEvent({ files: [file], html: '<p>document.pdf</p>' }) |
| 64 | + |
| 65 | + try { |
| 66 | + editor.view.dom.dispatchEvent(event) |
| 67 | + |
| 68 | + expect(getImageSources(editor)).toHaveLength(0) |
| 69 | + expect(editor.state.doc.textContent).toBe('document.pdf') |
| 70 | + } finally { |
| 71 | + editor.destroy() |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + it('does not paste images into read-only editors', async () => { |
| 76 | + const editor = createEditor() |
| 77 | + editor.setEditable(false) |
| 78 | + const file = new File(['image'], 'image.png', { type: 'image/png' }) |
| 79 | + const event = createClipboardEvent({ files: [file] }) |
| 80 | + |
| 81 | + try { |
| 82 | + editor.view.dom.dispatchEvent(event) |
| 83 | + |
| 84 | + await new Promise((resolve) => setTimeout(resolve)) |
| 85 | + expect(getImageSources(editor)).toHaveLength(0) |
| 86 | + } finally { |
| 87 | + editor.destroy() |
| 88 | + } |
| 89 | + }) |
| 90 | +}) |
0 commit comments