ContainerContext.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 { createContext, useState, useCallback } from 'react'
  21. import { DisplayDefaultGridSize } from '../Setting'
  22. type ContainerContextValue = {
  23. scale: [number, number]
  24. grid: [number, number]
  25. zoomRatio: number
  26. sliderValue: number
  27. slideTranslate: [number, number]
  28. scaleChange: (newScale: [number, number]) => void
  29. sliderChange: (newSliderValue: number) => void
  30. slideTranslateChange: (newTranslate: [number, number]) => void
  31. zoomIn: () => void
  32. zoomOut: () => void
  33. }
  34. const DefaultContainerContextValue: ContainerContextValue = {
  35. scale: [1, 1],
  36. grid: [DisplayDefaultGridSize, DisplayDefaultGridSize],
  37. zoomRatio: 1,
  38. sliderValue: 20,
  39. slideTranslate: [0, 0],
  40. scaleChange: () => {},
  41. sliderChange: () => {},
  42. slideTranslateChange: () => {},
  43. zoomIn: () => {},
  44. zoomOut: () => {}
  45. }
  46. export const ContainerContext = createContext<ContainerContextValue>(DefaultContainerContextValue)
  47. export const useContainerContext = (
  48. grid: [number, number] = [DisplayDefaultGridSize, DisplayDefaultGridSize]
  49. ): ContainerContextValue => {
  50. const [scale, setScale] = useState(DefaultContainerContextValue.scale)
  51. const [zoomRatio, setZoomRatio] = useState(DefaultContainerContextValue.zoomRatio)
  52. const [sliderValue, setSliderValue] = useState(DefaultContainerContextValue.sliderValue)
  53. const [slideTranslate, setSlideTranslate] = useState<
  54. ContainerContextValue['slideTranslate']
  55. >(DefaultContainerContextValue.slideTranslate)
  56. const sliderChange = useCallback((newSliderValue: number) => {
  57. const newZoomRatio = newSliderValue / 40 + 0.5
  58. setSliderValue(newSliderValue)
  59. setZoomRatio(newZoomRatio)
  60. }, [])
  61. const zoomIn = useCallback(() => {
  62. sliderChange(Math.max(sliderValue - 10, 0))
  63. }, [sliderValue])
  64. const zoomOut = useCallback(() => {
  65. sliderChange(Math.max(sliderValue + 10, 0))
  66. }, [sliderValue])
  67. const scaleChange = useCallback(
  68. (nextScale: [number, number]) => {
  69. if (nextScale.join() !== scale.join()) {
  70. setScale(nextScale)
  71. }
  72. },
  73. [scale]
  74. )
  75. const slideTranslateChange = useCallback(
  76. (nextSlideTranslate: [number, number]) => {
  77. if (nextSlideTranslate !== slideTranslate) {
  78. setSlideTranslate(nextSlideTranslate)
  79. }
  80. },
  81. [slideTranslate]
  82. )
  83. return {
  84. scale,
  85. grid,
  86. zoomRatio,
  87. sliderValue,
  88. slideTranslate,
  89. scaleChange,
  90. sliderChange,
  91. slideTranslateChange,
  92. zoomIn,
  93. zoomOut
  94. }
  95. }