No Description

index.jsx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import BraftEditor, { EditorState } from '../dist'
  4. import { ContentUtils } from 'braft-utils'
  5. import '../dist/index.css'
  6. class Demo extends React.Component {
  7. constructor(props) {
  8. super(props)
  9. this.state = {
  10. editorState: EditorState.createFrom()
  11. }
  12. this.editorInstance = null
  13. }
  14. handleChange = (editorState) => {
  15. this.setState({ editorState })
  16. }
  17. insertText = (text) => {
  18. this.setState({
  19. editorState: ContentUtils.insertText(this.state.editorState, text)
  20. })
  21. }
  22. uploadFn = (param) => {
  23. const xhr = new XMLHttpRequest
  24. const fd = new FormData()
  25. const successFn = () => {
  26. param.success({
  27. url: JSON.parse(xhr.responseText)[0].url,
  28. meta: {
  29. controls: true,
  30. loop: true,
  31. autoPlay: false,
  32. }
  33. })
  34. }
  35. const progressFn = (event) => {
  36. param.progress(event.loaded / event.total * 100)
  37. }
  38. const errorFn = () => {
  39. param.error({
  40. msg: "unable to upload."
  41. })
  42. }
  43. xhr.upload.addEventListener("progress", progressFn, false)
  44. xhr.addEventListener("load", successFn, false)
  45. xhr.addEventListener("error", errorFn, false)
  46. xhr.addEventListener("abort", errorFn, false)
  47. fd.append("file", param.file)
  48. xhr.open("POST", "http://localhost:9090", true)
  49. xhr.send(fd)
  50. }
  51. hooks = {
  52. 'change-block-type': console.log,
  53. 'insert-emoji': (fontSize) => {
  54. return '123'
  55. },
  56. 'insert-medias': console.log,
  57. 'exec-editor-command': console.log,
  58. 'select-medias': console.log,
  59. 'remove-medias': (medias) => {
  60. return [medias[0]]
  61. },
  62. 'deselect-medias': console.log,
  63. 'toggle-link': () => {
  64. return {
  65. href: 'http://www.baidu.com',
  66. target: '_blank'
  67. }
  68. }
  69. }
  70. render() {
  71. const controls = BraftEditor.defaultProps.controls.map(item => {
  72. return item === 'bold' ? {
  73. key: 'bold', // 使用key来指定控件类型
  74. title: '加粗选中文字哦', // 自定义控件title
  75. text: '点我加粗', // 使用自定义文案来代替默认图标(B),此处也可传入jsx
  76. } : item
  77. })
  78. const extendControls = [
  79. {
  80. key: 'insert-123',
  81. type: 'button',
  82. text: 'Insert 123',
  83. onClick: () => {
  84. this.insertText('123')
  85. }
  86. }
  87. ]
  88. return (
  89. <div>
  90. <div className="demo" id="demo">
  91. <BraftEditor
  92. hooks={this.hooks}
  93. controls={controls}
  94. extendControls={extendControls}
  95. media={{
  96. // uploadFn: this.uploadFn
  97. }}
  98. onChange={this.handleChange}
  99. defaultValue={this.state.editorState}
  100. />
  101. </div>
  102. </div>
  103. )
  104. }
  105. }
  106. ReactDOM.render(<Demo />, document.querySelector('#root'))