Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

73 linhas
2.1 KiB

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