Nenhuma descrição

index.jsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import './style.scss'
  2. import React from 'react'
  3. import getEditorControls from 'configs/controls'
  4. import LinkEditor from 'components/business/LinkEditor'
  5. import HeadingPicker from 'components/business/Headings'
  6. import TextColorPicker from 'components/business/TextColor'
  7. import FontSizePicker from 'components/business/FontSize'
  8. import LineHeightPicker from 'components/business/LineHeight'
  9. import FontFamilyPicker from 'components/business/FontFamily'
  10. import TextAlign from 'components/business/TextAlign'
  11. import EmojiPicker from 'components/business/EmojiPicker'
  12. import LetterSpacingPicker from 'components/business/LetterSpacing'
  13. import TextIndentPicker from 'components/business/TextIndent'
  14. import DropDown from 'components/common/DropDown'
  15. import { ContentUtils } from 'braft-utils'
  16. import { showModal } from 'components/common/Modal'
  17. const commandHookMap = {
  18. 'inline-style': 'toggle-inline-style',
  19. 'block-type': 'change-block-type',
  20. 'editor-method': 'exec-editor-command'
  21. }
  22. export default class ControlBar extends React.Component {
  23. mediaLibiraryModal = null
  24. extendedModals = {}
  25. componentDidUpdate () {
  26. const { controls, language } = this.props
  27. controls.forEach(item => {
  28. if (item.type === 'modal') {
  29. if (item.modal && item.modal.id && this.extendedModals[item.modal.id]) {
  30. this.extendedModals[item.modal.id].update({ ...item.modal, language })
  31. }
  32. }
  33. })
  34. }
  35. getControlItemClassName (data) {
  36. let className = 'control-item button'
  37. let { type, command } = data
  38. if (type === 'inline-style' && ContentUtils.selectionHasInlineStyle(this.props.editorState, command)) {
  39. className += ' active'
  40. } else if (type === 'block-type' && ContentUtils.getSelectionBlockType(this.props.editorState) === command) {
  41. className += ' active'
  42. }
  43. return className
  44. }
  45. applyControl (command, type) {
  46. const hookReturns = this.props.hooks(commandHookMap[type] || type, command)(command)
  47. if (hookReturns === false) {
  48. return false
  49. }
  50. if (typeof hookReturns === 'string') {
  51. command = hookReturns
  52. }
  53. if (type === 'inline-style') {
  54. this.props.editor.setValue(ContentUtils.toggleSelectionInlineStyle(this.props.editorState, command))
  55. } else if (type === 'block-type') {
  56. this.props.editor.setValue(ContentUtils.toggleSelectionBlockType(this.props.editorState, command))
  57. } else if (type === 'editor-method') {
  58. this.props.editor[command] && this.props.editor[command]()
  59. }
  60. this.props.editor.requestFocus()
  61. }
  62. openBraftFinder = () => {
  63. if (!this.props.braftFinder || !this.props.braftFinder.ReactComponent) {
  64. return false
  65. }
  66. if (this.props.hooks('open-braft-finder')() === false) {
  67. return false
  68. }
  69. const mediaProps = this.props.media
  70. const MediaLibrary = this.props.braftFinder.ReactComponent
  71. this.mediaLibiraryModal = showModal({
  72. title: this.props.language.controls.mediaLibirary,
  73. language: this.props.language,
  74. width: 640,
  75. showFooter: false,
  76. children: (
  77. <MediaLibrary
  78. onCancel={this.closeBraftFinder}
  79. onInsert={this.insertMedias}
  80. onChange={mediaProps.onChange}
  81. externals={mediaProps.externals}
  82. onBeforeSelect={this.bindBraftFinderHook('select-medias')}
  83. onBeforeDeselect={this.bindBraftFinderHook('deselect-medias')}
  84. onBeforeRemove={this.bindBraftFinderHook('remove-medias')}
  85. onBeforeInsert={this.bindBraftFinderHook('insert-medias')}
  86. onFileSelect={this.bindBraftFinderHook('select-files')}
  87. />
  88. )
  89. })
  90. }
  91. bindBraftFinderHook = (hookName) => (...params) => {
  92. return this.props.hooks(hookName, params[0])(...params)
  93. }
  94. insertMedias = (medias) => {
  95. this.props.editor.setValue(ContentUtils.insertMedias(this.props.editorState, medias))
  96. this.props.editor.requestFocus()
  97. this.props.media.onInsert && this.props.media.onInsert(medias)
  98. this.closeBraftFinder()
  99. }
  100. closeBraftFinder = () => {
  101. this.props.media.onCancel && this.props.media.onCancel()
  102. this.mediaLibiraryModal && this.mediaLibiraryModal.close()
  103. }
  104. render() {
  105. const { editor, editorState, controls, media, extendControls, language, hooks, colors, fontSizes, fontFamilies, emojis, containerNode, lineHeights, letterSpacings, textAligns, textBackgroundColor, textIndents} = this.props
  106. const currentBlockType = ContentUtils.getSelectionBlockType(editorState)
  107. const editorControls = getEditorControls(language)
  108. const commonProps = { editor, editorState, language, containerNode, hooks }
  109. const renderedControls = []
  110. return (
  111. <div className='bf-controlbar'>
  112. {
  113. [
  114. ...controls,
  115. ...extendControls
  116. ].map((item, index) => {
  117. let itemKey = typeof item === 'string' ? item : item.key
  118. if (typeof itemKey !== 'string') {
  119. return null
  120. }
  121. if (renderedControls.indexOf(itemKey) > -1) {
  122. return null
  123. }
  124. if (itemKey.toLowerCase() === 'separator') {
  125. return <span key={index} className='separator-line'></span>
  126. }
  127. let controlItem = editorControls.find((subItem) => {
  128. return subItem.key.toLowerCase() === itemKey.toLowerCase()
  129. })
  130. if (typeof item !== 'string') {
  131. controlItem = { ...controlItem, ...item }
  132. }
  133. if (!controlItem) {
  134. return null
  135. }
  136. renderedControls.push(itemKey)
  137. if (controlItem.type === 'headings') {
  138. return <HeadingPicker
  139. key={index}
  140. current={currentBlockType}
  141. onChange={(command) => this.applyControl(command, 'block-type')}
  142. {...commonProps}
  143. />
  144. } else if (controlItem.type === 'text-color') {
  145. return <TextColorPicker
  146. key={index}
  147. colors={colors}
  148. enableBackgroundColor={textBackgroundColor}
  149. {...commonProps}
  150. />
  151. } else if (controlItem.type === 'font-size') {
  152. return <FontSizePicker
  153. key={index}
  154. fontSizes={fontSizes}
  155. defaultCaption={controlItem.title}
  156. {...commonProps}
  157. />
  158. } else if (controlItem.type === 'line-height') {
  159. return <LineHeightPicker
  160. key={index}
  161. lineHeights={lineHeights}
  162. defaultCaption={controlItem.title}
  163. {...commonProps}
  164. />
  165. } else if (controlItem.type === 'letter-spacing') {
  166. return <LetterSpacingPicker
  167. key={index}
  168. letterSpacings={letterSpacings}
  169. defaultCaption={controlItem.title}
  170. {...commonProps}
  171. />
  172. } else if (controlItem.type === 'text-indent') {
  173. return <TextIndentPicker
  174. key={index}
  175. textIndents={textIndents}
  176. defaultCaption={controlItem.title}
  177. {...commonProps}
  178. />
  179. } else if (controlItem.type === 'font-family') {
  180. return <FontFamilyPicker
  181. key={index}
  182. fontFamilies={fontFamilies}
  183. defaultCaption={controlItem.title}
  184. {...commonProps}
  185. />
  186. } else if (controlItem.type === 'emoji') {
  187. return <EmojiPicker
  188. key={index}
  189. emojis={emojis}
  190. defaultCaption={controlItem.text}
  191. {...commonProps}
  192. />
  193. } else if (controlItem.type === 'link') {
  194. return <LinkEditor
  195. key={index}
  196. {...commonProps}
  197. />
  198. } else if (controlItem.type === 'text-align') {
  199. return (
  200. <TextAlign
  201. key={index}
  202. textAligns={textAligns}
  203. {...commonProps}
  204. />
  205. )
  206. } else if (controlItem.type === 'media') {
  207. if (!media.image && !media.video && !media.audio) {
  208. return null
  209. }
  210. return (
  211. <button
  212. type='button'
  213. key={index}
  214. data-title={controlItem.title}
  215. className='control-item media button'
  216. onClick={this.openBraftFinder}
  217. >
  218. {controlItem.text}
  219. </button>
  220. )
  221. } else if (controlItem.type === 'dropdown') {
  222. return (
  223. <DropDown
  224. key={index}
  225. className={'control-item extend-control-item dropdown ' + controlItem.className}
  226. caption={controlItem.text}
  227. htmlCaption={controlItem.html}
  228. showArrow={controlItem.showArrow}
  229. containerNode={controlItem.containerNode}
  230. title={controlItem.title}
  231. arrowActive={controlItem.arrowActive}
  232. autoHide={controlItem.autoHide}
  233. disabled={controlItem.disabled}
  234. ref={controlItem.ref}
  235. >
  236. {controlItem.component}
  237. </DropDown>
  238. )
  239. } else if (controlItem.type === 'modal') {
  240. return (
  241. <button
  242. type='button'
  243. key={index}
  244. data-title={controlItem.title}
  245. className={'control-item extend-control-item button ' + controlItem.className}
  246. dangerouslySetInnerHTML={controlItem.html ? { __html: controlItem.html } : null}
  247. onClick={(event) => {
  248. if (controlItem.modal && controlItem.modal.id) {
  249. if (this.extendedModals[controlItem.modal.id]) {
  250. this.extendedModals[controlItem.modal.id].active = true
  251. this.extendedModals[controlItem.modal.id].update({ ...controlItem.modal, language })
  252. } else {
  253. this.extendedModals[controlItem.modal.id] = showModal({ ...controlItem.modal, language })
  254. controlItem.modal.onCreate && controlItem.modal.onCreate(this.extendedModals[controlItem.modal.id])
  255. }
  256. }
  257. controlItem.onClick && controlItem.onClick(event)
  258. }}
  259. >
  260. {!controlItem.html ? controlItem.text : null}
  261. </button>
  262. )
  263. } else if (controlItem.type === 'component') {
  264. return (
  265. <div
  266. key={index}
  267. className={'control-item component-wrapper ' + controlItem.className}
  268. >{controlItem.component}</div>
  269. )
  270. } else if (controlItem.type === 'button') {
  271. return (
  272. <button
  273. type='button'
  274. key={index}
  275. data-title={controlItem.title}
  276. className={'control-item button ' + controlItem.className}
  277. dangerouslySetInnerHTML={controlItem.html ? { __html: controlItem.html } : null}
  278. onClick={(event) => controlItem.onClick && controlItem.onClick(event)}
  279. >
  280. {!controlItem.html ? controlItem.text : null}
  281. </button>
  282. )
  283. } else {
  284. return (
  285. <button
  286. type='button'
  287. key={index}
  288. data-title={controlItem.title}
  289. className={this.getControlItemClassName({
  290. type: controlItem.type,
  291. command: controlItem.command
  292. })}
  293. onClick={() => this.applyControl(controlItem.command, controlItem.type)}
  294. >
  295. {controlItem.text}
  296. </button>
  297. )
  298. }
  299. })
  300. }
  301. </div>
  302. )
  303. }
  304. }