Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

iOS: Replace NBSP with ZWSP to allow block rendering #520

Merged
merged 12 commits into from
Jan 27, 2023

Conversation

Velin92
Copy link
Member

@Velin92 Velin92 commented Jan 25, 2023

No description provided.

…d_of_blocks

# Conflicts:
#	platforms/ios/lib/WysiwygComposer/Sources/HTMLParser/HTMLParser.swift
@Velin92 Velin92 marked this pull request as draft January 25, 2023 15:27
@Velin92 Velin92 marked this pull request as ready for review January 27, 2023 14:20
@Velin92 Velin92 requested a review from aringenbach January 27, 2023 14:28
Copy link
Contributor

@aringenbach aringenbach left a comment

Choose a reason for hiding this comment

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

LGTM

@Velin92 Velin92 merged commit 3c4d457 into feature/paragraphs Jan 27, 2023
@Velin92 Velin92 deleted the mauroromito/add_zwsp_at_the_end_of_blocks branch January 27, 2023 14:33
Comment on lines +115 to +119
for discardableTextRange in discardableTextRanges {
if discardableTextRange.upperBound <= attributedIndex {
actualIndex -= discardableTextRange.length
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Think this raises a Swiftlint warning, I'm guessing we can e.g. write something like:

actualIndex -= discardableTextRanges
    .filter { $0.upperBound <= attributedIndex }
    .reduce(0) { $1 + $2.length }

Comment on lines +145 to +149
for discardableTextRange in discardableTextRanges {
if discardableTextRange.location < actualIndex {
actualIndex += discardableTextRange.length
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

but more generally I'm sure we can globally improve the code here, and perhaps merge list prefixes and discardable text into the same Array so we don't have multiple arrays that achieve same kind of computation

Copy link
Member Author

@Velin92 Velin92 Jan 27, 2023

Choose a reason for hiding this comment

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

I think there is actually something missing in my mapping, because when I delete the last character in the last newline, the rendering is lost for that line, I think that I need just like for the list, create some kind of exception based on the cursor position, because deleting the last character probably is also deleting the ZWSP character and removing the rendering, would be nice to be able to keep it, and removing it alongside the newline

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah not sure what happens in that case, technically on backspace we do replace the HTML so we should be re-parsing and adding that ZWSP.

enumerateTypedAttribute(.discardableText) { (discardable: Bool, range: NSRange, _) in
guard discardable == true else { return }

self.deleteCharacters(in: range)
let attributes = self.attributes(at: range.location, effectiveRange: nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

we can directly get the backgroundStyle alone here and avoid looking up the entire dictionary ourselves

Comment on lines +49 to +65
var leadingDiscardableElement: DiscardableTextHTMLElement?
var trailingDiscardableElement: DiscardableTextHTMLElement?
var shouldReplaceNodes = false

if text.hasPrefix("\(Character.nbsp)") {
shouldReplaceNodes = true
text.removeFirst()
leadingDiscardableElement = createDiscardableElement()
}

if text.hasSuffix("\(Character.nbsp)") {
shouldReplaceNodes = true
text.removeLast()
trailingDiscardableElement = createDiscardableElement()
}

if shouldReplaceNodes {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm guessing this could be replaced by

let hasLeadingNbsp = text.hasPrefix(...)
let hasTrailingNbsp = text.hasSuffix(...)

if hasLeadingNbsp || hasTrailingNbsp {
    removeAllChildNodes() 
    
    // Prepare nodes and push them, we only need to create the new nodes at this point
    // Don't need to work with these optionals.
    [...] 
}

Comment on lines +73 to +77
let newTextNode = DTTextHTMLElement()
newTextNode.inheritAttributes(from: self)
newTextNode.interpretAttributes()
newTextNode.setText(text)
addChildNode(newTextNode)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you might be able to re-use the same text node after detaching it instead of creating a new one (no need to re-inherit/interpret attributes then)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants