-
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
386 additions
and
48 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...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,78 @@ | ||
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); | ||
} | ||
}); | ||
} | ||
|
||
} |
102 changes: 102 additions & 0 deletions
102
...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,102 @@ | ||
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()]); | ||
} | ||
} | ||
|
||
public void registerSelectionListener(final ReactEditText view) { | ||
view.setCustomSelectionActionModeCallback(new Callback() { | ||
@Override | ||
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { | ||
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) { | ||
} | ||
|
||
@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
Oops, something went wrong.