Files
dify/web/app/components/workflow/note-node/note-editor/utils.ts
T

17 lines
765 B
TypeScript
Raw Normal View History

2024-06-14 17:08:11 +08:00
import type { ElementNode, RangeSelection, TextNode } from 'lexical'
2025-12-23 16:58:55 +08:00
import { $isAtNodeEnd } from '@lexical/selection'
2024-06-14 17:08:11 +08:00
2026-07-12 23:57:46 +08:00
export function getSelectedNode(selection: RangeSelection): TextNode | ElementNode {
2024-06-14 17:08:11 +08:00
const anchor = selection.anchor
const focus = selection.focus
const anchorNode = selection.anchor.getNode()
const focusNode = selection.focus.getNode()
2026-07-12 23:57:46 +08:00
if (anchorNode === focusNode) return anchorNode
2024-06-14 17:08:11 +08:00
const isBackward = selection.isBackward()
2026-07-12 23:57:46 +08:00
if (isBackward) return $isAtNodeEnd(focus) ? anchorNode : focusNode
else return $isAtNodeEnd(anchor) ? anchorNode : focusNode
2024-06-14 17:08:11 +08:00
}
2026-07-12 23:57:46 +08:00
export const urlRegExp =
/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-]*)?\??[-+=&;%@.\w]*#?\w*)?)/