utils.ts 792 B

123456789101112131415161718192021
  1. import { $isAtNodeEnd } from '@lexical/selection'
  2. import type { ElementNode, RangeSelection, TextNode } from 'lexical'
  3. export function getSelectedNode(
  4. selection: RangeSelection,
  5. ): TextNode | ElementNode {
  6. const anchor = selection.anchor
  7. const focus = selection.focus
  8. const anchorNode = selection.anchor.getNode()
  9. const focusNode = selection.focus.getNode()
  10. if (anchorNode === focusNode)
  11. return anchorNode
  12. const isBackward = selection.isBackward()
  13. if (isBackward)
  14. return $isAtNodeEnd(focus) ? anchorNode : focusNode
  15. else
  16. return $isAtNodeEnd(anchor) ? anchorNode : focusNode
  17. }
  18. export const urlRegExp = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[\w]*))?)/