Skip to content

Commit 83144cb

Browse files
committed
Update mac code to support image drop
1 parent 059488b commit 83144cb

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// PasteboardImageReader.swift
3+
// RichEditorSwiftUI
4+
//
5+
// Created by Divyesh Vekariya on 23/12/24.
6+
//
7+
8+
import Foundation
9+
10+
/**
11+
This protocol can be implemented by types that can fetch an
12+
image or multiple images from the pasteboard.
13+
14+
The protocol is implemented by the UIKit `UIPasteboard`, as
15+
well as the AppKit `NSPasteboard`.
16+
*/
17+
public protocol PasteboardImageReader {
18+
19+
/// Get the first image in the pasteboard, if any.
20+
var image: ImageRepresentable? { get }
21+
22+
/// Get all images in the pasteboard.
23+
var images: [ImageRepresentable]? { get }
24+
}
25+
26+
public extension PasteboardImageReader {
27+
28+
/// Check whether or not the pasteboard han any images.
29+
var hasImages: Bool {
30+
guard let images = images else { return false }
31+
return !images.isEmpty
32+
}
33+
}
34+
35+
#if iOS || os(visionOS)
36+
import UIKit
37+
38+
extension UIPasteboard: PasteboardImageReader {}
39+
#endif
40+
41+
#if macOS
42+
import AppKit
43+
44+
extension NSPasteboard: PasteboardImageReader {}
45+
46+
public extension NSPasteboard {
47+
48+
/// Get the first image in the pasteboard, if any.
49+
var image: ImageRepresentable? {
50+
images?.first
51+
}
52+
53+
/// Get all images in the pasteboard.
54+
var images: [ImageRepresentable]? {
55+
readObjects(forClasses: [NSImage.self]) as? [NSImage]
56+
}
57+
}
58+
#endif

Sources/RichEditorSwiftUI/UI/TextViewUI/RichTextView+Setup.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
_ format: RichTextDataFormat?
1616
) {
1717
attributedString = .empty
18+
if let format, !imageConfigurationWasSet {
19+
imageConfiguration = standardImageConfiguration(for: format)
20+
}
1821
attributedString = text
1922

2023
setContentCompressionResistancePriority(

Sources/RichEditorSwiftUI/UI/TextViewUI/RichTextView_AppKit.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636
public var highlightingStyle: RichTextHighlightingStyle = .standard
3737

3838
/// The image configuration to use by the rich text view.
39-
// public var imageConfiguration: RichTextImageConfiguration = .disabled
39+
public var imageConfiguration: RichTextImageConfiguration = .disabled {
40+
didSet { imageConfigurationWasSet = true }
41+
}
42+
43+
/// The image configuration to use by the rich text view.
44+
var imageConfigurationWasSet = false
4045

4146
// MARK: - Overrides
4247

Sources/RichEditorSwiftUI/UI/TextViewUI/RichTextView_UIKit.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,14 @@
8181
didSet {
8282
#if os(iOS) || os(visionOS)
8383
refreshDropInteraction()
84+
imageConfigurationWasSet = true
8485
#endif
8586
}
8687
}
8788

89+
/// The image configuration to use by the rich text view.
90+
var imageConfigurationWasSet = false
91+
8892
#if os(iOS) || os(visionOS)
8993

9094
/// The image drop interaction to use.

0 commit comments

Comments
 (0)