45 lines
1.7 KiB
Plaintext
45 lines
1.7 KiB
Plaintext
/**
|
|
* 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 keyCommandPlainBackspace
|
|
* @format
|
|
* @flow
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var EditorState = require('./EditorState');
|
|
var UnicodeUtils = require('fbjs/lib/UnicodeUtils');
|
|
|
|
var moveSelectionBackward = require('./moveSelectionBackward');
|
|
var removeTextWithStrategy = require('./removeTextWithStrategy');
|
|
|
|
/**
|
|
* Remove the selected range. If the cursor is collapsed, remove the preceding
|
|
* character. This operation is Unicode-aware, so removing a single character
|
|
* will remove a surrogate pair properly as well.
|
|
*/
|
|
function keyCommandPlainBackspace(editorState: EditorState): EditorState {
|
|
var afterRemoval = removeTextWithStrategy(editorState, strategyState => {
|
|
var selection = strategyState.getSelection();
|
|
var content = strategyState.getCurrentContent();
|
|
var key = selection.getAnchorKey();
|
|
var offset = selection.getAnchorOffset();
|
|
var charBehind = content.getBlockForKey(key).getText()[offset - 1];
|
|
return moveSelectionBackward(strategyState, charBehind ? UnicodeUtils.getUTF16Length(charBehind, 0) : 1);
|
|
}, 'backward');
|
|
|
|
if (afterRemoval === editorState.getCurrentContent()) {
|
|
return editorState;
|
|
}
|
|
|
|
var selection = editorState.getSelection();
|
|
return EditorState.push(editorState, afterRemoval.set('selectionBefore', selection), selection.isCollapsed() ? 'backspace-character' : 'remove-range');
|
|
}
|
|
|
|
module.exports = keyCommandPlainBackspace; |