moveSelection.js 1.4 KB

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