|
| 1 | +import type { App } from 'obsidian'; |
| 2 | +import { TFile, TFolder } from 'obsidian'; |
| 3 | + |
| 4 | +import { t } from '@/i18n/i18n'; |
| 5 | + |
| 6 | +/** A vault file or folder reference extracted from an Obsidian drag payload. */ |
| 7 | +export interface VaultDropReference { |
| 8 | + path: string; |
| 9 | + kind: 'file' | 'folder'; |
| 10 | +} |
| 11 | + |
| 12 | +interface DragManagerHost { |
| 13 | + dragManager?: unknown; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Accepts Obsidian file-explorer drags on the composer and inserts them as |
| 18 | + * `@path` / `@path/ ` mention tokens at the caret position. |
| 19 | + * |
| 20 | + * Must be attached before ImageContextManager so vault drags can be claimed |
| 21 | + * via stopImmediatePropagation before the image drop handlers run. |
| 22 | + */ |
| 23 | +export class VaultDropController { |
| 24 | + private readonly dropOverlayEl: HTMLElement; |
| 25 | + |
| 26 | + constructor( |
| 27 | + private readonly app: App, |
| 28 | + private readonly inputWrapperEl: HTMLElement, |
| 29 | + private readonly inputEl: HTMLTextAreaElement, |
| 30 | + ) { |
| 31 | + this.dropOverlayEl = this.createDropOverlay(); |
| 32 | + this.inputWrapperEl.addEventListener('dragenter', this.handleDragEnter); |
| 33 | + this.inputWrapperEl.addEventListener('dragover', this.handleDragOver); |
| 34 | + this.inputWrapperEl.addEventListener('dragleave', this.handleDragLeave); |
| 35 | + this.inputWrapperEl.addEventListener('drop', this.handleDrop); |
| 36 | + } |
| 37 | + |
| 38 | + destroy(): void { |
| 39 | + this.inputWrapperEl.removeEventListener('dragenter', this.handleDragEnter); |
| 40 | + this.inputWrapperEl.removeEventListener('dragover', this.handleDragOver); |
| 41 | + this.inputWrapperEl.removeEventListener('dragleave', this.handleDragLeave); |
| 42 | + this.inputWrapperEl.removeEventListener('drop', this.handleDrop); |
| 43 | + this.dropOverlayEl.remove(); |
| 44 | + } |
| 45 | + |
| 46 | + private readonly handleDragEnter = (event: DragEvent): void => { |
| 47 | + if (this.getDraggedReferences().length === 0) return; |
| 48 | + event.preventDefault(); |
| 49 | + event.stopImmediatePropagation(); |
| 50 | + this.dropOverlayEl.addClass('visible'); |
| 51 | + }; |
| 52 | + |
| 53 | + private readonly handleDragOver = (event: DragEvent): void => { |
| 54 | + if (this.getDraggedReferences().length === 0) return; |
| 55 | + event.preventDefault(); |
| 56 | + event.stopImmediatePropagation(); |
| 57 | + }; |
| 58 | + |
| 59 | + private readonly handleDragLeave = (event: DragEvent): void => { |
| 60 | + if (this.getDraggedReferences().length === 0) return; |
| 61 | + event.stopImmediatePropagation(); |
| 62 | + |
| 63 | + const rect = this.inputWrapperEl.getBoundingClientRect(); |
| 64 | + if ( |
| 65 | + event.clientX <= rect.left || |
| 66 | + event.clientX >= rect.right || |
| 67 | + event.clientY <= rect.top || |
| 68 | + event.clientY >= rect.bottom |
| 69 | + ) { |
| 70 | + this.dropOverlayEl.removeClass('visible'); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + private readonly handleDrop = (event: DragEvent): void => { |
| 75 | + const references = this.getDraggedReferences(); |
| 76 | + if (references.length === 0) return; |
| 77 | + event.preventDefault(); |
| 78 | + event.stopImmediatePropagation(); |
| 79 | + this.dropOverlayEl.removeClass('visible'); |
| 80 | + |
| 81 | + const newReferences = references.filter((reference) => !this.inputContainsReference(reference)); |
| 82 | + if (newReferences.length > 0) { |
| 83 | + this.insertReferences(newReferences); |
| 84 | + this.inputEl.dispatchEvent(new Event('input', { bubbles: true })); |
| 85 | + } |
| 86 | + this.inputEl.focus(); |
| 87 | + }; |
| 88 | + |
| 89 | + private getDraggedItems(): unknown[] { |
| 90 | + const host = this.app as unknown as DragManagerHost; |
| 91 | + const dragManager = host.dragManager; |
| 92 | + if (!this.isRecord(dragManager)) return []; |
| 93 | + |
| 94 | + const draggable = dragManager.draggable; |
| 95 | + if (!this.isRecord(draggable)) return []; |
| 96 | + |
| 97 | + return draggable.type === 'files' && Array.isArray(draggable.files) |
| 98 | + ? draggable.files |
| 99 | + : draggable.file |
| 100 | + ? [draggable.file] |
| 101 | + : []; |
| 102 | + } |
| 103 | + |
| 104 | + private isRecord(value: unknown): value is Record<string, unknown> { |
| 105 | + return typeof value === 'object' && value !== null; |
| 106 | + } |
| 107 | + |
| 108 | + private getDraggedReferences(): VaultDropReference[] { |
| 109 | + const references: VaultDropReference[] = []; |
| 110 | + const seenPaths = new Set<string>(); |
| 111 | + for (const item of this.getDraggedItems()) { |
| 112 | + const reference = |
| 113 | + item instanceof TFolder && item.path !== '/' && item.path !== '' |
| 114 | + ? { path: item.path, kind: 'folder' as const } |
| 115 | + : item instanceof TFile && item.extension.toLowerCase() === 'md' |
| 116 | + ? { path: item.path, kind: 'file' as const } |
| 117 | + : null; |
| 118 | + if (!reference || seenPaths.has(reference.path)) continue; |
| 119 | + seenPaths.add(reference.path); |
| 120 | + references.push(reference); |
| 121 | + } |
| 122 | + return references; |
| 123 | + } |
| 124 | + |
| 125 | + private mentionToken(reference: VaultDropReference): string { |
| 126 | + return `@${reference.path}${reference.kind === 'folder' ? '/' : ''}`; |
| 127 | + } |
| 128 | + |
| 129 | + private inputContainsReference(reference: VaultDropReference): boolean { |
| 130 | + const escapedToken = this.mentionToken(reference).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 131 | + return new RegExp(`(^|\\s)${escapedToken}(?=\\s|$)`).test(this.inputEl.value); |
| 132 | + } |
| 133 | + |
| 134 | + private insertReferences(references: readonly VaultDropReference[]): void { |
| 135 | + const caret = this.inputEl.selectionStart ?? this.inputEl.value.length; |
| 136 | + const before = this.inputEl.value.slice(0, caret); |
| 137 | + const after = this.inputEl.value.slice(caret); |
| 138 | + const tokens = references.map((reference) => this.mentionToken(reference)).join(' '); |
| 139 | + const prefix = before.length > 0 && !/\s$/.test(before) ? ' ' : ''; |
| 140 | + const suffix = after.length > 0 && !/^\s/.test(after) ? ' ' : ''; |
| 141 | + this.inputEl.value = `${before}${prefix}${tokens} ${suffix}${after}`; |
| 142 | + const newCaret = (before + prefix + tokens + ' ').length; |
| 143 | + this.inputEl.setSelectionRange(newCaret, newCaret); |
| 144 | + } |
| 145 | + |
| 146 | + private createDropOverlay(): HTMLElement { |
| 147 | + const overlayEl = this.inputWrapperEl.createDiv({ cls: 'qoderian-vault-drop-overlay' }); |
| 148 | + const contentEl = overlayEl.createDiv({ cls: 'qoderian-vault-drop-content' }); |
| 149 | + const svg = contentEl.createSvg('svg', { |
| 150 | + attr: { |
| 151 | + viewBox: '0 0 24 24', |
| 152 | + width: '32', |
| 153 | + height: '32', |
| 154 | + fill: 'none', |
| 155 | + stroke: 'currentColor', |
| 156 | + 'stroke-width': '2', |
| 157 | + }, |
| 158 | + }); |
| 159 | + // paperclip icon |
| 160 | + svg.createSvg('path', { |
| 161 | + attr: { |
| 162 | + d: 'M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48', |
| 163 | + }, |
| 164 | + }); |
| 165 | + contentEl.createSpan({ text: t('chat.drop.context') }); |
| 166 | + return overlayEl; |
| 167 | + } |
| 168 | +} |
0 commit comments