Skip to content

Commit 9224f58

Browse files
authored
Merge pull request #1209 from wordpress-mobile/feature/expose-editor-html-storage
[Dark Mode] Expose editor html storage as property
2 parents 2c50567 + c1d1d4c commit 9224f58

6 files changed

Lines changed: 55 additions & 6 deletions

File tree

Aztec/Classes/EditorView/EditorView.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ import UIKit
66
public class EditorView: UIView {
77
public let htmlTextView: UITextView
88
public let richTextView: TextView
9+
public var htmlStorage: HTMLStorage {
10+
guard let htmlStorage = htmlTextView.textStorage as? HTMLStorage else {
11+
fatalError("If this happens, something is very off on the init config")
12+
}
13+
return htmlStorage
14+
}
915

1016
// MARK: - Encoding / Decoding
1117

1218
static let htmlTextViewKey = "Aztec.EditorView.htmlTextView"
1319
static let richTextViewKey = "Aztec.EditorView.richTextView"
14-
20+
1521
// MARK: - Content Insets
1622

1723
public var contentInset: UIEdgeInsets {
@@ -117,10 +123,10 @@ public class EditorView: UIView {
117123
let storage = HTMLStorage(defaultFont: defaultHTMLFont)
118124
let layoutManager = NSLayoutManager()
119125
let container = NSTextContainer()
120-
126+
121127
storage.addLayoutManager(layoutManager)
122128
layoutManager.addTextContainer(container)
123-
129+
124130
self.htmlTextView = UITextView(frame: .zero, textContainer: container)
125131
self.richTextView = TextView(defaultFont: defaultFont, defaultParagraphStyle: defaultParagraphStyle, defaultMissingImage: defaultMissingImage)
126132

Aztec/Classes/TextKit/HTMLStorage.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ open class HTMLStorage: NSTextStorage {
1515
///
1616
open var font: UIFont
1717

18+
/// Color to be applied over HTML text
19+
///
20+
open var textColor = Styles.defaultTextColor
21+
1822
/// Color to be applied over HTML Comments
1923
///
2024
open var commentColor = Styles.defaultCommentColor
@@ -124,7 +128,7 @@ private extension HTMLStorage {
124128
func colorizeHTML() {
125129
let fullStringRange = rangeOfEntireString
126130

127-
removeAttribute(.foregroundColor, range: fullStringRange)
131+
addAttribute(.foregroundColor, value: textColor, range: fullStringRange)
128132
addAttribute(.font, value: font, range: fullStringRange)
129133

130134
let tags = RegExes.html.matches(in: string, options: [], range: fullStringRange)
@@ -164,6 +168,7 @@ extension HTMLStorage {
164168
/// Default Styles
165169
///
166170
public struct Styles {
171+
static let defaultTextColor = UIColor.black
167172
static let defaultCommentColor = UIColor.lightGray
168173
static let defaultTagColor = UIColor(red: 0x00/255.0, green: 0x75/255.0, blue: 0xB6/255.0, alpha: 0xFF/255.0)
169174
static let defaultQuotedColor = UIColor(red: 0x6E/255.0, green: 0x96/255.0, blue: 0xB1/255.0, alpha: 0xFF/255.0)

AztecTests/EditorView/EditorViewTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,24 @@ class EditorViewTests: XCTestCase {
8080
XCTAssertEqual(editorView.editingMode, .richText)
8181
XCTAssertEqual(editorView.activeView, editorView.richTextView)
8282
}
83+
84+
func testHTMLStorageTextColor() {
85+
let font = UIFont.systemFont(ofSize: 14)
86+
let editorView = Aztec.EditorView(
87+
defaultFont: font,
88+
defaultHTMLFont: font,
89+
defaultParagraphStyle: ParagraphStyle(),
90+
defaultMissingImage: UIImage())
91+
92+
XCTAssertEqual(editorView.htmlStorage.textColor, HTMLStorage.Styles.defaultTextColor)
93+
94+
editorView.htmlStorage.textColor = .red
95+
editorView.richTextView.text = "Hello World"
96+
editorView.toggleEditingMode()
97+
98+
let textColor = editorView.htmlStorage.attribute(.foregroundColor, at: 3, effectiveRange: nil) as! UIColor
99+
100+
XCTAssertEqual(editorView.htmlStorage.textColor, UIColor.red)
101+
XCTAssertEqual(textColor, UIColor.red)
102+
}
83103
}

AztecTests/TextKit/HTMLStorageTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,22 @@ class HTMLStorageTests: XCTestCase {
6363
XCTAssertEqual(openingTagColor, HTMLStorage.Styles.defaultTagColor)
6464
XCTAssertEqual(closingTagColor, HTMLStorage.Styles.defaultTagColor)
6565
}
66+
67+
func testSetTextColor() {
68+
let font = UIFont.boldSystemFont(ofSize: 12)
69+
let storage = HTMLStorage(defaultFont: font)
70+
let initialString = "Hello there"
71+
72+
storage.insert(NSAttributedString(string: initialString), at: 0)
73+
74+
XCTAssertEqual(storage.string, initialString)
75+
XCTAssertEqual(storage.textColor, HTMLStorage.Styles.defaultTextColor)
76+
77+
storage.textColor = .red
78+
storage.replaceCharacters(in: NSRange(location: 0, length: 4), with: NSAttributedString(string: "Hello world"))
79+
80+
let textColor = storage.attribute(.foregroundColor, at: 0, effectiveRange: nil) as! UIColor
81+
82+
XCTAssertEqual(textColor, UIColor.red)
83+
}
6684
}

WordPress-Aztec-iOS.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'WordPress-Aztec-iOS'
11-
s.version = '1.8.1'
11+
s.version = '1.9.0-beta.1'
1212
s.summary = 'The native HTML Editor.'
1313

1414
# This description is used to generate tags and improve search results.

WordPress-Editor-iOS.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'WordPress-Editor-iOS'
11-
s.version = '1.8.1'
11+
s.version = '1.9.0-beta.1'
1212
s.summary = 'The WordPress HTML Editor.'
1313

1414
# This description is used to generate tags and improve search results.

0 commit comments

Comments
 (0)