-
Notifications
You must be signed in to change notification settings - Fork 984
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow styling text in composer when selecting it with native actions
- Loading branch information
Showing
5 changed files
with
343 additions
and
13 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...e-status/android/src/main/java/im/status/ethereum/module/RNSelectableTextInputModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package im.status.ethereum.module; | ||
|
||
import android.view.ActionMode; | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.uimanager.NativeViewHierarchyManager; | ||
import com.facebook.react.uimanager.UIBlock; | ||
import com.facebook.react.uimanager.UIManagerModule; | ||
import com.facebook.react.views.textinput.ReactEditText; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
class RNSelectableTextInputModule extends ReactContextBaseJavaModule { | ||
|
||
private ActionMode lastActionMode; | ||
|
||
public RNSelectableTextInputModule(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getName() { | ||
return "RNSelectableTextInputManager"; | ||
} | ||
|
||
@ReactMethod | ||
public void setupMenuItems(final Integer selectableTextViewReactTag, final Integer textInputReactTag) { | ||
ReactApplicationContext reactContext = this.getReactApplicationContext(); | ||
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); | ||
uiManager.addUIBlock(new UIBlock() { | ||
public void execute (NativeViewHierarchyManager nvhm) { | ||
RNSelectableTextInputViewManager rnSelectableTextManager = (RNSelectableTextInputViewManager) nvhm.resolveViewManager(selectableTextViewReactTag); | ||
ReactEditText reactTextView = (ReactEditText) nvhm.resolveView(textInputReactTag); | ||
rnSelectableTextManager.registerSelectionListener(reactTextView); | ||
} | ||
}); | ||
} | ||
|
||
@ReactMethod | ||
public void startActionMode(final Integer textInputReactTag) { | ||
ReactApplicationContext reactContext = this.getReactApplicationContext(); | ||
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); | ||
uiManager.addUIBlock(new UIBlock() { | ||
public void execute (NativeViewHierarchyManager nvhm) { | ||
ReactEditText reactTextView = (ReactEditText) nvhm.resolveView(textInputReactTag); | ||
lastActionMode = reactTextView.startActionMode(reactTextView.getCustomSelectionActionModeCallback(), ActionMode.TYPE_FLOATING); | ||
} | ||
}); | ||
} | ||
|
||
@ReactMethod | ||
public void hideLastActionMode(){ | ||
ReactApplicationContext reactContext = this.getReactApplicationContext(); | ||
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); | ||
uiManager.addUIBlock(new UIBlock() { | ||
public void execute (NativeViewHierarchyManager nvhm) { | ||
if(lastActionMode!=null){ | ||
lastActionMode.finish(); | ||
lastActionMode = null; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@ReactMethod | ||
public void setSelection(final Integer textInputReactTag, final Integer start, final Integer end){ | ||
ReactApplicationContext reactContext = this.getReactApplicationContext(); | ||
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class); | ||
uiManager.addUIBlock(new UIBlock() { | ||
public void execute (NativeViewHierarchyManager nvhm) { | ||
ReactEditText reactTextView = (ReactEditText) nvhm.resolveView(textInputReactTag); | ||
reactTextView.setSelection(start, end); | ||
} | ||
}); | ||
} | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
...tus/android/src/main/java/im/status/ethereum/module/RNSelectableTextInputViewManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package im.status.ethereum.module; | ||
|
||
|
||
import android.view.ActionMode; | ||
import android.view.ActionMode.Callback; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
|
||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.ReactContext; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.bridge.WritableMap; | ||
import com.facebook.react.common.MapBuilder; | ||
import com.facebook.react.uimanager.ThemedReactContext; | ||
import com.facebook.react.uimanager.annotations.ReactProp; | ||
import com.facebook.react.uimanager.events.RCTEventEmitter; | ||
import com.facebook.react.views.textinput.ReactEditText; | ||
import com.facebook.react.views.view.ReactViewGroup; | ||
import com.facebook.react.views.view.ReactViewManager; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class RNSelectableTextInputViewManager extends ReactViewManager { | ||
public static final String REACT_CLASS = "RNSelectableTextInput"; | ||
private String[] _menuItems = new String[0]; | ||
|
||
|
||
@Override | ||
public String getName() { | ||
return REACT_CLASS; | ||
} | ||
|
||
@Override | ||
public ReactViewGroup createViewInstance(ThemedReactContext context) { | ||
return new ReactViewGroup(context); | ||
} | ||
|
||
|
||
@ReactProp(name = "menuItems") | ||
public void setMenuItems(ReactViewGroup reactViewGroup, ReadableArray items) { | ||
if(items != null) { | ||
List<String> result = new ArrayList<String>(items.size()); | ||
for (int i = 0; i < items.size(); i++) { | ||
result.add(items.getString(i)); | ||
} | ||
|
||
this._menuItems = result.toArray(new String[items.size()]); | ||
|
||
// PREVIOUS CODE | ||
// registerSelectionListener(result.toArray(new String[items.size()]), reactViewGroup); | ||
} | ||
|
||
} | ||
|
||
public void registerSelectionListener(final ReactEditText view) { | ||
view.setCustomSelectionActionModeCallback(new Callback() { | ||
@Override | ||
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { | ||
// Called when action mode is first created. The menu supplied | ||
// will be used to generate action buttons for the action mode | ||
// Android Smart Linkify feature pushes extra options into the menu | ||
// and would override the generated menu items | ||
menu.clear(); | ||
for (int i = 0; i < _menuItems.length; i++) { | ||
menu.add(0, i, 0, _menuItems[i]); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onCreateActionMode(ActionMode mode, Menu menu) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onDestroyActionMode(ActionMode mode) { | ||
// Called when an action mode is about to be exited and | ||
} | ||
|
||
@Override | ||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { | ||
int selectionStart = view.getSelectionStart(); | ||
int selectionEnd = view.getSelectionEnd(); | ||
String selectedText = view.getText().toString().substring(selectionStart, selectionEnd); | ||
|
||
// Dispatch event | ||
onSelectNativeEvent(view, item.getItemId(), selectedText, selectionStart, selectionEnd); | ||
|
||
mode.finish(); | ||
|
||
return true; | ||
} | ||
|
||
}); | ||
} | ||
|
||
public void onSelectNativeEvent(ReactEditText view, int eventType, String content, int selectionStart, int selectionEnd) { | ||
WritableMap event = Arguments.createMap(); | ||
event.putInt("eventType", eventType); | ||
event.putString("content", content); | ||
event.putInt("selectionStart", selectionStart); | ||
event.putInt("selectionEnd", selectionEnd); | ||
|
||
// Dispatch | ||
ReactContext reactContext = (ReactContext) view.getContext(); | ||
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(view.getId(), "topSelection", event); | ||
} | ||
|
||
@Override | ||
public Map getExportedCustomDirectEventTypeConstants() { | ||
return MapBuilder.builder() | ||
.put("topSelection", MapBuilder.of("registrationName","onSelection")) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.