-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom Fields: Refactor AztecEditorView to be reusable (#13925)
- Loading branch information
Showing
8 changed files
with
180 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
WooCommerce/Classes/ViewRelated/Editor/AztecEditorView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import SwiftUI | ||
|
||
/// `UIViewControllerRepresentable` to use an Aztec Editor in SwiftUI. | ||
/// Params: | ||
/// - content: the content of the editor. It's a Binding so that the parent View can get the latest Editor content. | ||
/// | ||
struct AztecEditorView: UIViewControllerRepresentable { | ||
@Binding var content: String | ||
|
||
func makeUIViewController(context: Context) -> AztecEditorViewController { | ||
let controller = EditorFactory().customFieldRichTextEditor(initialValue: content) | ||
var ignoreInitialContent = true | ||
|
||
guard let aztecController = controller as? AztecEditorViewController else { | ||
fatalError("EditorFactory must return an AztecEditorViewController, but returned \(type(of: controller))") | ||
} | ||
|
||
aztecController.onContentChanged = { text in | ||
/// In addition to user's change action, this callback is invokeddssd during the View Controller's `viewDidLoad` too. | ||
/// This check is needed to avoid setting the value back to the binding at that point, as doing so will trigger the | ||
/// "Modifying state during view update, this will cause undefined behavior" issue. | ||
if ignoreInitialContent { | ||
ignoreInitialContent = false | ||
return | ||
} | ||
|
||
content = text | ||
} | ||
return aztecController | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: AztecEditorViewController, context: Context) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.