Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Aztec/Classes/TextKit/TextStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions Aztec/Classes/TextKit/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is no longer called from anywhere in the code. We should remove it from TextStorage.swift.

guard let range = storage.rangeFor(attachmentID: attachmentID) else {
return
}
let originalText = storage.attributedSubstring(from: range)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From line 983 to line 990 we're repeating code that's in several other places in TextView.swift

Why not implement in TextView.swift method

func replaceCharacters(in: range, with attrString: NSAttributedString)

to take care of unified undo support?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion may I do this in another PR just dedicated to that refactor?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

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)
}
Expand Down
15 changes: 1 addition & 14 deletions AztecTests/TextStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions AztecTests/TextViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<img src=\"https://wordpress.com\">")

textView.remove(attachmentID: attachment.identifier)

html = textView.getHTML()

XCTAssertEqual(html, "")
}
}