Skip to content

Commit

Permalink
cup/paste/delete. fix selection range
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzanowskim committed Feb 20, 2022
1 parent c8c28bf commit 4b602e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/STTextView/NSTextLayoutManager+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension NSTextLayoutManager {
output += substring
}

if textRange.location >= range.endLocation {
if textRange.endLocation >= range.endLocation {
stop.pointee = true
}
})
Expand Down
15 changes: 13 additions & 2 deletions Sources/STTextView/STTextView+CopyPaste.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@ extension STTextView {
}

@objc func paste(_ sender: Any?) {
guard let string = NSPasteboard.general.string(forType: .string) else {
return
}

let nsrange = NSRange(textLayoutManager.insertionPointLocation!, in: textContentStorage)
insertText(string, replacementRange: nsrange)
}

@objc func cut(_ sender: Any?) {

copy(sender)
delete(sender)
}

@objc func delete(_ sender: Any?) {

for textRange in textLayoutManager.textSelections.flatMap(\.textRanges) {
// "replaceContents" doesn't work with NSTextContentStorage at all
// textLayoutManager.replaceContents(in: textRange, with: NSAttributedString())
let nsrange = NSRange(textRange, in: textContentStorage)
insertText("", replacementRange: nsrange)
}
}

private func updatePasteboard(with text: String) {
Expand Down

0 comments on commit 4b602e0

Please sign in to comment.