wysiwygエディタについての学習12


draft-jsを使ってみる続き

ハンドルキーコマンドの引数

引数の、コマンド(command)と状態(editorState)が入れ替わる。。
入れ替わらないほうが便利じゃないかと思ってしまうが、、なぜこうなっているのだろう。

test.js
  /**
   * キーコマンドのハンドル
   * @param command
   * @param editorState
   * @returns {*}
   */
  handleKeyCommand (command, editorState) {
    logging('handleKeyCommand')
    const newState = RichUtils.handleKeyCommand(editorState, command)
    if (command === 'my-editor-save') {
      // Perform a request to save your contents, set
      // a new `editorState`, etc.
      logging(command)
      return 'handled'
    }
    if (newState) {
      // this.onChange(newState)
      // this.onFocus(newState)
      return 'handled'
    }
    return 'not-handled'
  }

EditorState.createWithContentでデコレータを渡す

ドキュメントにも書いてあるけどメモ。

test.js
this.state = {
  editorState: EditorState.createWithContent(state, onlyHashTags),
}

ContentStateの生化と復元

エディタの状態から取得してconvertToRawし、Json文字列にする。
使用可能なContentStateオブジェクトに元の状態を戻す。

// the raw state, stringified
const rawDraftContentState = JSON.stringify(convertToRaw(editorState.getCurrentContent()))
// convert the raw state back to a useable ContentState object
const contentState = convertFromRaw(JSON.parse(rawDraftContentState))

rawDraftContentStateとは何を指すのか。。
convertFromRawの引数がそれだとすると、文字列化する前のjsonがそれかな。
そうすると、DB保存などで文字列化する必要がある場合を考えずに下記が説明としてはあってるのかどうか。。

// the raw state, stringified
const rawDraftContentState = convertToRaw(editorState.getCurrentContent())
// convert the raw state back to a useable ContentState object
const contentState = convertFromRaw(rawDraftContentState)

AtomicBlockでのEntityの使い回し

下記のようにAtomicBlockを作るときにentityKeyってどうするのか。

let editorState = AtomicBlockUtils.insertAtomicBlock(this.state.editorState, entityKey, 'MEDIA')

エンティティキーはどう受け渡されている追いかけてみた。

  • AtomicBlockUtils
const charData = CharacterMetadata.create({ entity: entityKey });
  • CharacterMetadata.create
sample.js
  /**
   * Use this function instead of the `CharacterMetadata` constructor.
   * Since most content generally uses only a very small number of
   * style/entity permutations, we can reuse these objects as often as
   * possible.
   */
  static create(config?: CharacterMetadataConfig): CharacterMetadata {
    if (!config) {
      return EMPTY;
    }

    const defaultConfig: CharacterMetadataConfig = { style: EMPTY_SET, entity: (null: ?string) };

    // Fill in unspecified properties, if necessary.
    var configMap = Map(defaultConfig).merge(config);

    var existing: ?CharacterMetadata = pool.get(configMap);
    if (existing) {
      return existing;
    }

    var newCharacter = new CharacterMetadata(configMap);
    pool = pool.set(configMap, newCharacter);
    return newCharacter;
  }

ここのコメントには下記のようなことが記載。

CharacterMetadataコンストラクタの代わりにこの関数を使います。
ほとんどのコンテンツは一般に非常に少数のスタイル/エンティティ順列しか使用しないため、
これらのオブジェクトを可能な限り頻繁に再利用することができます。

また、entityKeyはnullでも良さそう。

いろいろ悩んだけど、エンティティは使いまわしのきくメタ書式情報と書いてあったと思うので、
特別に毎回作成する必要がなければ使い回すべきかと思われる。
いらないならnullでも動いた。
ドキュメントにこの辺も詳しく書いてあると嬉しいけど。。

Reactのライフサイクルのおさらい

class

React.jsのメソッドが呼ばれる順番 - Qiita
https://qiita.com/kwst/items/b1f36d0a384eab1bc284

component

React component ライフサイクル図 - Qiita
https://qiita.com/kawachi/items/092bfc281f88e3a6e456

React Componentのライフサイクルのまとめと利用用途 - Qiita
https://qiita.com/yukika/items/1859743921a10d7e3e6b

はじめてReact.jsでアプリ書いてはまったこと[ES6含む] | KentaKomai Blog
http://komaken.me/blog/2015/11/17/%E3%81%AF%E3%81%98%E3%82%81%E3%81%A6react-js%E3%81%A7%E3%82%A2%E3%83%97%E3%83%AA%E6%9B%B8%E3%81%84%E3%81%A6%E3%81%AF%E3%81%BE%E3%81%A3%E3%81%9F%E3%81%93%E3%81%A8es6%E5%90%AB%E3%82%80/

実行の順番がわかりやすい。

React + Redux 入門 - うまとま君の技術めも
http://umatomakun.hatenablog.com/entry/2016/11/12/213351

使い分けがわかりやすい。

ES2016

static - JavaScript | MDN
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Classes/static

Isomorphic

アメブロ2016 ~ Isomorphic JavaScriptの詳しい話
https://developers.cyberagent.co.jp/blog/archives/3513/

Universal / Isomorphic JavaScript について - Qiita
https://qiita.com/kyrieleison/items/4ac5bcc331aee6394440

SPA で問題となるのがSEO対策、初回ロード時間
SSR はこれを解決してくれるが、クライアントとサーバでロジックの重複が発生
同じコードをクライアントとサーバで実行しよう = Isomophic!
同じコードをネイティブアプリやデスクトップアプリや組み込みデバイスでも実行しよう = Universal!
...
クライアントとサーバで同じ (HTML) ページを生成する技術のことをIsomorphic
クライアントとサーバなど異なる環境で同じコードが動くことをUniversal