Created
June 15, 2017 19:20
-
-
Save danneu/89cbcf64bcb62995b91cb80acf8bf79b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SecondaryClipboard = { | |
cut: function(editorState: EditorState): EditorState { | |
var content = editorState.getCurrentContent(); | |
var selection = editorState.getSelection(); | |
var targetRange: ?SelectionState = null; | |
if (selection.isCollapsed()) { | |
var anchorKey = selection.getAnchorKey(); | |
var block = content.getBlockForKey(anchorKey); | |
var blockEnd = block.getLength(); | |
// Delete the block if it is empty | |
if (blockEnd === 0) { | |
return // TODO | |
} | |
// Delete from cursor to next newline or block end | |
var restText = block.getText().slice(selection.getStartOffset()) | |
var nextNewlineIdx = restText.indexOf('\n') | |
var cutUntilOffset | |
if (nextNewlineIdx === 0) { | |
// If next char is the newline, delete it and stop | |
cutUntilOffset = selection.getStartOffset() + 1 | |
} else if (nextNewlineIdx > -1) { | |
// If rest of string has a newline, delete until it | |
cutUntilOffset = nextNewlineIdx + selection.getStartOffset() | |
} else { | |
cutUntilOffset = blockEnd | |
} | |
targetRange = selection.set('focusOffset', cutUntilOffset); | |
} else { | |
targetRange = selection; | |
} | |
targetRange = nullthrows(targetRange); | |
clipboard = getContentStateFragment(content, targetRange); | |
var afterRemoval = DraftModifier.removeRange( | |
content, | |
targetRange, | |
'forward', | |
); | |
if (afterRemoval === content) { | |
return editorState; | |
} | |
return EditorState.push(editorState, afterRemoval, 'remove-range'); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment