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

294 lines
8.7 KiB
JavaScript
Raw Normal View History

2020-01-16 19:55:52 +05:00
import React from 'react';
2020-02-06 13:08:35 +05:00
import {Dimensions, Text, TouchableOpacity, View} from 'react-native';
2020-02-11 20:33:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2019-12-14 16:00:16 +05:00
import {DDS} from '../../../App';
2020-01-18 01:04:33 +05:00
import {ph, pv, SIZE, WEIGHT} from '../../common/common';
2020-02-06 13:08:35 +05:00
import {eSendEvent} from '../../services/eventManager';
2020-02-07 20:24:29 +05:00
import {eOnLoadNote, eOpenVaultDialog} from '../../services/events';
2020-01-18 01:04:33 +05:00
import NavigationService from '../../services/NavigationService';
import {getElevation, timeSince} from '../../utils/utils';
2020-02-02 16:18:52 +05:00
import {
ActionSheetEvent,
simpleDialogEvent,
2020-02-06 13:08:35 +05:00
TEMPLATE_TRASH,
2020-02-02 16:18:52 +05:00
} from '../DialogManager';
2020-03-03 09:49:54 +05:00
import {openEditorAnimation} from '../../utils/animations';
2020-03-10 23:19:16 +05:00
import {openVault} from '../VaultDialog';
2020-01-17 10:58:12 +05:00
2019-11-17 16:02:19 +05:00
const w = Dimensions.get('window').width;
const h = Dimensions.get('window').height;
2019-12-05 17:40:09 +05:00
2020-01-16 19:55:52 +05:00
export default class NoteItem extends React.Component {
constructor(props) {
super(props);
2020-01-17 21:05:53 +05:00
this.cipher = {
value: false,
};
2020-02-07 00:26:44 +05:00
this.colors = [];
2020-01-16 19:55:52 +05:00
this.actionSheet;
this.show = null;
this.setMenuRef = {};
this.willRefresh = false;
}
shouldComponentUpdate(nextProps, nextState) {
2020-02-07 00:26:44 +05:00
if (
nextProps.item.locked !== this.cipher.value ||
2020-03-03 11:01:50 +05:00
nextProps.item.colors.length !== this.colors.length ||
nextProps.selectionMode !== this.props.selectionMode
2020-02-07 00:26:44 +05:00
) {
2020-01-17 21:05:53 +05:00
return true;
} else {
return (
JSON.stringify(nextProps) !== JSON.stringify(this.props) ||
nextState !== this.state
);
}
}
componentDidUpdate() {
2020-02-07 00:26:44 +05:00
this.colors = [...this.props.item.colors];
2020-01-17 21:05:53 +05:00
this.cipher.value = this.props.item.locked ? true : false;
}
componentWillUnmount() {
2020-02-07 00:26:44 +05:00
this.colors = [];
2020-01-17 21:05:53 +05:00
this.cipher.value = false;
}
componentDidMount() {
2020-02-07 00:26:44 +05:00
this.colors = [];
2020-01-17 21:05:53 +05:00
if (this.props.item.locked) {
this.cipher.value = true;
}
2020-01-16 19:55:52 +05:00
}
2019-12-09 13:17:40 +05:00
2020-01-16 19:55:52 +05:00
render() {
let {
colors,
item,
customStyle,
onLongPress,
isTrash,
pinned,
index,
} = this.props;
2020-02-07 00:26:44 +05:00
console.log('rerendering again', index);
2020-01-16 19:55:52 +05:00
return (
<View
style={[
{
paddingVertical: pv,
justifyContent: 'flex-start',
2019-12-21 09:46:08 +05:00
alignItems: 'center',
2020-01-16 19:55:52 +05:00
flexDirection: 'row',
2020-01-22 02:51:24 +05:00
maxWidth: '100%',
2020-02-02 23:43:46 +05:00
paddingRight: 6,
2020-01-16 19:55:52 +05:00
alignSelf: 'center',
borderBottomWidth: 1,
2020-01-24 19:36:58 +05:00
borderBottomColor: colors.nav,
2020-01-16 19:55:52 +05:00
},
customStyle ? customStyle : {},
]}>
{pinned ? (
2019-11-28 21:40:37 +05:00
<View
style={{
2020-01-16 19:55:52 +05:00
...getElevation(3),
width: 30,
height: 30,
backgroundColor: colors.accent,
borderRadius: 100,
position: 'absolute',
left: 20,
top: -15,
justifyContent: 'center',
alignItems: 'center',
2019-11-28 21:40:37 +05:00
}}>
2020-01-16 19:55:52 +05:00
<View
style={{
width: 5,
height: 5,
backgroundColor: 'white',
borderRadius: 100,
}}
/>
</View>
) : null}
<TouchableOpacity
2020-02-02 20:44:17 +05:00
activeOpacity={0.8}
2020-01-16 19:55:52 +05:00
onLongPress={() => onLongPress()}
onPress={() => {
2020-03-03 11:01:50 +05:00
if (this.props.selectionMode) {
this.props.onLongPress();
return;
} else if (item.locked) {
2020-03-14 12:13:00 +05:00
openVault(item, true, true, false, true, false);
2020-03-10 23:19:16 +05:00
2020-03-03 09:49:54 +05:00
return;
}
if (DDS.isTab) {
eSendEvent(eOnLoadNote, item);
} else if (isTrash) {
simpleDialogEvent(TEMPLATE_TRASH(item.type));
2020-01-16 19:55:52 +05:00
} else {
2020-03-03 09:49:54 +05:00
eSendEvent(eOnLoadNote, item);
openEditorAnimation();
2020-01-16 19:55:52 +05:00
}
}}
style={{
paddingLeft: 0,
width: '95%',
}}>
<>
2019-11-28 21:40:37 +05:00
<Text
2020-01-16 19:55:52 +05:00
numberOfLines={1}
2019-11-28 21:40:37 +05:00
style={{
2020-01-24 19:36:58 +05:00
color: colors.pri,
2020-02-02 19:09:33 +05:00
fontSize: SIZE.sm + 1,
2020-01-16 19:55:52 +05:00
fontFamily: WEIGHT.bold,
2020-02-02 23:43:46 +05:00
maxWidth: '100%',
2019-11-28 21:40:37 +05:00
}}>
2020-01-16 19:55:52 +05:00
{item.title.replace('\n', '')}
2019-11-28 21:40:37 +05:00
</Text>
2019-12-06 12:06:03 +05:00
<View
2019-11-28 21:40:37 +05:00
style={{
2019-12-06 12:06:03 +05:00
justifyContent: 'flex-start',
2020-01-16 19:55:52 +05:00
alignItems: 'flex-start',
2020-01-06 19:03:21 +05:00
width: '100%',
2019-11-28 21:40:37 +05:00
}}>
2020-01-16 19:55:52 +05:00
<Text
2020-01-24 19:11:31 +05:00
numberOfLines={2}
2020-01-16 19:55:52 +05:00
style={{
2020-02-02 19:09:33 +05:00
fontSize: SIZE.sm - 1,
2020-01-24 19:11:31 +05:00
color: colors.pri + 'B3',
2020-01-16 19:55:52 +05:00
fontFamily: WEIGHT.regular,
width: '100%',
maxWidth: '100%',
paddingRight: ph,
}}>
{item.headline[item.headline.length - 1] === '\n'
? item.headline.slice(0, item.headline.length - 1)
: item.headline}
</Text>
<View
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
width: '100%',
2020-02-02 23:05:14 +05:00
marginTop: 5,
2020-01-16 19:55:52 +05:00
}}>
{!isTrash ? (
<>
{item.colors.length > 0 ? (
<View
style={{
marginRight: 10,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
}}>
{item.colors.map(item => (
<View
key={item}
style={{
width: SIZE.xs,
height: SIZE.xs,
borderRadius: 100,
backgroundColor: item,
marginRight: -4.5,
}}></View>
))}
</View>
) : null}
{item.locked ? (
<Icon
style={{marginRight: 10}}
name="lock"
size={SIZE.xs}
color={colors.icon}
/>
) : null}
{item.favorite ? (
<Icon
style={{marginRight: 10}}
name="star"
size={SIZE.xs + 1}
color="orange"
/>
) : null}
<Text
2020-01-10 18:46:52 +05:00
style={{
2020-01-16 19:55:52 +05:00
color: colors.icon,
fontSize: SIZE.xs - 1,
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
2020-01-10 18:46:52 +05:00
marginRight: 10,
}}>
2020-01-16 19:55:52 +05:00
{timeSince(item.dateCreated)}
</Text>
</>
) : null}
2019-12-06 12:06:03 +05:00
2020-01-16 19:55:52 +05:00
{isTrash ? (
<>
<Text
style={{
color: colors.icon,
fontSize: SIZE.xs - 1,
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
}}>
{'Deleted on: ' +
new Date(item.dateDeleted).toISOString().slice(0, 10) +
' '}
</Text>
<Text
style={{
color: colors.accent,
fontSize: SIZE.xs - 1,
textAlignVertical: 'center',
fontFamily: WEIGHT.regular,
}}>
{item.type[0].toUpperCase() + item.type.slice(1) + ' '}
</Text>
</>
) : null}
</View>
2019-12-06 12:06:03 +05:00
</View>
2020-01-16 19:55:52 +05:00
</>
</TouchableOpacity>
2019-11-28 21:40:37 +05:00
2020-01-16 19:55:52 +05:00
<View
2020-01-09 19:48:39 +05:00
style={{
justifyContent: 'center',
minHeight: 70,
alignItems: 'center',
}}>
2020-01-16 19:55:52 +05:00
<TouchableOpacity
style={{
justifyContent: 'center',
minHeight: 70,
alignItems: 'center',
2020-01-11 23:06:29 +05:00
}}
2020-01-16 19:55:52 +05:00
onPress={() => {
ActionSheetEvent(
item,
2020-02-02 16:18:52 +05:00
isTrash ? false : true,
isTrash ? false : true,
isTrash
? ['Remove', 'Restore']
2020-02-03 12:18:45 +05:00
: ['Add to', 'Share', 'Export', 'Delete', 'Open'],
2020-02-02 19:09:33 +05:00
isTrash ? [] : ['Pin', 'Favorite', 'Add to Vault'],
2020-01-16 19:55:52 +05:00
);
}}>
2020-02-12 03:24:36 +05:00
<Icon name="dots-horizontal" size={SIZE.lg} color={colors.icon} />
2020-01-16 19:55:52 +05:00
</TouchableOpacity>
</View>
2019-12-04 19:38:19 +05:00
</View>
2020-01-16 19:55:52 +05:00
);
}
}