From 8648cb5061134e74d631b4776c11bf990b263027 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Wed, 28 Jun 2017 14:24:29 +0100 Subject: [PATCH 1/3] Implement undo and redo when removing attachments by code. --- Aztec/Classes/TextKit/TextStorage.swift | 17 ++++++++++++++++- Aztec/Classes/TextKit/TextView.swift | 16 +++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Aztec/Classes/TextKit/TextStorage.swift b/Aztec/Classes/TextKit/TextStorage.swift index ab95ed308..4bc7dbd85 100644 --- a/Aztec/Classes/TextKit/TextStorage.swift +++ b/Aztec/Classes/TextKit/TextStorage.swift @@ -293,7 +293,22 @@ open class TextStorage: NSTextStorage { let stringWithAttachment = NSAttributedString(attachment: attachment) replaceCharacters(in: range, with: stringWithAttachment) } - + + /// Return the range of an attachment with the specified identifier if any + /// + /// - Parameter attachmentID: the id of the attachment + /// - Returns: the range of the attachment + open func rangeFor(attachmentID: String) -> NSRange? { + var foundRange: NSRange? + enumerateAttachmentsOfType(MediaAttachment.self) { (attachment, range, stop) in + if attachment.identifier == attachmentID { + foundRange = range + stop.pointee = true + } + } + return foundRange + } + /// Removes the attachments that match the attachament identifier provided from the storage /// /// - Parameter attachmentID: the unique id of the attachment diff --git a/Aztec/Classes/TextKit/TextView.swift b/Aztec/Classes/TextKit/TextView.swift index cec506d6a..81b6d9e38 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 match 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) } From b6ba092dc571719a7e630c1d4d5088d1f8e0eaff Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Wed, 28 Jun 2017 17:15:12 +0100 Subject: [PATCH 2/3] Remove unused method and update tests. --- Aztec/Classes/TextKit/TextStorage.swift | 14 +------------- AztecTests/TextStorageTests.swift | 15 +-------------- AztecTests/TextViewTests.swift | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/Aztec/Classes/TextKit/TextStorage.swift b/Aztec/Classes/TextKit/TextStorage.swift index 4bc7dbd85..59538e18f 100644 --- a/Aztec/Classes/TextKit/TextStorage.swift +++ b/Aztec/Classes/TextKit/TextStorage.swift @@ -298,6 +298,7 @@ open class TextStorage: NSTextStorage { /// /// - Parameter attachmentID: the id of the attachment /// - Returns: the range of the attachment + /// open func rangeFor(attachmentID: String) -> NSRange? { var foundRange: NSRange? enumerateAttachmentsOfType(MediaAttachment.self) { (attachment, range, stop) in @@ -309,19 +310,6 @@ open class TextStorage: NSTextStorage { return foundRange } - /// Removes the attachments that match the attachament identifier provided from the storage - /// - /// - Parameter attachmentID: the unique id of the attachment - /// - open func remove(attachmentID: String) { - enumerateAttachmentsOfType(MediaAttachment.self) { (attachment, range, stop) in - if attachment.identifier == attachmentID { - self.replaceCharacters(in: range, with: NSAttributedString(string: "")) - stop.pointee = true - } - } - } - /// Removes all of the TextAttachments from the storage /// open func removeMediaAttachments() { 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 fe42f52c2..1a88daa20 100644 --- a/AztecTests/TextViewTests.swift +++ b/AztecTests/TextViewTests.swift @@ -1441,4 +1441,20 @@ class TextViewTests: XCTestCase { } XCTAssertEqual(font, textView.defaultFont) } + + 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, "") + } } From 370e7e4c009fa01b472745bf68fc47fe43bb8aa1 Mon Sep 17 00:00:00 2001 From: Sergio Estevao Date: Wed, 28 Jun 2017 17:19:08 +0100 Subject: [PATCH 3/3] Fix typo on the documentation. --- Aztec/Classes/TextKit/TextView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Aztec/Classes/TextKit/TextView.swift b/Aztec/Classes/TextKit/TextView.swift index 81b6d9e38..57819b046 100644 --- a/Aztec/Classes/TextKit/TextView.swift +++ b/Aztec/Classes/TextKit/TextView.swift @@ -972,7 +972,7 @@ open class TextView: UITextView { return storage.attachment(withId: id) } - /// Removes the attachment that match the attachment 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 ///