Skip to content

Commit 538b532

Browse files
committed
Add Documentation, Adjust Undo Grouping
1 parent 060abd0 commit 538b532

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

Sources/CodeEditTextView/TextView/TextView+Drag.swift

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,27 @@ extension TextView: NSDraggingSource {
3232
}
3333
}
3434

35+
/// Adds a gesture for recognizing selection dragging gestures to the text view.
36+
/// See ``TextView/DragSelectionGesture`` for details.
3537
func setUpDragGesture() {
3638
let dragGesture = DragSelectionGesture(target: self, action: #selector(dragGestureHandler(_:)))
3739
dragGesture.minimumPressDuration = NSEvent.doubleClickInterval / 3
3840
dragGesture.isEnabled = isSelectable
3941
addGestureRecognizer(dragGesture)
4042
}
41-
43+
44+
/// Handles state change on the drag and drop gesture recognizer.
45+
///
46+
/// This will ignore any gesture state besides `.began`, and will end by setting the state to `.ended`. The gesture
47+
/// is only meant to handle *recognizing* the drag, but the system drag interaction handles the rest.
48+
///
49+
/// This will create a ``DraggingTextRenderer`` with the contents of the visible text selection. That is converted
50+
/// into an image and given to a new dragging session on the text view
51+
///
52+
/// The rest of the drag interaction is handled by ``performDragOperation(_:)``, ``draggingUpdated(_:)``,
53+
/// ``draggingSession(_:willBeginAt:)`` and family.
54+
///
55+
/// - Parameter sender: The gesture that's sending the state change.
4256
@objc private func dragGestureHandler(_ sender: DragSelectionGesture) {
4357
guard sender.state == .began else { return }
4458
defer {
@@ -180,6 +194,18 @@ extension TextView: NSDraggingSource {
180194
return .move
181195
}
182196

197+
// MARK: - Perform Drag
198+
199+
/// Performs the final drop operation.
200+
///
201+
/// This method accepts a number of items from the dragging info's pasteboard, and cuts them into the
202+
/// destination determined by the ``TextView/draggingCursorView``.
203+
///
204+
/// If the app's current event has the `option` key pressed, this will only paste the text from the pasteboard,
205+
/// and not remove the original dragged text.
206+
///
207+
/// - Parameter sender: The dragging info to use.
208+
/// - Returns: `true`, if the drag was accepted.
183209
override public func performDragOperation(_ sender: any NSDraggingInfo) -> Bool {
184210
guard let objects = sender.draggingPasteboard.readObjects(forClasses: pasteboardObjects)?
185211
.compactMap({ anyObject in
@@ -221,9 +247,10 @@ extension TextView: NSDraggingSource {
221247
insertText("") // Replace the selected ranges with nothing
222248
}
223249

224-
replaceCharacters(in: [NSRange(location: insertionOffset, length: 0)], with: insertionString)
225250
undoManager?.endUndoGrouping()
226251

252+
replaceCharacters(in: [NSRange(location: insertionOffset, length: 0)], with: insertionString)
253+
227254
selectionManager.setSelectedRange(
228255
NSRange(location: insertionOffset, length: NSString(string: insertionString).length)
229256
)

0 commit comments

Comments
 (0)