Reactjs - 画像の追加draft-js-image-plugin

okwaves2024-01-25  9

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 にあります。 /画像/AddImageEditor。また、画像プラグインを使用せずに同様のことを行うこのメディア記事に従ってみました https://medium.com/@siobhanpmahoney/building-a-rich-text-editor-with-react-and-draft- js-part-2-4-persisting-data-to-server-cd68e81c820.

画像が既に埋め込まれた状態で初期状態を設定すると、レンダリングされます。ただし、後から追加する仕組みを使用している場合は機能しないようです。さまざまなバージョンのプラグイン/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>
  )
}


------------------------

editorState の変更は、insertImage からの変更をオーバーライドします。 onClick insertImage

とともに focusEditor をトリガーしています。

総合生活情報サイト - OKWAVES
総合生活情報サイト - OKWAVES
生活総合情報サイトokwaves(オールアバウト)。その道のプロ(専門家)が、日常生活をより豊かに快適にするノウハウから業界の最新動向、読み物コラムまで、多彩なコンテンツを発信。