diff --git a/Aztec/Classes/TextKit/TextStorage.swift b/Aztec/Classes/TextKit/TextStorage.swift index ab95ed308..59538e18f 100644 --- a/Aztec/Classes/TextKit/TextStorage.swift +++ b/Aztec/Classes/TextKit/TextStorage.swift @@ -293,18 +293,21 @@ open class TextStorage: NSTextStorage { let stringWithAttachment = NSAttributedString(attachment: attachment) replaceCharacters(in: range, with: stringWithAttachment) } - - /// Removes the attachments that match the attachament identifier provided from the storage + + /// Return the range of an attachment with the specified identifier if any /// - /// - Parameter attachmentID: the unique id of the attachment + /// - Parameter attachmentID: the id of the attachment + /// - Returns: the range of the attachment /// - open func remove(attachmentID: String) { + open func rangeFor(attachmentID: String) -> NSRange? { + var foundRange: NSRange? enumerateAttachmentsOfType(MediaAttachment.self) { (attachment, range, stop) in if attachment.identifier == attachmentID { - self.replaceCharacters(in: range, with: NSAttributedString(string: "")) + foundRange = range stop.pointee = true } } + return foundRange } /// Removes all of the TextAttachments from the storage diff --git a/Aztec/Classes/TextKit/TextView.swift b/Aztec/Classes/TextKit/TextView.swift index 703956046..399f5bd74 100644 --- a/Aztec/Classes/TextKit/TextView.swift +++ b/Aztec/Classes/TextKit/TextView.swift @@ -972,18 +972,28 @@ open class TextView: UITextView { return storage.attachment(withId: id) } - /// Removes the attachments that match the attachament identifier provided from the storage + /// Removes the attachment that matches the attachment identifier provided from the storage /// /// - Parameter attachmentID: the unique id of the attachment /// open func remove(attachmentID: String) { - storage.remove(attachmentID: attachmentID) + guard let range = storage.rangeFor(attachmentID: attachmentID) else { + return + } + let originalText = storage.attributedSubstring(from: range) + let finalRange = NSRange(location: range.location, length: 0) + + undoManager?.registerUndo(withTarget: self, handler: { [weak self] target in + self?.undoTextReplacement(of: originalText, finalRange: finalRange) + }) + + storage.replaceCharacters(in: range, with: NSAttributedString(string: "", attributes: typingAttributes)) delegate?.textViewDidChange?(self) } /// Removes all of the text attachments contained within the storage /// - open func removeTextAttachments() { + open func removeMediaAttachments() { storage.removeMediaAttachments() delegate?.textViewDidChange?(self) } diff --git a/AztecTests/TextStorageTests.swift b/AztecTests/TextStorageTests.swift index 0005d449c..845653ee4 100644 --- a/AztecTests/TextStorageTests.swift +++ b/AztecTests/TextStorageTests.swift @@ -126,20 +126,7 @@ class TextStorageTests: XCTestCase func storage(_ storage: TextStorage, imageFor attachment: NSTextAttachment, with size: CGSize) -> UIImage? { return UIImage() } - } - - func testRemovalOfAttachment() { - let storage = TextStorage() - let mockDelegate = MockAttachmentsDelegate() - storage.attachmentsDelegate = mockDelegate - - let attachment = ImageAttachment(identifier: UUID().uuidString, url: URL(string:"test://")!) - storage.replaceCharacters(in: NSRange(location:0, length: 0), with: NSAttributedString(attachment: attachment)) - - storage.remove(attachmentID: attachment.identifier) - - XCTAssertTrue(mockDelegate.deletedAttachmendIDCalledWithString == attachment.identifier) - } + } func testInsertImage() { let storage = TextStorage() diff --git a/AztecTests/TextViewTests.swift b/AztecTests/TextViewTests.swift index 82f03001e..ec8b46d45 100644 --- a/AztecTests/TextViewTests.swift +++ b/AztecTests/TextViewTests.swift @@ -1460,4 +1460,20 @@ class TextViewTests: XCTestCase { } + + func testRemovalOfAttachment() { + let textView = createEmptyTextView() + + let attachment = textView.replaceWithImage(at: .zero, sourceURL: URL(string:"https://wordpress.com")!, placeHolderImage: nil) + + var html = textView.getHTML() + + XCTAssertEqual(html, "") + + textView.remove(attachmentID: attachment.identifier) + + html = textView.getHTML() + + XCTAssertEqual(html, "") + } }