Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

keyCommandBackspaceToStartOfLine.js.flow 1.6 KiB

3 lat temu
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow strict-local
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. const EditorState = require("./EditorState");
  13. const expandRangeToStartOfLine = require("./expandRangeToStartOfLine");
  14. const getDraftEditorSelectionWithNodes = require("./getDraftEditorSelectionWithNodes");
  15. const moveSelectionBackward = require("./moveSelectionBackward");
  16. const removeTextWithStrategy = require("./removeTextWithStrategy");
  17. function keyCommandBackspaceToStartOfLine(editorState: EditorState, e: SyntheticKeyboardEvent<HTMLElement>): EditorState {
  18. const afterRemoval = removeTextWithStrategy(editorState, strategyState => {
  19. const selection = strategyState.getSelection();
  20. if (selection.isCollapsed() && selection.getAnchorOffset() === 0) {
  21. return moveSelectionBackward(strategyState, 1);
  22. }
  23. const {
  24. ownerDocument
  25. } = e.currentTarget;
  26. const domSelection = ownerDocument.defaultView.getSelection();
  27. let range = domSelection.getRangeAt(0);
  28. range = expandRangeToStartOfLine(range);
  29. return getDraftEditorSelectionWithNodes(strategyState, null, range.endContainer, range.endOffset, range.startContainer, range.startOffset).selectionState;
  30. }, 'backward');
  31. if (afterRemoval === editorState.getCurrentContent()) {
  32. return editorState;
  33. }
  34. return EditorState.push(editorState, afterRemoval, 'remove-range');
  35. }
  36. module.exports = keyCommandBackspaceToStartOfLine;