1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

とりあえず動くDraft.js

1
Last updated at Posted at 2019-01-29

はじめに

Draft.jsを使うとリッチテキストエディタを簡単に作れるらしいです。
ただ npmがどうのこうの…とか、とりあえず試したい人には余計な情報が多すぎるので、とりあえず動くサンプルコードを置いておきます。

コード

適当にコピペしてください。

index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Minimal Draft.js</title>

    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js" crossorigin></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.10.5/Draft.min.js"></script>
  </head>
  <body>
    <div id="app"></div>

    <script type="text/babel">
      // 本来は別ファイルに書く
      const {Editor, EditorState} = Draft;

      class MyEditor extends React.Component {
        constructor(props) {
          super(props);
          this.state = {editorState: EditorState.createEmpty()};
          this.onChange = (editorState) => this.setState({editorState});
        }
        render() {
          return (
            <div>
              <h1>Minimal Draft.js</h1>
              <Editor editorState={this.state.editorState} onChange={this.onChange} />
            </div>
          );
        }
      }
      ReactDOM.render(
        <MyEditor/>,
        document.getElementById('app')
      )
    </script>
  </body>
</html>

おしまい

詳しいことは他を当たってください。

おまけ

CDNを使うとき、アクセス先が HTTPレスポンスヘッダに Access-Control-Allow-Origin : *とかいうのを持っていないとブラウザではじかれます。なぜかReactのチュートリアルで使われてるhttps://unpkg.com/react-dom@16/umd/react-dom.development.jsはダメだったので、https://unpkg.com/react-dom@16/umd/react-dom.production.min.jsを使っています。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?