Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update MarkFormatter to support incoming color changes
  • Loading branch information
Gerardo committed Jan 12, 2024
commit 4711da4fe44e0932ea13e0204d16890ec15cfaef
22 changes: 19 additions & 3 deletions Aztec/Classes/Formatters/Implementations/MarkFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,41 @@ import UIKit
class MarkFormatter: AttributeFormatter {

var placeholderAttributes: [NSAttributedString.Key: Any]?

var textColor: String?
var defaulTextColor: UIColor?

func applicationRange(for range: NSRange, in text: NSAttributedString) -> NSRange {
return range
}

func apply(to attributes: [NSAttributedString.Key: Any], andStore representation: HTMLRepresentation?) -> [NSAttributedString.Key: Any] {
var resultingAttributes = attributes
var resultingAttributes = attributes
let colorStyle = CSSAttribute(name: "color", value: textColor)
let backgroundColorStyle = CSSAttribute(name: "background-color", value: "rgba(0, 0, 0, 0)")

let styleAttribute = Attribute(type: .style, value: .inlineCss([backgroundColorStyle, colorStyle]))
let classAttribute = Attribute(type: .class, value: .string("has-inline-color"))

var representationToUse = HTMLRepresentation(for: .element(HTMLElementRepresentation.init(name: "mark", attributes: [])))
// Setting the HTML representation
var representationToUse = HTMLRepresentation(for: .element(HTMLElementRepresentation.init(name: "mark", attributes: [styleAttribute, classAttribute])))
if let requestedRepresentation = representation {
representationToUse = requestedRepresentation
}
resultingAttributes[.markHtmlRepresentation] = representationToUse

if (textColor != nil) {
resultingAttributes[.foregroundColor] = UIColor(hexString: textColor!)
}

return resultingAttributes
}

func remove(from attributes: [NSAttributedString.Key: Any]) -> [NSAttributedString.Key: Any] {
var resultingAttributes = attributes

if (defaulTextColor != nil) {
resultingAttributes[.foregroundColor] = defaulTextColor
}

resultingAttributes.removeValue(forKey: .markHtmlRepresentation)
return resultingAttributes
Expand Down