* feat: add copy_image_to_vault Rust command for native drag-drop Adds a new Tauri command that copies an image file from a source path (provided by Tauri's drag-drop event) directly into vault/attachments/. More efficient than base64 encoding for filesystem drag-drop. Also adds core:webview:allow-on-drag-drop-event permission. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: handle Tauri native drag-drop for filesystem images Listen for Tauri's onDragDropEvent to intercept OS-level file drops that bypass the webview's HTML5 DnD API. When image files are dropped: 1. Copy to vault/attachments via copy_image_to_vault command 2. Insert image block into BlockNote at cursor position Also passes vaultPath through Editor → EditorContent → SingleEditorView so the hook can invoke the Tauri command. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: remove nonexistent drag-drop permission from capabilities The onDragDropEvent API works through core:event:default (included in core:default), not a separate webview permission. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: correct DragDropEvent type handling for 'over' events Tauri's DragDropEvent discriminated union only provides `paths` on 'drop' events, not 'over'. Show drag overlay unconditionally on 'over' since we can't filter by image paths at that stage. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * design: drag-drop-images wireframes (idle + drag-over overlay) * style: rustfmt mcp.rs and image.rs --------- Co-authored-by: Test <test@test.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
171 lines
6.2 KiB
TypeScript
171 lines
6.2 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach, beforeAll } from 'vitest'
|
|
import { renderHook, act } from '@testing-library/react'
|
|
import { uploadImageFile, useImageDrop } from './useImageDrop'
|
|
import { createRef } from 'react'
|
|
|
|
vi.mock('@tauri-apps/api/core', () => ({
|
|
invoke: vi.fn(),
|
|
convertFileSrc: vi.fn((path: string) => `asset://localhost/${path}`),
|
|
}))
|
|
|
|
vi.mock('../mock-tauri', () => ({
|
|
isTauri: () => false,
|
|
}))
|
|
|
|
// JSDOM lacks DragEvent and File.arrayBuffer — polyfill for tests
|
|
beforeAll(() => {
|
|
if (typeof globalThis.DragEvent === 'undefined') {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
(globalThis as any).DragEvent = class DragEvent extends MouseEvent {
|
|
dataTransfer: DataTransfer | null
|
|
constructor(type: string, init?: DragEventInit) {
|
|
super(type, init)
|
|
this.dataTransfer = init?.dataTransfer ?? null
|
|
}
|
|
}
|
|
}
|
|
|
|
// File.prototype.arrayBuffer may be missing in older JSDOM
|
|
if (!File.prototype.arrayBuffer) {
|
|
File.prototype.arrayBuffer = function () {
|
|
return new Promise((resolve) => {
|
|
const reader = new FileReader()
|
|
reader.onload = () => resolve(reader.result as ArrayBuffer)
|
|
reader.readAsArrayBuffer(this)
|
|
})
|
|
}
|
|
}
|
|
})
|
|
|
|
// Mock DataTransfer (JSDOM doesn't implement it)
|
|
function createMockDataTransfer(files: File[]) {
|
|
const items = files.map(f => ({ kind: 'file' as const, type: f.type, getAsFile: () => f }))
|
|
return {
|
|
items: { ...items, length: items.length },
|
|
files: Object.assign(files, { item: (i: number) => files[i] }),
|
|
dropEffect: 'none',
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
} as any as DataTransfer
|
|
}
|
|
|
|
function createDragEvent(type: string, files: File[], opts?: { relatedTarget?: EventTarget | null }) {
|
|
const dt = createMockDataTransfer(files)
|
|
return new DragEvent(type, {
|
|
dataTransfer: dt,
|
|
bubbles: true,
|
|
cancelable: true,
|
|
relatedTarget: opts?.relatedTarget ?? null,
|
|
})
|
|
}
|
|
|
|
describe('uploadImageFile', () => {
|
|
it('returns a data URL in browser mode', async () => {
|
|
const blob = new Blob(['fake-image-data'], { type: 'image/png' })
|
|
const file = new File([blob], 'test.png', { type: 'image/png' })
|
|
|
|
const url = await uploadImageFile(file)
|
|
expect(url).toMatch(/^data:image\/png;base64,/)
|
|
})
|
|
|
|
it('passes file to Tauri save_image in Tauri mode', async () => {
|
|
const mockTauri = await import('../mock-tauri')
|
|
const originalIsTauri = mockTauri.isTauri
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
;(mockTauri as any).isTauri = () => true
|
|
|
|
const { invoke, convertFileSrc } = await import('@tauri-apps/api/core')
|
|
vi.mocked(invoke).mockResolvedValue('/vault/attachments/123-test.png')
|
|
vi.mocked(convertFileSrc).mockReturnValue('asset://localhost/vault/attachments/123-test.png')
|
|
|
|
const blob = new Blob([new Uint8Array([0x89, 0x50])], { type: 'image/png' })
|
|
const file = new File([blob], 'test.png', { type: 'image/png' })
|
|
|
|
const url = await uploadImageFile(file, '/vault')
|
|
expect(invoke).toHaveBeenCalledWith('save_image', {
|
|
vaultPath: '/vault',
|
|
filename: 'test.png',
|
|
data: expect.any(String),
|
|
})
|
|
expect(url).toBe('asset://localhost/vault/attachments/123-test.png')
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
;(mockTauri as any).isTauri = originalIsTauri
|
|
})
|
|
})
|
|
|
|
describe('useImageDrop', () => {
|
|
let container: HTMLDivElement
|
|
|
|
beforeEach(() => {
|
|
container = document.createElement('div')
|
|
document.body.appendChild(container)
|
|
})
|
|
|
|
afterEach(() => {
|
|
container.remove()
|
|
})
|
|
|
|
function renderImageDrop(opts?: { onImageUrl?: (url: string) => void; vaultPath?: string }) {
|
|
const ref = createRef<HTMLDivElement>()
|
|
Object.defineProperty(ref, 'current', { value: container, writable: true })
|
|
return renderHook(() => useImageDrop({ containerRef: ref, ...opts }))
|
|
}
|
|
|
|
it('sets isDragOver to true on dragover with image files', () => {
|
|
const { result } = renderImageDrop()
|
|
const file = new File(['data'], 'photo.png', { type: 'image/png' })
|
|
|
|
act(() => { container.dispatchEvent(createDragEvent('dragover', [file])) })
|
|
expect(result.current.isDragOver).toBe(true)
|
|
})
|
|
|
|
it('ignores dragover with non-image files', () => {
|
|
const { result } = renderImageDrop()
|
|
const file = new File(['data'], 'doc.pdf', { type: 'application/pdf' })
|
|
|
|
act(() => { container.dispatchEvent(createDragEvent('dragover', [file])) })
|
|
expect(result.current.isDragOver).toBe(false)
|
|
})
|
|
|
|
it('resets isDragOver on dragleave when leaving container', () => {
|
|
const { result } = renderImageDrop()
|
|
const file = new File(['data'], 'photo.png', { type: 'image/png' })
|
|
|
|
act(() => { container.dispatchEvent(createDragEvent('dragover', [file])) })
|
|
expect(result.current.isDragOver).toBe(true)
|
|
|
|
act(() => { container.dispatchEvent(createDragEvent('dragleave', [], { relatedTarget: document.body })) })
|
|
expect(result.current.isDragOver).toBe(false)
|
|
})
|
|
|
|
it('resets isDragOver on drop (upload handled by BlockNote natively)', () => {
|
|
const { result } = renderImageDrop()
|
|
const file = new File(['data'], 'photo.png', { type: 'image/png' })
|
|
|
|
act(() => { container.dispatchEvent(createDragEvent('dragover', [file])) })
|
|
expect(result.current.isDragOver).toBe(true)
|
|
|
|
act(() => { container.dispatchEvent(createDragEvent('drop', [file])) })
|
|
expect(result.current.isDragOver).toBe(false)
|
|
})
|
|
|
|
it('accepts jpeg, gif, and webp types', () => {
|
|
const { result } = renderImageDrop()
|
|
|
|
for (const type of ['image/jpeg', 'image/gif', 'image/webp']) {
|
|
const file = new File(['data'], `img.${type.split('/')[1]}`, { type })
|
|
act(() => { container.dispatchEvent(createDragEvent('dragover', [file])) })
|
|
expect(result.current.isDragOver).toBe(true)
|
|
|
|
act(() => { container.dispatchEvent(createDragEvent('dragleave', [], { relatedTarget: document.body })) })
|
|
}
|
|
})
|
|
|
|
it('passes onImageUrl and vaultPath without error', () => {
|
|
const onImageUrl = vi.fn()
|
|
const { result } = renderImageDrop({ onImageUrl, vaultPath: '/vault' })
|
|
// Should render without error; Tauri event listener is skipped in browser mode
|
|
expect(result.current.isDragOver).toBe(false)
|
|
})
|
|
})
|