Skip to content

Improve drag & drop image inlining functionality #243

@schuyler

Description

@schuyler

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

  • NSFilenamesPboardType is deprecated - should use NSPasteboardTypeFileURL
  • base64Encoding is deprecated - should use base64EncodedStringWithOptions:

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

  1. Create a helper method to detect image type from file path/UTI
  2. Update draggingEntered: to properly filter for supported image types
  3. Update performDragOperation: to:
    • Validate the file is a supported image type
    • Determine the correct MIME type
    • Use insertText:replacementRange: for undo support
  4. Replace deprecated APIs with modern equivalents

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions