-
Notifications
You must be signed in to change notification settings - Fork 19
Improve drag & drop image inlining functionality #243
Description
Background
PR #240 fixed the drag & drop functionality by correctly delegating to super when the operation wasn't handled. However, as noted in the TODO comments added by @wltb, there are several remaining issues with the image inlining feature that should be addressed.
Issues Identified
1. draggingEntered: filtering is ineffective (NOOP)
Location: MacDown/Code/View/MPEditorView.m:58-65
The method checks for public.jpeg UTI but performDragOperation: doesn't respect this filter - it processes any file with NSFilenamesPboardType. This means the filter is essentially a no-op.
Recommended fix: Either remove the filter entirely or make performDragOperation: respect it by checking the UTI of dropped files.
2. All file types are inlined as JPEG
Location: MacDown/Code/View/MPEditorView.m:77-95
Currently, any file dragged into the editor is inlined with a hardcoded data:image/jpeg MIME type, even if it's a PNG, GIF, PDF, or non-image file. This produces invalid/broken image references.
Recommended fix:
- Detect the actual file type using UTI or file extension
- Only allow supported image types (JPEG, PNG, GIF, etc.)
- Use the correct MIME type in the data URL (
image/png,image/gif, etc.) - Reject non-image files or fall back to default behavior
3. Drag operation is not undoable
Location: MacDown/Code/View/MPEditorView.m:93-95
The code uses setString: directly, which bypasses the undo manager. Users cannot undo an accidental image drop.
Recommended fix: Use insertText:replacementRange: or register the change with the undo manager properly, similar to how paste: handles text insertion (line 259).
4. Deprecated API usage
NSFilenamesPboardTypeis deprecated - should useNSPasteboardTypeFileURLbase64Encodingis deprecated - should usebase64EncodedStringWithOptions:
5. Only first file is processed
Location: MacDown/Code/View/MPEditorView.m:84
When multiple files are dropped, only files[0] is processed. The rest are silently ignored.
Recommended fix: Either process all dropped files or provide user feedback that only single-file drops are supported.
Suggested Implementation Approach
- Create a helper method to detect image type from file path/UTI
- Update
draggingEntered:to properly filter for supported image types - Update
performDragOperation:to:- Validate the file is a supported image type
- Determine the correct MIME type
- Use
insertText:replacementRange:for undo support
- Replace deprecated APIs with modern equivalents
Related
- PR Fix drag & drop #240 - Fix drag & drop
- Issue Drag and drop text #11 (referenced in PR Fix drag & drop #240)