Skip to content

Commit e0b014c

Browse files
JF-Cozyzatteo
authored andcommitted
feat(Viewer): Add ToolbarButtons component
to be able to add buttons in viewer toolbar
1 parent 6a2f8fe commit e0b014c

File tree

8 files changed

+114
-66
lines changed

8 files changed

+114
-66
lines changed

react/Viewer/Footer/FooterContent.jsx

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import React, { useMemo, Children, cloneElement, isValidElement } from 'react'
1+
import React, { useMemo } from 'react'
22
import PropTypes from 'prop-types'
33
import cx from 'classnames'
44

55
import BottomSheet, { BottomSheetHeader } from '../../BottomSheet'
66
import { makeStyles } from '../../styles'
77
import { isValidForPanel } from '../helpers'
8+
import { extractChildrenCompByName } from './helpers'
89
import BottomSheetContent from './BottomSheetContent'
910

1011
const useStyles = makeStyles(theme => ({
@@ -28,19 +29,11 @@ const FooterContent = ({ file, toolbarRef, children, isPublic }) => {
2829

2930
const toolbarProps = useMemo(() => ({ ref: toolbarRef }), [toolbarRef])
3031

31-
const FooterActionButtons =
32-
Children.toArray(children).find(child => {
33-
return (
34-
child.type.name === 'FooterActionButtons' ||
35-
child.type.displayName === 'FooterActionButtons'
36-
)
37-
}) || null
38-
39-
const FooterActionButtonsWithFile = isValidElement(FooterActionButtons)
40-
? cloneElement(FooterActionButtons, {
41-
file
42-
})
43-
: null
32+
const FooterActionButtonsWithFile = extractChildrenCompByName({
33+
children,
34+
file,
35+
name: 'FooterActionButtons'
36+
})
4437

4538
if (isValidForPanel({ file })) {
4639
return (
@@ -60,7 +53,7 @@ const FooterContent = ({ file, toolbarRef, children, isPublic }) => {
6053
}
6154

6255
// If `FooterActionButtons` hasn't children
63-
if (!FooterActionButtons) return null
56+
if (!FooterActionButtonsWithFile) return null
6457

6558
return <div className={styles.footer}>{FooterActionButtonsWithFile}</div>
6659
}

react/Viewer/Footer/helpers.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,18 @@ export const mapToAllChildren = (children, cb) => {
8989
return cb(child)
9090
})
9191
}
92+
93+
export const extractChildrenCompByName = ({ children, file, name }) => {
94+
const ChildrenComp =
95+
Children.toArray(children).find(child => {
96+
return child.type.name === name || child.type.displayName === name
97+
}) || null
98+
99+
const ChildrenCompWithFile = isValidElement(ChildrenComp)
100+
? cloneElement(ChildrenComp, {
101+
file
102+
})
103+
: null
104+
105+
return ChildrenCompWithFile
106+
}

react/Viewer/Viewer.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class Viewer extends Component {
7070
showNavigation,
7171
renderFallbackExtraContent,
7272
validForPanel,
73+
children,
7374
componentsProps
7475
} = this.props
7576

@@ -90,6 +91,7 @@ class Viewer extends Component {
9091
showNavigation={showNavigation}
9192
showInfoPanel={validForPanel}
9293
>
94+
{children}
9395
<ViewerByFile
9496
file={currentFile}
9597
onClose={this.onClose}

react/Viewer/ViewerContainer.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ const ViewerContainer = props => {
6363
hasNext={hasNext}
6464
validForPanel={validForPanel}
6565
toolbarRef={toolbarRef}
66-
/>
66+
>
67+
{children}
68+
</Viewer>
6769
</EncryptedProvider>
6870
<ViewerInformationsWrapper
6971
isPublic={isPublic}

react/Viewer/components/Toolbar.jsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { withViewerLocales } from '../hoc/withViewerLocales'
1717
import { downloadFile } from '../helpers'
1818
import { useEncrypted } from '../providers/EncryptedProvider'
1919
import { ToolbarFilePath } from './ToolbarFilePath'
20+
import { extractChildrenCompByName } from '../Footer/helpers'
2021

2122
import styles from './styles.styl'
2223
import MidEllipsis from '../../MidEllipsis'
@@ -38,13 +39,20 @@ const Toolbar = ({
3839
t,
3940
toolbarRef,
4041
breakpoints: { isDesktop },
42+
children,
4143
showFilePath
4244
}) => {
4345
const client = useClient()
4446
const classes = useClasses()
4547

4648
const { url } = useEncrypted()
4749

50+
const ToolbarButtons = extractChildrenCompByName({
51+
children,
52+
file,
53+
name: 'ToolbarButtons'
54+
})
55+
4856
return (
4957
<div
5058
ref={toolbarRef}
@@ -76,13 +84,16 @@ const Toolbar = ({
7684

7785
<div className="u-flex">
7886
{isDesktop && (
79-
<IconButton
80-
className="u-white"
81-
aria-label={t('Viewer.download')}
82-
onClick={() => downloadFile({ client, file, url })}
83-
>
84-
<Icon icon={DownloadIcon} />
85-
</IconButton>
87+
<>
88+
{ToolbarButtons}
89+
<IconButton
90+
className="u-white"
91+
aria-label={t('Viewer.download')}
92+
onClick={() => downloadFile({ client, file, url })}
93+
>
94+
<Icon icon={DownloadIcon} />
95+
</IconButton>
96+
</>
8697
)}
8798
</div>
8899
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react'
2+
3+
import FooterActionButtons from '../Footer/FooterActionButtons'
4+
5+
const ToolbarButtons = props => {
6+
return <FooterActionButtons {...props} />
7+
}
8+
9+
ToolbarButtons.displayName = 'ToolbarButtons'
10+
11+
export default ToolbarButtons

react/Viewer/components/ViewerByFile.jsx

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,44 +52,46 @@ export const getViewerComponentName = ({
5252
}
5353
}
5454

55-
const ViewerByFile = ({
56-
file,
57-
onClose,
58-
renderFallbackExtraContent,
59-
gestures,
60-
gesturesRef,
61-
onSwipe,
62-
breakpoints: { isDesktop },
63-
componentsProps
64-
}) => {
65-
const isOnlyOfficeEnabled = componentsProps?.OnlyOfficeViewer?.isEnabled
66-
const onlyOfficeOpener = componentsProps?.OnlyOfficeViewer?.opener
55+
const ViewerByFile = withBreakpoints()(
56+
({
57+
file,
58+
onClose,
59+
renderFallbackExtraContent,
60+
gestures,
61+
gesturesRef,
62+
onSwipe,
63+
breakpoints: { isDesktop },
64+
componentsProps
65+
}) => {
66+
const isOnlyOfficeEnabled = componentsProps?.OnlyOfficeViewer?.isEnabled
67+
const onlyOfficeOpener = componentsProps?.OnlyOfficeViewer?.opener
6768

68-
const { url } = useEncrypted()
69+
const { url } = useEncrypted()
6970

70-
const ComponentName = useMemo(
71-
() =>
72-
getViewerComponentName({
73-
file,
74-
isDesktop,
75-
isOnlyOfficeEnabled
76-
}),
77-
[file, isDesktop, isOnlyOfficeEnabled]
78-
)
71+
const ComponentName = useMemo(
72+
() =>
73+
getViewerComponentName({
74+
file,
75+
isDesktop,
76+
isOnlyOfficeEnabled
77+
}),
78+
[file, isDesktop, isOnlyOfficeEnabled]
79+
)
7980

80-
return (
81-
<ComponentName
82-
file={file}
83-
url={url}
84-
onClose={onClose}
85-
renderFallbackExtraContent={renderFallbackExtraContent}
86-
gestures={gestures}
87-
gesturesRef={gesturesRef}
88-
onSwipe={onSwipe}
89-
onlyOfficeOpener={onlyOfficeOpener}
90-
/>
91-
)
92-
}
81+
return (
82+
<ComponentName
83+
file={file}
84+
url={url}
85+
onClose={onClose}
86+
renderFallbackExtraContent={renderFallbackExtraContent}
87+
gestures={gestures}
88+
gesturesRef={gesturesRef}
89+
onSwipe={onSwipe}
90+
onlyOfficeOpener={onlyOfficeOpener}
91+
/>
92+
)
93+
}
94+
)
9395

9496
ViewerByFile.propTypes = {
9597
file: FileDoctype.isRequired,
@@ -102,4 +104,6 @@ ViewerByFile.propTypes = {
102104
componentsProps: PropTypes.object
103105
}
104106

105-
export default withBreakpoints()(ViewerByFile)
107+
ViewerByFile.displayName = 'ViewerByFile'
108+
109+
export default ViewerByFile

react/Viewer/components/ViewerControls.jsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,18 @@ class ViewerControls extends Component {
9292

9393
renderChildren(children) {
9494
if (!children) return null
95-
return React.cloneElement(children, {
96-
gestures: this.state.gestures,
97-
gesturesRef: this.wrapped,
98-
onSwipe: this.onSwipe
95+
96+
return React.Children.map(children, child => {
97+
if (
98+
child.type.name === 'ViewerByFile' ||
99+
child.type.displayName === 'ViewerByFile'
100+
) {
101+
return React.cloneElement(child, {
102+
gestures: this.state.gestures,
103+
gesturesRef: this.wrapped,
104+
onSwipe: this.onSwipe
105+
})
106+
}
99107
})
100108
}
101109

@@ -137,11 +145,13 @@ class ViewerControls extends Component {
137145
<Toolbar
138146
toolbarRef={toolbarRef}
139147
file={file}
140-
onClose={showClose && onClose}
148+
showFilePath={showFilePath}
141149
onMouseEnter={this.showControls}
142150
onMouseLeave={this.hideControls}
143-
showFilePath={showFilePath}
144-
/>
151+
onClose={showClose && onClose}
152+
>
153+
{children}
154+
</Toolbar>
145155
)}
146156
{showNavigation && isDesktop && hasPrevious && (
147157
<Navigation

0 commit comments

Comments
 (0)