Nenhuma descrição

index.tsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React, { useState } from "react";
  2. import classnames from "classnames";
  3. import BraftEditor, { ControlType, EditorState } from "braft-editor";
  4. import styles from "./index.less";
  5. import "braft-editor/dist/index.css";
  6. // 引入表情包扩展模块样式文件
  7. import "braft-extensions/dist/emoticon.css";
  8. // 引入表情包扩展模块和默认表情包列表
  9. import Emoticon, { defaultEmoticons } from "braft-extensions/dist/emoticon";
  10. import MaxLength from "braft-extensions/dist/max-length";
  11. const lengthOptions = {
  12. defaultValue: 100 // 指定默认限制数,如不指定则为Infinity(无限)
  13. // includeEditors: ['editor-id-1'], // 指定该模块对哪些BraftEditor生效,不传此属性则对所有BraftEditor有效
  14. // excludeEditors: ['editor-id-2'], // 指定该模块对哪些BraftEditor无效
  15. };
  16. BraftEditor.use(MaxLength(lengthOptions));
  17. // 转换默认表情包列表,让webpack可以正确加载到默认表情包中的图片,请确保已对png格式的文件配置了loader
  18. // 如果你使用的webpack版本不支持动态require,或者使用的其他打包工具,请勿使用此写法
  19. const emoticons = defaultEmoticons.map((item: string) =>
  20. require(`braft-extensions/dist/assets/${item}`)
  21. );
  22. // 也可以使用自己的表情包资源,不受打包工具限制
  23. // 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', ...]
  24. const emotionOptions = {
  25. // includeEditors: ['editor-id-1'], // 指定该模块对哪些BraftEditor生效,不传此属性则对所有BraftEditor有效
  26. // excludeEditors: ['editor-id-2'], // 指定该模块对哪些BraftEditor无效
  27. emoticons: emoticons, // 指定可用表情图片列表,默认为空
  28. closeOnBlur: true, // 指定是否在点击表情选择器之外的地方时关闭表情选择器,默认false
  29. closeOnSelect: false // 指定是否在选择表情后关闭表情选择器,默认false
  30. };
  31. BraftEditor.use(Emoticon(emotionOptions));
  32. export interface BaseEditorProps {
  33. value: EditorState;
  34. onChange: (editorState: EditorState) => void;
  35. contentStyle?: React.CSSProperties;
  36. controls?: ControlType[];
  37. FloatControls?: any;
  38. blockRenderMap?: any;
  39. blockRendererFn?: any;
  40. placeholder?: string;
  41. converts?: any;
  42. }
  43. export const BarftEditorPage = ({
  44. value,
  45. onChange,
  46. controls,
  47. contentStyle = {},
  48. FloatControls,
  49. placeholder,
  50. blockRenderMap,
  51. blockRendererFn,
  52. converts,
  53. }: BaseEditorProps) => {
  54. const optionsControls = controls
  55. ? controls
  56. : [
  57. "bold",
  58. "italic",
  59. "underline",
  60. "separator",
  61. "link",
  62. "emoji",
  63. "separator",
  64. "media"
  65. ];
  66. const options: any = {
  67. controls: optionsControls,
  68. showControlsBar:
  69. optionsControls && optionsControls.length > 0 && !FloatControls,
  70. showFloatControls: FloatControls
  71. };
  72. const [focusState, setFocusState] = useState(false);
  73. return (
  74. <div className={styles.baseWrapper}>
  75. <BraftEditor
  76. value={value}
  77. onChange={onChange}
  78. controls={options.controls}
  79. controlBarClassName={classnames(styles.controlBar, {
  80. [styles.focus]: focusState
  81. })}
  82. controlBarStyle={options.showControlsBar ? {} : { display: "none" }}
  83. contentClassName={classnames(styles.editorContent, {
  84. [styles.focus]: focusState,
  85. [styles.hasFloatControls]: options.showFloatControls
  86. })}
  87. contentStyle={contentStyle}
  88. onFocus={() => setFocusState(true)}
  89. onBlur={() => setFocusState(false)}
  90. hooks={{
  91. "toggle-link": ({ href, target }) => {
  92. href = href.indexOf("http") === 0 ? href : `http://${href}`;
  93. return { href, target };
  94. }
  95. }}
  96. {...{
  97. blockRenderMap,
  98. blockRendererFn,
  99. converts,
  100. placeholder,
  101. }}
  102. />
  103. {options.showFloatControls ? (
  104. <div className={classnames(styles.floatControls)}>
  105. {
  106. <FloatControls
  107. editorState={value}
  108. setEditorState={onChange}
  109. />
  110. }
  111. </div>
  112. ) : null}
  113. </div>
  114. );
  115. };
  116. export default BarftEditorPage;