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
Original file line number Diff line number Diff line change
Expand Up @@ -897,15 +897,7 @@ private extension AttributedStringParser {
finalValue = baseComponents.union(extraComponents).joined(separator: " ")
}
element.updateAttribute(named: key, value: .string(finalValue))
}

if let linkText = attachment.linkURL?.absoluteString {
let hrefValue = Attribute.Value(withString: linkText)
let hrefAttribute = Attribute(name: HTMLLinkAttribute.Href.rawValue, value: hrefValue)
let linkElement = ElementNode(type: .a, attributes: [hrefAttribute], children: [element])

return linkElement
}
}

return element
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,6 @@ private extension AttributedStringSerializer {
return nil
}

if let linkedImageAttributes = linkedImageAttributes(for: element, inheriting: attributes) {
return implicitRepresentation(for: .img, inheriting: linkedImageAttributes)
}

return implicitRepresentation(for: elementType, inheriting: attributes)
}

Expand All @@ -406,37 +402,6 @@ private extension AttributedStringSerializer {
return nil
}
}


/// Whenever the `element`'s nodeType is `a` (link!), and there's a single child of the `.img` type, this method will return the
/// NSAttributedString attributes representing the 'Linked Image' Element.
///
/// - Parameters:
/// - element: The container element.
/// - attributes: NSAttributedString attributes, to be inherited.
///
/// - Returns: The collection of 'Inherited Attributes' with it's internal ImageAttachment modified, so that it carries the 'linkElement'
/// target URL.
///
private func linkedImageAttributes(for element: ElementNode, inheriting attributes: [AttributedStringKey: Any]) -> [AttributedStringKey: Any]? {
guard element.isNodeType(.a),
let imgElement = element.onlyChild(ofType: .img)
else {
return nil
}

var attributesWithoutLink = attributes
attributesWithoutLink[.link] = nil
attributesWithoutLink[.linkHtmlRepresentation] = nil

let imgAttributes = self.attributes(for: imgElement, inheriting: attributesWithoutLink)
let linkText = element.stringValueForAttribute(named: HTMLLinkAttribute.Href.rawValue) ?? ""

let attachment = imgAttributes[.attachment] as! ImageAttachment
attachment.linkURL = URL(string: linkText)

return imgAttributes
}
}


Expand Down
15 changes: 2 additions & 13 deletions Aztec/Classes/TextKit/ImageAttachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import UIKit
/// Custom text attachment.
///
open class ImageAttachment: MediaAttachment {

/// Attachment Link URL
///
open var linkURL: URL?


/// Attachment Alignment
///
open var alignment: Alignment = .center {
Expand Down Expand Up @@ -59,8 +55,6 @@ open class ImageAttachment: MediaAttachment {
self.size = size
}
}

linkURL = aDecoder.decodeObject(forKey: EncodeKeys.linkURL.rawValue) as? URL
}

/// Required Initializer
Expand All @@ -76,15 +70,11 @@ open class ImageAttachment: MediaAttachment {
super.encode(with: aCoder)
aCoder.encode(alignment.rawValue, forKey: EncodeKeys.alignment.rawValue)
aCoder.encode(size.rawValue, forKey: EncodeKeys.size.rawValue)
if let linkURL = self.linkURL {
aCoder.encode(linkURL, forKey: EncodeKeys.linkURL.rawValue)
}
}

fileprivate enum EncodeKeys: String {
case alignment
case size
case linkURL
case size
}


Expand Down Expand Up @@ -140,7 +130,6 @@ extension ImageAttachment {

clone.size = size
clone.alignment = alignment
clone.linkURL = linkURL

return clone
}
Expand Down
3 changes: 2 additions & 1 deletion Example/Example/AttachmentDetailsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AttachmentDetailsViewController: UITableViewController
@IBOutlet var altTextField: UITextField!

var attachment: ImageAttachment?
var linkURL: URL?
var onUpdate: ((_ alignment: ImageAttachment.Alignment, _ size: ImageAttachment.Size, _ imageURL: URL, _ linkURL: URL?, _ altText: String?) -> Void)?


Expand Down Expand Up @@ -45,7 +46,7 @@ class AttachmentDetailsViewController: UITableViewController

sourceURLTextField.text = attachment.url?.absoluteString

linkURLTextField.text = attachment.linkURL?.absoluteString
linkURLTextField.text = linkURL?.absoluteString

altTextField.text = attachment.extraAttributes["alt"]
}
Expand Down
24 changes: 22 additions & 2 deletions Example/Example/EditorDemoController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class EditorDemoController: UIViewController {

if #available(iOS 11, *) {
textView.smartDashesType = .no
textView.smartQuotesType = .no
}

return textView
Expand All @@ -73,6 +74,7 @@ class EditorDemoController: UIViewController {

if #available(iOS 11, *) {
textView.smartDashesType = .no
textView.smartQuotesType = .no
}

return textView
Expand Down Expand Up @@ -1226,7 +1228,9 @@ private extension EditorDemoController

let attachment = richTextView.replaceWithImage(at: richTextView.selectedRange, sourceURL: fileURL, placeHolderImage: image)
attachment.size = .full
attachment.linkURL = fileURL
if let attachmentRange = richTextView.textStorage.ranges(forAttachment: attachment).first {
richTextView.setLink(fileURL, inRange: attachmentRange)
}
let imageID = attachment.identifier
let progress = Progress(parent: nil, userInfo: [MediaProgressKey.mediaID: imageID])
progress.totalUnitCount = 100
Expand Down Expand Up @@ -1334,8 +1338,19 @@ private extension EditorDemoController
}

func displayDetailsForAttachment(_ attachment: ImageAttachment, position:CGPoint) {
guard let attachmentRange = richTextView.textStorage.ranges(forAttachment: attachment).first else {
return
}
let detailsViewController = AttachmentDetailsViewController.controller()
detailsViewController.attachment = attachment
var oldURL: URL?
if let linkRange = richTextView.linkFullRange(forRange: attachmentRange),
let url = richTextView.linkURL(forRange: attachmentRange),
attachmentRange == linkRange {
oldURL = url
detailsViewController.linkURL = url
}

detailsViewController.onUpdate = { (alignment, size, url, linkURL, alt) in
self.richTextView.edit(attachment) { updated in
if let alt = alt {
Expand All @@ -1344,10 +1359,15 @@ private extension EditorDemoController

updated.alignment = alignment
updated.size = size
updated.linkURL = linkURL

updated.updateURL(url)
}
// Update associated link
if let updatedURL = linkURL {
self.richTextView.setLink(updatedURL, inRange: attachmentRange)
} else if oldURL != nil && linkURL == nil {
self.richTextView.removeLink(inRange: attachmentRange)
}
}

let navigationController = UINavigationController(rootViewController: detailsViewController)
Expand Down
4 changes: 4 additions & 0 deletions Example/Example/SampleContent/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ <h4>Image:</h4>
<p>
<img src="https://httpbin.org/image/jpeg" alt="Coyote" />
</p>
<h4>Image with link:</h4>
<p>
<a href="www.automattic.com"><img src="https://httpbin.org/image/jpeg" alt="Coyote" /></a>
</p>
<h4>Image with caption:</h4>
<p>
[caption id="attachment_6" align="alignright" width="300"]<img src="https://httpbin.org/image/jpeg" alt="Coyote" />Ahoi![/caption]
Expand Down