Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SecondaryClipboard.js.flow 2.3 KiB

3 лет назад
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. import type { BlockMap } from "./BlockMap";
  13. import type SelectionState from "./SelectionState";
  14. const DraftModifier = require("./DraftModifier");
  15. const EditorState = require("./EditorState");
  16. const getContentStateFragment = require("./getContentStateFragment");
  17. const nullthrows = require("fbjs/lib/nullthrows");
  18. let clipboard: ?BlockMap = null;
  19. /**
  20. * Some systems offer a "secondary" clipboard to allow quick internal cut
  21. * and paste behavior. For instance, Ctrl+K (cut) and Ctrl+Y (paste).
  22. */
  23. const SecondaryClipboard = {
  24. cut: function (editorState: EditorState): EditorState {
  25. const content = editorState.getCurrentContent();
  26. const selection = editorState.getSelection();
  27. let targetRange: ?SelectionState = null;
  28. if (selection.isCollapsed()) {
  29. const anchorKey = selection.getAnchorKey();
  30. const blockEnd = content.getBlockForKey(anchorKey).getLength();
  31. if (blockEnd === selection.getAnchorOffset()) {
  32. const keyAfter = content.getKeyAfter(anchorKey);
  33. if (keyAfter == null) {
  34. return editorState;
  35. }
  36. targetRange = selection.set('focusKey', keyAfter).set('focusOffset', 0);
  37. } else {
  38. targetRange = selection.set('focusOffset', blockEnd);
  39. }
  40. } else {
  41. targetRange = selection;
  42. }
  43. targetRange = nullthrows(targetRange); // TODO: This should actually append to the current state when doing
  44. // successive ^K commands without any other cursor movement
  45. clipboard = getContentStateFragment(content, targetRange);
  46. const afterRemoval = DraftModifier.removeRange(content, targetRange, 'forward');
  47. if (afterRemoval === content) {
  48. return editorState;
  49. }
  50. return EditorState.push(editorState, afterRemoval, 'remove-range');
  51. },
  52. paste: function (editorState: EditorState): EditorState {
  53. if (!clipboard) {
  54. return editorState;
  55. }
  56. const newContent = DraftModifier.replaceWithFragment(editorState.getCurrentContent(), editorState.getSelection(), clipboard);
  57. return EditorState.push(editorState, newContent, 'insert-fragment');
  58. }
  59. };
  60. module.exports = SecondaryClipboard;