Skip to content

Commit

Permalink
Add heading representation attribute if missing.
Browse files Browse the repository at this point in the history
  • Loading branch information
twstokes committed Oct 12, 2021
1 parent 6f58df6 commit acdfaaa
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion Aztec/Classes/TextKit/TextStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,44 @@ open class TextStorage: NSTextStorage {

private func preprocessAttributesForInsertion(_ attributedString: NSAttributedString) -> NSAttributedString {
let stringWithAttachments = preprocessAttachmentsForInsertion(attributedString)
let preprocessedString = preprocessHeadingsForInsertion(stringWithAttachments)

return stringWithAttachments
return preprocessedString
}

/// Preprocesses an attributed string that is missing a `headingRepresentation` attribute for insertion in the storage.
///
/// - Important: This method adds the `headingRepresentation` attribute by searching for it in storage. This may
/// change in future versions.
///
/// - Parameters:
/// - attributedString: the string we need to preprocess.
///
/// - Returns: the preprocessed string.
///
fileprivate func preprocessHeadingsForInsertion(_ attributedString: NSAttributedString) -> NSAttributedString {
// Ref. https://github.com/wordpress-mobile/AztecEditor-iOS/pull/1334

guard textStore.length > 0, attributedString.length > 0 else {
return attributedString
}

// Get the attributes of the current string in storage.
let currentAttrs = attributes(at: 0, effectiveRange: nil)

guard
let header = currentAttrs[.headingRepresentation],
attributedString.attribute(.headingRepresentation, at: 0, effectiveRange: nil) == nil
else {
// Either the heading attribute wasn't present,
// or the attributed string already had it.
return attributedString
}

let finalString = NSMutableAttributedString(attributedString: attributedString)
finalString.addAttribute(.headingRepresentation, value: header, range: attributedString.rangeOfEntireString)

return finalString
}

/// Preprocesses an attributed string's attachments for insertion in the storage.
Expand Down

0 comments on commit acdfaaa

Please sign in to comment.