|
| 1 | +import React, { PointerEvent, ReactNode, useCallback, useEffect, useState } from 'react'; |
| 2 | +import { |
| 3 | + autoUpdate, |
| 4 | + flip, |
| 5 | + FloatingFocusManager, |
| 6 | + FloatingPortal, |
| 7 | + inline, |
| 8 | + offset, |
| 9 | + shift, |
| 10 | + useDismiss, |
| 11 | + useFloating, |
| 12 | + useInteractions, |
| 13 | + useRole |
| 14 | +} from '@floating-ui/react'; |
| 15 | + |
| 16 | +import { useAnnotator, useSelection } from '@annotorious/react'; |
| 17 | +import type { TextAnnotation, TextAnnotator } from '@recogito/text-annotator'; |
| 18 | + |
| 19 | +import './TextAnnotatorPopup.css'; |
| 20 | + |
| 21 | +interface TextAnnotationPopupProps { |
| 22 | + |
| 23 | + popup(props: TextAnnotationPopupContentProps): ReactNode; |
| 24 | + |
| 25 | +} |
| 26 | + |
| 27 | +export interface TextAnnotationPopupContentProps { |
| 28 | + |
| 29 | + annotation: TextAnnotation; |
| 30 | + |
| 31 | + editable?: boolean; |
| 32 | + |
| 33 | + event?: PointerEvent; |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => { |
| 38 | + |
| 39 | + const r = useAnnotator<TextAnnotator>(); |
| 40 | + |
| 41 | + const { selected, event } = useSelection<TextAnnotation>(); |
| 42 | + const annotation = selected[0]?.annotation; |
| 43 | + |
| 44 | + const [isOpen, setOpen] = useState(selected?.length > 0); |
| 45 | + |
| 46 | + const handleClose = () => { |
| 47 | + r?.cancelSelected(); |
| 48 | + }; |
| 49 | + |
| 50 | + const { refs, floatingStyles, update, context } = useFloating({ |
| 51 | + placement: 'top', |
| 52 | + open: isOpen, |
| 53 | + onOpenChange: (open, _event, reason) => { |
| 54 | + setOpen(open); |
| 55 | + |
| 56 | + if (!open) { |
| 57 | + if (reason === 'escape-key' || reason === 'focus-out') { |
| 58 | + r?.cancelSelected(); |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + middleware: [ |
| 63 | + offset(10), |
| 64 | + inline(), |
| 65 | + flip(), |
| 66 | + shift({ mainAxis: false, crossAxis: true, padding: 10 }) |
| 67 | + ], |
| 68 | + whileElementsMounted: autoUpdate |
| 69 | + }); |
| 70 | + |
| 71 | + const dismiss = useDismiss(context); |
| 72 | + const role = useRole(context, { role: 'dialog' }); |
| 73 | + const { getFloatingProps } = useInteractions([dismiss, role]); |
| 74 | + |
| 75 | + const selectedKey = selected.map(a => a.annotation.id).join('-'); |
| 76 | + useEffect(() => { |
| 77 | + // Ignore all selection changes except those accompanied by a user event. |
| 78 | + if (selected.length > 0 && event) { |
| 79 | + setOpen(event.type === 'pointerup' || event.type === 'keydown'); |
| 80 | + } |
| 81 | + }, [selectedKey, event]); |
| 82 | + |
| 83 | + useEffect(() => { |
| 84 | + // Close the popup if the selection is cleared |
| 85 | + if (selected.length === 0 && isOpen) { |
| 86 | + setOpen(false); |
| 87 | + } |
| 88 | + }, [isOpen, selectedKey]); |
| 89 | + |
| 90 | + useEffect(() => { |
| 91 | + if (isOpen && annotation) { |
| 92 | + const { |
| 93 | + target: { |
| 94 | + selector: [{ range }] |
| 95 | + } |
| 96 | + } = annotation; |
| 97 | + |
| 98 | + refs.setPositionReference({ |
| 99 | + getBoundingClientRect: range.getBoundingClientRect.bind(range), |
| 100 | + getClientRects: range.getClientRects.bind(range) |
| 101 | + }); |
| 102 | + } else { |
| 103 | + // Don't leave the reference depending on the previously selected annotation |
| 104 | + refs.setPositionReference(null); |
| 105 | + } |
| 106 | + }, [isOpen, annotation, refs]); |
| 107 | + |
| 108 | + // Prevent text-annotator from handling the irrelevant events triggered from the popup |
| 109 | + const getStopEventsPropagationProps = useCallback( |
| 110 | + () => ({ onPointerUp: (event: PointerEvent<HTMLDivElement>) => event.stopPropagation() }), |
| 111 | + [] |
| 112 | + ); |
| 113 | + |
| 114 | + useEffect(() => { |
| 115 | + const config: MutationObserverInit = { attributes: true, childList: true, subtree: true }; |
| 116 | + |
| 117 | + const mutationObserver = new MutationObserver(() => update()); |
| 118 | + mutationObserver.observe(document.body, config); |
| 119 | + |
| 120 | + window.document.addEventListener('scroll', update, true); |
| 121 | + |
| 122 | + return () => { |
| 123 | + mutationObserver.disconnect(); |
| 124 | + window.document.removeEventListener('scroll', update, true); |
| 125 | + }; |
| 126 | + }, [update]); |
| 127 | + |
| 128 | + return isOpen && selected.length > 0 ? ( |
| 129 | + <FloatingPortal> |
| 130 | + <FloatingFocusManager |
| 131 | + context={context} |
| 132 | + modal={false} |
| 133 | + closeOnFocusOut={true} |
| 134 | + initialFocus={ |
| 135 | + /** |
| 136 | + * Don't shift focus to the floating element |
| 137 | + * when the selection performed with the keyboard |
| 138 | + */ |
| 139 | + event?.type === 'keydown' ? -1 : 0 |
| 140 | + } |
| 141 | + returnFocus={false} |
| 142 | + > |
| 143 | + <div |
| 144 | + className="annotation-popup text-annotation-popup not-annotatable" |
| 145 | + ref={refs.setFloating} |
| 146 | + style={floatingStyles} |
| 147 | + {...getFloatingProps()} |
| 148 | + {...getStopEventsPropagationProps()}> |
| 149 | + {props.popup({ |
| 150 | + annotation: selected[0].annotation, |
| 151 | + editable: selected[0].editable, |
| 152 | + event |
| 153 | + })} |
| 154 | + |
| 155 | + {/* It lets keyboard/sr users to know that the dialog closes when they focus out of it */} |
| 156 | + <button className="popup-close-message" onClick={handleClose}> |
| 157 | + This dialog closes when you leave it. |
| 158 | + </button> |
| 159 | + </div> |
| 160 | + </FloatingFocusManager> |
| 161 | + </FloatingPortal> |
| 162 | + ) : null; |
| 163 | + |
| 164 | +}; |
0 commit comments