Skip to content

Instantly share code, notes, and snippets.

@danneu
Created June 15, 2017 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danneu/89cbcf64bcb62995b91cb80acf8bf79b to your computer and use it in GitHub Desktop.
Save danneu/89cbcf64bcb62995b91cb80acf8bf79b to your computer and use it in GitHub Desktop.
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