Skip to content

Commit

Permalink
Merge pull request #62 from wordpress-mobile/try/add-html-with-cursor…
Browse files Browse the repository at this point in the history
…-mehcanisms

Adds the mechanisms we need to provide HTML with cursor to JS.
  • Loading branch information
daniloercoli authored Oct 19, 2018
2 parents 42faeeb + 017e90d commit 6f98414
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
37 changes: 29 additions & 8 deletions ios/RNTAztecView/RCTAztecView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class RCTAztecView: Aztec.TextView {
@objc var onContentSizeChange: RCTBubblingEventBlock? = nil

@objc var onActiveFormatsChange: RCTBubblingEventBlock? = nil
@objc var onHTMLContentWithCursor: RCTBubblingEventBlock? = nil

private var previousContentSize: CGSize = .zero

Expand Down Expand Up @@ -75,13 +76,6 @@ class RCTAztecView: Aztec.TextView {
super.deleteBackward()
updatePlaceholderVisibility()
}

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

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

Expand Down Expand Up @@ -137,7 +131,7 @@ class RCTAztecView: Aztec.TextView {
placeholderLabel.isHidden = !self.text.isEmpty
}

// MARK: Format interface
// MARK: - Formatting interface

@objc func apply(format: String) {
switch format {
Expand All @@ -147,6 +141,21 @@ class RCTAztecView: Aztec.TextView {
default: print("Format not recognized")
}
}

// MARK: - Cursor Positioning

@objc func returnHTMLWithCursor() {
propagateHTMLContentWithCursor()
}

// MARK: - Event Propagation

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

func propagateFormatChanges() {
guard let onActiveFormatsChange = onActiveFormatsChange else {
Expand All @@ -168,6 +177,18 @@ class RCTAztecView: Aztec.TextView {
})
onActiveFormatsChange(["formats": formats])
}

func propagateHTMLContentWithCursor() {
guard let onHTMLContentWithCursor = onHTMLContentWithCursor else {
return
}

onHTMLContentWithCursor([
"text": getHTML(),
"selectionStart": selectedRange.location,
"selectionEnd": selectedRange.location + selectedRange.length,
])
}
}

// MARK: UITextView Delegate Methods
Expand Down
8 changes: 8 additions & 0 deletions ios/RNTAztecView/RCTAztecViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ @implementation RCTAztecViewManager (RCTExternModule)
RCT_EXPORT_VIEW_PROPERTY(onEnter, RCTBubblingEventBlock)

RCT_EXPORT_VIEW_PROPERTY(onActiveFormatsChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onHTMLContentWithCursor, RCTBubblingEventBlock)

RCT_EXPORT_VIEW_PROPERTY(placeholder, NSString)
RCT_EXPORT_VIEW_PROPERTY(placeholderTextColor, UIColor)
Expand All @@ -39,4 +40,11 @@ - (void)executeBlock:(ActionBlock)block onNode:(NSNumber *)node {
} onNode:node];
}

RCT_EXPORT_METHOD(returnHTMLWithCursor:(nonnull NSNumber *)node)
{
[self executeBlock:^(RCTAztecView *aztecView) {
[aztecView returnHTMLWithCursor];
} onNode:node];
}

@end

0 comments on commit 6f98414

Please sign in to comment.