File size: 8,406 Bytes
e079a73 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | import type {DisplayProfile} from "../profiles/types"
import {TextMeasurer} from "../measurer/TextMeasurer"
import {TextWrapper} from "../wrapper/TextWrapper"
import type {WrapOptions, WrapResult} from "../wrapper/types"
/**
* Scroll position information.
*/
export interface ScrollPosition {
/** Current scroll offset (0 = top) */
offset: number
/** Total number of lines in content */
totalLines: number
/** Number of visible lines (viewport size) */
visibleLines: number
/** Maximum scroll offset */
maxOffset: number
/** Whether we're at the top */
atTop: boolean
/** Whether we're at the bottom */
atBottom: boolean
/** Scroll percentage (0-100) */
scrollPercent: number
}
/**
* Visible content from the scroll view.
*/
export interface ScrollViewport {
/** Lines currently visible */
lines: string[]
/** Scroll position info */
position: ScrollPosition
/** Whether content was truncated during wrapping */
contentTruncated: boolean
}
/**
* ScrollView provides a scrollable viewport into long wrapped text.
*
* Unlike pagination (discrete pages), scrolling allows continuous
* movement through content line by line.
*
* @example
* ```typescript
* const scrollView = new ScrollView(measurer, wrapper)
* scrollView.setContent("Very long text that wraps to many lines...")
*
* // Get initial view (top)
* let view = scrollView.getViewport()
* console.log(view.lines) // First 5 lines
*
* // Scroll down
* scrollView.scrollDown(2) // Move down 2 lines
* view = scrollView.getViewport()
*
* // Scroll to bottom
* scrollView.scrollToBottom()
*
* // Scroll to specific position
* scrollView.scrollTo(10) // Line 10 at top of viewport
* ```
*/
export class ScrollView {
private readonly measurer: TextMeasurer
private readonly wrapper: TextWrapper
private readonly profile: DisplayProfile
private readonly viewportSize: number
private allLines: string[] = []
private wrapResult: WrapResult | null = null
private scrollOffset: number = 0
constructor(
measurer: TextMeasurer,
wrapper: TextWrapper,
viewportSize?: number
) {
this.measurer = measurer
this.wrapper = wrapper
this.profile = measurer.getProfile()
this.viewportSize = viewportSize ?? this.profile.maxLines
}
/**
* Set the content to display in the scroll view.
* Wraps the text and resets scroll position to top.
*
* @param text - Text content to display
* @param options - Optional wrap options
*/
setContent(text: string, options?: Omit<WrapOptions, "maxLines">): void {
// Wrap without line limit to get all lines
this.wrapResult = this.wrapper.wrap(text, {
...options,
maxLines: Infinity,
maxBytes: Infinity,
})
this.allLines = this.wrapResult.lines
this.scrollOffset = 0
}
/**
* Append content to the existing scroll view.
* Useful for streaming/live content like captions.
*
* @param text - Text to append
* @param options - Optional wrap options
* @param autoScroll - If true, scroll to show new content (default: true)
*/
appendContent(
text: string,
options?: Omit<WrapOptions, "maxLines">,
autoScroll: boolean = true
): void {
const wasAtBottom = this.isAtBottom()
// Wrap the new text
const newResult = this.wrapper.wrap(text, {
...options,
maxLines: Infinity,
maxBytes: Infinity,
})
// Append new lines
this.allLines = [...this.allLines, ...newResult.lines]
// Auto-scroll to bottom if we were already there
if (autoScroll && wasAtBottom) {
this.scrollToBottom()
}
}
/**
* Get the current viewport (visible lines).
*/
getViewport(): ScrollViewport {
const visibleLines = this.allLines.slice(
this.scrollOffset,
this.scrollOffset + this.viewportSize
)
// Pad to viewport size if needed
while (visibleLines.length < this.viewportSize) {
visibleLines.push("")
}
return {
lines: visibleLines,
position: this.getPosition(),
contentTruncated: this.wrapResult?.truncated ?? false,
}
}
/**
* Get current scroll position information.
*/
getPosition(): ScrollPosition {
const totalLines = this.allLines.length
const maxOffset = Math.max(0, totalLines - this.viewportSize)
return {
offset: this.scrollOffset,
totalLines,
visibleLines: this.viewportSize,
maxOffset,
atTop: this.scrollOffset === 0,
atBottom: this.scrollOffset >= maxOffset,
scrollPercent: maxOffset > 0 ? Math.round((this.scrollOffset / maxOffset) * 100) : 100,
}
}
/**
* Scroll to a specific line offset.
*
* @param offset - Line offset (0 = top)
*/
scrollTo(offset: number): void {
const maxOffset = Math.max(0, this.allLines.length - this.viewportSize)
this.scrollOffset = Math.max(0, Math.min(offset, maxOffset))
}
/**
* Scroll down by a number of lines.
*
* @param lines - Number of lines to scroll (default: 1)
*/
scrollDown(lines: number = 1): void {
this.scrollTo(this.scrollOffset + lines)
}
/**
* Scroll up by a number of lines.
*
* @param lines - Number of lines to scroll (default: 1)
*/
scrollUp(lines: number = 1): void {
this.scrollTo(this.scrollOffset - lines)
}
/**
* Scroll down by one viewport (page down).
*/
pageDown(): void {
this.scrollDown(this.viewportSize)
}
/**
* Scroll up by one viewport (page up).
*/
pageUp(): void {
this.scrollUp(this.viewportSize)
}
/**
* Scroll to the top.
*/
scrollToTop(): void {
this.scrollOffset = 0
}
/**
* Scroll to the bottom.
*/
scrollToBottom(): void {
const maxOffset = Math.max(0, this.allLines.length - this.viewportSize)
this.scrollOffset = maxOffset
}
/**
* Scroll to show a specific line in the viewport.
*
* @param lineIndex - The line index to show
* @param position - Where in viewport: 'top', 'center', 'bottom' (default: 'top')
*/
scrollToLine(lineIndex: number, position: "top" | "center" | "bottom" = "top"): void {
let targetOffset: number
switch (position) {
case "top":
targetOffset = lineIndex
break
case "center":
targetOffset = lineIndex - Math.floor(this.viewportSize / 2)
break
case "bottom":
targetOffset = lineIndex - this.viewportSize + 1
break
}
this.scrollTo(targetOffset)
}
/**
* Scroll by a percentage of total content.
*
* @param percent - Percentage (0-100)
*/
scrollToPercent(percent: number): void {
const maxOffset = Math.max(0, this.allLines.length - this.viewportSize)
const targetOffset = Math.round((percent / 100) * maxOffset)
this.scrollTo(targetOffset)
}
/**
* Check if currently at the top.
*/
isAtTop(): boolean {
return this.scrollOffset === 0
}
/**
* Check if currently at the bottom.
*/
isAtBottom(): boolean {
const maxOffset = Math.max(0, this.allLines.length - this.viewportSize)
return this.scrollOffset >= maxOffset
}
/**
* Check if content is scrollable (more lines than viewport).
*/
isScrollable(): boolean {
return this.allLines.length > this.viewportSize
}
/**
* Get all lines (not just visible).
*/
getAllLines(): string[] {
return [...this.allLines]
}
/**
* Get total line count.
*/
getTotalLines(): number {
return this.allLines.length
}
/**
* Get the viewport size.
*/
getViewportSize(): number {
return this.viewportSize
}
/**
* Clear all content and reset scroll position.
*/
clear(): void {
this.allLines = []
this.wrapResult = null
this.scrollOffset = 0
}
/**
* Get the measurer instance.
*/
getMeasurer(): TextMeasurer {
return this.measurer
}
/**
* Get the wrapper instance.
*/
getWrapper(): TextWrapper {
return this.wrapper
}
/**
* Get the display profile.
*/
getProfile(): DisplayProfile {
return this.profile
}
}
|