No Description

index.tsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. draftProps?: any;
  41. placeholder?: string;
  42. converts?: any;
  43. }
  44. export const BarftEditorPage = ({
  45. value,
  46. onChange,
  47. controls,
  48. contentStyle = {},
  49. FloatControls,
  50. draftProps,
  51. placeholder,
  52. blockRenderMap,
  53. blockRendererFn,
  54. converts,
  55. }: BaseEditorProps) => {
  56. const optionsControls = controls
  57. ? controls
  58. : [
  59. "bold",
  60. "italic",
  61. "underline",
  62. "separator",
  63. "link",
  64. "emoji",
  65. "separator",
  66. "media"
  67. ];
  68. const options: any = {
  69. controls: optionsControls,
  70. showControlsBar:
  71. optionsControls && optionsControls.length > 0 && !FloatControls,
  72. showFloatControls: FloatControls
  73. };
  74. const [focusState, setFocusState] = useState(false);
  75. return (
  76. <div className={styles.baseWrapper}>
  77. <BraftEditor
  78. value={value}
  79. onChange={onChange}
  80. controls={options.controls}
  81. controlBarClassName={classnames(styles.controlBar, {
  82. [styles.focus]: focusState
  83. })}
  84. controlBarStyle={options.showControlsBar ? {} : { display: "none" }}
  85. contentClassName={classnames(styles.editorContent, {
  86. [styles.focus]: focusState,
  87. [styles.hasFloatControls]: options.showFloatControls
  88. })}
  89. contentStyle={contentStyle}
  90. onFocus={() => setFocusState(true)}
  91. onBlur={() => setFocusState(false)}
  92. hooks={{
  93. "toggle-link": ({ href, target }) => {
  94. href = href.indexOf("http") === 0 ? href : `http://${href}`;
  95. return { href, target };
  96. }
  97. }}
  98. {...{
  99. blockRenderMap,
  100. blockRendererFn,
  101. converts,
  102. placeholder,
  103. draftProps,
  104. }}
  105. />
  106. {options.showFloatControls ? (
  107. <div className={classnames(styles.floatControls)}>
  108. {
  109. <FloatControls
  110. editorState={value}
  111. setEditorState={onChange}
  112. />
  113. }
  114. </div>
  115. ) : null}
  116. </div>
  117. );
  118. };
  119. export default BarftEditorPage;