Sources/OrgEditorAppKit/OrgTextView+Media.swift

design-themes-widgets
orgstar/Sources/OrgEditorAppKit/OrgTextView+Media.swift history · blame · raw

60 lines · 3011 bytes

 1#if os(macOS)
 2import AppKit
 3
 4/// Images and files pasted or dropped on the text view, for `yank-media` and Org's
 5/// drag-and-drop handlers. Text goes to the text system as before.
 6extension OrgTextView {
 7    static let jpeg = NSPasteboard.PasteboardType("public.jpeg")
 8    static let gif = NSPasteboard.PasteboardType("com.compuserve.gif")
 9
10    /// The files on `pasteboard`, if it has any.
11    static func files(on pasteboard: NSPasteboard) -> [URL]? {
12        let urls = pasteboard.readObjects(forClasses: [NSURL.self], options: [.urlReadingFileURLsOnly: true]) as? [URL] ?? []
13        return urls.isEmpty ? nil : urls
14    }
15
16    /// The image on `pasteboard` and the extension for it, when there is no text: PNG, JPEG
17    /// and GIF as they are, anything else AppKit reads as PNG.
18    static func image(on pasteboard: NSPasteboard) -> (data: Data, ext: String)? {
19        guard pasteboard.availableType(from: [.string]) == nil else { return nil }
20        if let data = pasteboard.data(forType: .png) { return (data, "png") }
21        if let data = pasteboard.data(forType: jpeg) { return (data, "jpeg") }
22        if let data = pasteboard.data(forType: gif) { return (data, "gif") }
23        guard let image = NSImage(pasteboard: pasteboard), let tiff = image.tiffRepresentation,
24              let png = NSBitmapImageRep(data: tiff)?.representation(using: .png, properties: [:]) else { return nil }
25        return (png, "png")
26    }
27
28    /// Hands files or an image to the editor; false when it doesn't take them.
29    func takeMedia(from pasteboard: NSPasteboard) -> Bool {
30        guard isEditable else { return false }
31        if let files = Self.files(on: pasteboard) { return onFiles?(files) ?? false }
32        if let image = Self.image(on: pasteboard) { return onImage?(image.data, image.ext) ?? false }
33        return false
34    }
35
36    override func paste(_ sender: Any?) {
37        if !takeMedia(from: OrgEditor.pasteboard) { super.paste(sender) }
38    }
39
40    override var acceptableDragTypes: [NSPasteboard.PasteboardType] {
41        super.acceptableDragTypes + [.fileURL, .png, .tiff, Self.jpeg, Self.gif]
42    }
43
44    override func dragOperation(for dragInfo: any NSDraggingInfo, type: NSPasteboard.PasteboardType) -> NSDragOperation {
45        if isEditable, Self.files(on: dragInfo.draggingPasteboard) != nil || Self.image(on: dragInfo.draggingPasteboard) != nil { return .copy }
46        return super.dragOperation(for: dragInfo, type: type)
47    }
48
49    override func performDragOperation(_ sender: any NSDraggingInfo) -> Bool {
50        let pasteboard = sender.draggingPasteboard
51        guard isEditable, Self.files(on: pasteboard) != nil || Self.image(on: pasteboard) != nil else {
52            return super.performDragOperation(sender)
53        }
54        window?.makeFirstResponder(self)
55        let index = characterIndexForInsertion(at: convert(sender.draggingLocation, from: nil))
56        setSelectedRange(NSRange(location: index, length: 0))
57        return takeMedia(from: pasteboard)
58    }
59}
60#endif