-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathFileAttachmentPanel.js
95 lines (89 loc) · 3.22 KB
/
FileAttachmentPanel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { getFileAttachments } from 'helpers/getFileAttachments';
import { saveAs } from 'file-saver';
import { getSaveAsHandler } from 'helpers/saveAs';
import Icon from 'components/Icon';
import core from 'core';
import './FileAttachmentPanel.scss';
const renderAttachment = (filename, onClickCallback, key) => {
const fileExtension = filename.split('.').pop().toUpperCase();
return (
<li onClick={onClickCallback} key={key}>
<span>{`[${fileExtension}] ${filename}`}</span>
</li>
);
};
const FileAttachmentPanel = () => {
const [t] = useTranslation();
const [fileAttachments, setFileAttachments] = useState({
embeddedFiles: [],
fileAttachmentAnnotations: [],
});
useEffect(() => {
const updateFileAttachments = async () => {
const attachments = await getFileAttachments();
setFileAttachments(attachments);
};
core.addEventListener('annotationChanged', updateFileAttachments);
updateFileAttachments();
return () => {
core.removeEventListener('annotationChanged', updateFileAttachments);
};
}, []);
if (
fileAttachments.embeddedFiles.length === 0 &&
Object.entries(fileAttachments.fileAttachmentAnnotations).length === 0
) {
return (
<div className="no-attachment">
<Icon className="empty-icon" glyph="illustration - empty state - outlines" />
<div className="msg">{t('message.noAttachments')}</div>
</div>
);
}
return (
<div className="fileAttachmentPanel">
<div className="section">
{fileAttachments.embeddedFiles.length ? <p className="title">{t('message.embeddedFiles')}</p> : null}
<ul className="downloadable">
{fileAttachments.embeddedFiles.map((file, idx) => renderAttachment(
file.filename,
() => {
if (getSaveAsHandler() !== null) {
const handler = getSaveAsHandler();
handler(file.blob, file.filename);
} else {
saveAs(file.blob, file.filename);
}
},
`embeddedFile_${idx}`,
),
)}
</ul>
</div>
{Object.entries(fileAttachments.fileAttachmentAnnotations).map(([pageNumber, fileAttachmentAnnotsPerPage]) => {
return (
<div key={pageNumber} className="section">
<p className="title">
{t('message.pageNum')}: {pageNumber}
</p>
<ul className="downloadable">
{fileAttachmentAnnotsPerPage.map((fileAttachmentAnnot, idx) => renderAttachment(
fileAttachmentAnnot.filename,
() => {
core.setCurrentPage(fileAttachmentAnnot['PageNumber']);
core.selectAnnotation(fileAttachmentAnnot);
core.getAnnotationManager().trigger('annotationDoubleClicked', fileAttachmentAnnot);
},
`fileAttachmentAnnotation_${idx}`,
),
)}
</ul>
</div>
);
})}
</div>
);
};
export default FileAttachmentPanel;