Skip to content

Commit

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

Add a new `returnHTMLWithCursor` command & returning event to the editor
  • Loading branch information
daniloercoli authored Sep 20, 2018
2 parents de7d167 + 1119d4c commit f728339
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.wordpress.mobile.ReactNativeAztec;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.RCTEventEmitter;

/**
* Event emitted by AztecText native view when full content with cursor is requested by JS
*/
class ReactAztecHtmlWithCursorEvent extends Event<ReactAztecHtmlWithCursorEvent> {

private static final String EVENT_NAME = "topHTMLWithCursorRequested";

private String mText;
private int mCursorPosition;

public ReactAztecHtmlWithCursorEvent(int viewId, String text, int cursorPosition) {
super(viewId);
mText = text;
mCursorPosition = cursorPosition;
}

@Override
public String getEventName() {
return EVENT_NAME;
}

@Override
public boolean canCoalesce() {
return false;
}

@Override
public void dispatch(RCTEventEmitter rctEventEmitter) {
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), serializeEventData());
}

private WritableMap serializeEventData() {
WritableMap eventData = Arguments.createMap();
eventData.putInt("target", getViewTag());
eventData.putString("text", mText);
eventData.putInt("cursorPosition", mCursorPosition);
return eventData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ 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 @@ -193,11 +198,15 @@ 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.of("applyFormat", COMMAND_NOTIFY_APPLY_FORMAT);
return MapBuilder.<String, Integer>builder()
.put("applyFormat", COMMAND_NOTIFY_APPLY_FORMAT)
.put("returnHTMLWithCursor", COMMAND_RETURN_HTML_WITH_CURSOR)
.build();
}

@Override
Expand All @@ -211,6 +220,10 @@ 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 @@ -215,6 +215,21 @@ public void setIsSettingTextFromJS(boolean mIsSettingTextFromJS) {
this.mIsSettingTextFromJS = mIsSettingTextFromJS;
}

void emitHTMLWithCursorEvent() {
disableTextChangedListener();
String content = toPlainHtml(true);
int cursorPosition = content.indexOf("aztec_cursor");
if (cursorPosition != -1) {
content = content.replaceFirst("aztec_cursor", "");
} else {
cursorPosition = 0; // Something went wrong in Aztec - default to 0 if not found.
}
enableTextChangedListener();
ReactContext reactContext = (ReactContext) getContext();
EventDispatcher eventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
eventDispatcher.dispatchEvent(new ReactAztecHtmlWithCursorEvent( getId(), content, cursorPosition));
}

public void applyFormat(String format) {
ArrayList<ITextFormat> newFormats = new ArrayList<>();
switch (format) {
Expand Down
21 changes: 21 additions & 0 deletions src/AztecView.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AztecView extends React.Component {
onEnter: PropTypes.func,
onScroll: PropTypes.func,
onActiveFormatsChange: PropTypes.func,
onHTMLContentWithCursor: PropTypes.func,
...ViewPropTypes, // include the default view properties
}

Expand All @@ -27,6 +28,14 @@ class AztecView extends React.Component {
);
}

requestHTMLWithCursor() {
UIManager.dispatchViewManagerCommand(
ReactNative.findNodeHandle(this),
UIManager.RCTAztecView.Commands.returnHTMLWithCursor,
[],
);
}

_onActiveFormatsChange = (event) => {
if (!this.props.onActiveFormatsChange) {
return;
Expand Down Expand Up @@ -54,12 +63,24 @@ class AztecView extends React.Component {
onEnter();
}

_onHTMLContentWithCursor = (event) => {
if (!this.props.onHTMLContentWithCursor) {
return;
}

const text = event.nativeEvent.text;
const cursorPosition = event.nativeEvent.cursorPosition;
const { onHTMLContentWithCursor } = this.props;
onHTMLContentWithCursor(text, cursorPosition);
}

render() {
const { onActiveFormatsChange, ...otherProps } = this.props
return (
<RCTAztecView {...otherProps}
onActiveFormatsChange={ this._onActiveFormatsChange }
onContentSizeChange = { this._onContentSizeChange }
onHTMLContentWithCursor = { this._onHTMLContentWithCursor }
onEnter = { this._onEnter }
/>
);
Expand Down

0 comments on commit f728339

Please sign in to comment.