0

我尝试了多种不同的方法来尝试在 Draft-js 中附加图像。

我的尝试遵循教程https://www.draft-js-plugins.com/plugin/image中的示例。对于“添加图像按钮示例”,可以在此处找到完整的源代码https://github.com/draft-js-plugins/draft-js-plugins/tree/master/docs/client/components/pages/图像/添加图像编辑器。我也尝试关注这篇不使用图像插件做类似事情的中篇文章https://medium.com/@siobhanpmahoney/building-a-rich-text-editor-with-react-and-draft- js-part-2-4-persisting-data-to-server-cd68e81c820

如果我使用已嵌入的图像设置初始状态,它会呈现。但是,当使用稍后添加它的机制时,它似乎不起作用。我读过一些关于各种版本的 plugins / draft.js 有问题的提及。
我的版本:

“draft-js-image-plugin”:“^2.0.7”,“draft-js”:“^0.11.6”,

import { EditorState } from 'draft-js';
import Editor from 'draft-js-plugins-editor';
import createInlineToolbarPlugin, { Separator } from 'draft-js-inline-toolbar-plugin';
import createImagePlugin from 'draft-js-image-plugin';
import 'draft-js/dist/Draft.css';
import 'draft-js-inline-toolbar-plugin/lib/plugin.css';
import {
  ItalicButton,
  BoldButton,
  UnderlineButton,
  HeadlineOneButton,
  CodeButton,
  UnorderedListButton,
  OrderedListButton,
  BlockquoteButton,
} from 'draft-js-buttons';

interface TextEditorProps {
  label?: string;
  value: EditorState;
  onChange?: (editorState: EditorState) => void;
  className?: string;
  disabled?: boolean;
}

const useStyles = makeStyles((theme) => ({
  label: {
    margin: theme.spacing(1)
  },
  editor: {
    boxSizing: 'border-box',
    border: '1px solid #ddd',
    cursor: 'text',
    padding: '16px',
    borderRadius: '2px',
    marginBottom: '2em',
    boxShadow: 'inset 0px 1px 8px -3px #ABABAB',
    background: '#fefefe',
    minHeight: '50vh'
  }
}));
const inlineToolbarPlugin = createInlineToolbarPlugin();
const imagePlugin = createImagePlugin();
const { InlineToolbar } = inlineToolbarPlugin;
const plugins = [inlineToolbarPlugin, imagePlugin];
const TextEditor:React.FunctionComponent<TextEditorProps> = ({label, value, onChange, className, disabled }: TextEditorProps) => {
  const editor = React.useRef(null);
  const focusEditor = () => {
    editor.current.focus();
  }
  React.useEffect(() => {
    focusEditor()
  }, []);
  const classes = useStyles();
  const insertImage = () => {
    onChange(imagePlugin.addImage(value, "https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg"));
  }
  return (
    <Box className={className} onClick={focusEditor}>
      {label && <InputLabel className={classes.label}>{label}</InputLabel>}
      <div className="menuButtons">
          <button className="inline styleButton">
            <i
              className="material-icons"
              style={{
                fontSize: "16px",
                textAlign: "center",
                padding: "0px",
                margin: "0px"
              }}
              onClick={insertImage}
            >
              image
            </i>
          </button>
        </div>
      <Box className={!disabled && classes.editor} >
        <Editor
          ref={editor}
          editorState={value}
          onChange={onChange}
          plugins={plugins}
          spellCheck={true}
          readOnly={disabled}
        />
        {<InlineToolbar>
          {(externalProps) => (
            <>
              <BoldButton {...externalProps} />
              <ItalicButton {...externalProps} />
              <UnderlineButton {...externalProps} />
              <CodeButton {...externalProps} />
              <Separator {...externalProps} />
              <UnorderedListButton {...externalProps} />
              <OrderedListButton {...externalProps} />
              <BlockquoteButton {...externalProps} />
              <HeadlineOneButton {...externalProps} />
            </>
          )}  
        </InlineToolbar>}
      </Box>
    </Box>
  )
}
4

2 回答 2

0

如果您不介意弄脏手并学习如何插入内容,则不需要额外的插件。

           <i
              className="material-icons"
              style={{
                fontSize: "16px",
                textAlign: "center",
                padding: "0px",
                margin: "0px"
              }}
              onClick={() => insertImage('https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg')}
            >

自定义 insertImage 函数,在您的情况下,editorState 应替换为 'value'

import { EditorState, AtomicBlockUtils } from “draft-js”; //<-- need add this import

const insertImage = ( url ) => {
    const contentState = editorState.getCurrentContent();
    const contentStateWithEntity = contentState.createEntity(
        'IMAGE',
        'IMMUTABLE',
        { src: url },)
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
const newEditorState = EditorState.set( editorState, { currentContent: contentStateWithEntity });
onChange(AtomicBlockUtils.insertAtomicBlock(newEditorState, entityKey, '')) //Update the editor state
};

使用 Draftjs 上传图片

于 2020-11-27T22:57:25.487 回答
0

您的editorState更改覆盖了insertImage的更改。您正在触发focusEditor以及 onClick insertImage

于 2020-11-27T22:45:18.660 回答