@prisma-cms/editor
Version:
Editor for @prisma-cms
40 lines (33 loc) • 1.44 kB
JavaScript
/**
* Copyright (c) 2013-present, Facebook, Inc. All rights reserved.
*
* This file provided by Facebook is for non-commercial testing and evaluation
* purposes only. Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { EditorState, Modifier, SelectionState } from 'draft-js-android-fix'
export function removeTextBlock(editorState, blockKey) {
const content = editorState.getCurrentContent()
const block = content.getBlockForKey(blockKey)
const targetRange = new SelectionState({
anchorKey: blockKey,
anchorOffset: 0,
focusKey: blockKey,
focusOffset: block.getLength(),
})
const withoutTeX = Modifier.removeRange(content, targetRange, 'backward')
const resetBlock = Modifier.setBlockType(
withoutTeX,
withoutTeX.getSelectionAfter(),
'unstyled'
)
const newState = EditorState.push(editorState, resetBlock, 'remove-range')
return EditorState.forceSelection(newState, resetBlock.getSelectionAfter())
}