11import { shownBox , shownParts , shownRect } from "./geometry.js" ;
2+ import { moveScrollerBy } from "./scrolling.js" ;
23
34/* Anchor resolution, painting, and anchor-specific travel. */
45let publishedAnchors ;
@@ -21,7 +22,6 @@ export function createAnchors(dependencies) {
2122 const {
2223 DATUM ,
2324 SCROLL ,
24- TEXT_BLOCK ,
2525 actionAnchor,
2626 activateVisual,
2727 aimBox,
@@ -51,7 +51,6 @@ export function createAnchors(dependencies) {
5151 inChrome,
5252 inUi,
5353 inspectEl,
54- landedAt,
5554 offer,
5655 pageQueryAll,
5756 pageScroller,
@@ -66,9 +65,7 @@ export function createAnchors(dependencies) {
6665 refreshAction,
6766 registry,
6867 reveal,
69- runtime,
7068 scrollerFor,
71- setLanded,
7269 setPanel,
7370 settledAway,
7471 tagsDeclaring,
@@ -79,127 +76,6 @@ export function createAnchors(dependencies) {
7976 worksSelector,
8077 } = dependencies ;
8178
82- // ---------- view continuity ----------
83- // Following a new version replaces the authored main, whether the live root keeps this
84- // document or historical travel opens another one. A raw replacement leaves the reader
85- // at the top mid-session, standing nowhere in the walk they were making. Where they are
86- // rides across as one semantic view — and through tabStore on document travel, per-tab
87- // because a place in a page shouldn't outlive it. Two things are recorded, because
88- // askPosition reads two the runtime can write down: the passage they were reading, and
89- // the ask the n/p walk had stepped them to. The passage travels as a landmark rather
90- // than a pixel offset, since content moves between versions: re-find it by its text
91- // within its section, then the section alone, and only fall back to the raw offset when
92- // neither survived the revision. The panel's own open state is restored separately
93- // (PANEL_KEY); because that runs first, the column is already reflowed by the time we
94- // scroll.
95- const VIEW_KEY = "lf-view" ;
96- const LANDMARK_CAP = 160 ;
97-
98- // The page's own text blocks the reader can see, in document order, with the rect of each
99- // one's first line — one reading of what is in front of them, for the two questions that
100- // ask it: which passage a version change should land them back on (below), and where a
101- // walk over the page's asks starts when they have pointed at nothing (askPosition).
102- // A block's landmark is the top of its first line (a range), not its border box; restore
103- // measures the matched text the same way, so the line box's leading cancels out.
104- function * blocksOnScreen ( ) {
105- // Where the banner's lower edge is, off the declaration the banner is drawn to
106- // rather than as a number copied from it. --lf-banner-h is stated once on body
107- // (chrome-style.js) and seven rules place themselves against it; this used to
108- // carry 42 with a comment saying whose 42 it was, which is a copy with a note
109- // attached rather than a reading.
110- //
111- // What that costs is quiet, because this decides which blocks count as the ones
112- // being read: captureView stores the first as the reader's landmark and the ask
113- // walk starts from it. A taller banner and a block hidden behind it is the one
114- // the reader is scrolled back to on their next visit; a shorter one and the first
115- // paragraph they can actually see is skipped. Neither raises anything.
116- const bannerBottom =
117- parseFloat ( getComputedStyle ( document . body ) . getPropertyValue ( "--lf-banner-h" ) ) ||
118- 0 ;
119- for ( const block of document . querySelectorAll ( TEXT_BLOCK ) ) {
120- // [hidden] needs an explicit skip: hidden="until-found" resolves to
121- // content-visibility, under which descendants still report real rects —
122- // but what's behind an inactive tab isn't what the reader is reading.
123- if ( inChrome ( block ) || block . closest ( "[hidden]" ) ) continue ;
124- const range = document . createRange ( ) ;
125- range . selectNodeContents ( block ) ;
126- const rect = range . getBoundingClientRect ( ) ;
127- if ( rect . height && rect . bottom > bannerBottom ) yield [ block , rect ] ;
128- }
129- }
130- // The quote and the section it's searched in come from the same block, or the search is
131- // filtered to a section the text isn't in and can only ever fail — restore then falls back
132- // to the section, which doesn't absorb content added above the reader inside it.
133- function captureView ( ) {
134- const view = { revision : runtime . currentRevision , y : pageScroller . scrollTop } ;
135- // Where the ask walk left off, which is the reader's place stated more exactly than
136- // any block can state it — the walk put them there on purpose. Its element identity
137- // does not survive an authored-main replacement, and the module variable does not
138- // survive document travel, so the id is the one form both can restore. The ring is not
139- // recorded beside it: it is painted from focus, and another document starts on the page.
140- view . ask = landedAt ( ) ?. id ;
141- for ( const [ block , rect ] of blocksOnScreen ( ) ) {
142- const section = block . closest ( "[id]" ) ;
143- if ( ! view . section && section ) {
144- // The first on-screen block's section, kept only until a quotable block supplies
145- // its own: a page with nothing quotable on screen still has somewhere to land.
146- view . section = section . id ;
147- view . sectionTop = shownBox ( section ) . top ;
148- }
149- // Written down the way a comment's quote is, so the search that re-finds it is
150- // looking for a string of the same kind.
151- const text = cut ( quoteFrom ( textNodesUnder ( block ) ) , 0 , LANDMARK_CAP ) ;
152- // A short line ("Risks") would match anywhere; keep scanning for a quotable block.
153- if ( text . length >= 24 ) {
154- // Unconditionally, so a quotable block under no section clears the earlier one
155- // rather than sending the search into a subtree its text isn't in.
156- view . section = section ?. id ;
157- view . sectionTop = section && shownBox ( section ) . top ;
158- view . quote = text ;
159- view . quoteTop = rect . top ;
160- break ;
161- }
162- }
163- return view ;
164- }
165-
166- // A restore jumps rather than glides: a page is free to set scroll-behavior: smooth, and
167- // animating from the replacement's raw position is worse than the jump it replaces.
168- // Moving to a mark the reader asked for is the other case, and says so.
169- // The document scrolls for its own content; the panel's list scrolls for a widget an
170- // agent sent in a reply. Most callers mean the document, so it remains the default.
171- const jumpBy = ( dy , behavior = "instant" , box = pageScroller ) =>
172- box . scrollBy ( { top : dy , behavior } ) ;
173- function restoreView ( view ) {
174- // Where the walk left off, put back before the scroll below restores the coarser
175- // reading of the same fact — and put back whether or not this version answered that
176- // ask, since an ask the reader has not stepped off is still the one they would step
177- // from. The document's own lookup rather than elementById: the ask list is the
178- // document's (openAsks), and a landing inside a shadow tree is one askStep could never
179- // measure against. A thread's ask is not here yet — the panel is rebuilt from the log
180- // on the first poll, which is behind this — so the record answers for the page's asks
181- // and says nothing about the panel's, rather than restoring a second time later over a
182- // walk the reader has made since.
183- setLanded ( ( view . ask && document . getElementById ( view . ask ) ) || null ) ;
184- const text = pageText ( ) ;
185- const found = view . quote && resolveAnchor ( view , text ) ;
186- if ( found ?. segments ) {
187- reveal ( found . segments [ 0 ] . node . parentElement ) ; // the passage may sit behind a tab
188- jumpBy ( rangeOf ( found . segments ) . getBoundingClientRect ( ) . top - view . quoteTop ) ;
189- return ;
190- }
191- const section = resolveAnchor ( { section : view . section } , text ) ?. element ;
192- if ( section ) {
193- reveal ( section ) ;
194- // The shown reading on both sides of the subtraction, because the landmark is
195- // whatever id stands nearest the block the reader was on, and a section that
196- // generates no box of its own is one a suggestion wrapping whole sections leaves
197- // there. Read raw, both sides come back 0 and the correction is 0 — so the restore
198- // that had somewhere to land did nothing, silently, and left the reader at the top.
199- jumpBy ( shownBox ( section ) . top - view . sectionTop ) ;
200- } else pageScroller . scrollTo ( { top : view . y , behavior : "instant" } ) ;
201- }
202-
20379 // ---------- anchors ----------
20480 // An anchor names a passage: a section id, a quote, or both. Resolving one is the only
20581 // place the page is searched, so the three things that read a passage back — a thread's
@@ -1087,7 +963,8 @@ export function createAnchors(dependencies) {
1087963 // is the panel's list where that is what scrolls.
1088964 //
1089965 // It glides, because a page the reader is already holding is one the motion keeps their
1090- // place in — the same reason a restore doesn't (jumpBy). An arrival passes "instant" for
966+ // place in — the same reason a restore doesn't (moveScrollerBy). An arrival passes
967+ // "instant" for
1091968 // exactly that reason: a document that appeared a moment ago holds no place to keep, so
1092969 // the glide would be animating from nowhere.
1093970 function centreBy ( where , block = "center" , box = pageScroller ) {
@@ -1110,7 +987,7 @@ export function createAnchors(dependencies) {
1110987 el . scrollIntoView ( { block : "nearest" , inline : "nearest" , behavior : "instant" } ) ;
1111988 const box = scrollerFor ( el ) ;
1112989 if ( ! under ( el , box ) ) return ;
1113- jumpBy ( centreBy ( el , block , box ) , behavior , box ) ;
990+ moveScrollerBy ( box , centreBy ( el , block , box ) , behavior ) ;
1114991 }
1115992
1116993 // Move to where a thread is painted, if it still is — asked of the pass's own record, so the
@@ -1137,7 +1014,7 @@ export function createAnchors(dependencies) {
11371014 // rendered diagram sits in a box with its own horizontal scroll, which the vertical
11381015 // jump below cannot reach — scrolling to it in one axis leaves it off-screen in the other.
11391016 holder . scrollIntoView ( { block : "nearest" , inline : "nearest" , behavior : "instant" } ) ;
1140- jumpBy ( centreBy ( where ) , SCROLL ) ;
1017+ moveScrollerBy ( pageScroller , centreBy ( where ) , SCROLL ) ;
11411018 }
11421019
11431020 // Pointer feedback a wrapped <mark> got from :hover and cursor: pointer, neither of which
@@ -1305,10 +1182,6 @@ export function createAnchors(dependencies) {
13051182 document . addEventListener ( "scroll" , pageShifted , { capture : true , passive : true } ) ;
13061183
13071184 const anchors = {
1308- VIEW_KEY ,
1309- blocksOnScreen,
1310- captureView,
1311- restoreView,
13121185 sectionOf,
13131186 ITEM ,
13141187 isItem,
0 commit comments