Skip to content

Commit

Permalink
Merge pull request #34 from wordpress-mobile/try/expose-on-change-pro…
Browse files Browse the repository at this point in the history
…perty

iOS: Expose and wire onChange propType for Aztec.
  • Loading branch information
diegoreymendez authored Jul 25, 2018
2 parents 4a5fec8 + abcef62 commit 7e5e062
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ios/RNTAztecView/RCTAztecView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Aztec
import Foundation

class RCTAztecView: Aztec.TextView {
@objc var onChange: RCTBubblingEventBlock? = nil
@objc var onContentSizeChange: RCTBubblingEventBlock? = nil

private var previousContentSize: CGSize = .zero
Expand All @@ -28,8 +29,32 @@ class RCTAztecView: Aztec.TextView {
onContentSizeChange(body)
}

// MARK: - Edits

open override func insertText(_ text: String) {
super.insertText(text)

if let onChange = onChange {
let text = packForRN(getHTML(), withName: "text")
onChange(text)
}
}

open override func deleteBackward() {
super.deleteBackward()

if let onChange = onChange {
let text = packForRN(getHTML(), withName: "text")
onChange(text)
}
}

// MARK: - Native-to-RN Value Packing Logic

func packForRN(_ text: String, withName name: String) -> [AnyHashable: Any] {
return [name: text, "eventCount": 1]
}

func packForRN(_ size: CGSize, withName name: String) -> [AnyHashable: Any] {

let size = ["width": size.width,
Expand All @@ -42,6 +67,10 @@ class RCTAztecView: Aztec.TextView {

@objc
func setContents(_ contents: NSDictionary) {
guard contents["eventCount"] == nil else {
return
}

let html = contents["text"] as? String ?? ""

setHTML(html)
Expand Down
1 change: 1 addition & 0 deletions ios/RNTAztecView/RCTAztecViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ @implementation RCTAztecViewManager (RCTExternModule)

RCT_REMAP_VIEW_PROPERTY(text, contents, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(onContentSizeChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)

@end
1 change: 1 addition & 0 deletions src/AztecView.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var aztex = {
color: PropTypes.string,
maxImagesWidth: PropTypes.number,
minImagesWidth: PropTypes.number,
onChange: PropTypes.func,
onContentSizeChange: PropTypes.func,
onScroll: PropTypes.func,
...ViewPropTypes, // include the default view properties
Expand Down

0 comments on commit 7e5e062

Please sign in to comment.