44 * being read, the zoom and the download; the document's sections sit to the left; the right margin
55 * holds whatever is placed beside a passage, level with it.
66 */
7- import { Minus , Plus } from "lucide-react" ;
7+ import { Button , useDismissablePopover } from "@corbits/react-ui" ;
8+ import { MessageSquare , Minus , Plus } from "lucide-react" ;
89import {
910 useCallback ,
1011 useEffect ,
12+ useId ,
1113 useLayoutEffect ,
1214 useRef ,
1315 useState ,
1416 type CSSProperties ,
17+ type MouseEvent ,
1518 type ReactNode ,
1619} from "react" ;
20+ import { createPortal } from "react-dom" ;
1721import { Fault } from "../../components.tsx" ;
1822import { wording } from "../../config.ts" ;
1923import {
@@ -49,11 +53,13 @@ const READING_LINE = 0.4;
4953// A margin item starts a little above its passage, so its first line sits level with it.
5054const MARGIN_LEAD = 15 ;
5155const MARGIN_GAP = 8 ;
52- // A margin item is a card this wide; with less room than that beside the page, the margin stays
53- // empty and the underlines alone mark the passages. The stylesheet takes the width from here.
56+ // A margin item is a card at most this wide; with less room than that beside the page, the cards
57+ // leave the margin and open from their passages instead . The stylesheet takes the width from here.
5458const CARD_WIDTH = 280 ;
5559// Cards sit --space-8 clear of the page and want --space-6 clear of the pane's edge.
5660const MARGIN_INSET = 32 + 24 ;
61+ // The underline sits below the line's box, so a press just under the words still reaches it.
62+ const UNDERLINE_REACH = 8 ;
5763// Each state is its own highlight, named as the stylesheet paints it.
5864const HIGHLIGHTS : Readonly < Record < PassageState | "commented" , string > > = {
5965 commented : "document-passage" ,
@@ -180,6 +186,21 @@ export function DocumentPage({
180186 const [ page , setPage ] = useState ( { current : 1 , total : 1 } ) ;
181187 const [ places , setPlaces ] = useState < readonly PassagePlace [ ] > ( [ ] ) ;
182188 const [ roomy , setRoomy ] = useState ( true ) ;
189+ const ranges = useRef ( new Map < string , Range > ( ) ) ;
190+ const cardId = useId ( ) ;
191+ const [ card , setCard ] = useState < {
192+ place : PassagePlace ;
193+ line : DOMRect ;
194+ } | null > ( null ) ;
195+ const { rootRef : cardRef , triggerRef : cardTrigger } = useDismissablePopover <
196+ HTMLDivElement ,
197+ HTMLButtonElement
198+ > ( {
199+ open : card !== null ,
200+ onOpenChange : ( open ) => {
201+ if ( ! open ) setCard ( null ) ;
202+ } ,
203+ } ) ;
183204
184205 const follow = useCallback ( ( ) => {
185206 const view = scroller . current ;
@@ -239,6 +260,7 @@ export function DocumentPage({
239260 range : hit . range ,
240261 } ) ) ,
241262 ) ;
263+ ranges . current = new Map ( found . map ( ( hit ) => [ hit . place . id , hit . range ] ) ) ;
242264 setPlaces ( ( was ) =>
243265 same (
244266 was ,
@@ -284,7 +306,26 @@ export function DocumentPage({
284306 } ;
285307 } , [ measure ] ) ;
286308
287- const inMargin = margin !== undefined && roomy ;
309+ // A card opened from its passage sits under the line, or above it when the view ends first.
310+ useLayoutEffect ( ( ) => {
311+ const box = cardRef . current ;
312+ if ( card === null || box === null ) return ;
313+ const below = card . line . bottom + MARGIN_GAP ;
314+ const fitsBelow =
315+ below + box . offsetHeight <= window . innerHeight - MARGIN_GAP ;
316+ box . style . top = `${ String ( fitsBelow ? below : card . line . top - MARGIN_GAP - box . offsetHeight ) } px` ;
317+ box . style . left = `${ String ( Math . max ( MARGIN_GAP , Math . min ( card . line . left , window . innerWidth - box . offsetWidth - MARGIN_GAP ) ) ) } px` ;
318+ // Opened from the keyboard, the card takes focus so Escape can hand it back, and appears at
319+ // once: a key press is answered without a flourish.
320+ if ( cardTrigger . current === null ) delete box . dataset . keys ;
321+ else {
322+ box . dataset . keys = "" ;
323+ box . focus ( { preventScroll : true } ) ;
324+ }
325+ } , [ card , cardRef , cardTrigger ] ) ;
326+
327+ const narrow = margin !== undefined && ! roomy ;
328+ const inMargin = margin !== undefined && ! narrow ;
288329
289330 // Items too close to fit level with their passages stack below one another, in page order.
290331 // Every height is read before any top is written, so placing them costs one layout. An item
@@ -309,6 +350,48 @@ export function DocumentPage({
309350 return ( ) => resized . disconnect ( ) ;
310351 } , [ places , inMargin ] ) ;
311352
353+ /** The passage under a point on the page, by the line of it that was pressed. */
354+ const passageAt = ( x : number , y : number ) => {
355+ for ( const place of places ) {
356+ const lines = ranges . current . get ( place . id ) ?. getClientRects ( ) ?? [ ] ;
357+ const line = [ ...lines ] . find (
358+ ( rect ) =>
359+ x >= rect . left &&
360+ x <= rect . right &&
361+ y >= rect . top &&
362+ y <= rect . bottom + UNDERLINE_REACH ,
363+ ) ;
364+ if ( line !== undefined ) return { place, line } ;
365+ }
366+ return null ;
367+ } ;
368+
369+ // With the margin too narrow for its cards, a passage's underline opens its card instead.
370+ const openFromMark = ( event : MouseEvent ) => {
371+ if ( ! narrow || window . getSelection ( ) ?. isCollapsed === false ) return ;
372+ const hit = passageAt ( event . clientX , event . clientY ) ;
373+ if ( hit === null ) return ;
374+ // The card takes this press; quoting the passage into a message is not offered as well.
375+ event . stopPropagation ( ) ;
376+ cardTrigger . current = null ;
377+ setCard ( hit ) ;
378+ } ;
379+
380+ const pointAtMark = ( event : MouseEvent < HTMLElement > ) => {
381+ if ( ! narrow ) return ;
382+ event . currentTarget . toggleAttribute (
383+ "data-pointing" ,
384+ passageAt ( event . clientX , event . clientY ) !== null ,
385+ ) ;
386+ } ;
387+
388+ const openFromKeys = ( place : PassagePlace , button : HTMLButtonElement ) => {
389+ const line = ranges . current . get ( place . id ) ?. getClientRects ( ) [ 0 ] ;
390+ if ( line === undefined ) return ;
391+ cardTrigger . current = button ;
392+ setCard ( { place, line } ) ;
393+ } ;
394+
312395 // The reader's scroll padding keeps a section clear of the bar; its scroll behaviour is smooth
313396 // unless the person asks for less motion.
314397 const open = ( id : string | null ) => {
@@ -376,7 +459,10 @@ export function DocumentPage({
376459 } as CSSProperties
377460 }
378461 data-slot = "document-page"
379- onScroll = { follow }
462+ onScroll = { ( ) => {
463+ follow ( ) ;
464+ setCard ( null ) ;
465+ } }
380466 >
381467 < PaneBar
382468 label = { wording . documentBar }
@@ -395,7 +481,13 @@ export function DocumentPage({
395481 { notes === undefined || notes === null ? null : (
396482 < div className = { styles . notes } > { notes } </ div >
397483 ) }
398- < article ref = { sheet } className = { styles . sheet } data-writing = { writing } >
484+ < article
485+ ref = { sheet }
486+ className = { styles . sheet }
487+ data-writing = { writing }
488+ onClickCapture = { openFromMark }
489+ onPointerMove = { pointAtMark }
490+ >
399491 < header className = { styles . head } >
400492 < span > { project } </ span >
401493 < span > { meta } </ span >
@@ -406,9 +498,29 @@ export function DocumentPage({
406498 < Fault onRetry = { onResume } > { words . stopped } </ Fault >
407499 ) : null }
408500 </ article >
501+ { narrow
502+ ? places . map ( ( place ) => (
503+ < Button
504+ key = { place . id }
505+ variant = "ghost"
506+ size = "icon"
507+ className = { styles . mark }
508+ style = { { top : place . top } }
509+ aria-label = { words . passageNote ( place . quote ) }
510+ aria-haspopup = "dialog"
511+ aria-expanded = { card ?. place . id === place . id }
512+ aria-controls = {
513+ card ?. place . id === place . id ? cardId : undefined
514+ }
515+ onClick = { ( event ) => openFromKeys ( place , event . currentTarget ) }
516+ >
517+ < MessageSquare aria-hidden = "true" />
518+ </ Button >
519+ ) )
520+ : null }
409521 </ div >
410522 < div ref = { marginColumn } className = { styles . margin } >
411- { margin === undefined || ! roomy
523+ { margin === undefined || narrow
412524 ? null
413525 : places . map ( ( place , index ) => (
414526 < div
@@ -424,6 +536,32 @@ export function DocumentPage({
424536 ) ) }
425537 </ div >
426538 </ div >
539+ { card === null || margin === undefined
540+ ? null
541+ : createPortal (
542+ < div
543+ ref = { cardRef }
544+ className = { styles . card }
545+ style = {
546+ { "--card-width" : `${ String ( CARD_WIDTH ) } px` } as CSSProperties
547+ }
548+ id = { cardId }
549+ data-slot = "document-margin-card"
550+ role = "dialog"
551+ aria-label = { words . passageNote ( card . place . quote ) }
552+ tabIndex = { - 1 }
553+ onBlur = { ( event ) => {
554+ if (
555+ event . relatedTarget !== null &&
556+ ! event . currentTarget . contains ( event . relatedTarget )
557+ )
558+ setCard ( null ) ;
559+ } }
560+ >
561+ { margin ( card . place ) }
562+ </ div > ,
563+ document . body ,
564+ ) }
427565 </ div >
428566 ) ;
429567}
0 commit comments