Skip to content
This repository was archived by the owner on Jun 24, 2021. It is now read-only.

Commit 9f4d44f

Browse files
committed
Initial commit
0 parents  commit 9f4d44f

15 files changed

+633
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj -text

.gitignore

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# OSX
3+
#
4+
.DS_Store
5+
6+
# node.js
7+
#
8+
node_modules/
9+
npm-debug.log
10+
yarn-error.log
11+
12+
13+
# Xcode
14+
#
15+
build/
16+
*.pbxuser
17+
!default.pbxuser
18+
*.mode1v3
19+
!default.mode1v3
20+
*.mode2v3
21+
!default.mode2v3
22+
*.perspectivev3
23+
!default.perspectivev3
24+
xcuserdata
25+
*.xccheckout
26+
*.moved-aside
27+
DerivedData
28+
*.hmap
29+
*.ipa
30+
*.xcuserstate
31+
project.xcworkspace
32+
33+
34+
# Android/IntelliJ
35+
#
36+
build/
37+
.idea
38+
.gradle
39+
local.properties
40+
*.iml
41+
42+
# BUCK
43+
buck-out/
44+
\.buckd/
45+
*.keystore
46+

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
# react-native-selectable-text
3+
4+
## Getting started
5+
6+
`$ npm install react-native-selectable-text --save`
7+
8+
### Mostly automatic installation
9+
10+
`$ react-native link react-native-selectable-text`
11+
12+
### Manual installation
13+
14+
15+
#### iOS
16+
17+
1. In XCode, in the project navigator, right click `Libraries``Add Files to [your project's name]`
18+
2. Go to `node_modules``react-native-selectable-text` and add `RNSelectableText.xcodeproj`
19+
3. In XCode, in the project navigator, select your project. Add `libRNSelectableText.a` to your project's `Build Phases``Link Binary With Libraries`
20+
4. Run your project (`Cmd+R`)<
21+
22+
#### Android
23+
24+
1. Open up `android/app/src/main/java/[...]/MainActivity.java`
25+
- Add `import com.astrocoders.selectabletext.RNSelectableTextPackage;` to the imports at the top of the file
26+
- Add `new RNSelectableTextPackage()` to the list returned by the `getPackages()` method
27+
2. Append the following lines to `android/settings.gradle`:
28+
```
29+
include ':react-native-selectable-text'
30+
project(':react-native-selectable-text').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-selectable-text/android')
31+
```
32+
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
33+
```
34+
compile project(':react-native-selectable-text')
35+
```
36+
37+
38+
## Usage
39+
```javascript
40+
import RNSelectableText from 'react-native-selectable-text';
41+
42+
// TODO: What to do with the module?
43+
RNSelectableText;
44+
```
45+

android/build.gradle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
buildscript {
3+
repositories {
4+
jcenter()
5+
}
6+
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:1.3.1'
9+
}
10+
}
11+
12+
apply plugin: 'com.android.library'
13+
14+
android {
15+
compileSdkVersion 23
16+
buildToolsVersion "23.0.1"
17+
18+
defaultConfig {
19+
minSdkVersion 16
20+
targetSdkVersion 22
21+
versionCode 1
22+
versionName "1.0"
23+
}
24+
lintOptions {
25+
abortOnError false
26+
}
27+
}
28+
29+
repositories {
30+
mavenCentral()
31+
}
32+
33+
dependencies {
34+
compile 'com.facebook.react:react-native:+'
35+
}
36+

android/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.astrocoders.selectabletext">
4+
5+
</manifest>
6+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.astrocoders.selectabletext;
2+
3+
import android.content.Context;
4+
import android.view.ViewGroup;
5+
import com.facebook.react.views.view.ReactViewGroup;
6+
7+
public class RNSelectableTextLayout extends ReactViewGroup {
8+
9+
public RNSelectableTextLayout(Context context) {
10+
super(context);
11+
}
12+
13+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.astrocoders.selectabletext;
2+
3+
import com.facebook.react.uimanager.SimpleViewManager;
4+
import com.facebook.react.uimanager.ThemedReactContext;
5+
import com.facebook.react.bridge.WritableMap;
6+
7+
import com.facebook.react.uimanager.annotations.ReactProp;
8+
import com.facebook.react.uimanager.ReactTextView;
9+
10+
11+
public class RNSelectableTextManager extends SimpleViewManager<ReactTextView> {
12+
public static final String REACT_CLASS = "RNSelectableText";
13+
14+
String[] menuItems;
15+
16+
@Override
17+
public String getName() {
18+
return REACT_CLASS;
19+
}
20+
21+
@Overrid
22+
public ReactTextView createViewInstance(ThemedReactContext context){
23+
return new ReactTextView(context);
24+
}
25+
26+
public RNSelectableTextManager(Context context) {
27+
super(context);
28+
29+
registerSelectionListener(view)
30+
}
31+
32+
@ReactProp(name = "menuItems")
33+
public void setMenuItems(ReactTextView textView, String[] items) {
34+
menuItems = items;
35+
}
36+
37+
public void registerSelectionListener(ReactTextView view) {
38+
view.setCustomSelectionActionModeCallback(new Callback() {
39+
@Override
40+
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
41+
menu.clear();
42+
return true;
43+
}
44+
45+
@Override
46+
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
47+
// Called when action mode is first created. The menu supplied
48+
// will be used to generate action buttons for the action mode
49+
50+
for (int i=0; i<menuItems.length; i++) {
51+
menu.add(0, i, 0, menuItems[i]);
52+
}
53+
54+
return true;
55+
}
56+
57+
@Override
58+
public void onDestroyActionMode(ActionMode mode) {
59+
// Called when an action mode is about to be exited and
60+
}
61+
62+
@Override
63+
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
64+
65+
int min = 0;
66+
int max = view.getText().length();
67+
if (view.isFocused()) {
68+
// TODO: yield selection positions back to user also
69+
final int selStart = view.getSelectionStart();
70+
final int selEnd = view.getSelectionEnd();
71+
72+
min = Math.max(0, Math.min(selStart, selEnd));
73+
max = Math.max(0, Math.max(selStart, selEnd));
74+
}
75+
// Perform your definition lookup with the selected text
76+
final CharSequence selectedText = view.getText().subSequence(min, max);
77+
78+
// Finish and close the ActionMode
79+
mode.finish();
80+
81+
// Dispatch event
82+
onSelectNativeEvent(menuItems[item.getItemId()], selectedText);
83+
84+
return true;
85+
return false;
86+
}
87+
88+
});
89+
}
90+
91+
public void onSelectNativeEvent(String eventType, String content) {
92+
WritableMap event = Arguments.createMap();
93+
event.putString("eventType", eventType);
94+
event.putString("content", content);
95+
96+
// Dispatch
97+
ReactContext reactContext = (ReactContext)getContext();
98+
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
99+
getId(),
100+
"topChange",
101+
event
102+
);
103+
}
104+
105+
@Override
106+
public Map getExportedCustomDirectEventTypeConstants() {
107+
return MapBuilder.of(
108+
"topChange",
109+
MapBuilder.of("onSelection"));
110+
}
111+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
package com.astrocoders.selectabletext;
3+
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import com.facebook.react.ReactPackage;
9+
import com.facebook.react.bridge.NativeModule;
10+
import com.facebook.react.bridge.ReactApplicationContext;
11+
import com.facebook.react.uimanager.ViewManager;
12+
import com.facebook.react.bridge.JavaScriptModule;
13+
public class RNSelectableTextPackage implements ReactPackage {
14+
@Override
15+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
16+
return Arrays.<NativeModule>asList(new RNSelectableTextModule(reactContext));
17+
}
18+
19+
// Deprecated from RN 0.47
20+
public List<Class<? extends JavaScriptModule>> createJSModules() {
21+
return Collections.emptyList();
22+
}
23+
24+
@Override
25+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
26+
return Collections.emptyList();
27+
}
28+
}

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {requireNativeComponent} from 'react-native';
2+
3+
const { RNSelectableText } = requireNativeComponent('RNSelectableText');
4+
5+
export const SelectableText = ({ ...props, onSelection }, forwardedRef) => {
6+
const onSelectionNative = ({ nativeEvent: { content, eventType } }) => {
7+
onSelection({ content, eventType })
8+
}
9+
10+
return (
11+
<RNSelectableText {...props} ref={forwardedRef} onSelection={onSelectionNative} />
12+
)
13+
}

ios/RNSelectableText.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#if __has_include("RCTBridgeModule.h")
3+
#import "RCTBridgeModule.h"
4+
#else
5+
#import <React/RCTBridgeModule.h>
6+
#endif
7+
8+
@interface RNSelectableText : NSObject <RCTBridgeModule>
9+
10+
@end
11+

ios/RNSelectableText.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#import "RNSelectableText.h"
3+
4+
@implementation RNSelectableText
5+
6+
- (dispatch_queue_t)methodQueue
7+
{
8+
return dispatch_get_main_queue();
9+
}
10+
RCT_EXPORT_MODULE()
11+
12+
@end
13+

ios/RNSelectableText.podspec

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
Pod::Spec.new do |s|
3+
s.name = "RNSelectableText"
4+
s.version = "1.0.0"
5+
s.summary = "RNSelectableText"
6+
s.description = <<-DESC
7+
RNSelectableText
8+
DESC
9+
s.homepage = ""
10+
s.license = "MIT"
11+
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }
12+
s.author = { "author" => "[email protected]" }
13+
s.platform = :ios, "7.0"
14+
s.source = { :git => "https://github.com/author/RNSelectableText.git", :tag => "master" }
15+
s.source_files = "RNSelectableText/**/*.{h,m}"
16+
s.requires_arc = true
17+
18+
19+
s.dependency "React"
20+
#s.dependency "others"
21+
22+
end
23+
24+

0 commit comments

Comments
 (0)