Skip to content

Commit 8eb3a8f

Browse files
committed
Extract semantic view continuity
Reading landmarks serve version activation, document arrival, and ask navigation. Give that state a dedicated runtime owner and put relative scroller movement in the shared scrolling owner.
1 parent 43ada1a commit 8eb3a8f

6 files changed

Lines changed: 185 additions & 144 deletions

File tree

‎plugins/leaf/skills/leaf/CLAUDE.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ rendering, and async settlement;
7373
`runtime/widget-elements.js` owns widget-element construction, labels, gesture
7474
guards, deferred measurement, and control sizing;
7575
`runtime/registry.js` owns vocabulary queries;
76-
`runtime/scrolling.js` owns the document scroller identity and the gutter its bar
77-
takes;
76+
`runtime/scrolling.js` owns the document scroller identity, relative scroller moves,
77+
and the gutter its bar takes;
7878
`runtime/presentation.js` owns runtime paint and the words it projects;
7979
`runtime/reach.js` owns keyboard access to overflow and the containing block a
8080
scroller owes what it scrolls;
@@ -86,6 +86,8 @@ dynamic widget imports, and initial settlement;
8686
`runtime/syntax.js` owns code tokenization and highlighting;
8787
`runtime/passages.js` owns the DOM reading and quote resolver;
8888
`runtime/text-alignment.js` owns lossless, language-aware whole-text alignment;
89+
`runtime/view-continuity.js` owns semantic reading landmarks across authored-document
90+
replacement and supplies the page-block reading used to start directional walks;
8991
`runtime/pointer.js` owns the shared unrounded pointer position;
9092
`runtime/geometry.js` owns the shared readings of visible boxes and clipping;
9193
`runtime/navigation.js` owns reader travel and scroller selection;
@@ -1069,7 +1071,7 @@ without closing the thread.
10691071

10701072
`scrollToThread` is the one travel every "show me that comment's passage" ends
10711073
in. The target's own box first comes into view instantly, including inside a
1072-
sideways scroller, then `jumpBy` glides the exact mark to its final position in
1074+
sideways scroller, then `moveScrollerBy` glides the exact mark to its final position in
10731075
the region that holds it. The travel owns no standing or arrival state. Focus
10741076
already supplies the durable answer through `paintStanding`, and a transient
10751077
page effect does not observe, restart, or reconcile across the browser's

‎plugins/leaf/skills/leaf/assets/leaf.js‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ import {
267267
uiInside,
268268
wrote,
269269
} from "./runtime/passages.js";
270+
import { createViewContinuity, VIEW_KEY } from "./runtime/view-continuity.js";
270271
import { textUnits } from "./runtime/text-alignment.js";
271272
import {
272273
ago,
@@ -3181,6 +3182,7 @@ latestChip.onclick = () => goActive();
31813182
// (restatement_errors), not something inferred here from silence.
31823183
let conversationRuntime;
31833184
let anchorRuntime;
3185+
let viewRuntime;
31843186

31853187
function buildThreads(...args) {
31863188
return conversationRuntime.buildThreads(...args);
@@ -3226,13 +3228,13 @@ function showThread(...args) {
32263228
}
32273229

32283230
function blocksOnScreen(...args) {
3229-
return anchorRuntime.blocksOnScreen(...args);
3231+
return viewRuntime.blocksOnScreen(...args);
32303232
}
32313233
function captureView(...args) {
3232-
return anchorRuntime.captureView(...args);
3234+
return viewRuntime.captureView(...args);
32333235
}
32343236
function restoreView(...args) {
3235-
return anchorRuntime.restoreView(...args);
3237+
return viewRuntime.restoreView(...args);
32363238
}
32373239
function sectionOf(...args) {
32383240
return anchorRuntime.sectionOf(...args);
@@ -3526,7 +3528,6 @@ updateRuntime = createUpdates(runtime, {
35263528
anchorRuntime = createAnchors({
35273529
DATUM,
35283530
SCROLL,
3529-
TEXT_BLOCK,
35303531
actionAnchor: fabAnchorAt,
35313532
activateVisual,
35323533
aimBox,
@@ -3556,7 +3557,6 @@ anchorRuntime = createAnchors({
35563557
inChrome,
35573558
inUi,
35583559
inspectEl,
3559-
landedAt,
35603560
offer,
35613561
pageQueryAll,
35623562
pageScroller,
@@ -3571,9 +3571,7 @@ anchorRuntime = createAnchors({
35713571
refreshAction: refreshFab,
35723572
registry,
35733573
reveal,
3574-
runtime,
35753574
scrollerFor,
3576-
setLanded,
35773575
setPanel,
35783576
settledAway,
35793577
tagsDeclaring,
@@ -3583,7 +3581,23 @@ anchorRuntime = createAnchors({
35833581
withdraw,
35843582
worksSelector: WORKS,
35853583
});
3586-
const { VIEW_KEY, ITEM, NOTE } = anchorRuntime;
3584+
const { ITEM, NOTE } = anchorRuntime;
3585+
3586+
viewRuntime = createViewContinuity({
3587+
TEXT_BLOCK,
3588+
cut,
3589+
inChrome,
3590+
landedAt,
3591+
pageScroller,
3592+
pageText,
3593+
quoteFrom,
3594+
rangeOf,
3595+
resolveAnchor,
3596+
reveal,
3597+
runtime,
3598+
setLanded,
3599+
textNodesUnder,
3600+
});
35873601

35883602
createConversationLanding({ scrollToThread });
35893603
createConversationBox({ post, renderPanel, showToast, wireInput });

‎plugins/leaf/skills/leaf/assets/runtime/anchors.js‎

Lines changed: 5 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { shownBox, shownParts, shownRect } from "./geometry.js";
2+
import { moveScrollerBy } from "./scrolling.js";
23

34
/* Anchor resolution, painting, and anchor-specific travel. */
45
let 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,

‎plugins/leaf/skills/leaf/assets/runtime/navigation.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ export function createNavigation({
120120
// scrollTo's smooth takes three times as long over the same distance and has no dial,
121121
// which is what read as gradual when the step rode it. So the runtime drives the step
122122
// itself: PAGE_MS of easing out, each write `instant` rather than `auto` since a page is
123-
// free to set `scroll-behavior: smooth` on the box it scrolls (jumpBy says the same) and
123+
// free to set `scroll-behavior: smooth` on the box it scrolls (moveScrollerBy says the
124+
// same) and
124125
// a glide built from smooth writes would never land. A press mid-flight retargets from
125126
// the goal, so two quick presses move exactly a page; the goal is clamped, so pressing on
126127
// at the foot banks no debt for u to press back through; and the step stands down the

‎plugins/leaf/skills/leaf/assets/runtime/scrolling.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
// SortableJS walks up from the dragged card and, on reaching body, hands back
77
// document.scrollingElement, so lf-board passes this in rather than letting it guess.
88
export const pageScroller = document.body;
9+
10+
// Apply a relative movement to the scroller the caller resolved. Keeping the box explicit
11+
// lets document reading continuity and anchor travel share the operation without either
12+
// owning the other's default destination.
13+
export const moveScrollerBy = (box, top, behavior = "instant") =>
14+
box.scrollBy({ top, behavior });
15+
916
// The room the scroller's own bar takes out of the page, which is a fact about the
1017
// scroller and so belongs beside it: every geometry reading that starts from the window
1118
// owes it, and each of them differs in what the gutter is coming off rather than in how it

0 commit comments

Comments
 (0)