Alignment.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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, { useCallback, useContext } from 'react'
  21. import classnames from 'classnames'
  22. import { Icon } from 'antd'
  23. import { BlockProperties, BlockAlignments } from '../Element/constants'
  24. import { EditorContext } from '../context'
  25. const Alignment: React.FC = () => {
  26. const { isBlockPropertyActive, toggleBlockProperty } = useContext(
  27. EditorContext
  28. )
  29. const isActive = (value: BlockAlignments) =>
  30. !!isBlockPropertyActive(BlockProperties.TextAlign, value)
  31. const toggle = (value) => {
  32. toggleBlockProperty(BlockProperties.TextAlign, value)
  33. }
  34. return (
  35. <>
  36. <AlignmentItem
  37. value={BlockAlignments.AlignLeft}
  38. type="align-left"
  39. isActive={isActive}
  40. onToggle={toggle}
  41. />
  42. <AlignmentItem
  43. value={BlockAlignments.AlignCenter}
  44. type="align-center"
  45. isActive={isActive}
  46. onToggle={toggle}
  47. />
  48. <AlignmentItem
  49. value={BlockAlignments.AlignRight}
  50. type="align-right"
  51. isActive={isActive}
  52. onToggle={toggle}
  53. />
  54. </>
  55. )
  56. }
  57. export default Alignment
  58. interface IAlignmentItemProps {
  59. value: BlockAlignments
  60. type: string
  61. isActive: (value: BlockAlignments) => boolean
  62. onToggle: (value: BlockAlignments) => void
  63. }
  64. const AlignmentItem: React.FC<IAlignmentItemProps> = (props) => {
  65. const { value, type, isActive, onToggle } = props
  66. const toggle = useCallback(() => {
  67. onToggle(value)
  68. }, [value])
  69. const cls = classnames({
  70. 'richtext-toolbar-icon': true,
  71. 'richtext-toolbar-icon-active': isActive(value)
  72. })
  73. return <Icon className={cls} type={type} onClick={toggle} />
  74. }