ElementIcon.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 } from 'react'
  21. import classnames from 'classnames'
  22. import { Icon } from 'antd'
  23. import { IconProps } from 'antd/lib/icon'
  24. import IconFont from 'components/IconFont'
  25. import { ElementType } from '../../Element'
  26. import { EditorContext } from '../../context'
  27. interface IElementIconProps extends IconProps {
  28. value: ElementType
  29. iconFont?: boolean
  30. }
  31. const ElementIcon: React.FC<IElementIconProps> = (props) => {
  32. const { value, iconFont, ...rest } = props
  33. const { isElementActive, toggleElement } = useContext(EditorContext)
  34. const toggle = () => {
  35. toggleElement(value)
  36. }
  37. const cls = classnames({
  38. 'richtext-toolbar-icon': true,
  39. 'richtext-toolbar-icon-active': isElementActive(value)
  40. })
  41. return iconFont ? (
  42. <IconFont className={cls} {...rest} onClick={toggle} />
  43. ) : (
  44. <Icon className={cls} {...rest} onClick={toggle} />
  45. )
  46. }
  47. export default ElementIcon