BlockNoteReact.jsx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import React from 'react';
  2. import { useCreateBlockNote } from '@blocknote/react';
  3. import { BlockNoteView } from '@blocknote/mantine';
  4. import { BlockNoteSchema, createHeadingBlockSpec } from "@blocknote/core";
  5. import { zh, en } from "@blocknote/core/locales";
  6. import '@blocknote/mantine/style.css';
  7. import '@blocknote/core/fonts/inter.css';
  8. import { uploadFile } from '@/api/common.js'
  9. // 正确导入需要的组件
  10. import { ColumnBlock, ColumnListBlock, withMultiColumn, multiColumnSchema} from "@blocknote/xl-multi-column"
  11. const BlockNoteReact = ({ modelValue, onUpdateModelValue, editable = true, currentLang = 'zh' }) => {
  12. let isEditable = editable;
  13. //currentLang变化时重新创建editor实例
  14. React.useEffect(() => {
  15. }, [currentLang]);
  16. // 创建带有多列支持的 schema
  17. // const schema = BlockNoteSchema.create({
  18. // blockSpecs: {
  19. // },
  20. // });
  21. // 在React组件内部使用Hook
  22. const editor = useCreateBlockNote({
  23. // 正确的方式:创建扩展的schema
  24. //schema, // 使用扩展后的 schema
  25. // schema: BlockNoteSchema.create().extend({
  26. // blockSpecs: {
  27. // heading: createHeadingBlockSpec({
  28. // column: ColumnBlock,
  29. // columnList: ColumnListBlock,
  30. // }),
  31. // // 添加多列相关的块类型
  32. // // column: ColumnBlock,
  33. // // columnList: ColumnListBlock
  34. // }
  35. // }),
  36. id: "blocknote-react-editor",
  37. dictionary: currentLang === 'en' ? en : zh,
  38. initialContent: modelValue || undefined,
  39. iseditable : false,
  40. autofocus: false, // 默认 false,true 开启自动聚焦
  41. // 3. 允许使用的块类型(限制编辑器功能)
  42. // allowedBlockTypes: [
  43. // "paragraph", // 普通段落
  44. // "heading", // 标题
  45. // "list", // 列表(有序/无序)
  46. // "image", // 图片
  47. // "quote", // 引用
  48. // ],
  49. uploadFile: async (file) => {
  50. // 上传文件到服务器
  51. const formData = new FormData();
  52. formData.append('file', file);
  53. const response = await uploadFile('/upload', formData);
  54. console.log('上传响应:', response);
  55. const data = await response.json();
  56. return data.url; // 返回图片 URL
  57. },
  58. // placeholder: "请输入内容(支持块级编辑、格式设置)"
  59. });
  60. editor.onMount(() => {
  61. console.log('BlockNote 编辑器已挂载(React 版)', editor);
  62. });
  63. // 监听编辑器内容变化
  64. React.useEffect(() => {
  65. const unsubscribe = editor.onChange(async() => {
  66. const html = await editor.blocksToFullHTML(editor.document);
  67. // onUpdateModelValue(editor.document);
  68. onUpdateModelValue(editor.document,html);
  69. });
  70. return unsubscribe;
  71. }, [editor, onUpdateModelValue]);
  72. // 监听外部值变化
  73. React.useEffect(() => {
  74. if (modelValue && JSON.stringify(modelValue) !== JSON.stringify(editor.document)) {
  75. editor.replaceBlocks(editor.document, modelValue);
  76. }
  77. }, [modelValue, editor]);
  78. return (
  79. <BlockNoteView
  80. editor={editor}
  81. key={editor.id}
  82. editable={isEditable}
  83. />
  84. );
  85. };
  86. export default BlockNoteReact;