0
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?

Draft.jsでカーソル位置にEditorStateを挿入する

Last updated at Posted at 2023-12-01

背景

Draft.jsで、既に入力済みであるEditorStateのカーソル位置に、別のEditorStateを挿入したい。
それを以下のコードで解決。

import { EditorState, Modifier } from "draft-js";


// 挿入するテンプレートの内容を取得
const insertContent = insertEditorState.getCurrentContent();

// 現在のEditorStateを取得
const editorState = currentEditorState;
// 現在のEditorStateのcontentを取得
const currentContent = editorState.getCurrentContent();
// カーソル位置を取得
const selectionState = editorState.getSelection();

// カーソル位置にテンプレートの内容を挿入
const newContent = Modifier.replaceWithFragment(
  currentContent,
  selectionState,
  insertContent.getBlockMap()
);

// エディターの内容を再生成
const newEditorState = EditorState.push(
  editorState,
  newContent,
  "insert-characters"
);
0
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
0
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?