No Description

index.tsx 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React, { useState } from "react";
  2. import BraftEditor, { ControlType } from "braft-editor";
  3. import "braft-editor/dist/index.css";
  4. // 引入表情包扩展模块样式文件
  5. import 'braft-extensions/dist/emoticon.css'
  6. // 引入表情包扩展模块和默认表情包列表
  7. import Emoticon, { defaultEmoticons } from 'braft-extensions/dist/emoticon';
  8. import MaxLength from 'braft-extensions/dist/max-length'
  9. const lengthOptions = {
  10. defaultValue: 100, // 指定默认限制数,如不指定则为Infinity(无限)
  11. // includeEditors: ['editor-id-1'], // 指定该模块对哪些BraftEditor生效,不传此属性则对所有BraftEditor有效
  12. // excludeEditors: ['editor-id-2'], // 指定该模块对哪些BraftEditor无效
  13. }
  14. BraftEditor.use(MaxLength(lengthOptions))
  15. // 转换默认表情包列表,让webpack可以正确加载到默认表情包中的图片,请确保已对png格式的文件配置了loader
  16. // 如果你使用的webpack版本不支持动态require,或者使用的其他打包工具,请勿使用此写法
  17. const emoticons = defaultEmoticons.map((item: string) => require(`braft-extensions/dist/assets/${item}`))
  18. // 也可以使用自己的表情包资源,不受打包工具限制
  19. // const emoticons = ['http://path/to/emoticon-1.png', 'http://path/to/emoticon-2.png', 'http://path/to/emoticon-3.png', 'http://path/to/emoticon-4.png', ...]
  20. const emotionOptions = {
  21. // includeEditors: ['editor-id-1'], // 指定该模块对哪些BraftEditor生效,不传此属性则对所有BraftEditor有效
  22. // excludeEditors: ['editor-id-2'], // 指定该模块对哪些BraftEditor无效
  23. emoticons: emoticons, // 指定可用表情图片列表,默认为空
  24. closeOnBlur: true, // 指定是否在点击表情选择器之外的地方时关闭表情选择器,默认false
  25. closeOnSelect: false // 指定是否在选择表情后关闭表情选择器,默认false
  26. }
  27. BraftEditor.use(Emoticon(emotionOptions))
  28. export const BarftEditorPage = () => {
  29. const controls: ControlType[] = [
  30. "bold",
  31. "italic",
  32. "underline",
  33. "separator",
  34. "link",
  35. "emoji",
  36. "separator",
  37. "media"
  38. ];
  39. const [editorState, setEditorState] = useState(
  40. BraftEditor.createEditorState("<p>Hello <b>World!</b></p>")
  41. );
  42. return (
  43. <div className="editor-wrapper">
  44. <BraftEditor
  45. value={editorState}
  46. onChange={setEditorState}
  47. controls={controls}
  48. contentStyle={{
  49. height: 210,
  50. boxShadow: "inset 0 1px 3px rgba(0,0,0,.1)"
  51. }}
  52. />
  53. </div>
  54. );
  55. };
  56. export default BarftEditorPage;