Content.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { useContext, useMemo, PropsWithChildren } from 'react'
  21. import { ContainerContext } from '../ContainerContext'
  22. import { SlideContext } from './SlideContext'
  23. import { ASSETS_HOST } from 'app/globalConstants'
  24. const SlideContent: React.RefForwardingComponent<
  25. HTMLDivElement,
  26. PropsWithChildren<{}>
  27. > = (props, ref) => {
  28. const { slideParams } = useContext(SlideContext)
  29. const { scale, slideTranslate } = useContext(ContainerContext)
  30. const slideStyle = useMemo(() => {
  31. const [translateX, translateY] = slideTranslate
  32. const cssStyle: React.CSSProperties = {
  33. width: `${slideParams.width}px`,
  34. height: `${slideParams.height}px`,
  35. transform: `translate(${translateX}%, ${translateY}%) scale(${
  36. scale[0]
  37. }, ${scale[1]})`
  38. }
  39. const backgroundStyle: React.CSSProperties = {
  40. backgroundSize: 'cover'
  41. }
  42. const { backgroundColor, backgroundImage, scaleMode } = slideParams
  43. if (backgroundColor) {
  44. const rgb = backgroundColor.join()
  45. backgroundStyle.backgroundColor = `rgba(${rgb})`
  46. }
  47. if (backgroundImage) {
  48. backgroundStyle.backgroundImage = `url("${ASSETS_HOST}${backgroundImage}")`
  49. }
  50. // to adjust full screen style in mobile
  51. const setStyleToBody =
  52. scaleMode === 'scaleWidth' && window.screen.width <= 1024
  53. Object.entries(backgroundStyle).forEach(([key, value]) => {
  54. setStyleToBody
  55. ? (document.body.style[key] = value)
  56. : (cssStyle[key] = value)
  57. })
  58. return cssStyle
  59. }, [slideParams, scale, slideTranslate])
  60. return (
  61. <div ref={ref} className="display-slide" style={slideStyle}>
  62. {props.children}
  63. </div>
  64. )
  65. }
  66. export default React.forwardRef(SlideContent)