Skip to content

Commit 6714f4a

Browse files
lethemanhlethemanh
authored andcommitted
feat: Implement new actions ExportToText and new icons ✨
1 parent 0ca7487 commit 6714f4a

File tree

15 files changed

+323
-7
lines changed

15 files changed

+323
-7
lines changed

assets/icons/ui/expand.svg

Lines changed: 1 addition & 0 deletions
Loading

assets/icons/ui/export.svg

Lines changed: 1 addition & 0 deletions
Loading

assets/icons/ui/narrow.svg

Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React, { forwardRef } from 'react'
2+
3+
import logger from 'cozy-logger'
4+
5+
import { downloadBlob } from './helpers'
6+
import { getActionsI18n } from './locales/withActionsLocales'
7+
import { makePdfBlobFromText } from './pdfHelpers'
8+
import Icon from '../../Icon'
9+
import ExportIcon from '../../Icons/Export'
10+
import ListItemIcon from '../../ListItemIcon'
11+
import ListItemText from '../../ListItemText'
12+
import ActionsMenuItem from '../ActionsMenuItem'
13+
14+
const makeComponent = (label, icon) => {
15+
const Component = forwardRef((props, ref) => {
16+
return (
17+
<ActionsMenuItem {...props} ref={ref}>
18+
<ListItemIcon>
19+
<Icon icon={icon} />
20+
</ListItemIcon>
21+
<ListItemText primary={label} />
22+
</ActionsMenuItem>
23+
)
24+
})
25+
26+
Component.displayName = 'exportToText'
27+
28+
return Component
29+
}
30+
31+
export const exportToText = ({ exportedText, file }) => {
32+
const { t } = getActionsI18n()
33+
const icon = ExportIcon
34+
const label = t('exportToText')
35+
36+
return {
37+
name: 'exportToText',
38+
icon,
39+
label,
40+
displayCondition: () => !!exportedText,
41+
Component: makeComponent(label, icon),
42+
action: async () => {
43+
try {
44+
const blob = await makePdfBlobFromText(exportedText)
45+
const baseName = file?.name
46+
? `${file.name.replace(/\.[^/.]+$/, '')}_export`
47+
: 'export'
48+
downloadBlob(blob, `${baseName}.pdf`)
49+
} catch (error) {
50+
logger.error(error)
51+
}
52+
}
53+
}
54+
}

react/ActionsMenu/Actions/helpers.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,23 @@ export const makePdfBlob = async ({ client, docs, fetchBlobFileById }) => {
238238

239239
return blob
240240
}
241+
242+
/**
243+
* Trigger a browser download for a given blob.
244+
*
245+
* @param {Blob} blob - File content
246+
* @param {string} filename - Desired filename
247+
* @returns {void}
248+
*/
249+
export const downloadBlob = (blob, filename) => {
250+
if (!blob || !filename) return
251+
252+
const url = URL.createObjectURL(blob)
253+
const a = document.createElement('a')
254+
a.href = url
255+
a.download = filename
256+
a.style.display = 'none'
257+
document.body.appendChild(a)
258+
a.click()
259+
a.remove()
260+
}

react/ActionsMenu/Actions/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export { viewInDrive } from './viewInDrive'
1313
export { copyToClipboard } from './copyToClipboard'
1414
export { editAttribute } from './editAttribute'
1515
export { others } from './others'
16+
export { exportToText } from './exportToText'

react/ActionsMenu/Actions/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
"success": "Copied to clipboard",
2626
"error": "Cannot copy to clipboard"
2727
},
28-
"call": "Call"
28+
"call": "Call",
29+
"exportToText": "Export as text"
2930
}

react/ActionsMenu/Actions/locales/fr.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
"success": "Copié dans le presse-papier",
2626
"error": "Impossible de copier dans le presse-papier"
2727
},
28-
"call": "Appeler"
28+
"call": "Appeler",
29+
"exportToText": "Exporter en texte"
2930
}

react/ActionsMenu/Actions/locales/ru.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
"success": "Скопировано в буфер обмена",
2626
"error": "Не удается скопировать в буфер обмена"
2727
},
28-
"call": "Звонить"
29-
}
28+
"call": "Звонить",
29+
"exportToText": "Экспортировать как текст"
30+
}

react/ActionsMenu/Actions/locales/vi.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
"success": "Đã sao chép vào clipboard",
2626
"error": "Không thể sao chép vào clipboard"
2727
},
28-
"call": "Gọi"
29-
}
28+
"call": "Gọi",
29+
"exportToText": "Xuất dưới dạng văn bản"
30+
}

0 commit comments

Comments
 (0)