modifySelectionPerMouse.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var UI = require('../../document/UI.js');
  3. require('../../utils/dataTransfer/Clipboard.js');
  4. var resolveCaretPosition = require('./resolveCaretPosition.js');
  5. function modifySelectionPerMouseMove(selectionRange, { document, target, node, offset }) {
  6. const selectionFocus = resolveCaretPosition.resolveCaretPosition({
  7. target,
  8. node,
  9. offset
  10. });
  11. if ('node' in selectionRange) {
  12. // When the mouse is dragged outside of an input/textarea,
  13. // the selection is extended to the beginning or end of the input
  14. // depending on pointer position.
  15. // TODO: extend selection according to pointer position
  16. /* istanbul ignore else */ if (selectionFocus.node === selectionRange.node) {
  17. const anchorOffset = selectionFocus.offset < selectionRange.start ? selectionRange.end : selectionRange.start;
  18. const focusOffset = selectionFocus.offset > selectionRange.end || selectionFocus.offset < selectionRange.start ? selectionFocus.offset : selectionRange.end;
  19. UI.setUISelection(selectionRange.node, {
  20. anchorOffset,
  21. focusOffset
  22. });
  23. }
  24. } else {
  25. const range = selectionRange.cloneRange();
  26. const cmp = range.comparePoint(selectionFocus.node, selectionFocus.offset);
  27. if (cmp < 0) {
  28. range.setStart(selectionFocus.node, selectionFocus.offset);
  29. } else if (cmp > 0) {
  30. range.setEnd(selectionFocus.node, selectionFocus.offset);
  31. }
  32. const selection = document.getSelection();
  33. selection === null || selection === undefined ? undefined : selection.removeAllRanges();
  34. selection === null || selection === undefined ? undefined : selection.addRange(range.cloneRange());
  35. }
  36. }
  37. exports.modifySelectionPerMouseMove = modifySelectionPerMouseMove;