mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
add attachment dialog
This commit is contained in:
153
apps/mobile/src/components/AttachmentDialog/index.js
Normal file
153
apps/mobile/src/components/AttachmentDialog/index.js
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
import React, {useEffect, useRef, useState} from 'react';
|
||||||
|
import {ScrollView, Text, View} from 'react-native';
|
||||||
|
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
|
||||||
|
import {useTracked} from '../../provider';
|
||||||
|
import {eSubscribeEvent, eUnSubscribeEvent} from '../../services/EventManager';
|
||||||
|
import {eCloseTagsDialog, eOpenTagsDialog} from '../../utils/Events';
|
||||||
|
import {SIZE} from '../../utils/SizeUtils';
|
||||||
|
import ActionSheetWrapper from '../ActionSheetComponent/ActionSheetWrapper';
|
||||||
|
import DialogHeader from '../Dialog/dialog-header';
|
||||||
|
import {PressableButton} from '../PressableButton';
|
||||||
|
import Heading from '../Typography/Heading';
|
||||||
|
import Paragraph from '../Typography/Paragraph';
|
||||||
|
|
||||||
|
export const AttachmentDialog = () => {
|
||||||
|
const [state] = useTracked();
|
||||||
|
const colors = state.colors;
|
||||||
|
const [visible, setVisible] = useState(false);
|
||||||
|
const [note, setNote] = useState(null);
|
||||||
|
const actionSheetRef = useRef();
|
||||||
|
const [attachments, setAttachments] = useState([
|
||||||
|
{
|
||||||
|
name: 'my-file.png',
|
||||||
|
length: 10000,
|
||||||
|
type: 'text/pdf'
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
eSubscribeEvent(eOpenTagsDialog, open);
|
||||||
|
eSubscribeEvent(eCloseTagsDialog, close);
|
||||||
|
return () => {
|
||||||
|
eUnSubscribeEvent(eOpenTagsDialog, open);
|
||||||
|
eUnSubscribeEvent(eCloseTagsDialog, close);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const open = item => {
|
||||||
|
setNote(item);
|
||||||
|
setVisible(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (visible) {
|
||||||
|
actionSheetRef.current?.show();
|
||||||
|
}
|
||||||
|
}, [visible]);
|
||||||
|
|
||||||
|
const close = () => {
|
||||||
|
actionSheetRef.current?.hide();
|
||||||
|
};
|
||||||
|
|
||||||
|
return visible ? null : (
|
||||||
|
<ActionSheetWrapper
|
||||||
|
centered={false}
|
||||||
|
fwdRef={actionSheetRef}
|
||||||
|
onClose={async () => {
|
||||||
|
setVisible(false);
|
||||||
|
}}>
|
||||||
|
<View
|
||||||
|
style={{
|
||||||
|
width: '100%',
|
||||||
|
alignSelf: 'center',
|
||||||
|
paddingHorizontal: 12,
|
||||||
|
minHeight: '60%'
|
||||||
|
}}>
|
||||||
|
<DialogHeader title="Attachments" />
|
||||||
|
<ScrollView
|
||||||
|
nestedScrollEnabled
|
||||||
|
overScrollMode="never"
|
||||||
|
scrollToOverflowEnabled={false}
|
||||||
|
keyboardDismissMode="none"
|
||||||
|
keyboardShouldPersistTaps="always"
|
||||||
|
onMomentumScrollEnd={() => {
|
||||||
|
actionSheetRef.current?.handleChildScrollEnd();
|
||||||
|
}}>
|
||||||
|
{attachments.map(item => (
|
||||||
|
<Attachment attachment={item} note={note} setNote={setNote} />
|
||||||
|
))}
|
||||||
|
</ScrollView>
|
||||||
|
</View>
|
||||||
|
</ActionSheetWrapper>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
function formatBytes(bytes, decimals = 2) {
|
||||||
|
if (bytes === 0) return '0 Bytes';
|
||||||
|
|
||||||
|
const k = 1024;
|
||||||
|
const dm = decimals < 0 ? 0 : decimals;
|
||||||
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||||
|
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
|
|
||||||
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
let icons = {
|
||||||
|
image: 'image',
|
||||||
|
file: 'file',
|
||||||
|
pdf: 'file-pdf-box',
|
||||||
|
video: 'file-video-outline',
|
||||||
|
audio: 'file-music-outline'
|
||||||
|
};
|
||||||
|
|
||||||
|
function getIcon(type) {
|
||||||
|
let types = type.split('/');
|
||||||
|
let icon = Object.keys(icons).find(
|
||||||
|
i => i.includes(types[0]) || i.includes(types[1])
|
||||||
|
);
|
||||||
|
return icons[icon] || 'file';
|
||||||
|
}
|
||||||
|
|
||||||
|
const Attachment = ({attachment, note, setNote}) => {
|
||||||
|
const [state] = useTracked();
|
||||||
|
const colors = state.colors;
|
||||||
|
|
||||||
|
const onPress = async () => {};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<PressableButton
|
||||||
|
customStyle={{
|
||||||
|
flexDirection: 'row',
|
||||||
|
marginVertical: 5,
|
||||||
|
justifyContent: 'space-between',
|
||||||
|
padding: 12
|
||||||
|
}}
|
||||||
|
onPress={onPress}
|
||||||
|
type="grayBg">
|
||||||
|
<Text
|
||||||
|
style={{
|
||||||
|
flexWrap: 'wrap',
|
||||||
|
flexShrink: 1
|
||||||
|
}}>
|
||||||
|
<Icon
|
||||||
|
name={getIcon(attachment.type)}
|
||||||
|
size={SIZE.md}
|
||||||
|
color={colors.pri}
|
||||||
|
/>{' '}
|
||||||
|
<Heading
|
||||||
|
size={SIZE.sm}
|
||||||
|
style={{
|
||||||
|
flexWrap: 'wrap'
|
||||||
|
}}
|
||||||
|
color={colors.pri}>
|
||||||
|
{attachment.name}
|
||||||
|
</Heading>
|
||||||
|
<Paragraph size={12}> ({formatBytes(attachment.length)})</Paragraph>
|
||||||
|
</Text>
|
||||||
|
|
||||||
|
<Icon name="download" size={SIZE.lg} color={colors.pri} />
|
||||||
|
</PressableButton>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -1,27 +1,32 @@
|
|||||||
import React, { Component, createRef } from 'react';
|
import React, {Component, createRef} from 'react';
|
||||||
import {
|
import {
|
||||||
eSendEvent,
|
eSendEvent,
|
||||||
eSubscribeEvent,
|
eSubscribeEvent,
|
||||||
eUnSubscribeEvent,
|
eUnSubscribeEvent,
|
||||||
openVault
|
openVault
|
||||||
} from '../../services/EventManager';
|
} from '../../services/EventManager';
|
||||||
import { getCurrentColors } from '../../utils/Colors';
|
import {getCurrentColors} from '../../utils/Colors';
|
||||||
import {
|
import {
|
||||||
eCloseActionSheet,
|
eCloseActionSheet,
|
||||||
eCloseAddNotebookDialog,
|
eCloseAddNotebookDialog,
|
||||||
eCloseLoginDialog, eClosePremiumDialog, eOnLoadNote,
|
eCloseLoginDialog,
|
||||||
|
eClosePremiumDialog,
|
||||||
|
eOnLoadNote,
|
||||||
eOpenActionSheet,
|
eOpenActionSheet,
|
||||||
eOpenAddNotebookDialog,
|
eOpenAddNotebookDialog,
|
||||||
eOpenExportDialog,
|
eOpenExportDialog,
|
||||||
eOpenLoginDialog, eOpenPremiumDialog, eShowGetPremium,
|
eOpenLoginDialog,
|
||||||
|
eOpenPremiumDialog,
|
||||||
|
eShowGetPremium,
|
||||||
eThemeUpdated
|
eThemeUpdated
|
||||||
} from '../../utils/Events';
|
} from '../../utils/Events';
|
||||||
import { EditorSettings } from '../../views/Editor/EditorSettings';
|
import {EditorSettings} from '../../views/Editor/EditorSettings';
|
||||||
import { ActionSheetComponent } from '../ActionSheetComponent';
|
import {ActionSheetComponent} from '../ActionSheetComponent';
|
||||||
import ActionSheetWrapper from '../ActionSheetComponent/ActionSheetWrapper';
|
import ActionSheetWrapper from '../ActionSheetComponent/ActionSheetWrapper';
|
||||||
import { AddNotebookDialog } from '../AddNotebookDialog';
|
import {AddNotebookDialog} from '../AddNotebookDialog';
|
||||||
import { AddTopicDialog } from '../AddTopicDialog';
|
import {AddTopicDialog} from '../AddTopicDialog';
|
||||||
import { Dialog } from '../Dialog';
|
import {AttachmentDialog} from '../AttachmentDialog';
|
||||||
|
import {Dialog} from '../Dialog';
|
||||||
import ExportDialog from '../ExportDialog';
|
import ExportDialog from '../ExportDialog';
|
||||||
import ImagePreview from '../ImagePreview';
|
import ImagePreview from '../ImagePreview';
|
||||||
import LoginDialog from '../LoginDialog';
|
import LoginDialog from '../LoginDialog';
|
||||||
@@ -38,8 +43,8 @@ import RestoreDialog from '../RestoreDialog';
|
|||||||
import ResultDialog from '../ResultDialog';
|
import ResultDialog from '../ResultDialog';
|
||||||
import SortDialog from '../SortDialog';
|
import SortDialog from '../SortDialog';
|
||||||
import TagsDialog from '../TagsDialog';
|
import TagsDialog from '../TagsDialog';
|
||||||
import { UpdateDialog } from '../UpdateDialog';
|
import {UpdateDialog} from '../UpdateDialog';
|
||||||
import { VaultDialog } from '../VaultDialog';
|
import {VaultDialog} from '../VaultDialog';
|
||||||
|
|
||||||
export class DialogManager extends Component {
|
export class DialogManager extends Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
@@ -54,7 +59,7 @@ export class DialogManager extends Component {
|
|||||||
colors: false,
|
colors: false,
|
||||||
tags: false,
|
tags: false,
|
||||||
rowItems: [],
|
rowItems: [],
|
||||||
columnItems: [],
|
columnItems: []
|
||||||
},
|
},
|
||||||
simpleDialog: {
|
simpleDialog: {
|
||||||
title: '',
|
title: '',
|
||||||
@@ -62,8 +67,8 @@ export class DialogManager extends Component {
|
|||||||
positiveText: '',
|
positiveText: '',
|
||||||
negativeText: '',
|
negativeText: '',
|
||||||
action: 0,
|
action: 0,
|
||||||
icon: '',
|
icon: ''
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,11 +84,11 @@ export class DialogManager extends Component {
|
|||||||
{
|
{
|
||||||
actionSheetData: data,
|
actionSheetData: data,
|
||||||
item: data.item ? data.item : {},
|
item: data.item ? data.item : {},
|
||||||
actionSheetVisible: true,
|
actionSheetVisible: true
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
this.actionSheet.current?.setModalVisible();
|
this.actionSheet.current?.setModalVisible();
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -94,11 +99,11 @@ export class DialogManager extends Component {
|
|||||||
loadNote = i => {
|
loadNote = i => {
|
||||||
if (i && i.type === 'new') {
|
if (i && i.type === 'new') {
|
||||||
this.setState({
|
this.setState({
|
||||||
item: {},
|
item: {}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.setState({
|
this.setState({
|
||||||
item: i,
|
item: i
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -107,7 +112,7 @@ export class DialogManager extends Component {
|
|||||||
let item = this.state.item;
|
let item = this.state.item;
|
||||||
this.addTopicsDialog.open({
|
this.addTopicsDialog.open({
|
||||||
notebookId: item?.type !== 'topic' ? item.id : item.notebookId,
|
notebookId: item?.type !== 'topic' ? item.id : item.notebookId,
|
||||||
toEdit: item?.type === 'topic' ? item : null,
|
toEdit: item?.type === 'topic' ? item : null
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -117,7 +122,7 @@ export class DialogManager extends Component {
|
|||||||
|
|
||||||
onThemeChange = () => {
|
onThemeChange = () => {
|
||||||
this.setState({
|
this.setState({
|
||||||
colors: getCurrentColors(),
|
colors: getCurrentColors()
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -163,11 +168,11 @@ export class DialogManager extends Component {
|
|||||||
showAddNotebook = data => {
|
showAddNotebook = data => {
|
||||||
this.setState(
|
this.setState(
|
||||||
{
|
{
|
||||||
item: data.item ? data.item : data.type === 'notebook' ? data : {},
|
item: data.item ? data.item : data.type === 'notebook' ? data : {}
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
this.addNotebooksDialog.open();
|
this.addNotebooksDialog.open();
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
hideAddNotebook = () => {
|
hideAddNotebook = () => {
|
||||||
@@ -182,7 +187,7 @@ export class DialogManager extends Component {
|
|||||||
item: this.state.item,
|
item: this.state.item,
|
||||||
novault: false,
|
novault: false,
|
||||||
title: 'Create vault',
|
title: 'Create vault',
|
||||||
description: 'Set a password to create a vault and lock note.',
|
description: 'Set a password to create a vault and lock note.'
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -192,7 +197,7 @@ export class DialogManager extends Component {
|
|||||||
novault: true,
|
novault: true,
|
||||||
locked: true,
|
locked: true,
|
||||||
title: 'Lock note',
|
title: 'Lock note',
|
||||||
description: 'Give access to vault to lock this note.',
|
description: 'Give access to vault to lock this note.'
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -203,7 +208,7 @@ export class DialogManager extends Component {
|
|||||||
locked: true,
|
locked: true,
|
||||||
permanant: true,
|
permanant: true,
|
||||||
title: 'Unlock note',
|
title: 'Unlock note',
|
||||||
description: 'Remove note from the vault.',
|
description: 'Remove note from the vault.'
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -239,7 +244,7 @@ export class DialogManager extends Component {
|
|||||||
eSendEvent(eShowGetPremium, null);
|
eSendEvent(eShowGetPremium, null);
|
||||||
this.onActionSheetHide();
|
this.onActionSheetHide();
|
||||||
this.setState({
|
this.setState({
|
||||||
actionSheetVisible: false,
|
actionSheetVisible: false
|
||||||
});
|
});
|
||||||
}}>
|
}}>
|
||||||
<ActionSheetComponent
|
<ActionSheetComponent
|
||||||
@@ -250,7 +255,7 @@ export class DialogManager extends Component {
|
|||||||
getRef={() => this.actionSheet}
|
getRef={() => this.actionSheet}
|
||||||
hasColors={actionSheetData.colors}
|
hasColors={actionSheetData.colors}
|
||||||
hasTags={actionSheetData.colors}
|
hasTags={actionSheetData.colors}
|
||||||
overlayColor='rgba(0,0,0,0.3)'
|
overlayColor="rgba(0,0,0,0.3)"
|
||||||
rowItems={actionSheetData.rowItems}
|
rowItems={actionSheetData.rowItems}
|
||||||
columnItems={actionSheetData.columnItems}
|
columnItems={actionSheetData.columnItems}
|
||||||
close={value => {
|
close={value => {
|
||||||
@@ -262,13 +267,12 @@ export class DialogManager extends Component {
|
|||||||
/>
|
/>
|
||||||
</ActionSheetWrapper>
|
</ActionSheetWrapper>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<Dialog context="global" />
|
<Dialog context="global" />
|
||||||
<AddTopicDialog
|
<AddTopicDialog
|
||||||
ref={ref => (this.addTopicsDialog = ref)}
|
ref={ref => (this.addTopicsDialog = ref)}
|
||||||
close={() => {
|
close={() => {
|
||||||
this.setState({
|
this.setState({
|
||||||
item: {},
|
item: {}
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
colors={colors}
|
colors={colors}
|
||||||
@@ -293,13 +297,13 @@ export class DialogManager extends Component {
|
|||||||
<ResultDialog />
|
<ResultDialog />
|
||||||
<VaultDialog colors={colors} />
|
<VaultDialog colors={colors} />
|
||||||
<MoveNoteDialog colors={colors} />
|
<MoveNoteDialog colors={colors} />
|
||||||
|
|
||||||
<UpdateDialog />
|
<UpdateDialog />
|
||||||
<RateDialog />
|
<RateDialog />
|
||||||
<ImagePreview />
|
<ImagePreview />
|
||||||
<EditorSettings />
|
<EditorSettings />
|
||||||
<PublishNoteDialog />
|
<PublishNoteDialog />
|
||||||
<TagsDialog/>
|
<TagsDialog />
|
||||||
|
<AttachmentDialog />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,4 +129,7 @@ export const eOpenPublishNoteDialog = '566';
|
|||||||
export const eClosePublishNoteDialog = '567';
|
export const eClosePublishNoteDialog = '567';
|
||||||
|
|
||||||
export const eOpenTagsDialog = '568';
|
export const eOpenTagsDialog = '568';
|
||||||
export const eCloseTagsDialog = '569';
|
export const eCloseTagsDialog = '569';
|
||||||
|
|
||||||
|
export const eOpenAttachmentsDialog = '600';
|
||||||
|
export const eCloseAttachmentDialog = '601';
|
||||||
Reference in New Issue
Block a user