LayerResizable.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, useState, useEffect, useCallback, useMemo } from 'react'
  21. import {
  22. ContainerContext,
  23. SlideContext
  24. } from 'containers/Display/components/Container'
  25. import { LayerListContext, LayerContext } from '../util'
  26. import { SecondaryGraphTypes } from '../../Setting'
  27. import { Resizable, ResizeCallbackData } from 'libs/react-resizable'
  28. import { DeltaSize } from '../types'
  29. const LayerResizable: React.FC = (props) => {
  30. const { scale, grid } = useContext(ContainerContext)
  31. const { slideParams } = useContext(SlideContext)
  32. const { onResize } = useContext(LayerListContext)
  33. const {
  34. layer: { id: layerId, params, subType }
  35. } = useContext(LayerContext)
  36. const { width: slideWidth, height: slideHeight } = slideParams
  37. const { width, height, positionX, positionY } = params
  38. const maxConstraints: [number, number] = [
  39. slideWidth - positionX,
  40. slideHeight - positionY
  41. ]
  42. const resizeProps = useMemo(
  43. (): Array<'s' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne'> =>
  44. subType === SecondaryGraphTypes.Label
  45. ? ['w', 'e']
  46. : ['se'],
  47. [subType]
  48. )
  49. const resize = useCallback(
  50. (e: React.SyntheticEvent, { size }: ResizeCallbackData) => {
  51. e.stopPropagation()
  52. const { width: newWidth, height: newHeight } = size
  53. onResize(layerId, { deltaWidth: newWidth - width, deltaHeight: newHeight - height })
  54. },
  55. [width, height, layerId, onResize]
  56. )
  57. const resizeStop = useCallback(
  58. (e: React.SyntheticEvent, { size }: ResizeCallbackData) => {
  59. e.stopPropagation()
  60. const { width: newWidth, height: newHeight } = size
  61. const deltaSize: DeltaSize = { deltaWidth: newWidth - width, deltaHeight: newHeight - height }
  62. onResize(layerId, deltaSize, true)
  63. },
  64. [layerId, width, height, onResize]
  65. )
  66. return (
  67. <Resizable
  68. width={width}
  69. height={height}
  70. scale={scale[0]}
  71. onResize={resize}
  72. onResizeStop={resizeStop}
  73. draggableOpts={{ grid }}
  74. minConstraints={[50, 50]}
  75. maxConstraints={maxConstraints}
  76. handleSize={[20, 20]}
  77. resizeHandles={resizeProps}
  78. >
  79. {props.children as React.ReactElement}
  80. </Resizable>
  81. )
  82. }
  83. export default LayerResizable