d2-ui
Version:
69 lines (53 loc) • 1.98 kB
JavaScript
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule SecondaryClipboard
*
*/
;
var DraftModifier = require('./DraftModifier');
var EditorState = require('./EditorState');
var getContentStateFragment = require('./getContentStateFragment');
var nullthrows = require('fbjs/lib/nullthrows');
var clipboard = null;
/**
* Some systems offer a "secondary" clipboard to allow quick internal cut
* and paste behavior. For instance, Ctrl+K (cut) and Ctrl+Y (paste).
*/
var SecondaryClipboard = {
cut: function cut(editorState) {
var content = editorState.getCurrentContent();
var selection = editorState.getSelection();
var targetRange = null;
if (selection.isCollapsed()) {
var anchorKey = selection.getAnchorKey();
var blockEnd = content.getBlockForKey(anchorKey).getLength();
if (blockEnd === selection.getAnchorOffset()) {
return editorState;
}
targetRange = selection.set('focusOffset', blockEnd);
} 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');
},
paste: function paste(editorState) {
if (!clipboard) {
return editorState;
}
var newContent = DraftModifier.replaceWithFragment(editorState.getCurrentContent(), editorState.getSelection(), clipboard);
return EditorState.push(editorState, newContent, 'insert-fragment');
}
};
module.exports = SecondaryClipboard;