From 4b602e0e2790bd5c8aeeaea8b0cadf8a51468b0a Mon Sep 17 00:00:00 2001 From: Marcin Krzyzanowski Date: Sun, 20 Feb 2022 14:31:25 +0100 Subject: [PATCH] cup/paste/delete. fix selection range --- .../STTextView/NSTextLayoutManager+Helpers.swift | 2 +- Sources/STTextView/STTextView+CopyPaste.swift | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Sources/STTextView/NSTextLayoutManager+Helpers.swift b/Sources/STTextView/NSTextLayoutManager+Helpers.swift index 52bbe43..1126ba1 100644 --- a/Sources/STTextView/NSTextLayoutManager+Helpers.swift +++ b/Sources/STTextView/NSTextLayoutManager+Helpers.swift @@ -22,7 +22,7 @@ extension NSTextLayoutManager { output += substring } - if textRange.location >= range.endLocation { + if textRange.endLocation >= range.endLocation { stop.pointee = true } }) diff --git a/Sources/STTextView/STTextView+CopyPaste.swift b/Sources/STTextView/STTextView+CopyPaste.swift index 53e14ee..b48d653 100644 --- a/Sources/STTextView/STTextView+CopyPaste.swift +++ b/Sources/STTextView/STTextView+CopyPaste.swift @@ -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) {