draft-js常用操作

1. 操作自定义block

1.1 添加
const { AtomicBlockUtils } = Draft;

function add() {
	const { editorState } = this.state;
	const contentState = editorState.getCurrentContent();
	// 创建entity,并返回新的contentState
    const contentStateWithEntity = contentState.createEntity(type, 'MUTABLE' | 'IMMUTABLE', data: Object);
    // 获取刚创建的entity
	const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
	// 创建新的editorState
	const newEditorState = AtomicBlockUtils.insertAtomicBlock(editorState: EditState, entityKey, blockChars: string);

	this.setState({ editorState: newEditorState });
}
1.2 修改
function modify() {
	const { editorState } = this.state;
	const contentState = editorState.getCurrentContent();
	// 创建新entity,并返回新的contentState
    const contentStateWithEntity = contentState.createEntity(type, 'MUTABLE' | 'IMMUTABLE', newData: Object);
    // 获取刚创建的entity
	const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
	// 创建新的contentState
	const newContentState = Modifier.applyEntity(contentStateWithEntity, selection, entityKey);
	// 创建新的editorState
	const newEditorState = EditorState.push(editorState, newContentState, 'apply-entity');

	this.setState({ editorState: newEditorState });
}

2. 移动光标

2.1 移动到指定位置(好像对自定义block的光标移动无效)
const { editorState } = this.state;
// 获取当前光标位置
const oldSelectionState = editorState.getSelection();
// 重新指定光标位置
const newSelectionState = oldSelectionState.merge({ anchorOffset: 0, focusOffset: 2})) });
// 更新editorState
this.setState({ editorState: EditorState.forceSelection(editorState, newSelectionState) });
2.2 移动到文章末尾
const { editorState } = this.state;
/******* 方式一 start ********/
// 移动光标位置
const newSelectionState = EditorState.moveSelectionToEnd(editorState).getSelection();
// 更新editorState
this.setState({ editorState: EditorState.forceSelection(editorState, newSelectionState) });
/******* 方式一 end ********/

/******* 方式二 start ********/
this.setState({ editorState: EditorState.moveSelectionToEnd(editorState) });
/******* 方式二 end ********/

3. 插入文本

const { editorState } = this.state;
// 更新contentState
const newContentState = Modifier.insertText(
	// oldContentState: ContentState
	editorState.getCurrentContent(),
	// SelectionState;注意⚠️:插入文本要求selectionState必须闭合
	editorState.getSelection(),
	// insertText: string
	'this is insert text',
	// inlineStyle: DraftInlineStyle | null
	OrderedSet.of('BOLD', 'ITALIC')),
	// entityKey?: string;
);
// 更新eidtorState
this.setState({ editorState: EditorState.push(editorState, newContentState, 'insert-characters') })
3.1 创建行内样式
OrderedSet.of('BOLD')
// 或
OrderedSet.of('BOLD', 'ITALIC')),

4. entity

4.1 修改entity数据

const newEditorState = EditorState.push(
	editorState,
	editorState.getCurrentContent().mergeEntityData(entityKey, data)
	, 'change-block-data');
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值