|
|
|
|
|
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef } from 'react'; |
|
|
import { MdxIndex, TargetInfo } from '../types'; |
|
|
import { EntriesViewRender } from './EntriesView.ui'; |
|
|
import { |
|
|
setMatchCursor, |
|
|
getMatchCursorIndex, |
|
|
getMatchCount, |
|
|
clearMatchCursor, |
|
|
} from '../utils/iframeTools/highlight'; |
|
|
|
|
|
export interface EntriesViewRef { |
|
|
scrollToEntry: (entryNo: number, fragment?: string) => void; |
|
|
setHighlight: (highlight: string) => Promise<void>; |
|
|
hasNextMatch: () => boolean; |
|
|
hasPrevMatch: () => boolean; |
|
|
scrollToNextMatch: () => void; |
|
|
scrollToPrevMatch: () => void; |
|
|
} |
|
|
|
|
|
export interface EntriesViewProps { |
|
|
indexes: MdxIndex[]; |
|
|
targetInfo?: TargetInfo; |
|
|
highlight?: string; |
|
|
} |
|
|
|
|
|
export const EntriesView = forwardRef<EntriesViewRef, EntriesViewProps>(({ |
|
|
indexes, |
|
|
targetInfo, |
|
|
highlight, |
|
|
}, ref) => { |
|
|
|
|
|
const contentItemRefs = useRef<Map<string, React.RefObject<any>>>(new Map()); |
|
|
|
|
|
const makeKey = useCallback((entryNo: number) => { |
|
|
const idx = indexes.find(i => i.entry_no === entryNo); |
|
|
return idx ? `${idx.profile_id}_${entryNo}` : undefined; |
|
|
}, [indexes]); |
|
|
|
|
|
const getItemRef = useCallback((key: string) => { |
|
|
if (!contentItemRefs.current.has(key)) { |
|
|
contentItemRefs.current.set(key, React.createRef()); |
|
|
} |
|
|
return contentItemRefs.current.get(key)!; |
|
|
}, []); |
|
|
|
|
|
const scrollToEntry = useCallback((entryNo: number, fragment?: string) => { |
|
|
const key = makeKey(entryNo); |
|
|
if (!key) return; |
|
|
const itemRef = contentItemRefs.current.get(key); |
|
|
|
|
|
if (itemRef?.current) { |
|
|
const element = document.getElementById(`content-item-${key}`); |
|
|
if (element) { |
|
|
element.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
|
|
} |
|
|
|
|
|
if (fragment && itemRef.current.scrollToAnchor) { |
|
|
setTimeout(() => { |
|
|
itemRef.current.scrollToAnchor(fragment); |
|
|
}, 300); |
|
|
} |
|
|
} |
|
|
}, [makeKey]); |
|
|
|
|
|
|
|
|
const getAllIframes = useCallback((): { iframe: HTMLIFrameElement, itemRef: any }[] => { |
|
|
const result: { iframe: HTMLIFrameElement, itemRef: any }[] = []; |
|
|
contentItemRefs.current.forEach((itemRef) => { |
|
|
if (itemRef.current?.getIframe) { |
|
|
const iframe = itemRef.current.getIframe(); |
|
|
if (iframe) { |
|
|
result.push({ iframe, itemRef: itemRef.current }); |
|
|
} |
|
|
} |
|
|
}); |
|
|
return result; |
|
|
}, []); |
|
|
|
|
|
const setHighlight = useCallback(async (next: string) => { |
|
|
|
|
|
console.debug("EntriesView setHighlight", next); |
|
|
const promises: Promise<number>[] = []; |
|
|
contentItemRefs.current.forEach(itemRef => { |
|
|
if (itemRef.current?.setHighlight) { |
|
|
promises.push(itemRef.current.setHighlight(next)); |
|
|
} |
|
|
}); |
|
|
|
|
|
await Promise.all(promises); |
|
|
|
|
|
|
|
|
const firstMatchIframe = findInitialMatchFrame(getAllIframes(), 'next'); |
|
|
if (firstMatchIframe) { |
|
|
setMatchCursor(firstMatchIframe.iframe, 0, true); |
|
|
} |
|
|
}, [getAllIframes]); |
|
|
|
|
|
|
|
|
const findActiveMatchIndex = useCallback((iframes: { iframe: HTMLIFrameElement, itemRef: any }[]): number => { |
|
|
return iframes.findIndex(item => getMatchCursorIndex(item.iframe) >= 0); |
|
|
}, []); |
|
|
|
|
|
|
|
|
const hasMatchInDirection = useCallback((direction: 'next' | 'prev'): boolean => { |
|
|
const iframes = getAllIframes(); |
|
|
const activeIndex = findActiveMatchIndex(iframes); |
|
|
|
|
|
|
|
|
if (activeIndex === -1) { |
|
|
return iframes.some(item => getMatchCount(item.iframe) > 0); |
|
|
} |
|
|
|
|
|
const currentMatchIndex = getMatchCursorIndex(iframes[activeIndex].iframe); |
|
|
const currentMatchCount = getMatchCount(iframes[activeIndex].iframe); |
|
|
|
|
|
|
|
|
if (direction === 'next') { |
|
|
|
|
|
if (currentMatchIndex < currentMatchCount - 1) { |
|
|
return true; |
|
|
} |
|
|
|
|
|
return iframes.slice(activeIndex + 1).some(item => getMatchCount(item.iframe) > 0); |
|
|
} else { |
|
|
|
|
|
if (currentMatchIndex > 0) { |
|
|
return true; |
|
|
} |
|
|
|
|
|
return iframes.slice(0, activeIndex).some(item => getMatchCount(item.iframe) > 0); |
|
|
} |
|
|
}, [getAllIframes, findActiveMatchIndex]); |
|
|
|
|
|
|
|
|
const hasNextMatch = useCallback((): boolean => { |
|
|
return hasMatchInDirection('next'); |
|
|
}, [hasMatchInDirection]); |
|
|
|
|
|
|
|
|
const hasPrevMatch = useCallback((): boolean => { |
|
|
return hasMatchInDirection('prev'); |
|
|
}, [hasMatchInDirection]); |
|
|
|
|
|
|
|
|
const findInitialMatchFrame = useCallback((iframes: { iframe: HTMLIFrameElement, itemRef: any }[], direction: 'next' | 'prev') => { |
|
|
if (direction === 'next') { |
|
|
return iframes.find(item => getMatchCount(item.iframe) > 0); |
|
|
} else { |
|
|
|
|
|
for (let i = iframes.length - 1; i >= 0; i--) { |
|
|
if (getMatchCount(iframes[i].iframe) > 0) { |
|
|
return iframes[i]; |
|
|
} |
|
|
} |
|
|
} |
|
|
return null; |
|
|
}, []); |
|
|
|
|
|
|
|
|
const findAdjacentMatchFrame = useCallback((iframes: { iframe: HTMLIFrameElement, itemRef: any }[], activeIndex: number, direction: 'next' | 'prev') => { |
|
|
const searchFrames = direction === 'next' |
|
|
? iframes.slice(activeIndex + 1) |
|
|
: iframes.slice(0, activeIndex).reverse(); |
|
|
|
|
|
return searchFrames.find(item => getMatchCount(item.iframe) > 0) || null; |
|
|
}, []); |
|
|
|
|
|
|
|
|
const scrollToMatchInDirection = useCallback((direction: 'next' | 'prev') => { |
|
|
const iframes = getAllIframes(); |
|
|
const activeIndex = findActiveMatchIndex(iframes); |
|
|
|
|
|
|
|
|
if (activeIndex === -1) { |
|
|
const initialFrame = findInitialMatchFrame(iframes, direction); |
|
|
if (initialFrame) { |
|
|
const matchCount = getMatchCount(initialFrame.iframe); |
|
|
const targetMatchIndex = direction === 'next' ? 0 : matchCount - 1; |
|
|
setMatchCursor(initialFrame.iframe, targetMatchIndex, true); |
|
|
} |
|
|
return; |
|
|
} |
|
|
|
|
|
const currentMatchIndex = getMatchCursorIndex(iframes[activeIndex].iframe); |
|
|
const currentMatchCount = getMatchCount(iframes[activeIndex].iframe); |
|
|
|
|
|
|
|
|
const canMoveWithinCurrent = direction === 'next' |
|
|
? currentMatchIndex < currentMatchCount - 1 |
|
|
: currentMatchIndex > 0; |
|
|
|
|
|
if (canMoveWithinCurrent) { |
|
|
const nextIndex = direction === 'next' ? currentMatchIndex + 1 : currentMatchIndex - 1; |
|
|
setMatchCursor(iframes[activeIndex].iframe, nextIndex, true); |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
const adjacentFrame = findAdjacentMatchFrame(iframes, activeIndex, direction); |
|
|
if (adjacentFrame) { |
|
|
clearMatchCursor(iframes[activeIndex].iframe); |
|
|
const matchCount = getMatchCount(adjacentFrame.iframe); |
|
|
const targetMatchIndex = direction === 'next' ? 0 : matchCount - 1; |
|
|
setMatchCursor(adjacentFrame.iframe, targetMatchIndex, true); |
|
|
} |
|
|
}, [getAllIframes, findActiveMatchIndex, findInitialMatchFrame, findAdjacentMatchFrame]); |
|
|
|
|
|
|
|
|
const scrollToNextMatch = useCallback(() => { |
|
|
scrollToMatchInDirection('next'); |
|
|
}, [scrollToMatchInDirection]); |
|
|
|
|
|
|
|
|
const scrollToPrevMatch = useCallback(() => { |
|
|
scrollToMatchInDirection('prev'); |
|
|
}, [scrollToMatchInDirection]); |
|
|
|
|
|
useEffect(() => { |
|
|
if (highlight) { |
|
|
console.debug("EntriesView useEffect highlight", highlight); |
|
|
setHighlight(highlight); |
|
|
} |
|
|
}, [highlight, setHighlight]); |
|
|
|
|
|
useEffect(() => { |
|
|
if (targetInfo !== undefined) { |
|
|
scrollToEntry(targetInfo.entry_no, targetInfo.fragment); |
|
|
} |
|
|
}, [targetInfo, scrollToEntry]); |
|
|
|
|
|
useImperativeHandle(ref, () => ({ |
|
|
scrollToEntry, |
|
|
setHighlight, |
|
|
hasNextMatch, |
|
|
hasPrevMatch, |
|
|
scrollToNextMatch, |
|
|
scrollToPrevMatch, |
|
|
}), [scrollToEntry, setHighlight, hasNextMatch, hasPrevMatch, scrollToNextMatch, scrollToPrevMatch]); |
|
|
|
|
|
return ( |
|
|
<EntriesViewRender |
|
|
indexes={indexes} |
|
|
highlight={highlight} |
|
|
getItemRef={getItemRef} |
|
|
/> |
|
|
); |
|
|
}); |
|
|
EntriesView.displayName = 'EntriesView'; |
|
|
|
|
|
export default EntriesView; |