Files
notesnook/apps/mobile/NotesnookShare.js

495 lines
14 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-01-08 12:30:20 +05:00
View,
} 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-01-08 12:30:20 +05:00
import {COLOR_SCHEME_DARK, COLOR_SCHEME_LIGHT} from './src/utils/Colors';
import {db} from './src/utils/DB';
2021-07-07 11:13:43 +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-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',
'link',
]),
allowedClasses: true,
allowVulnerableTags: true,
allowedAttributes: false,
allowProtocolRelative: true,
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',
data: null,
},
};
let editorContentValue = null;
const NotesnookShare = () => {
2021-07-06 15:01:52 +05:00
const [colors, setColors] = useState(
Appearance.getColorScheme() === 'dark'
? COLOR_SCHEME_DARK
: COLOR_SCHEME_LIGHT,
);
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,
value: null,
});
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 = {
top: Platform.OS === 'ios' ? 30 : 0,
};
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,
easing: Easing.in(Easing.ease),
}).start();
timing(translate, {
toValue: translateV,
duration: 300,
easing: Easing.in(Easing.ease),
}).start();
};
2021-07-06 15:01:52 +05:00
useEffect(() => {
Keyboard.addListener('keyboardWillChangeFrame', onKeyboardWillChangeFrame);
return () => {
Keyboard.removeListener(
'keyboardWillChangeFrame',
onKeyboardWillChangeFrame,
);
};
});
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 () => {
2021-07-07 08:46:29 +05:00
setNote(defaultNote);
setLoadingIntent(true);
2021-07-07 11:44:15 +05:00
animate(0, 1000);
await sleep(300);
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,
value,
};
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-07 11:44:15 +05:00
opacity: Platform.OS !== 'ios' ? opacity : 1,
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%',
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-07 08:46:29 +05:00
backgroundColor: 'rgba(0,0,0,0.01)',
}}
/>
</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: [
{
translateY: Platform.OS !== 'ios' ? translate : 0,
},
],
2021-07-07 08:46:29 +05:00
}}
behavior="padding">
{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',
alignItems: 'center',
}}>
<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,
marginTop: 5,
}}>
Parsing Data...
</Text>
</View>
) : (
<>
2021-01-08 12:30:20 +05:00
<View
style={{
2021-07-07 08:46:29 +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
flexDirection: 'row',
alignItems: 'center',
borderBottomWidth: 1,
borderBottomColor: colors.nav,
paddingHorizontal: 12,
justifyContent: 'space-between',
2021-02-09 16:32:27 +05:00
}}>
2021-07-07 08:46:29 +05:00
<TextInput
ref={titleInputRef}
style={{
fontSize: 25,
fontWeight: 'bold',
color: colors.pri,
flexGrow: 1,
maxWidth: '100%',
}}
placeholderTextColor={colors.icon}
value={note?.title}
onChangeText={v =>
setNote(_note => {
_note.title = v;
return _note;
})
}
onSubmitEditing={() => {
textInputRef.current?.focus();
}}
blurOnSubmit={false}
placeholder="Note title"
/>
</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,
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%',
backgroundColor: 'transparent',
}}
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',
baseUrl: 'file:///android_asset/',
}
}
/>
</View>
<TextInput
ref={textInputRef}
style={{
fontSize: 15,
color: colors.pri,
marginBottom: 10,
2021-07-07 11:13:43 +05:00
width: '95%',
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',
borderRadius: 5,
2021-07-08 09:34:21 +05:00
marginTop: 10,
minHeight: 80,
paddingTop: 12,
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-07-07 11:44:15 +05:00
flexDirection: 'row',
justifyContent: validator.isURL(rawData.value)
? 'space-between'
: 'flex-end',
2021-07-07 08:46:29 +05:00
}}>
2021-07-07 11:44:15 +05:00
<Button
style={{
width: null,
paddingHorizontal: 10,
backgroundColor: colors.nav,
marginRight: 10,
}}
textStyle={{
color: colors.icon,
}}
title="Cancel"
onPress={close}
/>
2021-07-07 08:46:29 +05:00
{validator.isURL(rawData.value) && (
<Button
title="Clip Webpage"
color={colors.accent}
2021-07-07 11:44:15 +05:00
style={{
marginRight: 10,
}}
2021-07-07 08:46:29 +05:00
onPress={async () => {
let html = await sanitizeHtml(rawData.value);
setNote(note => {
note.content.data = html;
return note;
});
onLoad();
2021-02-09 16:32:27 +05:00
}}
/>
2021-07-07 08:46:29 +05:00
)}
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-02-09 16:32:27 +05:00
/>
</View>
2021-07-07 08:46:29 +05:00
<View
style={{
height: 25,
}}
/>
</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-07 11:44:15 +05:00
paddingHorizontal: 20,
2021-07-07 08:46:29 +05:00
},
style,
]}>
{loading && <ActivityIndicator color="white" />}
<Text
2021-07-07 11:13:43 +05:00
style={[
{
2021-07-07 11:44:15 +05:00
fontSize: 15,
2021-07-07 11:13:43 +05:00
fontWeight: 'bold',
color: 'white',
marginLeft: loading ? 10 : 0,
},
textStyle,
]}>
2021-07-07 08:46:29 +05:00
{title}
</Text>
</TouchableOpacity>
);
};
export default NotesnookShare;