Nenhuma descrição

index.jsx 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import './style.scss'
  2. import React from 'react'
  3. import DropDown from 'components/common/DropDown'
  4. import { ContentUtils } from 'braft-utils'
  5. const toggleFontFamily = (event, props) => {
  6. let fontFamilyName = event.currentTarget.dataset.name
  7. const hookReturns = props.hooks('toggle-font-family', fontFamilyName)(fontFamilyName, props.fontFamilies)
  8. if (hookReturns === false) {
  9. return false
  10. }
  11. if (typeof hookReturns === 'string') {
  12. fontFamilyName = hookReturns
  13. }
  14. props.editor.setValue(ContentUtils.toggleSelectionFontFamily(props.editorState, fontFamilyName, props.fontFamilies))
  15. props.editor.requestFocus()
  16. }
  17. export default (props) => {
  18. let caption = null
  19. let currentIndex = null
  20. props.fontFamilies.find((item, index) => {
  21. if (ContentUtils.selectionHasInlineStyle(props.editorState, 'FONTFAMILY-' + item.name)) {
  22. caption = item.name
  23. currentIndex = index
  24. return true
  25. }
  26. return false
  27. })
  28. return (
  29. <DropDown
  30. caption={caption || props.defaultCaption}
  31. containerNode={props.containerNode}
  32. title={props.language.controls.fontFamily}
  33. arrowActive={currentIndex === 0}
  34. className={'control-item dropdown font-family-dropdown'}
  35. >
  36. <ul className='menu'>
  37. {props.fontFamilies.map((item, index) => {
  38. return (
  39. <li
  40. key={index}
  41. className={'menu-item ' + (index === currentIndex ? 'active' : '')}
  42. data-name={item.name}
  43. onClick={(event) => toggleFontFamily(event, props)}
  44. >
  45. <span style={{fontFamily: item.family}}>{item.name}</span>
  46. </li>
  47. )
  48. })}
  49. </ul>
  50. </DropDown>
  51. )
  52. }