@adea-ai/ui 0.152.0 → 0.152.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/conversation/scroll-follow.d.ts.map +1 -1
- package/dist/components/conversation/scroll-follow.js +14 -1
- package/dist/components/conversation/scroll-follow.js.map +1 -1
- package/dist/r/src/components/conversation/scroll-follow.ts +24 -1
- package/package.json +1 -1
- package/src/components/conversation/scroll-follow.ts +24 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scroll-follow.d.ts","sourceRoot":"","sources":["../../../src/components/conversation/scroll-follow.ts"],"names":[],"mappings":"AAkCA,sGAAsG;AACtG,MAAM,MAAM,2BAA2B,GAAG,QAAQ,CAAC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,OAAO,CAAA;CAAE,CAAC,CAAA;AAEvF,yGAAyG;AACzG,MAAM,MAAM,gBAAgB,GAAG,MAAM,MAAM,IAAI,CAAA;AAE/C,kFAAkF;AAClF,wBAAgB,kBAAkB,CAAC,OAAO,EAAE;IAC1C,OAAO,EAAE,MAAM,OAAO,CAAA;IACtB,QAAQ,EAAE,MAAM,MAAM,GAAG,SAAS,CAAA;IAClC,SAAS,EAAE,MAAM,MAAM,CAAA;IACvB,eAAe,CAAC,EAAE,MAAM,2BAA2B,GAAG,SAAS,CAAA;IAC/D,gBAAgB,CAAC,EAAE,CAAC,QAAQ,EAAE,2BAA2B,KAAK,IAAI,CAAA;CACnE;;;;;
|
|
1
|
+
{"version":3,"file":"scroll-follow.d.ts","sourceRoot":"","sources":["../../../src/components/conversation/scroll-follow.ts"],"names":[],"mappings":"AAkCA,sGAAsG;AACtG,MAAM,MAAM,2BAA2B,GAAG,QAAQ,CAAC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,OAAO,CAAA;CAAE,CAAC,CAAA;AAEvF,yGAAyG;AACzG,MAAM,MAAM,gBAAgB,GAAG,MAAM,MAAM,IAAI,CAAA;AAE/C,kFAAkF;AAClF,wBAAgB,kBAAkB,CAAC,OAAO,EAAE;IAC1C,OAAO,EAAE,MAAM,OAAO,CAAA;IACtB,QAAQ,EAAE,MAAM,MAAM,GAAG,SAAS,CAAA;IAClC,SAAS,EAAE,MAAM,MAAM,CAAA;IACvB,eAAe,CAAC,EAAE,MAAM,2BAA2B,GAAG,SAAS,CAAA;IAC/D,gBAAgB,CAAC,EAAE,CAAC,QAAQ,EAAE,2BAA2B,KAAK,IAAI,CAAA;CACnE;;;;;4BAuU2B,cAAc;2BAKf,cAAc;EAIxC"}
|
|
@@ -13,6 +13,7 @@ function createScrollFollow(options) {
|
|
|
13
13
|
let readerMovedSinceWrite = false;
|
|
14
14
|
let previousTop = -1;
|
|
15
15
|
let lastScrollClientHeight = 0;
|
|
16
|
+
let endRow = null;
|
|
16
17
|
let pendingRestore;
|
|
17
18
|
let pendingRestoreUserIntent = false;
|
|
18
19
|
let holds = 0;
|
|
@@ -36,6 +37,7 @@ function createScrollFollow(options) {
|
|
|
36
37
|
};
|
|
37
38
|
onCleanup(reportPosition);
|
|
38
39
|
const rememberPosition = (element) => {
|
|
40
|
+
endRow = content?.lastElementChild ?? null;
|
|
39
41
|
lastWriteTop = element.scrollTop;
|
|
40
42
|
lastWriteClientHeight = element.clientHeight;
|
|
41
43
|
previousTop = element.scrollTop;
|
|
@@ -53,6 +55,11 @@ function createScrollFollow(options) {
|
|
|
53
55
|
readerMovedSinceWrite
|
|
54
56
|
})) rememberPosition(element);
|
|
55
57
|
};
|
|
58
|
+
const appendedHeight = () => {
|
|
59
|
+
const last = content?.lastElementChild;
|
|
60
|
+
if (!last || !endRow || last === endRow || endRow.parentElement !== content) return 0;
|
|
61
|
+
return Math.max(0, last.getBoundingClientRect().bottom - endRow.getBoundingClientRect().bottom);
|
|
62
|
+
};
|
|
56
63
|
const pinHeld = (element) => {
|
|
57
64
|
const geom = scrollGeometry(element);
|
|
58
65
|
stick = true;
|
|
@@ -194,7 +201,13 @@ function createScrollFollow(options) {
|
|
|
194
201
|
contentObserver = new MutationObserver(() => {
|
|
195
202
|
if (!scroller || !options.enabled() || pendingRestore || holds > 0 || !stick || readerMovedSinceWrite || lastWriteTop < 0) return;
|
|
196
203
|
if (scroller.scrollTop >= lastWriteTop - 2) return;
|
|
197
|
-
|
|
204
|
+
const geom = scrollGeometry(scroller);
|
|
205
|
+
if (isAtEnd(geom)) return rememberPosition(scroller);
|
|
206
|
+
const grown = appendedHeight();
|
|
207
|
+
if (grown > 0 && isAtEnd({
|
|
208
|
+
...geom,
|
|
209
|
+
scrollHeight: geom.scrollHeight - grown
|
|
210
|
+
})) rememberPosition(scroller);
|
|
198
211
|
});
|
|
199
212
|
contentObserver.observe(content, {
|
|
200
213
|
childList: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scroll-follow.js","names":["geometry"],"sources":["../../../src/components/conversation/scroll-follow.ts"],"sourcesContent":["/*\n * Substantially translated from KiroCrew website/src/app-sdk/useChatScrollFollow.ts,\n * pinned at 283e136c0f902e965a535a7c9548c57c7504fed0.\n * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n * This product includes software developed at Amazon.com, Inc. (https://www.amazon.com/).\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy at http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software distributed\n * under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR\n * CONDITIONS OF ANY KIND, either express or implied. See the License for the\n * specific language governing permissions and limitations under the License.\n * Solid owner effects replace React refs/effects; native geometry and the\n * content/viewport observer contract are retained. No session store is copied.\n */\nimport { createEffect, createSignal, onCleanup, untrack } from 'solid-js'\nimport {\n bottomTarget,\n computeAtBottom,\n isAtEnd,\n scrollGeometry as geometry,\n} from '#lib/scroll-geometry'\nimport {\n evaluateAutoPin,\n isScrollIntent,\n isScrollUpIntent,\n isSelfScroll,\n isViewportGrowthClamp,\n READER_INPUT_EVENTS,\n readerInput,\n resolveUserScrollStick,\n SELF_SCROLL_EPSILON,\n} from './scroll-follow-core'\n\n/** Where the reader is in a transcript: the native offset, and whether they are following its end. */\nexport type ConversationReadingPosition = Readonly<{ top: number; following: boolean }>\n\n/** Opens a hold on the follow position (see `createScrollFollow`); the function it returns closes it. */\nexport type ScrollFollowHold = () => () => void\n\n/** Internal plain-scroller binding; host-owned restoration can disable follow. */\nexport function createScrollFollow(options: {\n enabled: () => boolean\n resetKey: () => string | undefined\n threshold: () => number\n initialPosition?: () => ConversationReadingPosition | undefined\n onPositionChange?: (position: ConversationReadingPosition) => void\n}) {\n const [atBottom, setAtBottom] = createSignal(true)\n let scroller: HTMLDivElement | undefined\n let content: HTMLDivElement | undefined\n let stick = true\n let lastWriteTop = -1\n let lastWriteClientHeight = -1\n let readerMovedSinceWrite = false\n let previousTop = -1\n let lastScrollClientHeight = 0\n let pendingRestore: { frame: number; fromTop: number; target: number } | undefined\n let pendingRestoreUserIntent = false\n // A hold is the caller saying it is positioning the view (see `hold`): while one is\n // open and the reader was following, the content's moves are not read as the reader's.\n let holds = 0\n let holdFollowing = false\n let holdIntent = false\n let holdTop = -1\n // Bumped on every identity change, so a hold opened for the previous conversation can\n // neither pin the next one nor close over its state.\n let holdGeneration = 0\n const holding = () => holds > 0 && holdFollowing && !holdIntent\n // Is the view still where the hold left it? The content can move the offset only by\n // clamping it to the end it has now; anything else moved it from outside (a reveal, a\n // search jump), and that is the caller's to keep, so the hold gives way.\n const holdsPosition = (element: HTMLDivElement) => {\n if (!holding()) return false\n const end = bottomTarget(geometry(element))\n if (Math.abs(element.scrollTop - Math.min(holdTop, end)) <= SELF_SCROLL_EPSILON) return true\n holdIntent = true\n return false\n }\n const reportPosition = () => {\n if (scroller)\n untrack(() =>\n options.onPositionChange?.({\n top: scroller!.scrollTop,\n following: options.enabled() && stick,\n })\n )\n }\n onCleanup(reportPosition)\n\n const rememberPosition = (element: HTMLDivElement) => {\n lastWriteTop = element.scrollTop\n lastWriteClientHeight = element.clientHeight\n previousTop = element.scrollTop\n readerMovedSinceWrite = false\n }\n const writePin = (element: HTMLDivElement, target: number) => {\n // Instant writes keep the self-scroll reference synchronized. A smooth\n // animation would produce intermediate positions that resemble user input.\n element.scrollTop = target\n rememberPosition(element)\n }\n const acceptViewportClamp = (element: HTMLDivElement) => {\n if (\n stick &&\n isViewportGrowthClamp({\n geom: geometry(element),\n lastWriteTop,\n lastWriteClientHeight,\n readerMovedSinceWrite,\n })\n )\n rememberPosition(element)\n }\n const pinHeld = (element: HTMLDivElement) => {\n const geom = geometry(element)\n stick = true\n if (!isAtEnd(geom)) writePin(element, bottomTarget(geom))\n // An end the engine already clamped to is accepted as it stands, and is the reference\n // from here on: the write before it is further down than the content now reaches.\n else rememberPosition(element)\n holdTop = element.scrollTop\n setAtBottom(computeAtBottom(geometry(element), options.threshold()))\n reportPosition()\n }\n const pinAuto = () => {\n if (!scroller || !options.enabled() || pendingRestore) return\n if (holdsPosition(scroller)) return pinHeld(scroller)\n acceptViewportClamp(scroller)\n const geom = geometry(scroller)\n const result = evaluateAutoPin({\n stick,\n geom,\n lastWriteTop,\n viewportShrink: lastWriteClientHeight >= 0 ? lastWriteClientHeight - geom.clientHeight : 0,\n })\n stick = result.stick\n if (result.pin) writePin(scroller, result.target)\n else if (result.stick) rememberPosition(scroller)\n setAtBottom(computeAtBottom(geometry(scroller), options.threshold()))\n reportPosition()\n }\n const onScroll = () => {\n if (!scroller) return\n if (pendingRestore) {\n // A reset can clamp the outgoing offset while the incoming rows are\n // shorter. That notification is layout, not reader intent, and must not\n // cancel the snapshot waiting for the new rows to commit.\n const clampedFromTop = Math.min(pendingRestore.fromTop, bottomTarget(geometry(scroller)))\n if (\n pendingRestore.fromTop - clampedFromTop > 1 &&\n Math.abs(scroller.scrollTop - clampedFromTop) <= 1\n )\n return\n // An unexplained offset change can also be a browser clamp. Only actual\n // user scroll intent followed by a non-clamp movement cancels restoration.\n if (!pendingRestoreUserIntent) return\n cancelAnimationFrame(pendingRestore.frame)\n pendingRestore = undefined\n pendingRestoreUserIntent = false\n }\n if (!options.enabled()) {\n reportPosition()\n return\n }\n if (holdsPosition(scroller)) return pinHeld(scroller)\n acceptViewportClamp(scroller)\n const geom = geometry(scroller)\n setAtBottom(computeAtBottom(geom, options.threshold()))\n if (!isSelfScroll(geom.scrollTop, lastWriteTop)) {\n stick = resolveUserScrollStick({\n stick,\n scrollTop: geom.scrollTop,\n prevScrollTop: previousTop,\n geom,\n viewportGrowth: lastScrollClientHeight > 0 ? geom.clientHeight - lastScrollClientHeight : 0,\n })\n if (!stick) {\n lastWriteTop = -1\n lastWriteClientHeight = -1\n }\n }\n // A content-collapse clamp can arrive before ResizeObserver. Keep its new\n // bottom as our reference if viewport growth and more output follow next.\n if (stick && isAtEnd(geom)) rememberPosition(scroller)\n previousTop = geom.scrollTop\n // This baseline belongs only to scroll events, not observer callbacks.\n // Advancing it during resize would erase the viewport-clamp evidence.\n lastScrollClientHeight = geom.clientHeight\n reportPosition()\n }\n const jump = () => {\n if (!scroller || !options.enabled()) return\n stick = true\n writePin(scroller, bottomTarget(geometry(scroller)))\n setAtBottom(true)\n reportPosition()\n // The jump control disappears at the bottom. Return its keyboard focus to\n // the transcript instead of leaving the reader on a detached button.\n scroller.focus({ preventScroll: true })\n }\n\n // Open while the caller moves content under a reader who is following, so a clamp or an\n // insert it causes is not mistaken for the reader leaving the end. It gives way to the\n // reader's own scroll-up (`isScrollUpIntent`) and to anything else that moves the view\n // from outside (a reveal, a search jump); closing it puts a reader who is still following\n // back at the end. A change of conversation invalidates it, so the next conversation's\n // saved position is its own. A reveal or search jump outside a hold is never affected.\n const hold = () => {\n if (holds === 0) {\n holdFollowing = options.enabled() && stick && scroller !== undefined\n holdIntent = false\n holdTop = scroller ? scroller.scrollTop : -1\n }\n holds += 1\n const generation = holdGeneration\n let open = true\n return () => {\n if (!open || generation !== holdGeneration) return\n open = false\n // Asked while this hold still counts: `holdsPosition` is false once none is open.\n const last = holds === 1\n const resume = last && scroller !== undefined && holdsPosition(scroller)\n holds -= 1\n if (!last) return\n holdFollowing = false\n holdIntent = false\n if (resume && scroller && options.enabled() && !pendingRestore) pinHeld(scroller)\n }\n }\n const noteReaderInput = (event: Event) => {\n if (!scroller) return\n const input = readerInput(event, scroller)\n if (!input) return\n // Only reader intent followed by an unexplained offset change cancels a\n // pending snapshot restore; a browser clamp alone must not.\n if (pendingRestore && isScrollIntent(input)) pendingRestoreUserIntent = true\n if (isScrollUpIntent(input)) {\n readerMovedSinceWrite = true\n if (holds > 0) holdIntent = true\n }\n }\n\n createEffect(() => {\n options.resetKey()\n const enabled = options.enabled()\n // Capture before any reporting callback can replace the host snapshot.\n const candidate = untrack(() => options.initialPosition?.())\n const initial =\n candidate && Number.isFinite(candidate.top) && candidate.top >= 0 ? candidate : undefined\n let scheduledRestore: typeof pendingRestore\n let observer: ResizeObserver | undefined\n let contentObserver: MutationObserver | undefined\n onCleanup(() => {\n if (scheduledRestore) {\n cancelAnimationFrame(scheduledRestore.frame)\n if (pendingRestore === scheduledRestore) pendingRestore = undefined\n }\n pendingRestoreUserIntent = false\n observer?.disconnect()\n contentObserver?.disconnect()\n })\n stick = true\n holdGeneration += 1\n holds = 0\n holdFollowing = false\n holdIntent = false\n lastWriteTop = -1\n lastWriteClientHeight = -1\n readerMovedSinceWrite = false\n previousTop = -1\n lastScrollClientHeight = 0\n setAtBottom(true)\n if (!enabled || !scroller) return\n // Snapshot updates must not become another positioning owner. Consume the\n // host's snapshot only at entry/reset, then native reader intent owns follow.\n if (initial) {\n stick = enabled && initial.following\n }\n\n const observe = () => {\n if (!enabled || typeof ResizeObserver === 'undefined') return\n observer = new ResizeObserver(pinAuto)\n observer.observe(scroller!)\n if (content) {\n observer.observe(content)\n if (typeof MutationObserver !== 'undefined') {\n contentObserver = new MutationObserver(() => {\n // A collapsed turn can clamp the offset before any scroll/resize\n // callback. Remember that bottom at the mutation boundary, before\n // a later task grows the viewport or streams more content. This is\n // bookkeeping only: never pin, re-arm, or compete with another owner.\n if (\n !scroller ||\n !options.enabled() ||\n pendingRestore ||\n holds > 0 ||\n !stick ||\n readerMovedSinceWrite ||\n lastWriteTop < 0\n )\n return\n // Read the offset once per mutation batch. This can flush pending\n // layout, but deferring it loses a collapse clamp before a later task\n // grows the viewport/output. Ordinary growth needs no other geometry.\n if (scroller.scrollTop >= lastWriteTop - SELF_SCROLL_EPSILON) return\n if (isAtEnd(geometry(scroller))) rememberPosition(scroller)\n })\n contentObserver.observe(content, { childList: true, characterData: true, subtree: true })\n }\n }\n }\n\n if (initial && !stick) {\n // The identity effect can run before Solid has inserted the new rows. A\n // synchronous write is clamped by the outgoing short transcript, so defer\n // restoration until the browser has committed the new content and layout.\n const restore = {\n frame: 0,\n fromTop: scroller.scrollTop,\n target: initial.top,\n }\n scheduledRestore = restore\n pendingRestore = restore\n pendingRestoreUserIntent = false\n // Keep resize observation alive during the pending frame. Its callback is\n // guarded above, and remains active if user input cancels restoration.\n observe()\n restore.frame = requestAnimationFrame(() => {\n if (pendingRestore !== restore || !scroller) return\n pendingRestore = undefined\n pendingRestoreUserIntent = false\n writePin(scroller, restore.target)\n setAtBottom(computeAtBottom(geometry(scroller), options.threshold()))\n reportPosition()\n })\n return\n }\n\n if (!initial && enabled) writePin(scroller, bottomTarget(geometry(scroller)))\n else if (initial && stick) writePin(scroller, bottomTarget(geometry(scroller)))\n setAtBottom(computeAtBottom(geometry(scroller), options.threshold()))\n reportPosition()\n observe()\n })\n\n return {\n atBottom,\n onScroll,\n jump,\n hold,\n bindScroller: (element: HTMLDivElement) => {\n scroller = element\n for (const type of READER_INPUT_EVENTS)\n element.addEventListener(type, noteReaderInput, { passive: true })\n },\n bindContent: (element: HTMLDivElement) => {\n content = element\n },\n }\n}\n"],"mappings":";;;;;AAyCA,SAAgB,mBAAmB,SAMhC;CACD,MAAM,CAAC,UAAU,eAAe,aAAa,IAAI;CACjD,IAAI;CACJ,IAAI;CACJ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AU;IAC7C,iBAAiB,KAAA;IACjB,2BAA2B;IAC3B,SAAS,UAAU,QAAQ,MAAM;IACjC,YAAY,gBAAgBA,eAAS,QAAQ,GAAG,QAAQ,UAAU,CAAC,CAAC;IACpE,eAAe;GACjB,CAAC;GACD;EACF;EAEA,IAAI,CAAC,WAAW,SAAS,SAAS,UAAU,aAAaA,eAAS,QAAQ,CAAC,CAAC;OACvE,IAAI,WAAW,OAAO,SAAS,UAAU,aAAaA,eAAS,QAAQ,CAAC,CAAC;EAC9E,YAAY,gBAAgBA,eAAS,QAAQ,GAAG,QAAQ,UAAU,CAAC,CAAC;EACpE,eAAe;EACf,QAAQ;CACV,CAAC;CAED,OAAO;EACL;EACA;EACA;EACA;EACA,eAAe,YAA4B;GACzC,WAAW;GACX,KAAK,MAAM,QAAQ,qBACjB,QAAQ,iBAAiB,MAAM,iBAAiB,EAAE,SAAS,KAAK,CAAC;EACrE;EACA,cAAc,YAA4B;GACxC,UAAU;EACZ;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"scroll-follow.js","names":["geometry"],"sources":["../../../src/components/conversation/scroll-follow.ts"],"sourcesContent":["/*\n * Substantially translated from KiroCrew website/src/app-sdk/useChatScrollFollow.ts,\n * pinned at 283e136c0f902e965a535a7c9548c57c7504fed0.\n * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n * This product includes software developed at Amazon.com, Inc. (https://www.amazon.com/).\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy at http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software distributed\n * under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR\n * CONDITIONS OF ANY KIND, either express or implied. See the License for the\n * specific language governing permissions and limitations under the License.\n * Solid owner effects replace React refs/effects; native geometry and the\n * content/viewport observer contract are retained. No session store is copied.\n */\nimport { createEffect, createSignal, onCleanup, untrack } from 'solid-js'\nimport {\n bottomTarget,\n computeAtBottom,\n isAtEnd,\n scrollGeometry as geometry,\n} from '#lib/scroll-geometry'\nimport {\n evaluateAutoPin,\n isScrollIntent,\n isScrollUpIntent,\n isSelfScroll,\n isViewportGrowthClamp,\n READER_INPUT_EVENTS,\n readerInput,\n resolveUserScrollStick,\n SELF_SCROLL_EPSILON,\n} from './scroll-follow-core'\n\n/** Where the reader is in a transcript: the native offset, and whether they are following its end. */\nexport type ConversationReadingPosition = Readonly<{ top: number; following: boolean }>\n\n/** Opens a hold on the follow position (see `createScrollFollow`); the function it returns closes it. */\nexport type ScrollFollowHold = () => () => void\n\n/** Internal plain-scroller binding; host-owned restoration can disable follow. */\nexport function createScrollFollow(options: {\n enabled: () => boolean\n resetKey: () => string | undefined\n threshold: () => number\n initialPosition?: () => ConversationReadingPosition | undefined\n onPositionChange?: (position: ConversationReadingPosition) => void\n}) {\n const [atBottom, setAtBottom] = createSignal(true)\n let scroller: HTMLDivElement | undefined\n let content: HTMLDivElement | undefined\n let stick = true\n let lastWriteTop = -1\n let lastWriteClientHeight = -1\n let readerMovedSinceWrite = false\n let previousTop = -1\n let lastScrollClientHeight = 0\n // The content's last row when the offset was last taken as the reader's own. Rows added\n // after it are what an append put in since.\n let endRow: Element | null = null\n let pendingRestore: { frame: number; fromTop: number; target: number } | undefined\n let pendingRestoreUserIntent = false\n // A hold is the caller saying it is positioning the view (see `hold`): while one is\n // open and the reader was following, the content's moves are not read as the reader's.\n let holds = 0\n let holdFollowing = false\n let holdIntent = false\n let holdTop = -1\n // Bumped on every identity change, so a hold opened for the previous conversation can\n // neither pin the next one nor close over its state.\n let holdGeneration = 0\n const holding = () => holds > 0 && holdFollowing && !holdIntent\n // Is the view still where the hold left it? The content can move the offset only by\n // clamping it to the end it has now; anything else moved it from outside (a reveal, a\n // search jump), and that is the caller's to keep, so the hold gives way.\n const holdsPosition = (element: HTMLDivElement) => {\n if (!holding()) return false\n const end = bottomTarget(geometry(element))\n if (Math.abs(element.scrollTop - Math.min(holdTop, end)) <= SELF_SCROLL_EPSILON) return true\n holdIntent = true\n return false\n }\n const reportPosition = () => {\n if (scroller)\n untrack(() =>\n options.onPositionChange?.({\n top: scroller!.scrollTop,\n following: options.enabled() && stick,\n })\n )\n }\n onCleanup(reportPosition)\n\n const rememberPosition = (element: HTMLDivElement) => {\n endRow = content?.lastElementChild ?? null\n lastWriteTop = element.scrollTop\n lastWriteClientHeight = element.clientHeight\n previousTop = element.scrollTop\n readerMovedSinceWrite = false\n }\n const writePin = (element: HTMLDivElement, target: number) => {\n // Instant writes keep the self-scroll reference synchronized. A smooth\n // animation would produce intermediate positions that resemble user input.\n element.scrollTop = target\n rememberPosition(element)\n }\n const acceptViewportClamp = (element: HTMLDivElement) => {\n if (\n stick &&\n isViewportGrowthClamp({\n geom: geometry(element),\n lastWriteTop,\n lastWriteClientHeight,\n readerMovedSinceWrite,\n })\n )\n rememberPosition(element)\n }\n // How much the rows appended after `endRow` add to the content's height, or 0 when the\n // content changed in any other way. A tail append never moves the offset, so taking it\n // off the height gives the end the reader had before it.\n const appendedHeight = () => {\n const last = content?.lastElementChild\n if (!last || !endRow || last === endRow || endRow.parentElement !== content) return 0\n return Math.max(0, last.getBoundingClientRect().bottom - endRow.getBoundingClientRect().bottom)\n }\n const pinHeld = (element: HTMLDivElement) => {\n const geom = geometry(element)\n stick = true\n if (!isAtEnd(geom)) writePin(element, bottomTarget(geom))\n // An end the engine already clamped to is accepted as it stands, and is the reference\n // from here on: the write before it is further down than the content now reaches.\n else rememberPosition(element)\n holdTop = element.scrollTop\n setAtBottom(computeAtBottom(geometry(element), options.threshold()))\n reportPosition()\n }\n const pinAuto = () => {\n if (!scroller || !options.enabled() || pendingRestore) return\n if (holdsPosition(scroller)) return pinHeld(scroller)\n acceptViewportClamp(scroller)\n const geom = geometry(scroller)\n const result = evaluateAutoPin({\n stick,\n geom,\n lastWriteTop,\n viewportShrink: lastWriteClientHeight >= 0 ? lastWriteClientHeight - geom.clientHeight : 0,\n })\n stick = result.stick\n if (result.pin) writePin(scroller, result.target)\n else if (result.stick) rememberPosition(scroller)\n setAtBottom(computeAtBottom(geometry(scroller), options.threshold()))\n reportPosition()\n }\n const onScroll = () => {\n if (!scroller) return\n if (pendingRestore) {\n // A reset can clamp the outgoing offset while the incoming rows are\n // shorter. That notification is layout, not reader intent, and must not\n // cancel the snapshot waiting for the new rows to commit.\n const clampedFromTop = Math.min(pendingRestore.fromTop, bottomTarget(geometry(scroller)))\n if (\n pendingRestore.fromTop - clampedFromTop > 1 &&\n Math.abs(scroller.scrollTop - clampedFromTop) <= 1\n )\n return\n // An unexplained offset change can also be a browser clamp. Only actual\n // user scroll intent followed by a non-clamp movement cancels restoration.\n if (!pendingRestoreUserIntent) return\n cancelAnimationFrame(pendingRestore.frame)\n pendingRestore = undefined\n pendingRestoreUserIntent = false\n }\n if (!options.enabled()) {\n reportPosition()\n return\n }\n if (holdsPosition(scroller)) return pinHeld(scroller)\n acceptViewportClamp(scroller)\n const geom = geometry(scroller)\n setAtBottom(computeAtBottom(geom, options.threshold()))\n if (!isSelfScroll(geom.scrollTop, lastWriteTop)) {\n stick = resolveUserScrollStick({\n stick,\n scrollTop: geom.scrollTop,\n prevScrollTop: previousTop,\n geom,\n viewportGrowth: lastScrollClientHeight > 0 ? geom.clientHeight - lastScrollClientHeight : 0,\n })\n if (!stick) {\n lastWriteTop = -1\n lastWriteClientHeight = -1\n }\n }\n // A content-collapse clamp can arrive before ResizeObserver. Keep its new\n // bottom as our reference if viewport growth and more output follow next.\n if (stick && isAtEnd(geom)) rememberPosition(scroller)\n previousTop = geom.scrollTop\n // This baseline belongs only to scroll events, not observer callbacks.\n // Advancing it during resize would erase the viewport-clamp evidence.\n lastScrollClientHeight = geom.clientHeight\n reportPosition()\n }\n const jump = () => {\n if (!scroller || !options.enabled()) return\n stick = true\n writePin(scroller, bottomTarget(geometry(scroller)))\n setAtBottom(true)\n reportPosition()\n // The jump control disappears at the bottom. Return its keyboard focus to\n // the transcript instead of leaving the reader on a detached button.\n scroller.focus({ preventScroll: true })\n }\n\n // Open while the caller moves content under a reader who is following, so a clamp or an\n // insert it causes is not mistaken for the reader leaving the end. It gives way to the\n // reader's own scroll-up (`isScrollUpIntent`) and to anything else that moves the view\n // from outside (a reveal, a search jump); closing it puts a reader who is still following\n // back at the end. A change of conversation invalidates it, so the next conversation's\n // saved position is its own. A reveal or search jump outside a hold is never affected.\n const hold = () => {\n if (holds === 0) {\n holdFollowing = options.enabled() && stick && scroller !== undefined\n holdIntent = false\n holdTop = scroller ? scroller.scrollTop : -1\n }\n holds += 1\n const generation = holdGeneration\n let open = true\n return () => {\n if (!open || generation !== holdGeneration) return\n open = false\n // Asked while this hold still counts: `holdsPosition` is false once none is open.\n const last = holds === 1\n const resume = last && scroller !== undefined && holdsPosition(scroller)\n holds -= 1\n if (!last) return\n holdFollowing = false\n holdIntent = false\n if (resume && scroller && options.enabled() && !pendingRestore) pinHeld(scroller)\n }\n }\n const noteReaderInput = (event: Event) => {\n if (!scroller) return\n const input = readerInput(event, scroller)\n if (!input) return\n // Only reader intent followed by an unexplained offset change cancels a\n // pending snapshot restore; a browser clamp alone must not.\n if (pendingRestore && isScrollIntent(input)) pendingRestoreUserIntent = true\n if (isScrollUpIntent(input)) {\n readerMovedSinceWrite = true\n if (holds > 0) holdIntent = true\n }\n }\n\n createEffect(() => {\n options.resetKey()\n const enabled = options.enabled()\n // Capture before any reporting callback can replace the host snapshot.\n const candidate = untrack(() => options.initialPosition?.())\n const initial =\n candidate && Number.isFinite(candidate.top) && candidate.top >= 0 ? candidate : undefined\n let scheduledRestore: typeof pendingRestore\n let observer: ResizeObserver | undefined\n let contentObserver: MutationObserver | undefined\n onCleanup(() => {\n if (scheduledRestore) {\n cancelAnimationFrame(scheduledRestore.frame)\n if (pendingRestore === scheduledRestore) pendingRestore = undefined\n }\n pendingRestoreUserIntent = false\n observer?.disconnect()\n contentObserver?.disconnect()\n })\n stick = true\n holdGeneration += 1\n holds = 0\n holdFollowing = false\n holdIntent = false\n lastWriteTop = -1\n lastWriteClientHeight = -1\n readerMovedSinceWrite = false\n previousTop = -1\n lastScrollClientHeight = 0\n setAtBottom(true)\n if (!enabled || !scroller) return\n // Snapshot updates must not become another positioning owner. Consume the\n // host's snapshot only at entry/reset, then native reader intent owns follow.\n if (initial) {\n stick = enabled && initial.following\n }\n\n const observe = () => {\n if (!enabled || typeof ResizeObserver === 'undefined') return\n observer = new ResizeObserver(pinAuto)\n observer.observe(scroller!)\n if (content) {\n observer.observe(content)\n if (typeof MutationObserver !== 'undefined') {\n contentObserver = new MutationObserver(() => {\n // A collapsed turn can clamp the offset before any scroll/resize\n // callback. Remember that bottom at the mutation boundary, before\n // a later task grows the viewport or streams more content. This is\n // bookkeeping only: never pin, re-arm, or compete with another owner.\n if (\n !scroller ||\n !options.enabled() ||\n pendingRestore ||\n holds > 0 ||\n !stick ||\n readerMovedSinceWrite ||\n lastWriteTop < 0\n )\n return\n // Read the offset once per mutation batch. This can flush pending\n // layout, but deferring it loses a collapse clamp before a later task\n // grows the viewport/output. Ordinary growth needs no other geometry.\n if (scroller.scrollTop >= lastWriteTop - SELF_SCROLL_EPSILON) return\n const geom = geometry(scroller)\n if (isAtEnd(geom)) return rememberPosition(scroller)\n // Rows that size themselves late can shrink the content, and a layout read\n // anywhere clamps the offset at once, while the scroll event and resize\n // callback that report it wait for the next frame. A turn appended before\n // that frame (WebKit paints late under load) leaves the offset at the end the\n // content had without the new turn, which those callbacks would then measure\n // against the grown content and take for the reader scrolling up. An offset\n // exactly at that earlier end is the clamp; a reveal lands anywhere else.\n const grown = appendedHeight()\n if (grown > 0 && isAtEnd({ ...geom, scrollHeight: geom.scrollHeight - grown }))\n rememberPosition(scroller)\n })\n contentObserver.observe(content, { childList: true, characterData: true, subtree: true })\n }\n }\n }\n\n if (initial && !stick) {\n // The identity effect can run before Solid has inserted the new rows. A\n // synchronous write is clamped by the outgoing short transcript, so defer\n // restoration until the browser has committed the new content and layout.\n const restore = {\n frame: 0,\n fromTop: scroller.scrollTop,\n target: initial.top,\n }\n scheduledRestore = restore\n pendingRestore = restore\n pendingRestoreUserIntent = false\n // Keep resize observation alive during the pending frame. Its callback is\n // guarded above, and remains active if user input cancels restoration.\n observe()\n restore.frame = requestAnimationFrame(() => {\n if (pendingRestore !== restore || !scroller) return\n pendingRestore = undefined\n pendingRestoreUserIntent = false\n writePin(scroller, restore.target)\n setAtBottom(computeAtBottom(geometry(scroller), options.threshold()))\n reportPosition()\n })\n return\n }\n\n if (!initial && enabled) writePin(scroller, bottomTarget(geometry(scroller)))\n else if (initial && stick) writePin(scroller, bottomTarget(geometry(scroller)))\n setAtBottom(computeAtBottom(geometry(scroller), options.threshold()))\n reportPosition()\n observe()\n })\n\n return {\n atBottom,\n onScroll,\n jump,\n hold,\n bindScroller: (element: HTMLDivElement) => {\n scroller = element\n for (const type of READER_INPUT_EVENTS)\n element.addEventListener(type, noteReaderInput, { passive: true })\n },\n bindContent: (element: HTMLDivElement) => {\n content = element\n },\n }\n}\n"],"mappings":";;;;;AAyCA,SAAgB,mBAAmB,SAMhC;CACD,MAAM,CAAC,UAAU,eAAe,aAAa,IAAI;CACjD,IAAI;CACJ,IAAI;CACJ,IAAI,QAAQ;CACZ,IAAI,eAAe;CACnB,IAAI,wBAAwB;CAC5B,IAAI,wBAAwB;CAC5B,IAAI,cAAc;CAClB,IAAI,yBAAyB;CAG7B,IAAI,SAAyB;CAC7B,IAAI;CACJ,IAAI,2BAA2B;CAG/B,IAAI,QAAQ;CACZ,IAAI,gBAAgB;CACpB,IAAI,aAAa;CACjB,IAAI,UAAU;CAGd,IAAI,iBAAiB;CACrB,MAAM,gBAAgB,QAAQ,KAAK,iBAAiB,CAAC;CAIrD,MAAM,iBAAiB,YAA4B;EACjD,IAAI,CAAC,QAAQ,GAAG,OAAO;EACvB,MAAM,MAAM,aAAaA,eAAS,OAAO,CAAC;EAC1C,IAAI,KAAK,IAAI,QAAQ,YAAY,KAAK,IAAI,SAAS,GAAG,CAAC,KAAA,GAA0B,OAAO;E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BAAgBA,eAAS,QAAQ,GAAG,QAAQ,UAAU,CAAC,CAAC;IACpE,eAAe;GACjB,CAAC;GACD;EACF;EAEA,IAAI,CAAC,WAAW,SAAS,SAAS,UAAU,aAAaA,eAAS,QAAQ,CAAC,CAAC;OACvE,IAAI,WAAW,OAAO,SAAS,UAAU,aAAaA,eAAS,QAAQ,CAAC,CAAC;EAC9E,YAAY,gBAAgBA,eAAS,QAAQ,GAAG,QAAQ,UAAU,CAAC,CAAC;EACpE,eAAe;EACf,QAAQ;CACV,CAAC;CAED,OAAO;EACL;EACA;EACA;EACA;EACA,eAAe,YAA4B;GACzC,WAAW;GACX,KAAK,MAAM,QAAQ,qBACjB,QAAQ,iBAAiB,MAAM,iBAAiB,EAAE,SAAS,KAAK,CAAC;EACrE;EACA,cAAc,YAA4B;GACxC,UAAU;EACZ;CACF;AACF"}
|
|
@@ -55,6 +55,9 @@ export function createScrollFollow(options: {
|
|
|
55
55
|
let readerMovedSinceWrite = false
|
|
56
56
|
let previousTop = -1
|
|
57
57
|
let lastScrollClientHeight = 0
|
|
58
|
+
// The content's last row when the offset was last taken as the reader's own. Rows added
|
|
59
|
+
// after it are what an append put in since.
|
|
60
|
+
let endRow: Element | null = null
|
|
58
61
|
let pendingRestore: { frame: number; fromTop: number; target: number } | undefined
|
|
59
62
|
let pendingRestoreUserIntent = false
|
|
60
63
|
// A hold is the caller saying it is positioning the view (see `hold`): while one is
|
|
@@ -89,6 +92,7 @@ export function createScrollFollow(options: {
|
|
|
89
92
|
onCleanup(reportPosition)
|
|
90
93
|
|
|
91
94
|
const rememberPosition = (element: HTMLDivElement) => {
|
|
95
|
+
endRow = content?.lastElementChild ?? null
|
|
92
96
|
lastWriteTop = element.scrollTop
|
|
93
97
|
lastWriteClientHeight = element.clientHeight
|
|
94
98
|
previousTop = element.scrollTop
|
|
@@ -112,6 +116,14 @@ export function createScrollFollow(options: {
|
|
|
112
116
|
)
|
|
113
117
|
rememberPosition(element)
|
|
114
118
|
}
|
|
119
|
+
// How much the rows appended after `endRow` add to the content's height, or 0 when the
|
|
120
|
+
// content changed in any other way. A tail append never moves the offset, so taking it
|
|
121
|
+
// off the height gives the end the reader had before it.
|
|
122
|
+
const appendedHeight = () => {
|
|
123
|
+
const last = content?.lastElementChild
|
|
124
|
+
if (!last || !endRow || last === endRow || endRow.parentElement !== content) return 0
|
|
125
|
+
return Math.max(0, last.getBoundingClientRect().bottom - endRow.getBoundingClientRect().bottom)
|
|
126
|
+
}
|
|
115
127
|
const pinHeld = (element: HTMLDivElement) => {
|
|
116
128
|
const geom = geometry(element)
|
|
117
129
|
stick = true
|
|
@@ -304,7 +316,18 @@ export function createScrollFollow(options: {
|
|
|
304
316
|
// layout, but deferring it loses a collapse clamp before a later task
|
|
305
317
|
// grows the viewport/output. Ordinary growth needs no other geometry.
|
|
306
318
|
if (scroller.scrollTop >= lastWriteTop - SELF_SCROLL_EPSILON) return
|
|
307
|
-
|
|
319
|
+
const geom = geometry(scroller)
|
|
320
|
+
if (isAtEnd(geom)) return rememberPosition(scroller)
|
|
321
|
+
// Rows that size themselves late can shrink the content, and a layout read
|
|
322
|
+
// anywhere clamps the offset at once, while the scroll event and resize
|
|
323
|
+
// callback that report it wait for the next frame. A turn appended before
|
|
324
|
+
// that frame (WebKit paints late under load) leaves the offset at the end the
|
|
325
|
+
// content had without the new turn, which those callbacks would then measure
|
|
326
|
+
// against the grown content and take for the reader scrolling up. An offset
|
|
327
|
+
// exactly at that earlier end is the clamp; a reveal lands anywhere else.
|
|
328
|
+
const grown = appendedHeight()
|
|
329
|
+
if (grown > 0 && isAtEnd({ ...geom, scrollHeight: geom.scrollHeight - grown }))
|
|
330
|
+
rememberPosition(scroller)
|
|
308
331
|
})
|
|
309
332
|
contentObserver.observe(content, { childList: true, characterData: true, subtree: true })
|
|
310
333
|
}
|
package/package.json
CHANGED
|
@@ -55,6 +55,9 @@ export function createScrollFollow(options: {
|
|
|
55
55
|
let readerMovedSinceWrite = false
|
|
56
56
|
let previousTop = -1
|
|
57
57
|
let lastScrollClientHeight = 0
|
|
58
|
+
// The content's last row when the offset was last taken as the reader's own. Rows added
|
|
59
|
+
// after it are what an append put in since.
|
|
60
|
+
let endRow: Element | null = null
|
|
58
61
|
let pendingRestore: { frame: number; fromTop: number; target: number } | undefined
|
|
59
62
|
let pendingRestoreUserIntent = false
|
|
60
63
|
// A hold is the caller saying it is positioning the view (see `hold`): while one is
|
|
@@ -89,6 +92,7 @@ export function createScrollFollow(options: {
|
|
|
89
92
|
onCleanup(reportPosition)
|
|
90
93
|
|
|
91
94
|
const rememberPosition = (element: HTMLDivElement) => {
|
|
95
|
+
endRow = content?.lastElementChild ?? null
|
|
92
96
|
lastWriteTop = element.scrollTop
|
|
93
97
|
lastWriteClientHeight = element.clientHeight
|
|
94
98
|
previousTop = element.scrollTop
|
|
@@ -112,6 +116,14 @@ export function createScrollFollow(options: {
|
|
|
112
116
|
)
|
|
113
117
|
rememberPosition(element)
|
|
114
118
|
}
|
|
119
|
+
// How much the rows appended after `endRow` add to the content's height, or 0 when the
|
|
120
|
+
// content changed in any other way. A tail append never moves the offset, so taking it
|
|
121
|
+
// off the height gives the end the reader had before it.
|
|
122
|
+
const appendedHeight = () => {
|
|
123
|
+
const last = content?.lastElementChild
|
|
124
|
+
if (!last || !endRow || last === endRow || endRow.parentElement !== content) return 0
|
|
125
|
+
return Math.max(0, last.getBoundingClientRect().bottom - endRow.getBoundingClientRect().bottom)
|
|
126
|
+
}
|
|
115
127
|
const pinHeld = (element: HTMLDivElement) => {
|
|
116
128
|
const geom = geometry(element)
|
|
117
129
|
stick = true
|
|
@@ -304,7 +316,18 @@ export function createScrollFollow(options: {
|
|
|
304
316
|
// layout, but deferring it loses a collapse clamp before a later task
|
|
305
317
|
// grows the viewport/output. Ordinary growth needs no other geometry.
|
|
306
318
|
if (scroller.scrollTop >= lastWriteTop - SELF_SCROLL_EPSILON) return
|
|
307
|
-
|
|
319
|
+
const geom = geometry(scroller)
|
|
320
|
+
if (isAtEnd(geom)) return rememberPosition(scroller)
|
|
321
|
+
// Rows that size themselves late can shrink the content, and a layout read
|
|
322
|
+
// anywhere clamps the offset at once, while the scroll event and resize
|
|
323
|
+
// callback that report it wait for the next frame. A turn appended before
|
|
324
|
+
// that frame (WebKit paints late under load) leaves the offset at the end the
|
|
325
|
+
// content had without the new turn, which those callbacks would then measure
|
|
326
|
+
// against the grown content and take for the reader scrolling up. An offset
|
|
327
|
+
// exactly at that earlier end is the clamp; a reveal lands anywhere else.
|
|
328
|
+
const grown = appendedHeight()
|
|
329
|
+
if (grown > 0 && isAtEnd({ ...geom, scrollHeight: geom.scrollHeight - grown }))
|
|
330
|
+
rememberPosition(scroller)
|
|
308
331
|
})
|
|
309
332
|
contentObserver.observe(content, { childList: true, characterData: true, subtree: true })
|
|
310
333
|
}
|