Files
notesnook/apps/mobile/NotesnookShare.js

468 lines
13 KiB
JavaScript
Raw Normal View History

2021-01-08 12:30:20 +05:00
import React, {Component, createRef} from 'react';
import {Keyboard} from 'react-native';
2021-01-08 12:30:20 +05:00
import {
ActivityIndicator,
Appearance,
KeyboardAvoidingView,
Modal,
Platform,
Text,
TouchableOpacity,
View,
ScrollView,
TextInput,
2021-01-08 12:30:20 +05:00
} from 'react-native';
import {COLOR_SCHEME_DARK, COLOR_SCHEME_LIGHT} from './src/utils/Colors';
import {db} from './src/utils/DB';
2021-02-09 16:32:27 +05:00
import {SIZE} from './src/utils/SizeUtils';
2021-01-08 12:30:20 +05:00
import Storage from './src/utils/storage';
import {sleep} from './src/utils/TimeUtils';
2021-02-10 21:57:08 +05:00
let validator;
let linkPreview;
let ShareExtension;
2021-01-08 12:30:20 +05:00
export default class NotesnookShare extends Component {
constructor(props, context) {
super(props, context);
this.state = {
isOpen: true,
text: '',
title: '',
loading: false,
2021-02-09 16:32:27 +05:00
loadingIntent: true,
2021-01-08 12:30:20 +05:00
colors:
Appearance.getColorScheme() === 'dark'
? COLOR_SCHEME_DARK
: COLOR_SCHEME_LIGHT,
height: 0,
floating: false,
2021-01-08 12:30:20 +05:00
};
2021-02-09 16:12:43 +05:00
this.initialText = '';
2021-01-08 12:30:20 +05:00
this.textInputRef = createRef();
this.titleInputRef = createRef();
}
async componentDidMount() {
Keyboard.addListener(
'keyboardWillChangeFrame',
this.onKeyboardWillChangeFrame,
);
2021-01-08 12:30:20 +05:00
try {
ShareExtension = require('rn-extensions-share').default;
validator = require('validator').default;
linkPreview = require('link-preview-js');
2021-01-08 12:30:20 +05:00
const data = await ShareExtension.data();
let text;
2021-02-09 14:57:18 +05:00
let item = data[0];
if (item.type === 'text') {
text = item.value;
}
2021-02-09 16:32:27 +05:00
if (validator.isURL(text)) {
linkPreview
.getLinkPreview(text)
.then(r => {
2021-02-09 16:32:27 +05:00
if (r?.siteName) {
this.setState({
title: r.siteName,
text: text,
loadingIntent: false,
});
} else if (r?.title) {
this.setState({
title: r.title,
text: text,
loadingIntent: false,
});
} else {
this.setState({
title: 'Web Link',
text: text,
loadingIntent: false,
});
}
})
.catch(e => {
2021-02-09 16:32:27 +05:00
this.setState({
title: 'Web Link',
text: text,
loadingIntent: false,
});
});
} else {
this.setState({
text: text,
loadingIntent: false,
});
}
2021-02-09 16:12:43 +05:00
this.initialText = text;
2021-01-08 12:30:20 +05:00
} catch (e) {
console.log('errrr', e);
}
}
componentWillUnmount() {
Keyboard.removeListener(
'keyboardWillChangeFrame',
this.onKeyboardWillChangeFrame,
);
}
2021-01-08 12:30:20 +05:00
close = () => {
this.setState({
text: null,
});
ShareExtension.close();
};
onPress = async () => {
this.titleInputRef.current?.blur();
2021-02-04 14:37:04 +05:00
this.textInputRef.current?.blur();
2021-01-08 12:30:20 +05:00
this.setState({
loading: true,
});
2021-02-09 14:57:18 +05:00
2021-02-09 16:12:43 +05:00
let tag = validator.isURL(this.initialText)
? `<a href='${this.initialText}' target='_blank'>${
this.state.text.split(' ')[0]
}</a>
<p>${
this.state.text.split(' ').length > 0
? this.state.text.split(' ').slice(1).join(' ')
: ''
} </p>`
2021-02-09 14:57:18 +05:00
: `<p>${this.state.text}</p>`;
2021-02-04 14:37:04 +05:00
let add = async () => {
2021-01-08 12:30:20 +05:00
await db.notes.add({
title: this.state.title,
2021-02-05 10:15:16 +05:00
content: {
2021-02-09 14:50:18 +05:00
type: 'tiny',
2021-02-09 14:57:18 +05:00
data: tag,
2021-02-05 10:15:16 +05:00
},
2021-01-08 12:30:20 +05:00
id: null,
});
2021-02-04 14:37:04 +05:00
};
if (db && db.notes) {
await add();
2021-01-08 12:30:20 +05:00
} else {
await db.init();
2021-02-04 14:37:04 +05:00
await add();
2021-01-08 12:30:20 +05:00
}
await Storage.write('notesAddedFromIntent', 'added');
await sleep(500);
this.close();
};
saveBtn = () => (
<View
style={{
paddingHorizontal: 12,
}}>
<TouchableOpacity
onPress={this.onPress}
activeOpacity={0.8}
style={{
backgroundColor: this.state.colors.accent,
width: '100%',
height: 50,
borderRadius: 5,
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
}}>
{this.state.loading && (
<ActivityIndicator color={this.state.colors.light} />
)}
<Text
style={{
fontSize: 18,
fontWeight: 'bold',
color: this.state.colors.light,
marginLeft: this.state.loading ? 10 : 0,
}}>
Save Note
</Text>
</TouchableOpacity>
</View>
);
onKeyboardWillChangeFrame = event => {
this.setState({floating: event.endCoordinates.width !== windowWidth});
};
2021-01-08 12:30:20 +05:00
render() {
return Platform.OS === 'ios' ? (
<View
style={{
width: '100%',
height: '100%',
justifyContent: 'flex-end',
}}>
<TouchableOpacity
activeOpacity={1}
onPress={() => {
ShareExtension.close();
}}
style={{
width: '100%',
height: '100%',
position: 'absolute',
}}>
<View
style={{
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0.01)',
}}
/>
</TouchableOpacity>
<KeyboardAvoidingView
enabled={!this.state.floating && Platform.OS === 'ios'}
2021-01-08 12:30:20 +05:00
style={{
paddingVertical: 25,
backgroundColor: this.state.colors.bg,
borderTopRightRadius: 10,
borderTopLeftRadius: 10,
}}
behavior="padding">
2021-02-09 16:32:27 +05:00
{this.state.loadingIntent ? (
2021-01-08 12:30:20 +05:00
<View
style={{
2021-02-09 16:32:27 +05:00
height: 150,
width: '100%',
justifyContent: 'center',
2021-01-08 12:30:20 +05:00
alignItems: 'center',
}}>
2021-02-09 16:32:27 +05:00
<ActivityIndicator color={this.state.colors.accent} />
<Text
2021-01-08 12:30:20 +05:00
style={{
color: this.state.colors.pri,
2021-02-09 16:32:27 +05:00
fontSize: SIZE.md,
marginTop: 5,
}}>
Parsing Data...
</Text>
</View>
) : (
<>
<View
2021-01-08 12:30:20 +05:00
style={{
2021-02-09 16:32:27 +05:00
maxHeight: '100%',
2021-01-08 12:30:20 +05:00
}}>
2021-02-09 16:32:27 +05:00
<View
2021-01-08 12:30:20 +05:00
style={{
2021-02-09 16:32:27 +05:00
flexDirection: 'row',
alignItems: 'center',
borderBottomWidth: 1,
borderBottomColor: this.state.colors.nav,
paddingHorizontal: 12,
justifyContent: 'space-between',
2021-01-08 12:30:20 +05:00
}}>
2021-02-09 16:32:27 +05:00
<TextInput
ref={this.titleInputRef}
style={{
fontSize: 25,
fontWeight: 'bold',
color: this.state.colors.pri,
flexGrow: 1,
maxWidth: '85%',
}}
placeholderTextColor={this.state.colors.icon}
value={this.state.title}
onChangeText={v => this.setState({title: v})}
2021-02-09 16:32:27 +05:00
onSubmitEditing={() => {
this.textInputRef.current?.focus();
}}
blurOnSubmit={false}
2021-02-16 16:11:10 +05:00
placeholder="Note title"
2021-02-09 16:32:27 +05:00
/>
2021-01-08 12:30:20 +05:00
2021-02-09 16:32:27 +05:00
<TouchableOpacity
onPress={this.close}
activeOpacity={0.8}
style={{
width: 50,
height: 40,
borderRadius: 5,
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
}}>
<Text
style={{
fontSize: 14,
color: this.state.colors.accent,
marginLeft: this.state.loading ? 10 : 0,
}}>
Cancel
</Text>
</TouchableOpacity>
</View>
<TextInput
ref={this.textInputRef}
style={{
fontSize: 15,
color: this.state.colors.pri,
marginBottom: 10,
width: '100%',
maxHeight: '70%',
paddingVertical: 10,
paddingHorizontal: 12,
}}
placeholderTextColor={this.state.colors.icon}
onChangeText={v => this.setState({text: v})}
2021-02-09 16:32:27 +05:00
multiline={true}
value={this.state.text}
blurOnSubmit={false}
placeholder="Type your note here"
/>
{this.saveBtn()}
<View
style={{
height: 25,
}}
/>
</View>
</>
)}
2021-01-08 12:30:20 +05:00
</KeyboardAvoidingView>
</View>
) : (
<Modal visible transparent>
<View
onLayout={event => {
2021-01-08 12:30:20 +05:00
console.log(event.nativeEvent.layout.height);
this.setState({
height: event.nativeEvent.layout.height,
});
}}
style={{
justifyContent: 'flex-end',
width: '100%',
height: '100%',
}}>
<TouchableOpacity
activeOpacity={0}
onPress={() => {
ShareExtension.close();
}}
style={{
width: '100%',
height: '100%',
position: 'absolute',
}}
/>
<View
style={{
paddingVertical: 25,
backgroundColor: this.state.colors.bg,
borderTopRightRadius: 10,
borderTopLeftRadius: 10,
width: '100%',
2021-02-04 14:37:04 +05:00
paddingTop: 15,
2021-01-08 12:30:20 +05:00
}}>
2021-02-09 16:32:27 +05:00
{this.state.loadingIntent ? (
2021-01-08 12:30:20 +05:00
<View
style={{
2021-02-09 16:32:27 +05:00
height: 150,
width: '100%',
justifyContent: 'center',
2021-01-08 12:30:20 +05:00
alignItems: 'center',
}}>
2021-02-09 16:32:27 +05:00
<ActivityIndicator color={this.state.colors.accent} />
<Text
2021-01-08 12:30:20 +05:00
style={{
color: this.state.colors.pri,
2021-02-09 16:32:27 +05:00
fontSize: SIZE.md,
marginTop: 5,
}}>
Parsing Data...
</Text>
</View>
) : (
<>
<ScrollView
2021-01-08 12:30:20 +05:00
style={{
2021-02-09 16:32:27 +05:00
maxHeight: this.state.height * 0.8,
2021-01-08 12:30:20 +05:00
}}>
2021-02-09 16:32:27 +05:00
<View
2021-01-08 12:30:20 +05:00
style={{
2021-02-09 16:32:27 +05:00
flexDirection: 'row',
alignItems: 'center',
borderBottomWidth: 1,
borderBottomColor: this.state.colors.nav,
paddingHorizontal: 12,
justifyContent: 'space-between',
2021-01-08 12:30:20 +05:00
}}>
2021-02-09 16:32:27 +05:00
<TextInput
ref={this.titleInputRef}
style={{
fontSize: SIZE.lg,
fontWeight: 'bold',
color: this.state.colors.pri,
flexGrow: 1,
maxWidth: '85%',
}}
placeholderTextColor={this.state.colors.icon}
value={this.state.title}
onChangeText={v => this.setState({title: v})}
2021-02-09 16:32:27 +05:00
onSubmitEditing={() => {
this.textInputRef.current?.focus();
}}
blurOnSubmit={false}
2021-02-16 16:11:10 +05:00
placeholder="Note title"
2021-02-09 16:32:27 +05:00
/>
2021-01-08 12:30:20 +05:00
2021-02-09 16:32:27 +05:00
<TouchableOpacity
onPress={this.close}
activeOpacity={0.8}
style={{
width: 70,
height: 40,
borderRadius: 5,
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
}}>
<Text
style={{
fontSize: 14,
color: this.state.colors.accent,
marginLeft: this.state.loading ? 10 : 0,
}}>
Cancel
</Text>
</TouchableOpacity>
</View>
<TextInput
ref={this.textInputRef}
style={{
fontSize: 15,
color: this.state.colors.pri,
paddingTop: 0,
marginBottom: 10,
paddingHorizontal: 12,
}}
placeholderTextColor={this.state.colors.icon}
onChangeText={v => this.setState({text: v})}
2021-02-09 16:32:27 +05:00
multiline={true}
value={this.state.text}
placeholder="Type your note here"
/>
</ScrollView>
{this.saveBtn()}
</>
)}
2021-01-08 12:30:20 +05:00
</View>
</View>
</Modal>
);
}
}