@@ -268,13 +268,11 @@ export function createLivingMargin(dependencies) {
268268 preview . setAttribute ( "popover" , "auto" ) ;
269269 preview . setAttribute ( "role" , "dialog" ) ;
270270 const previewHead = el ( "div" , "lf-margin-preview-head" ) ;
271- const previewThreadAction = el ( "button" , "lf-btn lf-margin-thread-action" , "Threads" ) ;
272- previewThreadAction . type = "button" ;
273271 const previewTitle = el ( "strong" , "lf-margin-preview-title" ) ;
274272 const previewClose = el ( "button" , "lf-btn lf-margin-preview-close" , "×" ) ;
275273 previewClose . type = "button" ;
276274 previewClose . setAttribute ( "aria-label" , "Close thread" ) ;
277- previewHead . append ( previewThreadAction , previewTitle , previewClose ) ;
275+ previewHead . append ( previewTitle , previewClose ) ;
278276 const previewList = el ( "div" , "lf-margin-preview-list" ) ;
279277 preview . append ( previewHead , previewList ) ;
280278 chromeRoot . append ( preview ) ;
@@ -312,6 +310,7 @@ export function createLivingMargin(dependencies) {
312310 let highlighted = null ;
313311 let rovingFrame = 0 ;
314312 let sheetActivation = false ;
313+ let previewRequest = 0 ;
315314 // The cascade owns available room: panels and trays change the body's named
316315 // container, while an authored sidebar claims the page's left strip. Read the
317316 // posture it resolved instead of asking the viewport a different question.
@@ -451,27 +450,32 @@ export function createLivingMargin(dependencies) {
451450 render ( ) ;
452451 } ) ;
453452 }
453+ function placeThreadPreview ( ) {
454+ if (
455+ ! preview . matches ( ":popover-open" ) ||
456+ ! preview . hasAttribute ( "data-lf-thread" ) ||
457+ ! previewButton ?. isConnected
458+ )
459+ return ;
460+ const marker = previewButton . getBoundingClientRect ( ) ;
461+ const besideLeft = Math . max ( 8 , marker . right + 8 ) ;
462+ preview . style . setProperty ( "--lf-thread-left" , `${ besideLeft } px` ) ;
463+ const card = preview . getBoundingClientRect ( ) ;
464+ const bannerBottom =
465+ document . querySelector ( ".lf-banner" ) ?. getBoundingClientRect ( ) . bottom ?? 0 ;
466+ const firstTop = bannerBottom + 8 ;
467+ const lastTop = innerHeight - card . height - 8 ;
468+ const besideTop = ( marker . top + marker . bottom - card . height ) / 2 ;
469+ preview . style . setProperty (
470+ "--lf-thread-top" ,
471+ `${ Math . max ( firstTop , Math . min ( besideTop , lastTop ) ) } px` ,
472+ ) ;
473+ }
454474 function scheduleThreadPreviewPosition ( ) {
455475 if ( previewPositionFrame ) return ;
456476 previewPositionFrame = requestAnimationFrame ( ( ) => {
457477 previewPositionFrame = 0 ;
458- if (
459- ! preview . matches ( ":popover-open" ) ||
460- ! preview . hasAttribute ( "data-lf-thread" ) ||
461- ! previewButton ?. isConnected
462- )
463- return ;
464- const marker = previewButton . getBoundingClientRect ( ) ;
465- const cardHeight = preview . getBoundingClientRect ( ) . height ;
466- const bannerBottom =
467- document . querySelector ( ".lf-banner" ) ?. getBoundingClientRect ( ) . bottom ?? 0 ;
468- const firstTop = bannerBottom + 8 ;
469- const lastTop = innerHeight - cardHeight - 8 ;
470- const besideTop = ( marker . top + marker . bottom - cardHeight ) / 2 ;
471- preview . style . setProperty (
472- "--lf-thread-top" ,
473- `${ Math . max ( firstTop , Math . min ( besideTop , lastTop ) ) } px` ,
474- ) ;
478+ placeThreadPreview ( ) ;
475479 } ) ;
476480 }
477481 // A viewport posture change can replace the focused full conversation with its
@@ -1362,17 +1366,8 @@ export function createLivingMargin(dependencies) {
13621366 const targetHeading = entry . target ?. querySelector ( ":scope > strong" ) ?. textContent ;
13631367 const title = trimmed ( targetHeading || entry . title , 72 ) ;
13641368 preview . setAttribute ( "data-lf-thread" , "" ) ;
1365- scheduleThreadPreviewPosition ( ) ;
13661369 preview . setAttribute ( "aria-label" , `Thread for ${ title } ` ) ;
13671370 previewTitle . textContent = title ;
1368- previewThreadAction . hidden = false ;
1369- previewThreadAction . setAttribute (
1370- "aria-label" ,
1371- threadItems . length === 1
1372- ? "Open this thread in Threads"
1373- : "Open threads for this passage" ,
1374- ) ;
1375- previewThreadAction . onclick = ( ) => openThreads ( threadItems , entry ) ;
13761371 const nodes = threadItems . map ( previewItemNode ) ;
13771372 const keep = new Set ( nodes ) ;
13781373 for ( const child of [ ...previewList . children ] ) if ( ! keep . has ( child ) ) child . remove ( ) ;
@@ -1392,6 +1387,7 @@ export function createLivingMargin(dependencies) {
13921387 previewClose ) ;
13931388 destination . focus ( { preventScroll : true } ) ;
13941389 }
1390+ placeThreadPreview ( ) ;
13951391 }
13961392
13971393 function previewItemNode ( item ) {
@@ -1451,7 +1447,7 @@ export function createLivingMargin(dependencies) {
14511447 previewShowing = false ;
14521448 }
14531449 }
1454- scheduleThreadPreviewPosition ( ) ;
1450+ placeThreadPreview ( ) ;
14551451 highlight ( entry . target ) ;
14561452 for ( const row of rows . values ( ) )
14571453 syncThreadRelation ( row , markerNeedsPreview ( row . lfEntry ) ) ;
@@ -1504,17 +1500,29 @@ export function createLivingMargin(dependencies) {
15041500 }
15051501
15061502 function openThreadChoice ( entry , button ) {
1507- if ( panelIsOpen ( ) ) setPanel ( false ) ;
1508- if ( expandedOptionsKey && expandedOptionsKey !== entry . key )
1509- setOptionsOpen ( entry , false ) ;
1510- const choice = threadReading ( entry ) ;
1511- if ( ! choice ) return ;
1512- if ( ! threadBeside ( ) ) {
1513- setOptionsOpen ( entry , false ) ;
1514- openThreads ( choice . items , entry ) ;
1503+ const open = ( ) => {
1504+ if ( expandedOptionsKey && expandedOptionsKey !== entry . key )
1505+ setOptionsOpen ( entry , false ) ;
1506+ const choice = threadReading ( entry ) ;
1507+ if ( ! choice ) return ;
1508+ if ( ! threadBeside ( ) ) {
1509+ setOptionsOpen ( entry , false ) ;
1510+ openThreads ( choice . items , entry ) ;
1511+ return ;
1512+ }
1513+ togglePinned ( entry , button ) ;
1514+ } ;
1515+ if ( panelIsOpen ( ) ) {
1516+ const request = ++ previewRequest ;
1517+ setPanel ( false ) ;
1518+ const movements = document . body . getAnimations ( ) ;
1519+ Promise . allSettled ( movements . map ( ( movement ) => movement . finished ) ) . then ( ( ) => {
1520+ if ( request === previewRequest && button . isConnected ) open ( ) ;
1521+ } ) ;
15151522 return ;
15161523 }
1517- togglePinned ( entry , button ) ;
1524+ previewRequest += 1 ;
1525+ open ( ) ;
15181526 }
15191527
15201528 function openThreads ( threadItems , entry ) {
@@ -1658,10 +1666,27 @@ export function createLivingMargin(dependencies) {
16581666 } ,
16591667 { capture : true , passive : true } ,
16601668 ) ;
1661- window . addEventListener ( "resize" , schedulePostureRender ) ;
1669+ window . addEventListener ( "resize" , ( ) => {
1670+ placeThreadPreview ( ) ;
1671+ schedulePostureRender ( ) ;
1672+ } ) ;
16621673 render ( ) ;
16631674
16641675 return {
1676+ activeInlineThread : ( ) => {
1677+ if (
1678+ ! pinnedKey ||
1679+ previewEntry ?. key !== pinnedKey ||
1680+ document . activeElement !== previewButton ||
1681+ ! preview . matches ( ":popover-open" ) ||
1682+ ! preview . hasAttribute ( "data-lf-thread" )
1683+ )
1684+ return null ;
1685+ const conversations = previewList . querySelectorAll (
1686+ ".lf-margin-thread .lf-conversation-thread" ,
1687+ ) ;
1688+ return conversations . length === 1 ? conversations [ 0 ] : null ;
1689+ } ,
16651690 closePreview : ( ) => closePreview ( false ) ,
16661691 enterPageMap,
16671692 marginTargetAt,
0 commit comments