Files
notesnook/apps/mobile/src/components/EditorMenu/index.js

550 lines
15 KiB
JavaScript
Raw Normal View History

2020-01-05 18:03:40 +05:00
import React, {useState} from 'react';
import {
ScrollView,
View,
Text,
TouchableOpacity,
Platform,
FlatList,
ActivityIndicator,
2019-12-14 16:00:16 +05:00
KeyboardAvoidingView,
2020-01-08 11:25:26 +05:00
StatusBar,
} from 'react-native';
import {
SIZE,
pv,
opacity,
WEIGHT,
COLOR_SCHEME_DARK,
COLOR_SCHEME_LIGHT,
} from '../../common/common';
import Icon from 'react-native-vector-icons/Feather';
2019-12-21 11:12:55 +05:00
import {h} from '../../utils/utils';
2020-01-14 20:48:03 +05:00
import FastStorage from 'react-native-fast-storage';
import {AnimatedSafeAreaView} from '../../views/Home';
import {TextInput} from 'react-native-gesture-handler';
2019-12-14 19:26:44 +05:00
import {useAppContext} from '../../provider/useAppContext';
2020-01-05 18:03:40 +05:00
import {VaultDialog} from '../VaultDialog';
2020-01-17 16:23:13 +05:00
let tagsInputRef;
2020-01-06 19:03:21 +05:00
let tagsList;
2019-12-21 09:46:08 +05:00
export const EditorMenu = ({
close = () => {},
hide,
update = () => {},
updateProps = () => {},
noteProps,
2020-01-05 18:03:40 +05:00
note,
timestamp,
2019-12-21 09:46:08 +05:00
}) => {
2020-01-17 16:23:13 +05:00
const {colors, changeColorScheme} = useAppContext();
2020-01-17 00:23:16 +05:00
// Todo
///////
2020-01-05 18:03:40 +05:00
const [unlock, setUnlock] = useState(false);
const [vaultDialog, setVaultDialog] = useState(false);
2020-01-09 19:49:06 +05:00
const [focused, setFocused] = useState(false);
let tagToAdd = null;
let backPressCount = 0;
2019-12-21 11:12:55 +05:00
_renderListItem = ({item, index}) => (
<TouchableOpacity
activeOpacity={opacity}
onPress={() => {
item.func();
}}
style={{
width: '100%',
alignSelf: 'center',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'flex-end',
2020-01-09 19:49:06 +05:00
paddingHorizontal: 12,
2019-12-21 11:12:55 +05:00
paddingVertical: pv + 5,
2020-01-09 19:49:06 +05:00
paddingTop: index === 0 ? 5 : pv + 5,
2019-12-21 11:12:55 +05:00
}}>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
}}>
<Icon
style={{
width: 30,
}}
name={item.icon}
color={colors.pri}
size={SIZE.md}
/>
<Text
style={{
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm,
color: colors.pri,
}}>
{item.name}
</Text>
</View>
{item.switch ? (
<Icon
size={SIZE.lg + 2}
color={item.on ? colors.accent : colors.icon}
name={item.on ? 'toggle-right' : 'toggle-left'}
/>
) : (
undefined
)}
{item.check ? (
<TouchableOpacity
style={{
borderWidth: 2,
borderColor: item.on ? colors.accent : colors.icon,
width: 23,
height: 23,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 100,
paddingTop: 3,
}}>
{item.on ? (
<Icon size={SIZE.sm - 2} color={colors.accent} name="check" />
) : null}
</TouchableOpacity>
) : null}
</TouchableOpacity>
);
_renderTag = item => (
<TouchableOpacity
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
margin: 5,
paddingHorizontal: 5,
paddingVertical: 2.5,
backgroundColor: colors.accent,
borderRadius: 5,
}}>
<Text
style={{
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm - 2,
color: 'white',
}}>
{item}
</Text>
</TouchableOpacity>
);
_renderColor = item => (
<TouchableOpacity
onPress={() => {
let props = {...noteProps};
if (props.colors.includes(item)) {
props.colors.splice(props.colors.indexOf(item), 1);
} else {
props.colors.push(item);
}
updateProps(props);
}}
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
marginRight: 5,
marginBottom: 5,
borderWidth: 1.5,
borderRadius: 100,
padding: 3,
borderColor: noteProps.colors.includes(item)
? colors.pri
: colors.shade,
}}>
<View
style={{
width: 40,
height: 40,
backgroundColor: item,
borderRadius: 100,
justifyContent: 'center',
alignItems: 'center',
}}>
{noteProps.colors.includes(item) ? (
<Icon name="check" color="white" size={SIZE.md} />
) : null}
</View>
</TouchableOpacity>
);
_onSubmit = () => {
if (!tagToAdd || tagToAdd === '#') return;
let tag = tagToAdd;
if (tag[0] !== '#') {
tag = '#' + tag;
}
if (tag.includes(' ')) {
tag = tag.replace(' ', '_');
}
let oldProps = {...noteProps};
oldProps.tags.push(tag);
tagsInputRef.setNativeProps({
text: '#',
});
updateProps(oldProps);
tagToAdd = '';
setTimeout(() => {
tagsInputRef.focus();
}, 300);
};
_onKeyPress = event => {
if (event.nativeEvent.key === 'Backspace') {
if (backPressCount === 0 && !tagToAdd) {
backPressCount = 1;
return;
}
if (backPressCount === 1 && !tagToAdd) {
backPressCount = 0;
let tagInputValue = noteProps.tags[noteProps.tags.length - 1];
let oldProps = {...noteProps};
if (allTags.length === 1) return;
oldProps.tags.splice(allTags.length - 1);
updateProps(oldProps);
tagsInputRef.setNativeProps({
text: tagInputValue,
});
setTimeout(() => {
tagsInputRef.focus();
}, 300);
}
}
};
return (
<AnimatedSafeAreaView
2019-12-10 22:03:39 +05:00
transition={['backgroundColor', 'opacity']}
2019-12-14 16:00:16 +05:00
duration={300}
style={{
height: '100%',
opacity: hide ? 0 : 1,
2019-12-21 11:12:55 +05:00
backgroundColor: colors.shade,
}}>
2019-12-14 16:00:16 +05:00
<KeyboardAvoidingView
style={{height: '100%'}}
behavior={Platform.OS === 'ios' ? 'padding' : null}>
<ScrollView
contentContainerStyle={{
2020-01-09 19:49:06 +05:00
minHeight: '80%',
2019-12-14 16:00:16 +05:00
}}>
<View>
<View
style={{
2020-01-09 19:49:06 +05:00
height: 0,
2019-12-14 16:00:16 +05:00
width: '100%',
2020-01-09 19:49:06 +05:00
marginTop: Platform.OS == 'ios' ? 0 : StatusBar.currentHeight,
2019-12-14 16:00:16 +05:00
}}
/>
2020-01-09 19:49:06 +05:00
<View
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
width: '95%',
alignSelf: 'center',
height: 50,
}}>
<Text
style={{
fontFamily: WEIGHT.bold,
fontSize: SIZE.md,
color: colors.accent,
}}>
Properties
</Text>
</View>
2019-12-14 16:00:16 +05:00
<FlatList
data={[
{
name: 'Dark Mode',
icon: 'moon',
func: () => {
if (!colors.night) {
2020-01-14 20:48:03 +05:00
FastStorage.setItem(
2019-12-14 16:00:16 +05:00
'theme',
JSON.stringify(COLOR_SCHEME_DARK),
);
2019-12-21 09:46:08 +05:00
changeColorScheme(COLOR_SCHEME_DARK);
2019-12-14 16:00:16 +05:00
} else {
2020-01-14 20:48:03 +05:00
FastStorage.setItem(
2019-12-14 16:00:16 +05:00
'theme',
JSON.stringify(COLOR_SCHEME_LIGHT),
);
2019-12-21 09:46:08 +05:00
changeColorScheme(COLOR_SCHEME_LIGHT);
2019-12-14 16:00:16 +05:00
}
},
switch: true,
on: colors.night ? true : false,
close: false,
},
{
name: 'Pinned',
icon: 'tag',
2019-12-21 09:46:08 +05:00
func: () => {
let props = {...noteProps};
props.pinned = !noteProps.pinned;
updateProps(props);
},
close: false,
2019-12-14 16:00:16 +05:00
check: true,
2019-12-21 09:46:08 +05:00
on: noteProps.pinned,
},
2019-12-14 16:00:16 +05:00
{
name: 'Add to Favorites',
icon: 'star',
2019-12-21 09:46:08 +05:00
func: () => {
let props = {...noteProps};
props.favorite = !noteProps.favorite;
updateProps(props);
},
close: false,
2019-12-14 16:00:16 +05:00
check: true,
2019-12-21 09:46:08 +05:00
on: noteProps.favorite,
2019-12-14 16:00:16 +05:00
},
{
name: 'Share ',
icon: 'share',
2019-12-21 09:46:08 +05:00
func: () => {},
2019-12-14 16:00:16 +05:00
close: true,
},
{
name: 'Move to Notebook',
icon: 'arrow-right',
2019-12-21 09:46:08 +05:00
func: () => {},
2019-12-14 16:00:16 +05:00
close: true,
},
2020-01-09 19:49:06 +05:00
{
name: 'Export',
icon: 'external-link',
func: () => {},
close: true,
},
2019-12-14 16:00:16 +05:00
{
2020-01-09 19:49:06 +05:00
name: 'Delete Note',
2019-12-14 16:00:16 +05:00
icon: 'trash',
2019-12-21 09:46:08 +05:00
func: () => {},
2019-12-14 16:00:16 +05:00
close: true,
},
{
2020-01-05 18:03:40 +05:00
name: noteProps.locked ? 'Remove from Vault' : 'Add to Vault',
2019-12-14 16:00:16 +05:00
icon: 'lock',
2020-01-05 18:03:40 +05:00
func: () => {
if (noteProps.locked) {
setUnlock(true);
} else {
setUnlock(false);
}
setVaultDialog(true);
},
2019-12-14 16:00:16 +05:00
close: true,
check: true,
2020-01-05 18:03:40 +05:00
on: noteProps.locked,
2019-12-14 16:00:16 +05:00
},
]}
keyExtractor={(item, index) => item.name}
2019-12-21 11:12:55 +05:00
renderItem={_renderListItem}
2019-12-14 16:00:16 +05:00
/>
2019-12-14 16:00:16 +05:00
<TouchableOpacity
style={{
2019-12-14 16:00:16 +05:00
width: '100%',
alignSelf: 'center',
flexDirection: 'row',
2019-12-14 16:00:16 +05:00
justifyContent: 'space-between',
alignItems: 'flex-end',
2020-01-09 19:49:06 +05:00
paddingHorizontal: 12,
2019-12-14 16:00:16 +05:00
marginTop: 15,
}}>
2019-12-14 16:00:16 +05:00
<View
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
}}>
2019-12-14 16:00:16 +05:00
<Icon
style={{
width: 30,
}}
name="tag"
2019-12-21 11:12:55 +05:00
color={colors.pri}
2019-12-14 16:00:16 +05:00
size={SIZE.md}
/>
<Text
style={{
2019-12-14 21:01:39 +05:00
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm,
2019-12-14 16:00:16 +05:00
color: colors.pri,
}}>
2019-12-14 16:00:16 +05:00
Add Tags
</Text>
2019-12-14 16:00:16 +05:00
</View>
</TouchableOpacity>
2019-12-14 16:00:16 +05:00
<ScrollView
2020-01-06 19:03:21 +05:00
ref={ref => (tagsList = ref)}
2019-12-14 16:00:16 +05:00
contentContainerStyle={{
flexDirection: 'row',
flexWrap: 'wrap',
marginHorizontal: '5%',
marginBottom: 0,
marginTop: 10,
borderRadius: 5,
backgroundColor: colors.nav,
2020-01-09 19:49:06 +05:00
borderWidth: 1.5,
borderColor: focused ? colors.accent : 'transparent',
2019-12-14 16:00:16 +05:00
}}>
2019-12-21 11:12:55 +05:00
{noteProps.tags.map(_renderTag)}
2019-12-14 16:00:16 +05:00
<TextInput
style={{
backgroundColor: 'transparent',
minWidth: 100,
2019-12-14 21:01:39 +05:00
fontFamily: WEIGHT.regular,
2019-12-14 16:00:16 +05:00
color: colors.pri,
paddingHorizontal: 5,
paddingVertical: 2.5,
margin: 5,
}}
ref={ref => (tagsInputRef = ref)}
placeholderTextColor={colors.icon}
2020-01-09 19:49:06 +05:00
onFocus={() => {
setFocused(true);
}}
selectionColor={colors.accent}
selectTextOnFocus={true}
onBlur={() => {
setFocused(false);
}}
2019-12-14 16:00:16 +05:00
placeholder="#hashtag"
onChangeText={value => {
tagToAdd = value;
if (tagToAdd.length > 0) backPressCount = 0;
}}
2019-12-21 11:12:55 +05:00
onKeyPress={_onKeyPress}
onSubmitEditing={_onSubmit}
2019-12-14 16:00:16 +05:00
/>
</ScrollView>
2019-12-14 16:00:16 +05:00
<TouchableOpacity
style={{
2019-12-14 16:00:16 +05:00
width: '100%',
alignSelf: 'center',
flexDirection: 'row',
2019-12-14 16:00:16 +05:00
justifyContent: 'space-between',
alignItems: 'flex-end',
2020-01-09 19:49:06 +05:00
paddingHorizontal: 12,
2019-12-14 16:00:16 +05:00
marginTop: 15,
}}>
2019-12-14 16:00:16 +05:00
<View
style={{
2019-12-14 16:00:16 +05:00
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
}}>
2019-12-14 16:00:16 +05:00
<Icon
style={{
width: 30,
}}
name="tag"
2019-12-21 11:12:55 +05:00
color={colors.pri}
2019-12-14 16:00:16 +05:00
size={SIZE.md}
/>
<Text
style={{
2019-12-14 21:01:39 +05:00
fontFamily: WEIGHT.regular,
fontSize: SIZE.sm,
2019-12-14 16:00:16 +05:00
color: colors.pri,
}}>
2019-12-21 11:12:55 +05:00
Assign Colors
2019-12-14 16:00:16 +05:00
</Text>
</View>
</TouchableOpacity>
2019-12-14 16:00:16 +05:00
<ScrollView
contentContainerStyle={{
flexDirection: 'row',
flexWrap: 'wrap',
paddingHorizontal: '5%',
marginBottom: 15,
marginTop: 10,
}}>
{[
'red',
'yellow',
'green',
'blue',
'purple',
'orange',
'gray',
2019-12-21 11:12:55 +05:00
].map(_renderColor)}
2019-12-14 16:00:16 +05:00
</ScrollView>
</View>
2020-01-09 19:49:06 +05:00
</ScrollView>
<View
style={{
paddingHorizontal: '5%',
paddingVertical: pv + 5,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
}}>
<Text
style={{
2020-01-09 19:49:06 +05:00
color: colors.icon,
fontFamily: WEIGHT.regular,
fontSize: SIZE.xs,
}}>
2020-01-09 19:49:06 +05:00
Last Synced: 5 secs ago.
</Text>
{}
<ActivityIndicator color={colors.accent} />
</View>
2020-01-05 18:03:40 +05:00
<VaultDialog
close={(item, locked) => {
if (item) {
update(item);
}
let props = {...noteProps};
props.locked = locked;
updateProps(props);
setVaultDialog(false);
setUnlock(false);
}}
note={note}
timestamp={timestamp}
perm={true}
openedToUnlock={unlock}
visible={vaultDialog}
/>
2019-12-14 16:00:16 +05:00
</KeyboardAvoidingView>
</AnimatedSafeAreaView>
);
};