当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Modifier.splitBlock方法代码示例

本文整理汇总了TypeScript中draft-js.Modifier.splitBlock方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Modifier.splitBlock方法的具体用法?TypeScript Modifier.splitBlock怎么用?TypeScript Modifier.splitBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在draft-js.Modifier的用法示例。


在下文中一共展示了Modifier.splitBlock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: insertContentInState

function insertContentInState(
    editorState: EditorState,
    pastedContent: ContentState,
    onChange: (e: EditorState) => void,
    editorFormat: Array<string>): DraftHandleValue {
    let _pastedContent = pastedContent;
    const blockMap = _pastedContent.getBlockMap();
    const hasAtomicBlocks = blockMap.some((block) => block.getType() === 'atomic');
    const acceptedInlineStyles =
        Object.keys(inlineStyles)
            .filter((style) => editorFormat.includes(style))
            .map((style) => inlineStyles[style]);

    let contentState = editorState.getCurrentContent();
    let selection = editorState.getSelection();
    let blocks = [];

    if (hasAtomicBlocks) {
        contentState = Modifier.splitBlock(editorState.getCurrentContent(), editorState.getSelection());
        selection = contentState.getSelectionAfter();
    }

    _pastedContent = sanitizeContent(EditorState.createWithContent(_pastedContent), acceptedInlineStyles)
        .getCurrentContent();

    blockMap.forEach((block) => {
        if (!hasAtomicBlocks || block.getType() !== 'atomic') {
            return blocks.push(block);
        }

        const entityKey = block.getEntityAt(0);
        const entity = _pastedContent.getEntity(entityKey);

        contentState = contentState.addEntity(entity);

        blocks = blocks.concat(
            atomicBlock(block.getData(), contentState.getLastCreatedEntityKey()),
        );
    });

    if (hasAtomicBlocks) {
        contentState = Modifier.setBlockType(contentState, selection, 'atomic');

        blocks = blocks.concat(emptyBlock()); // add empty block to ensure writting afterwards
    }

    const newBlockMap = OrderedMap<string, ContentBlock>(blocks.map((b) => ([b.getKey(), b])));

    let nextEditorState = EditorState.push(
        editorState,
        Modifier.replaceWithFragment(contentState, selection, newBlockMap),
        'insert-fragment',
    );

    const selectionAfterInsert = nextEditorState.getSelection();
    const customData = getAllCustomDataFromEditor(editorState);

    // for the first block recover the initial block data because on replaceWithFragment the block data is
    // replaced with the data from pasted fragment
    nextEditorState = setAllCustomDataForEditor(nextEditorState, customData);

    // reset undo stack
    nextEditorState = EditorState.push(
        editorState,
        nextEditorState.getCurrentContent(),
        'insert-fragment',
    );

    nextEditorState = EditorState.forceSelection(nextEditorState, selectionAfterInsert);

    onChange(nextEditorState);

    return 'handled';
}
开发者ID:jerome-poisson,项目名称:superdesk-client-core,代码行数:74,代码来源:handlePastedText.ts


注:本文中的draft-js.Modifier.splitBlock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。