import { INoteTextHighlightInfo } from './Note/type' ; import { getCustomValue } from './customAttrValue' ; const getNode = ( wrap , path ) : Node | undefined => { if ( ! wrap || ! path . length ) return ; let node = wrap ; let index = 0 ; while ( node && index < path . length ) { const { childNodes } = node ; node = childNodes [ path [ index ] ] ; index ++ ; } return node ; } ; const getRealNodeAndOffset = ( node , offset ) => { const { attrName } = getCustomValue ( ) ; const wrap = node ; if ( node instanceof Element ) { if ( node . getAttribute ( attrName ) ) { let textOffset = 0 ; let index = 0 ; node = wrap . childNodes [ index ] ; outer : while ( node && textOffset < offset ) { if ( node . nodeType === 3 ) { textOffset += node ?. textContent ?. length || 0 ; if ( textOffset >= offset ) break ; } if ( node . childNodes . length ) { node = node . childNodes [ 0 ] ; } else if ( node . nextSibling ) { node = node . nextSibling ; } else { do { node = node ?. parentNode as Element ; if ( wrap === node ) { break outer ; } } while ( ! node . nextSibling ) ; node = node . nextSibling as Element ; } } return { node , offset : ( node . textContent ?. length || 0 ) - ( textOffset - offset ) , } ; } throw new Error ( '元素类型错误' ) ; } return { node , offset } ; } ; const selectRange = ( data ) => { const wrap = document . querySelector ( '#text-highlight' ) ; const getSelection = window . getSelection ( ) ; if ( ! document ?. createRange || ! wrap || ! getSelection ) return ; const range = new Range ( ) ; const startNode = getNode ( wrap , data . list [ 0 ] . level ) ; const startOffset = data . list [ 0 ] . start ; const endNode = getNode ( wrap , data . list [ data . list . length - 1 ] . level ) ; const endOffset = data . list [ data . list . length - 1 ] . end ; if ( ! startNode || ! endNode ) return ; const start = getRealNodeAndOffset ( startNode , startOffset ) ; const end = getRealNodeAndOffset ( endNode , endOffset ) ; range . setStart ( start . node , start . offset ) ; range . setEnd ( end . node , end . offset ) ; if ( getSelection . rangeCount > 0 ) getSelection . removeAllRanges ( ) ; getSelection . addRange ( range ) ; } ; export default selectRange ;