Skip to content

Commit 42485a7

Browse files
author
Gerardo
committed
Update TextStorage to process the mark formatting within replaceCharacters
1 parent 590b646 commit 42485a7

1 file changed

Lines changed: 25 additions & 25 deletions

File tree

Aztec/Classes/TextKit/TextStorage.swift

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ open class TextStorage: NSTextStorage {
146146
private func preprocessAttributesForInsertion(_ attributedString: NSAttributedString, _ range: NSRange) -> NSAttributedString {
147147
let stringWithAttachments = preprocessAttachmentsForInsertion(attributedString)
148148
let stringWithHeadings = preprocessHeadingsForInsertion(stringWithAttachments)
149-
let preprocessedString = preprocessMarkForInsertion(stringWithHeadings, range)
150149

151-
return preprocessedString
150+
return stringWithHeadings
152151
}
153152

154153
/// Preprocesses an attributed string's attachments for insertion in the storage.
@@ -254,34 +253,33 @@ open class TextStorage: NSTextStorage {
254253
return processedString
255254
}
256255

257-
/// Preprocesses an attributed string that is missing a `markHtmlRepresentation` attribute for insertion in the storage, this reuses the same behavior as preprocessHeadingsForInsertion
256+
/// Preprocesses an attributed string that is missing a `markHtmlRepresentation` attribute for insertion in the storage.
257+
/// This method ensures that the `markHtmlRepresentation` attribute, if present in the current text storage,
258+
/// is applied to the new attributed string being inserted. This is particularly useful for maintaining
259+
/// mark formatting in scenarios like autocorrection or predictive text input.
258260
///
259-
/// - Important: This method adds the `markHtmlRepresentation` attribute if it determines the string should contain it.
260-
/// This works around a problem where autocorrected text didn't contain the attribute. This may change in future versions.
261+
/// - Important: This method adds the `markHtmlRepresentation` attribute to the new string if it's determined
262+
/// that the string should contain it, based on existing attributes in the text storage.
263+
/// This helps to overcome issues where autocorrected text does not carry over the `markHtmlRepresentation` attribute.
261264
///
262265
/// - Parameters:
263-
/// - attributedString: the string we need to preprocess.
266+
/// - attributedString: The new string to be inserted.
267+
/// - range: The range in the current text storage where the new string is to be inserted. This is used to determine
268+
/// if `markHtmlRepresentation` should be applied to the new string.
264269
///
265-
/// - Returns: the preprocessed string.
270+
/// - Returns: The preprocessed attributed string with `markHtmlRepresentation` applied if necessary.
266271
///
267272
fileprivate func preprocessMarkForInsertion(_ attributedString: NSAttributedString, _ range: NSRange) -> NSAttributedString {
268-
// If the attributed string is empty, return as is
269-
if attributedString.length == 0 {
270-
return attributedString
271-
}
272-
273-
// Determine the effective range of attributes at the insertion point
274-
var effectiveRange = NSRange(location: NSNotFound, length: 0)
275-
let insertionPoint = range.location > 0 ? range.location - 1 : 0
276-
let currentAttrs = attributes(at: insertionPoint, effectiveRange: &effectiveRange)
277-
278-
// Apply 'markHtmlRepresentation' attribute if present
279-
if let markSize = currentAttrs[.markHtmlRepresentation] {
280-
let processedString = NSMutableAttributedString(attributedString: attributedString)
281-
processedString.addAttribute(.markHtmlRepresentation, value: markSize, range: NSRange(location: 0, length: attributedString.length))
282-
return processedString
273+
let mutableAttributedString = NSMutableAttributedString(attributedString: attributedString)
274+
275+
if range.location < textStore.length && range.length > 0 {
276+
let currentAttrs = textStore.attributes(at: range.location, effectiveRange: nil)
277+
278+
if let markAttribute = currentAttrs[.markHtmlRepresentation] {
279+
mutableAttributedString.addAttribute(.markHtmlRepresentation, value: markAttribute, range: NSRange(location: 0, length: mutableAttributedString.length))
280+
}
283281
}
284-
return attributedString
282+
return mutableAttributedString
285283
}
286284

287285
fileprivate func detectAttachmentRemoved(in range: NSRange) {
@@ -335,14 +333,16 @@ open class TextStorage: NSTextStorage {
335333
}
336334

337335
override open func replaceCharacters(in range: NSRange, with attrString: NSAttributedString) {
338-
339336
let preprocessedString = preprocessAttributesForInsertion(attrString, range)
340337

341338
beginEditing()
342339

343340
detectAttachmentRemoved(in: range)
344-
textStore.replaceCharacters(in: range, with: preprocessedString)
345341

342+
// Apply mark formatting to the replacement string
343+
let markFormattedString = preprocessMarkForInsertion(preprocessedString, range)
344+
345+
textStore.replaceCharacters(in: range, with: markFormattedString)
346346
replaceTextStoreString(range, with: attrString.string)
347347

348348
edited([.editedAttributes, .editedCharacters], range: range, changeInLength: attrString.length - range.length)

0 commit comments

Comments
 (0)