Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First steps to implement pasting external content #553

Merged
merged 18 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public Map<String, Object> getExportedCustomBubblingEventTypeConstants() {
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onBackspace")))
.put(
"topTextInputPaste",
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onPaste")))
.put(
"topFocus",
MapBuilder.of(
Expand Down Expand Up @@ -375,6 +380,11 @@ public void setOnBackspaceHandling(final ReactAztecText view, boolean onBackspac
view.shouldHandleOnBackspace = onBackspaceHandling;
}

@ReactProp(name = "onPaste", defaultBoolean = false)
public void setOnPasteHandling(final ReactAztecText view, boolean onPasteHandling) {
view.shouldHandleOnPaste = onPasteHandling;
}

@Override
public Map<String, Integer> getCommandsMap() {
return MapBuilder.<String, Integer>builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 Aztec native view when paste is detected.
*/
class ReactAztecPasteEvent extends Event<ReactAztecPasteEvent> {

private static final String EVENT_NAME = "topTextInputPaste";

private String mCurrentContent;
private int mSelectionStart;
private int mSelectionEnd;
private String mPastedText;
private String mPastedHtml;

public ReactAztecPasteEvent(int viewId, String currentContent, int selectionStart,
int selectionEnd, String pastedText, String pastedHtml) {
super(viewId);
mCurrentContent = currentContent;
mSelectionStart = selectionStart;
mSelectionEnd = selectionEnd;
mPastedText = pastedText;
mPastedHtml = pastedHtml;
}

@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("currentContent", mCurrentContent);
eventData.putInt("selectionStart", mSelectionStart);
eventData.putInt("selectionEnd", mSelectionEnd);
eventData.putString("pastedText", mPastedText);
eventData.putString("pastedHtml", mPastedHtml);
return eventData;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.wordpress.mobile.ReactNativeAztec;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Rect;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -27,6 +29,8 @@
import java.util.ArrayList;
import java.util.LinkedList;

import static android.content.ClipData.*;

public class ReactAztecText extends AztecText {

private final InputMethodManager mInputMethodManager;
Expand All @@ -49,6 +53,7 @@ public class ReactAztecText extends AztecText {
String lastSentFormattingOptionsEventString = "";
boolean shouldHandleOnEnter = false;
boolean shouldHandleOnBackspace = false;
boolean shouldHandleOnPaste = false;
boolean shouldHandleOnSelectionChange = false;
boolean shouldHandleActiveFormatsChange = false;

Expand Down Expand Up @@ -100,6 +105,15 @@ void addPlugin(IAztecPlugin plugin) {
}
}

@Override
public boolean onTextContextMenuItem(int id) {
if (id == android.R.id.paste && shouldHandleOnPaste) {
mkevins marked this conversation as resolved.
Show resolved Hide resolved
return onPaste();
} else {
return super.onTextContextMenuItem(id);
}
}

// VisibleForTesting from {@link TextInputEventsTestCase}.
public void requestFocusFromJS() {
mIsJSSettingFocus = true;
Expand Down Expand Up @@ -321,6 +335,39 @@ private boolean onBackspace() {
return true;
}

private boolean onPaste() {
ClipboardManager clipboardManager = (ClipboardManager) getContext().getSystemService(
Context.CLIPBOARD_SERVICE);

StringBuilder text = new StringBuilder();
StringBuilder html = new StringBuilder();

if (clipboardManager != null && clipboardManager.hasPrimaryClip()) {
ClipData clipData = clipboardManager.getPrimaryClip();
int itemCount = clipData.getItemCount();

for (int i = 0; i < itemCount; i++) {
Item item = clipData.getItemAt(i);
text.append(item.coerceToText(getContext()));
html.append(item.coerceToHtmlText(getContext()));
}
}

// temporarily disable listener during call to toHtml()
disableTextChangedListener();
String content = toHtml(false);
int cursorPositionStart = getSelectionStart();
int cursorPositionEnd = getSelectionEnd();
enableTextChangedListener();
ReactContext reactContext = (ReactContext) getContext();
EventDispatcher eventDispatcher = reactContext.getNativeModule(UIManagerModule.class)
.getEventDispatcher();
eventDispatcher.dispatchEvent(new ReactAztecPasteEvent(getId(), content,
cursorPositionStart, cursorPositionEnd, text.toString(), html.toString())
mkevins marked this conversation as resolved.
Show resolved Hide resolved
);
return true;
}

public void applyFormat(String format) {
ArrayList<ITextFormat> newFormats = new ArrayList<>();
switch (format) {
Expand Down