selectAll.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { getUIValue, getUISelection } from '../../document/UI.js';
  2. import '../../utils/dataTransfer/Clipboard.js';
  3. import { getContentEditable } from '../../utils/edit/isContentEditable.js';
  4. import { hasOwnSelection } from '../../utils/focus/selection.js';
  5. import { setSelection } from './setSelection.js';
  6. /**
  7. * Expand a selection like the browser does when pressing Ctrl+A.
  8. */ function selectAll(target) {
  9. if (hasOwnSelection(target)) {
  10. return setSelection({
  11. focusNode: target,
  12. anchorOffset: 0,
  13. focusOffset: getUIValue(target).length
  14. });
  15. }
  16. var _getContentEditable;
  17. const focusNode = (_getContentEditable = getContentEditable(target)) !== null && _getContentEditable !== undefined ? _getContentEditable : target.ownerDocument.body;
  18. setSelection({
  19. focusNode,
  20. anchorOffset: 0,
  21. focusOffset: focusNode.childNodes.length
  22. });
  23. }
  24. function isAllSelected(target) {
  25. if (hasOwnSelection(target)) {
  26. return getUISelection(target).startOffset === 0 && getUISelection(target).endOffset === getUIValue(target).length;
  27. }
  28. var _getContentEditable;
  29. const focusNode = (_getContentEditable = getContentEditable(target)) !== null && _getContentEditable !== undefined ? _getContentEditable : target.ownerDocument.body;
  30. const selection = target.ownerDocument.getSelection();
  31. return (selection === null || selection === undefined ? undefined : selection.anchorNode) === focusNode && selection.focusNode === focusNode && selection.anchorOffset === 0 && selection.focusOffset === focusNode.childNodes.length;
  32. }
  33. export { isAllSelected, selectAll };