Skip to content

Commit

Permalink
Merge pull request #63 from wordpress-mobile/try/simplify-onenter-logic
Browse files Browse the repository at this point in the history
Simplifies onEnter handling.
  • Loading branch information
daniloercoli authored Oct 23, 2018
2 parents 6f98414 + 0c178fe commit 277fbe1
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ class ReactAztecEnterEvent extends Event<ReactAztecEnterEvent> {

private static final String EVENT_NAME = "topTextInputEnter";

public ReactAztecEnterEvent(int viewId) {
private String mText;
private int mSelectionStart;
private int mSelectionEnd;

public ReactAztecEnterEvent(int viewId, String text, int selectionStart, int selectionEnd) {
super(viewId);
mText = text;
mSelectionStart = selectionStart;
mSelectionEnd = selectionEnd;
}

@Override
Expand All @@ -34,6 +41,9 @@ public void dispatch(RCTEventEmitter rctEventEmitter) {
private WritableMap serializeEventData() {
WritableMap eventData = Arguments.createMap();
eventData.putInt("target", getViewTag());
eventData.putString("text", mText);
eventData.putInt("selectionStart", mSelectionStart);
eventData.putInt("selectionEnd", mSelectionEnd);
return eventData;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ public Map<String, Object> getExportedCustomBubblingEventTypeConstants() {
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onEnter")))
.put(
"topHTMLWithCursorRequested",
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onHTMLContentWithCursor")))
.put(
"topFocus",
MapBuilder.of(
Expand Down Expand Up @@ -225,9 +220,7 @@ public void setOnEnterHandling(final ReactAztecText view, boolean onEnterHandlin
view.setOnEnterListener(new ReactAztecText.OnEnterListener() {
@Override
public boolean onEnterKey() {
ReactContext reactContext = (ReactContext) view.getContext();
EventDispatcher eventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
eventDispatcher.dispatchEvent(new ReactAztecEnterEvent(view.getId()));
view.onEnter();
return true;
}
});
Expand All @@ -237,14 +230,12 @@ public boolean onEnterKey() {
}

private static final int COMMAND_NOTIFY_APPLY_FORMAT = 1;
private static final int COMMAND_RETURN_HTML_WITH_CURSOR = 2;
private static final String TAG = "ReactAztecText";

@Override
public Map<String, Integer> getCommandsMap() {
return MapBuilder.<String, Integer>builder()
.put("applyFormat", COMMAND_NOTIFY_APPLY_FORMAT)
.put("returnHTMLWithCursor", COMMAND_RETURN_HTML_WITH_CURSOR)
.build();
}

Expand All @@ -259,10 +250,6 @@ public void receiveCommand(final ReactAztecText parent, int commandType, @Nullab
parent.applyFormat(format);
return;
}
case COMMAND_RETURN_HTML_WITH_CURSOR: {
parent.emitHTMLWithCursorEvent();
return;
}
default:
super.receiveCommand(parent, commandType, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void setIsSettingTextFromJS(boolean mIsSettingTextFromJS) {
this.mIsSettingTextFromJS = mIsSettingTextFromJS;
}

void emitHTMLWithCursorEvent() {
public void onEnter() {
disableTextChangedListener();
String content = toHtml(false);
int cursorPositionStart = getSelectionStart();
Expand All @@ -212,7 +212,7 @@ void emitHTMLWithCursorEvent() {
ReactContext reactContext = (ReactContext) getContext();
EventDispatcher eventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
eventDispatcher.dispatchEvent(
new ReactAztecHtmlWithCursorEvent(getId(), content, cursorPositionStart, cursorPositionEnd)
new ReactAztecEnterEvent(getId(), content, cursorPositionStart, cursorPositionEnd)
);
}

Expand Down
29 changes: 8 additions & 21 deletions ios/RNTAztecView/RCTAztecView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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 @@ -63,8 +62,14 @@ class RCTAztecView: Aztec.TextView {
// MARK: - Edits

open override func insertText(_ text: String) {
guard text != "\n" else {
onEnter?([:])
guard text != "\n" else {
let data: [AnyHashable: Any] = [
"text": getHTML(),
"selectionStart": selectedRange.location,
"selectionEnd": selectedRange.location + selectedRange.length,
]

onEnter?(data)
return
}

Expand Down Expand Up @@ -142,12 +147,6 @@ class RCTAztecView: Aztec.TextView {
}
}

// MARK: - Cursor Positioning

@objc func returnHTMLWithCursor() {
propagateHTMLContentWithCursor()
}

// MARK: - Event Propagation

func propagateContentChanges() {
Expand Down Expand Up @@ -177,18 +176,6 @@ 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: 0 additions & 8 deletions ios/RNTAztecView/RCTAztecViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ @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 @@ -40,11 +39,4 @@ - (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
2 changes: 1 addition & 1 deletion src/AztecView.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AztecView extends React.Component {
}

const { onEnter } = this.props;
onEnter();
onEnter(event);
}

_onHTMLContentWithCursor = (event) => {
Expand Down

0 comments on commit 277fbe1

Please sign in to comment.