From 492791e90258be3e80ea1d04572c528f2bbaa424 Mon Sep 17 00:00:00 2001 From: Daniele Bogo Date: Wed, 4 Sep 2019 13:34:41 +0100 Subject: [PATCH 1/8] Expose HTMLStorage as public property --- Aztec/Classes/EditorView/EditorView.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Aztec/Classes/EditorView/EditorView.swift b/Aztec/Classes/EditorView/EditorView.swift index 12eb0530c..2808cecfd 100644 --- a/Aztec/Classes/EditorView/EditorView.swift +++ b/Aztec/Classes/EditorView/EditorView.swift @@ -6,12 +6,14 @@ import UIKit public class EditorView: UIView { public let htmlTextView: UITextView public let richTextView: TextView + public let htmlStorage: HTMLStorage // MARK: - Encoding / Decoding static let htmlTextViewKey = "Aztec.EditorView.htmlTextView" static let richTextViewKey = "Aztec.EditorView.richTextView" - + static let htmlStorageKey = "Aztec.EditorView.htmlStorage" + // MARK: - Content Insets public var contentInset: UIEdgeInsets { @@ -95,12 +97,14 @@ public class EditorView: UIView { public required init?(coder aDecoder: NSCoder) { guard let htmlTextView = aDecoder.decodeObject(forKey: EditorView.htmlTextViewKey) as? UITextView, - let richTextView = aDecoder.decodeObject(forKey: EditorView.richTextViewKey) as? TextView else { + let richTextView = aDecoder.decodeObject(forKey: EditorView.richTextViewKey) as? TextView, + let htmlStorage = aDecoder.decodeObject(forKey: EditorView.htmlStorageKey) as? HTMLStorage else { return nil } self.htmlTextView = htmlTextView self.richTextView = richTextView + self.htmlStorage = htmlStorage if #available(iOS 11, *) { htmlTextView.smartInsertDeleteType = .no @@ -117,10 +121,11 @@ public class EditorView: UIView { let storage = HTMLStorage(defaultFont: defaultHTMLFont) let layoutManager = NSLayoutManager() let container = NSTextContainer() - + storage.addLayoutManager(layoutManager) layoutManager.addTextContainer(container) - + + self.htmlStorage = storage self.htmlTextView = UITextView(frame: .zero, textContainer: container) self.richTextView = TextView(defaultFont: defaultFont, defaultParagraphStyle: defaultParagraphStyle, defaultMissingImage: defaultMissingImage) From 7151146f072486c34c1ea56a35494cf4d17cb088 Mon Sep 17 00:00:00 2001 From: Daniele Bogo Date: Wed, 4 Sep 2019 13:34:58 +0100 Subject: [PATCH 2/8] Add a public property for text color --- Aztec/Classes/TextKit/HTMLStorage.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Aztec/Classes/TextKit/HTMLStorage.swift b/Aztec/Classes/TextKit/HTMLStorage.swift index 17e2e1eae..09873f8a8 100644 --- a/Aztec/Classes/TextKit/HTMLStorage.swift +++ b/Aztec/Classes/TextKit/HTMLStorage.swift @@ -15,6 +15,10 @@ open class HTMLStorage: NSTextStorage { /// open var font: UIFont + /// Color to be applied over HTML text + /// + open var textColor = Styles.defaultTextColor + /// Color to be applied over HTML Comments /// open var commentColor = Styles.defaultCommentColor @@ -124,7 +128,7 @@ private extension HTMLStorage { func colorizeHTML() { let fullStringRange = rangeOfEntireString - removeAttribute(.foregroundColor, range: fullStringRange) + addAttribute(.foregroundColor, value: textColor, range: fullStringRange) addAttribute(.font, value: font, range: fullStringRange) let tags = RegExes.html.matches(in: string, options: [], range: fullStringRange) @@ -164,6 +168,7 @@ extension HTMLStorage { /// Default Styles /// public struct Styles { + static let defaultTextColor = UIColor.black static let defaultCommentColor = UIColor.lightGray static let defaultTagColor = UIColor(red: 0x00/255.0, green: 0x75/255.0, blue: 0xB6/255.0, alpha: 0xFF/255.0) static let defaultQuotedColor = UIColor(red: 0x6E/255.0, green: 0x96/255.0, blue: 0xB1/255.0, alpha: 0xFF/255.0) From 233f686e1fc1acacc91451913ba444aba378176b Mon Sep 17 00:00:00 2001 From: Daniele Bogo Date: Wed, 4 Sep 2019 13:35:08 +0100 Subject: [PATCH 3/8] Add Unit Test --- AztecTests/TextKit/HTMLStorageTests.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/AztecTests/TextKit/HTMLStorageTests.swift b/AztecTests/TextKit/HTMLStorageTests.swift index a42778690..4b86d724e 100644 --- a/AztecTests/TextKit/HTMLStorageTests.swift +++ b/AztecTests/TextKit/HTMLStorageTests.swift @@ -63,4 +63,22 @@ class HTMLStorageTests: XCTestCase { XCTAssertEqual(openingTagColor, HTMLStorage.Styles.defaultTagColor) XCTAssertEqual(closingTagColor, HTMLStorage.Styles.defaultTagColor) } + + func testSetTextColor() { + let font = UIFont.boldSystemFont(ofSize: 12) + let storage = HTMLStorage(defaultFont: font) + let initialString = "Hello there" + + storage.insert(NSAttributedString(string: initialString), at: 0) + + XCTAssertEqual(storage.string, initialString) + XCTAssertEqual(storage.textColor, HTMLStorage.Styles.defaultTextColor) + + storage.textColor = .red + storage.replaceCharacters(in: NSRange(location: 0, length: 4), with: NSAttributedString(string: "Hello world")) + + let textColor = storage.attribute(.foregroundColor, at: 0, effectiveRange: nil) as! UIColor + + XCTAssertEqual(textColor, UIColor.red) + } } From f79be05b7ed00dbc0d5f84580bafaa331413c656 Mon Sep 17 00:00:00 2001 From: Daniele Bogo Date: Wed, 4 Sep 2019 13:37:47 +0100 Subject: [PATCH 4/8] Bump podspecs --- WordPress-Aztec-iOS.podspec | 2 +- WordPress-Editor-iOS.podspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/WordPress-Aztec-iOS.podspec b/WordPress-Aztec-iOS.podspec index 0be21edde..1701bf162 100644 --- a/WordPress-Aztec-iOS.podspec +++ b/WordPress-Aztec-iOS.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'WordPress-Aztec-iOS' - s.version = '1.8.1' + s.version = '1.9.0-beta.1' s.summary = 'The native HTML Editor.' # This description is used to generate tags and improve search results. diff --git a/WordPress-Editor-iOS.podspec b/WordPress-Editor-iOS.podspec index f7e6bb17c..b10d640ff 100644 --- a/WordPress-Editor-iOS.podspec +++ b/WordPress-Editor-iOS.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'WordPress-Editor-iOS' - s.version = '1.8.1' + s.version = '1.9.0-beta.1' s.summary = 'The WordPress HTML Editor.' # This description is used to generate tags and improve search results. From 726cded186cebe31f7889f31e4795103c4f8b59d Mon Sep 17 00:00:00 2001 From: Daniele Bogo Date: Wed, 4 Sep 2019 13:50:37 +0100 Subject: [PATCH 5/8] Add Unit Test test for editor view too --- AztecTests/EditorView/EditorViewTests.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/AztecTests/EditorView/EditorViewTests.swift b/AztecTests/EditorView/EditorViewTests.swift index e11ba1986..57a1e87a2 100644 --- a/AztecTests/EditorView/EditorViewTests.swift +++ b/AztecTests/EditorView/EditorViewTests.swift @@ -80,4 +80,24 @@ class EditorViewTests: XCTestCase { XCTAssertEqual(editorView.editingMode, .richText) XCTAssertEqual(editorView.activeView, editorView.richTextView) } + + func testHTMLStorageTextColor() { + let font = UIFont.systemFont(ofSize: 14) + let editorView = Aztec.EditorView( + defaultFont: font, + defaultHTMLFont: font, + defaultParagraphStyle: ParagraphStyle(), + defaultMissingImage: UIImage()) + + XCTAssertEqual(editorView.htmlStorage.textColor, HTMLStorage.Styles.defaultTextColor) + + editorView.htmlStorage.textColor = .red + editorView.richTextView.text = "Hello World" + editorView.toggleEditingMode() + + let textColor = editorView.htmlStorage.attribute(.foregroundColor, at: 3, effectiveRange: nil) as! UIColor + + XCTAssertEqual(editorView.htmlStorage.textColor, UIColor.red) + XCTAssertEqual(textColor, UIColor.red) + } } From 23ec5ed57dfe9e98f238eccc2f8bde1cfabf21f4 Mon Sep 17 00:00:00 2001 From: Daniele Bogo Date: Wed, 4 Sep 2019 13:53:21 +0100 Subject: [PATCH 6/8] Fix Podspecs update --- WordPress-Aztec-iOS.podspec | 2 +- WordPress-Editor-iOS.podspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/WordPress-Aztec-iOS.podspec b/WordPress-Aztec-iOS.podspec index 1701bf162..54e1be3b1 100644 --- a/WordPress-Aztec-iOS.podspec +++ b/WordPress-Aztec-iOS.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'WordPress-Aztec-iOS' - s.version = '1.9.0-beta.1' + s.version = '1.9.0' s.summary = 'The native HTML Editor.' # This description is used to generate tags and improve search results. diff --git a/WordPress-Editor-iOS.podspec b/WordPress-Editor-iOS.podspec index b10d640ff..c1631ac64 100644 --- a/WordPress-Editor-iOS.podspec +++ b/WordPress-Editor-iOS.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'WordPress-Editor-iOS' - s.version = '1.9.0-beta.1' + s.version = '1.9.0' s.summary = 'The WordPress HTML Editor.' # This description is used to generate tags and improve search results. From bc5a15e1305cf40c98895e552c52e872895617c2 Mon Sep 17 00:00:00 2001 From: Daniele Bogo Date: Wed, 4 Sep 2019 17:43:19 +0100 Subject: [PATCH 7/8] Use computed property instead --- Aztec/Classes/EditorView/EditorView.swift | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Aztec/Classes/EditorView/EditorView.swift b/Aztec/Classes/EditorView/EditorView.swift index 2808cecfd..3128cee57 100644 --- a/Aztec/Classes/EditorView/EditorView.swift +++ b/Aztec/Classes/EditorView/EditorView.swift @@ -6,13 +6,17 @@ import UIKit public class EditorView: UIView { public let htmlTextView: UITextView public let richTextView: TextView - public let htmlStorage: HTMLStorage + public var htmlStorage: HTMLStorage { + guard let htmlStorage = htmlTextView.textStorage as? HTMLStorage else { + fatalError("If this happens, something is very off on the init config") + } + return htmlStorage + } // MARK: - Encoding / Decoding static let htmlTextViewKey = "Aztec.EditorView.htmlTextView" static let richTextViewKey = "Aztec.EditorView.richTextView" - static let htmlStorageKey = "Aztec.EditorView.htmlStorage" // MARK: - Content Insets @@ -97,14 +101,12 @@ public class EditorView: UIView { public required init?(coder aDecoder: NSCoder) { guard let htmlTextView = aDecoder.decodeObject(forKey: EditorView.htmlTextViewKey) as? UITextView, - let richTextView = aDecoder.decodeObject(forKey: EditorView.richTextViewKey) as? TextView, - let htmlStorage = aDecoder.decodeObject(forKey: EditorView.htmlStorageKey) as? HTMLStorage else { + let richTextView = aDecoder.decodeObject(forKey: EditorView.richTextViewKey) as? TextView else { return nil } self.htmlTextView = htmlTextView self.richTextView = richTextView - self.htmlStorage = htmlStorage if #available(iOS 11, *) { htmlTextView.smartInsertDeleteType = .no @@ -125,7 +127,6 @@ public class EditorView: UIView { storage.addLayoutManager(layoutManager) layoutManager.addTextContainer(container) - self.htmlStorage = storage self.htmlTextView = UITextView(frame: .zero, textContainer: container) self.richTextView = TextView(defaultFont: defaultFont, defaultParagraphStyle: defaultParagraphStyle, defaultMissingImage: defaultMissingImage) From c1d1d4cf139d54d6d87f581f5925b99e6460b94d Mon Sep 17 00:00:00 2001 From: Daniele Bogo Date: Wed, 4 Sep 2019 17:43:34 +0100 Subject: [PATCH 8/8] Update podspecs --- WordPress-Aztec-iOS.podspec | 2 +- WordPress-Editor-iOS.podspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/WordPress-Aztec-iOS.podspec b/WordPress-Aztec-iOS.podspec index 54e1be3b1..1701bf162 100644 --- a/WordPress-Aztec-iOS.podspec +++ b/WordPress-Aztec-iOS.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'WordPress-Aztec-iOS' - s.version = '1.9.0' + s.version = '1.9.0-beta.1' s.summary = 'The native HTML Editor.' # This description is used to generate tags and improve search results. diff --git a/WordPress-Editor-iOS.podspec b/WordPress-Editor-iOS.podspec index c1631ac64..b10d640ff 100644 --- a/WordPress-Editor-iOS.podspec +++ b/WordPress-Editor-iOS.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'WordPress-Editor-iOS' - s.version = '1.9.0' + s.version = '1.9.0-beta.1' s.summary = 'The WordPress HTML Editor.' # This description is used to generate tags and improve search results.