moveSelection.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { getUISelection } from '../../document/UI.js';
  2. import '../../utils/dataTransfer/Clipboard.js';
  3. import { getNextCursorPosition } from '../../utils/focus/cursor.js';
  4. import { hasOwnSelection } from '../../utils/focus/selection.js';
  5. import { setSelection } from './setSelection.js';
  6. /**
  7. * Move the selection
  8. */ function moveSelection(node, direction) {
  9. // TODO: implement shift
  10. if (hasOwnSelection(node)) {
  11. const selection = getUISelection(node);
  12. setSelection({
  13. focusNode: node,
  14. focusOffset: selection.startOffset === selection.endOffset ? selection.focusOffset + direction : direction < 0 ? selection.startOffset : selection.endOffset
  15. });
  16. } else {
  17. const selection = node.ownerDocument.getSelection();
  18. if (!(selection === null || selection === undefined ? undefined : selection.focusNode)) {
  19. return;
  20. }
  21. if (selection.isCollapsed) {
  22. const nextPosition = getNextCursorPosition(selection.focusNode, selection.focusOffset, direction);
  23. if (nextPosition) {
  24. setSelection({
  25. focusNode: nextPosition.node,
  26. focusOffset: nextPosition.offset
  27. });
  28. }
  29. } else {
  30. selection[direction < 0 ? 'collapseToStart' : 'collapseToEnd']();
  31. }
  32. }
  33. }
  34. export { moveSelection };