modifySelectionPerMouse.js 1.8 KB

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