Files
notesnook/apps/mobile/share/index.js

540 lines
15 KiB
JavaScript
Raw Normal View History

2021-07-07 11:13:43 +05:00
import absolutify from 'absolutify';
import {getLinkPreview} from 'link-preview-js';
import React, {useEffect, useRef, useState} from 'react';
2021-01-08 12:30:20 +05:00
import {
ActivityIndicator,
Appearance,
2021-07-07 11:13:43 +05:00
Keyboard,
2021-01-08 12:30:20 +05:00
KeyboardAvoidingView,
Platform,
2021-07-08 09:34:21 +05:00
SafeAreaView,
2021-01-08 12:30:20 +05:00
Text,
2021-07-07 11:13:43 +05:00
TextInput,
2021-01-08 12:30:20 +05:00
TouchableOpacity,
2021-07-07 11:13:43 +05:00
useWindowDimensions,
2021-07-30 10:18:14 +05:00
View
2021-01-08 12:30:20 +05:00
} from 'react-native';
2021-07-07 11:44:15 +05:00
import Animated, {Easing, timing, useValue} from 'react-native-reanimated';
2021-07-07 11:13:43 +05:00
import WebView from 'react-native-webview';
import ShareExtension from 'rn-extensions-share';
2021-07-06 15:01:52 +05:00
import sanitize from 'sanitize-html';
2021-07-07 11:13:43 +05:00
import validator from 'validator';
2021-10-02 10:09:12 +05:00
import {COLOR_SCHEME_DARK, COLOR_SCHEME_LIGHT} from '../src/utils/Colors';
2021-10-02 10:26:29 +05:00
import {db} from '../src/utils/database';
2021-10-02 10:09:12 +05:00
import {SIZE} from '../src/utils/SizeUtils';
import Storage from '../src/utils/storage';
import {sleep} from '../src/utils/TimeUtils';
2021-07-06 15:01:52 +05:00
2021-07-07 11:44:15 +05:00
const AnimatedKAV = Animated.createAnimatedComponent(KeyboardAvoidingView);
2021-07-08 09:34:21 +05:00
const AnimatedSAV = Animated.createAnimatedComponent(SafeAreaView);
2021-07-06 15:01:52 +05:00
async function sanitizeHtml(site) {
try {
let html = await fetch(site);
html = await html.text();
let siteHtml = sanitize(html, {
2021-07-07 11:13:43 +05:00
allowedTags: sanitize.defaults.allowedTags.concat([
2021-07-06 15:01:52 +05:00
'img',
2021-07-07 11:13:43 +05:00
'style',
'head',
2021-07-30 10:18:14 +05:00
'link'
2021-07-07 11:13:43 +05:00
]),
allowedClasses: true,
allowVulnerableTags: true,
allowedAttributes: false,
allowProtocolRelative: true,
2021-07-30 10:18:14 +05:00
allowedSchemes: false
2021-07-06 15:01:52 +05:00
});
return absolutify(siteHtml, site);
} catch (e) {
return '';
}
}
function makeHtmlFromUrl(url) {
2021-07-07 11:13:43 +05:00
return `<a style="overflow-wrap:anywhere;white-space:pre-wrap" href='${url}' target='_blank'>${url}</a>`;
2021-07-06 15:01:52 +05:00
}
function makeHtmlFromPlainText(text) {
2021-07-07 11:13:43 +05:00
if (!text) return '';
return `<p style="overflow-wrap:anywhere;white-space:pre-wrap" >${text}</p>`;
2021-07-06 15:01:52 +05:00
}
2021-07-07 08:46:29 +05:00
let defaultNote = {
title: null,
id: null,
content: {
type: 'tiny',
2021-07-30 10:18:14 +05:00
data: null
}
2021-07-07 08:46:29 +05:00
};
let editorContentValue = null;
const NotesnookShare = () => {
2021-07-06 15:01:52 +05:00
const [colors, setColors] = useState(
Appearance.getColorScheme() === 'dark'
? COLOR_SCHEME_DARK
2021-07-30 10:18:14 +05:00
: COLOR_SCHEME_LIGHT
2021-07-06 15:01:52 +05:00
);
2021-07-07 08:46:29 +05:00
const [note, setNote] = useState(defaultNote);
2021-07-06 15:01:52 +05:00
const [loadingIntent, setLoadingIntent] = useState(true);
2021-07-07 08:46:29 +05:00
const [loading, setLoading] = useState(false);
2021-07-06 15:01:52 +05:00
const [floating, setFloating] = useState(false);
const [rawData, setRawData] = useState({
type: null,
2021-07-30 10:18:14 +05:00
value: null
2021-07-06 15:01:52 +05:00
});
const textInputRef = useRef();
const titleInputRef = useRef();
2021-07-07 08:46:29 +05:00
const {width, height} = useWindowDimensions();
const webviewRef = useRef();
2021-07-07 11:44:15 +05:00
const opacity = useValue(0);
const translate = useValue(1000);
2021-07-08 09:34:21 +05:00
const insets = {
2021-07-30 10:18:14 +05:00
top: Platform.OS === 'ios' ? 30 : 0
2021-07-08 09:34:21 +05:00
};
2021-07-07 11:44:15 +05:00
const animate = (opacityV, translateV) => {
2021-07-08 09:34:21 +05:00
if (Platform.OS === 'ios') return;
2021-07-07 11:44:15 +05:00
timing(opacity, {
toValue: opacityV,
duration: 300,
2021-07-30 10:18:14 +05:00
easing: Easing.in(Easing.ease)
2021-07-07 11:44:15 +05:00
}).start();
timing(translate, {
toValue: translateV,
duration: 300,
2021-07-30 10:18:14 +05:00
easing: Easing.in(Easing.ease)
2021-07-07 11:44:15 +05:00
}).start();
};
2021-07-06 15:01:52 +05:00
useEffect(() => {
Keyboard.addListener('keyboardWillChangeFrame', onKeyboardWillChangeFrame);
return () => {
Keyboard.removeListener(
'keyboardWillChangeFrame',
2021-07-30 10:18:14 +05:00
onKeyboardWillChangeFrame
2021-07-06 15:01:52 +05:00
);
};
});
const onKeyboardWillChangeFrame = event => {
2021-07-08 09:34:21 +05:00
setFloating(event.endCoordinates.width !== width);
2021-07-06 15:01:52 +05:00
};
const showLinkPreview = async link => {
2021-07-07 08:46:29 +05:00
let _note = {...defaultNote};
2021-07-06 15:01:52 +05:00
_note.title = 'Web link share';
_note.content.data = !note.content.data
? makeHtmlFromUrl(link)
: note.content.data + '\n' + makeHtmlFromUrl(link);
try {
2021-07-07 08:46:29 +05:00
let preview = await getLinkPreview(link);
_note.title = preview.siteName || preview.title;
} catch (e) {
console.log(e);
}
2021-07-06 15:01:52 +05:00
setNote(_note);
};
const loadData = async () => {
2021-01-08 12:30:20 +05:00
try {
2021-07-07 11:44:15 +05:00
setNote(() => {
2021-07-07 08:46:29 +05:00
defaultNote.content.data = null;
return defaultNote;
});
2021-01-08 12:30:20 +05:00
const data = await ShareExtension.data();
2021-07-07 08:46:29 +05:00
for (item of data) {
if (item.type === 'text') {
setRawData(item);
if (validator.isURL(item.value)) {
await showLinkPreview(item.value);
} else {
setNote(note => {
note.title = 'Note Share';
note.content.data = note.content.data
? note.content.data + '\n' + makeHtmlFromPlainText(item.value)
: makeHtmlFromPlainText(item.value);
return note;
2021-02-09 16:32:27 +05:00
});
2021-07-07 08:46:29 +05:00
}
}
2021-02-09 16:32:27 +05:00
}
2021-07-07 08:46:29 +05:00
} catch (e) {}
setLoadingIntent(false);
};
2021-02-09 16:32:27 +05:00
2021-07-07 08:46:29 +05:00
useEffect(() => {
setNote(defaultNote);
loadData();
2021-07-07 11:44:15 +05:00
sleep(300).then(() => {
animate(1, 0);
});
2021-07-07 08:46:29 +05:00
}, []);
2021-07-07 11:44:15 +05:00
const close = async () => {
animate(0, 1000);
await sleep(300);
2021-07-30 10:18:14 +05:00
setNote(defaultNote);
setLoadingIntent(true);
2021-01-08 12:30:20 +05:00
ShareExtension.close();
};
2021-07-07 08:46:29 +05:00
const onLoad = () => {
postMessage(webviewRef, 'htmldiff', note.content?.data);
let theme = {...colors};
theme.factor = 1;
postMessage(webviewRef, 'theme', JSON.stringify(theme));
};
function postMessage(webview, type, value = null) {
let message = {
type: type,
2021-07-30 10:18:14 +05:00
value
2021-07-07 08:46:29 +05:00
};
webview.current?.postMessage(JSON.stringify(message));
}
const onPress = async () => {
titleInputRef.current?.blur();
textInputRef.current?.blur();
setLoading(true);
2021-02-09 14:57:18 +05:00
2021-02-04 14:37:04 +05:00
let add = async () => {
2021-07-07 08:46:29 +05:00
let _note = {...note};
2021-07-07 11:13:43 +05:00
_note.content.data =
_note.content.data + makeHtmlFromPlainText(editorContentValue);
2021-07-07 08:46:29 +05:00
await db.notes.add(note);
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');
2021-07-07 11:13:43 +05:00
setLoading(false);
2021-07-08 09:34:21 +05:00
await sleep(300);
2021-07-07 11:13:43 +05:00
close();
2021-01-08 12:30:20 +05:00
};
2021-07-08 09:34:21 +05:00
const sourceUri = 'Plain.bundle/site/plaineditor.html';
const onShouldStartLoadWithRequest = request => {
if (request.url.includes('/site/plaineditor.html')) {
return true;
} else {
return false;
}
};
2021-07-07 08:46:29 +05:00
return (
2021-07-08 09:34:21 +05:00
<AnimatedSAV
2021-07-07 08:46:29 +05:00
style={{
width: width > 500 ? 500 : width,
height: height,
justifyContent: 'flex-end',
2021-07-30 10:18:14 +05:00
opacity: Platform.OS !== 'ios' ? opacity : 1
2021-07-07 08:46:29 +05:00
}}>
2021-07-30 10:18:14 +05:00
2021-07-07 08:46:29 +05:00
<TouchableOpacity
activeOpacity={1}
onPress={() => {
close();
}}
2021-01-08 12:30:20 +05:00
style={{
2021-07-07 08:46:29 +05:00
width: '100%',
height: '100%',
2021-07-30 10:18:14 +05:00
position: 'absolute'
2021-01-08 12:30:20 +05:00
}}>
2021-07-07 08:46:29 +05:00
<View
2021-01-08 12:30:20 +05:00
style={{
width: '100%',
height: '100%',
2021-07-30 10:18:14 +05:00
backgroundColor: 'rgba(0,0,0,0.01)'
2021-07-07 08:46:29 +05:00
}}
/>
</TouchableOpacity>
2021-07-07 11:44:15 +05:00
<AnimatedKAV
2021-07-07 08:46:29 +05:00
enabled={!floating && Platform.OS === 'ios'}
style={{
paddingVertical: 25,
backgroundColor: colors.bg,
borderTopRightRadius: 10,
borderTopLeftRadius: 10,
2021-07-08 09:34:21 +05:00
marginBottom: insets.top,
2021-07-07 11:44:15 +05:00
transform: [
{
2021-07-30 10:18:14 +05:00
translateY: Platform.OS !== 'ios' ? translate : 0
}
]
2021-07-07 08:46:29 +05:00
}}
behavior="padding">
2021-07-30 10:18:14 +05:00
<View
style={{
width:50,
height:6,
borderRadius:100,
backgroundColor:colors.nav,
alignSelf:'center',
position:'absolute',
marginTop:15
}}
/>
2021-07-07 08:46:29 +05:00
{loadingIntent ? (
2021-01-08 12:30:20 +05:00
<View
style={{
2021-07-07 08:46:29 +05:00
height: 150,
2021-01-08 12:30:20 +05:00
width: '100%',
2021-07-07 08:46:29 +05:00
justifyContent: 'center',
2021-07-30 10:18:14 +05:00
alignItems: 'center'
2021-07-07 08:46:29 +05:00
}}>
<ActivityIndicator color={colors.accent} />
2021-01-08 12:30:20 +05:00
2021-07-07 08:46:29 +05:00
<Text
style={{
color: colors.pri,
fontSize: SIZE.md,
2021-07-30 10:18:14 +05:00
marginTop: 5
2021-07-07 08:46:29 +05:00
}}>
Parsing Data...
</Text>
</View>
) : (
<>
2021-01-08 12:30:20 +05:00
<View
style={{
2021-07-30 10:18:14 +05:00
maxHeight: '100%'
2021-01-08 12:30:20 +05:00
}}>
2021-07-07 08:46:29 +05:00
<View
2021-01-08 12:30:20 +05:00
style={{
2021-07-07 08:46:29 +05:00
borderBottomWidth: 1,
borderBottomColor: colors.nav,
2021-07-30 10:18:14 +05:00
paddingHorizontal: 12
2021-02-09 16:32:27 +05:00
}}>
2021-07-07 08:46:29 +05:00
<TextInput
ref={titleInputRef}
style={{
fontSize: 25,
2021-07-30 10:18:14 +05:00
fontFamily:
Platform.OS === 'android' ? 'Roboto-Medium' : null,
fontWeight: Platform.OS === 'ios' ? '600' : null,
2021-07-07 08:46:29 +05:00
color: colors.pri,
flexGrow: 1,
2021-07-30 10:18:14 +05:00
maxWidth: '100%'
2021-07-07 08:46:29 +05:00
}}
placeholderTextColor={colors.icon}
2021-07-30 10:18:14 +05:00
defaultValue={note?.title}
2021-07-07 08:46:29 +05:00
onChangeText={v =>
setNote(_note => {
_note.title = v;
return _note;
})
}
onSubmitEditing={() => {
textInputRef.current?.focus();
}}
blurOnSubmit={false}
placeholder="Note title"
/>
</View>
2021-07-30 10:18:14 +05:00
<View
style={{
flexDirection: 'row',
alignItems: 'center',
marginVertical: 10,
borderBottomWidth: 1,
borderBottomColor: colors.nav
}}>
{[
{
title: 'Plain text',
onPress: () => {
let html = validator.isURL(rawData.value)
? makeHtmlFromUrl(rawData.value)
: makeHtmlFromPlainText(rawData.value);
setNote(note => {
note.content.data = html;
return note;
});
onLoad();
}
},
...[
validator.isURL(rawData.value)
? {
title: 'Clip webpage',
onPress: async () => {
let html = await sanitizeHtml(rawData.value);
setNote(note => {
note.content.data = html;
return note;
});
onLoad();
}
}
: null
]
].map(
(item, index) =>
item && (
<Button
title={item.title}
color={colors.nav}
textStyle={{
color: colors.icon,
fontWeight: 'normal',
fontSize: 14,
fontFamily:null
}}
onPress={item.onPress}
style={{
borderWidth: 0.5,
borderRadius: 100,
borderColor: colors.icon,
height: 30,
marginRight: 10,
marginLeft: index === 0 ? 12 : 0,
paddingHorizontal: 12
}}
/>
)
)}
</View>
2021-02-09 16:32:27 +05:00
<View
2021-01-08 12:30:20 +05:00
style={{
2021-07-07 08:46:29 +05:00
height: height * 0.25,
2021-07-30 10:18:14 +05:00
width: '100%'
2021-01-08 12:30:20 +05:00
}}>
2021-07-07 08:46:29 +05:00
<WebView
onLoad={onLoad}
ref={webviewRef}
2021-01-08 12:30:20 +05:00
style={{
2021-07-07 08:46:29 +05:00
width: '100%',
height: '100%',
2021-07-30 10:18:14 +05:00
backgroundColor: 'transparent'
2021-07-07 08:46:29 +05:00
}}
cacheMode="LOAD_DEFAULT"
domStorageEnabled={true}
scrollEnabled={true}
bounces={false}
allowFileAccess={true}
scalesPageToFit={true}
allowingReadAccessToURL={
Platform.OS === 'android' ? true : null
}
2021-07-08 09:34:21 +05:00
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
2021-07-07 08:46:29 +05:00
allowFileAccessFromFileURLs={true}
allowUniversalAccessFromFileURLs={true}
originWhitelist={['*']}
javaScriptEnabled={true}
cacheEnabled={true}
source={
Platform.OS === 'ios'
? {uri: sourceUri}
: {
uri: 'file:///android_asset/plaineditor.html',
2021-07-30 10:18:14 +05:00
baseUrl: 'file:///android_asset/'
2021-07-07 08:46:29 +05:00
}
}
/>
</View>
<TextInput
ref={textInputRef}
style={{
fontSize: 15,
color: colors.pri,
marginBottom: 10,
2021-07-30 10:18:14 +05:00
width: '100%',
2021-07-07 08:46:29 +05:00
maxHeight: '70%',
2021-07-07 11:13:43 +05:00
padding: 12,
backgroundColor: colors.nav,
alignSelf: 'center',
2021-07-08 09:34:21 +05:00
marginTop: 10,
minHeight: 80,
paddingTop: 12,
2021-07-30 10:18:14 +05:00
paddingBottom: 12
2021-07-07 08:46:29 +05:00
}}
placeholderTextColor={colors.icon}
onChangeText={v => (editorContentValue = v)}
multiline={true}
2021-07-07 11:13:43 +05:00
numberOfLines={3}
textAlignVertical="top"
2021-07-07 08:46:29 +05:00
value={editorContentValue}
blurOnSubmit={false}
2021-07-07 11:44:15 +05:00
placeholder="Add some additional notes here"
2021-07-07 08:46:29 +05:00
/>
2021-07-07 11:13:43 +05:00
2021-07-07 08:46:29 +05:00
<View
style={{
paddingHorizontal: 12,
}}>
2021-01-08 12:30:20 +05:00
2021-07-07 08:46:29 +05:00
<Button
2021-07-07 11:13:43 +05:00
title={loading ? 'Saving note' : 'Save note'}
2021-07-07 08:46:29 +05:00
color={colors.accent}
2021-07-07 11:13:43 +05:00
onPress={onPress}
2021-07-07 08:46:29 +05:00
loading={loading}
2021-07-30 10:18:14 +05:00
style={{
width: '100%'
}}
2021-02-09 16:32:27 +05:00
/>
</View>
2021-07-07 08:46:29 +05:00
<View
style={{
2021-07-30 10:18:14 +05:00
height: 10
2021-07-07 08:46:29 +05:00
}}
/>
</View>
</>
)}
2021-07-07 11:44:15 +05:00
</AnimatedKAV>
2021-07-08 09:34:21 +05:00
</AnimatedSAV>
2021-07-07 08:46:29 +05:00
);
};
2021-07-07 11:13:43 +05:00
const Button = ({title, onPress, color, loading, style, textStyle}) => {
2021-07-07 08:46:29 +05:00
return (
<TouchableOpacity
onPress={onPress}
activeOpacity={0.8}
style={[
{
backgroundColor: color,
height: 50,
borderRadius: 5,
justifyContent: 'center',
alignItems: 'center',
flexDirection: 'row',
marginBottom: 10,
2021-07-08 09:34:21 +05:00
minWidth: 80,
2021-07-30 10:18:14 +05:00
paddingHorizontal: 20
2021-07-07 08:46:29 +05:00
},
2021-07-30 10:18:14 +05:00
style
2021-07-07 08:46:29 +05:00
]}>
{loading && <ActivityIndicator color="white" />}
<Text
2021-07-07 11:13:43 +05:00
style={[
{
2021-07-30 10:18:14 +05:00
fontSize: 18,
fontFamily: Platform.OS === 'android' ? 'Roboto-Medium' : null,
fontWeight: Platform.OS === 'ios' ? '600' : null,
2021-07-07 11:13:43 +05:00
color: 'white',
2021-07-30 10:18:14 +05:00
marginLeft: loading ? 10 : 0
2021-07-07 11:13:43 +05:00
},
2021-07-30 10:18:14 +05:00
textStyle
2021-07-07 11:13:43 +05:00
]}>
2021-07-07 08:46:29 +05:00
{title}
</Text>
</TouchableOpacity>
);
};
export default NotesnookShare;