/** * Set the supplied SelectionState as the new current selection, and set * the `force` flag to trigger manual selection placement by the view. */ function updateSelection( editorState: EditorState, selection: SelectionState, forceSelection: boolean ): EditorState { var overrideStyle = null; var currentSelection = editorState.getSelection(); if(selection.isCollapsed() && currentSelection.isCollapsed() && selection.getStartKey() == currentSelection.getStartKey() && selection.getStartOffset() == currentSelection.getStartOffset() ){ overrideStyle = editorState.getInlineStyleOverride(); } return EditorState.set(editorState, { selection, forceSelection, nativelyRenderedContent: null, inlineStyleOverride: overrideStyle, }); }
function getInlineStyleForNonCollapsedSelection( content: ContentState, selection: SelectionState ): DraftInlineStyle { var startKey = selection.getStartKey(); var startOffset = selection.getStartOffset(); var startBlock = content.getBlockForKey(startKey); // If there is a character just inside the selection, use its style. if (startOffset < startBlock.getLength()) { return startBlock.getInlineStyleAt(startOffset); } // Check if the selection at the end of a non-empty block. Use the last // style in the block. if (startOffset > 0) { return startBlock.getInlineStyleAt(startOffset - 1); } // Otherwise, look upward in the document to find the closest character. return lookUpwardForInlineStyle(content, startKey); }
function getInlineStyleForCollapsedSelection( content: ContentState, selection: SelectionState, ): DraftInlineStyle { const startKey = selection.getStartKey(); const startOffset = selection.getStartOffset(); const startBlock = content.getBlockForKey(startKey); // If the cursor is not at the start of the block, look backward to // preserve the style of the preceding character. if (startOffset > 0) { return startBlock.getInlineStyleAt(startOffset - 1); } // The caret is at position zero in this block. If the block has any // text at all, use the style of the first character. if (startBlock.getLength()) { return startBlock.getInlineStyleAt(0); } // Otherwise, look upward in the document to find the closest character. return lookUpwardForInlineStyle(content, startKey); }
moveAtomicBlock: function( editorState: EditorState, atomicBlock: ContentBlock, targetRange: SelectionState, insertionMode?: DraftInsertionType, ): EditorState { const contentState = editorState.getCurrentContent(); const selectionState = editorState.getSelection(); let withMovedAtomicBlock; if (insertionMode === 'before' || insertionMode === 'after') { const targetBlock = contentState.getBlockForKey( insertionMode === 'before' ? targetRange.getStartKey() : targetRange.getEndKey(), ); withMovedAtomicBlock = moveBlockInContentState( contentState, atomicBlock, targetBlock, insertionMode, ); } else { const afterRemoval = DraftModifier.removeRange( contentState, targetRange, 'backward', ); const selectionAfterRemoval = afterRemoval.getSelectionAfter(); const targetBlock = afterRemoval.getBlockForKey( selectionAfterRemoval.getFocusKey(), ); if (selectionAfterRemoval.getStartOffset() === 0) { withMovedAtomicBlock = moveBlockInContentState( afterRemoval, atomicBlock, targetBlock, 'before', ); } else if (selectionAfterRemoval.getEndOffset() === targetBlock.getLength()) { withMovedAtomicBlock = moveBlockInContentState( afterRemoval, atomicBlock, targetBlock, 'after', ); } else { const afterSplit = DraftModifier.splitBlock( afterRemoval, selectionAfterRemoval, ); const selectionAfterSplit = afterSplit.getSelectionAfter(); const targetBlock = afterSplit.getBlockForKey( selectionAfterSplit.getFocusKey(), ); withMovedAtomicBlock = moveBlockInContentState( afterSplit, atomicBlock, targetBlock, 'before', ); } } const newContent = withMovedAtomicBlock.merge({ selectionBefore: selectionState, selectionAfter: withMovedAtomicBlock.getSelectionAfter().set('hasFocus', true), }); return EditorState.push(editorState, newContent, 'move-block'); },