@@ -2,6 +2,8 @@ import type { Color, StyleProps, TextInputProps } from "@cel-tui/types";
22 import type { Cell } from "./cell-buffer.js";
33 import { CellBuffer } from "./cell-buffer.js";
44 import type { LayoutNode, Rect } from "./layout.js";
5+import { getMaxScrollOffset } from "./scroll.js";
6+import { layoutText } from "./text-layout.js";
57 import { visibleWidth } from "./width.js";
68
79 const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
@@ -104,32 +106,6 @@ function fillBackground(
104106 }
105107 }
106108
107-/**
108- * Compute the maximum scroll offset for a scrollable container.
109- * This is the content size minus the viewport size along the main axis.
110- */
111-function computeMaxScrollOffset(ln: LayoutNode, isVertical: boolean): number {
112- const { rect, children } = ln;
113- const props = ln.node.type !== "text" ? ln.node.props : null;
114- const padX = (props as any)?.padding?.x ?? 0;
115- const padY = (props as any)?.padding?.y ?? 0;
116-
117- if (isVertical) {
118- let contentHeight = 0;
119- for (const child of children) {
120- const childBottom = child.rect.y + child.rect.height - rect.y;
121- if (childBottom > contentHeight) contentHeight = childBottom;
122- }
123- return Math.max(0, contentHeight + padY - rect.height);
124- }
125- let contentWidth = 0;
126- for (const child of children) {
127- const childRight = child.rect.x + child.rect.width - rect.x;
128- if (childRight > contentWidth) contentWidth = childRight;
129- }
130- return Math.max(0, contentWidth + padX - rect.width);
131-}
132-
133109 function paintLayoutNode(
134110 ln: LayoutNode,
135111 buf: CellBuffer,
@@ -203,7 +179,7 @@ function paintLayoutNode(
203179 if (isScrollable) {
204180 const raw = containerProps.scrollOffset ?? 0;
205181 // Clamp to valid range so apps can pass large values to mean "scroll to end"
206- const maxOffset = computeMaxScrollOffset(ln, isVertical);
182+ const maxOffset = getMaxScrollOffset(ln);
207183 scrollOffset = Math.max(0, Math.min(raw, maxOffset));
208184 }
209185
@@ -434,26 +410,11 @@ function paintText(
434410 text = content.repeat(props.repeat);
435411 }
436412
437- // Split into lines
438- const rawLines = text.split("\n");
439-
440- // Word-wrap if enabled
441- const lines: string[] = [];
442- if (props.wrap === "word") {
443- for (const rawLine of rawLines) {
444- if (visibleWidth(rawLine) <= w) {
445- lines.push(rawLine);
446- } else {
447- wrapLine(rawLine, w, lines);
448- }
449- }
450- } else {
451- lines.push(...rawLines);
452- }
413+ const textLayout = layoutText(text, w, props.wrap ?? "none");
453414
454415 // Paint lines, clipped to rect (grapheme-aware)
455- for (let row = 0; row < lines.length && row < h; row++) {
456- const line = lines[row]!;
416+ for (let row = 0; row < textLayout.lineCount && row < h; row++) {
417+ const line = textLayout.lines[row]!.text;
457418 paintLineGraphemes(line, x, y + row, w, clipRect, props, buf);
458419 }
459420 }
@@ -510,22 +471,14 @@ function paintTextInput(
510471 return;
511472 }
512473
513- // Word-wrap value (always on for TextInput) using content width
514- const lines: string[] = [];
515- for (const rawLine of value.split("\n")) {
516- if (visibleWidth(rawLine) <= cw) {
517- lines.push(rawLine);
518- } else {
519- wrapLine(rawLine, cw, lines);
520- }
521- }
474+ const textLayout = layoutText(value, cw, "word");
522475
523476 // Framework-managed scroll: auto-scroll to keep cursor visible
524477 let scrollOffset = getTextInputScroll(props);
525478
526479 if (props.focused) {
527480 const cursorOffset = getTextInputCursor(props);
528- const cursorPos = offsetToWrappedPos(value, cursorOffset, cw);
481+ const cursorPos = textLayout.offsetToPosition(cursorOffset);
529482 // Scroll down if cursor is below viewport
530483 if (cursorPos.line >= scrollOffset + ch) {
531484 scrollOffset = cursorPos.line - ch + 1;
@@ -540,8 +493,8 @@ function paintTextInput(
540493 // Paint visible lines (grapheme-aware) in content area
541494 for (let row = 0; row < ch; row++) {
542495 const lineIdx = scrollOffset + row;
543- if (lineIdx >= lines.length) break;
544- const line = lines[lineIdx]!;
496+ if (lineIdx >= textLayout.lineCount) break;
497+ const line = textLayout.lines[lineIdx]!.text;
545498 paintLineGraphemes(line, cx, cy + row, cw, clipRect, props, buf);
546499 }
547500
@@ -551,7 +504,7 @@ function paintTextInput(
551504 // for blinking.
552505 if (props.focused) {
553506 const cursorOffset = getTextInputCursor(props);
554- const pos = offsetToWrappedPos(value, cursorOffset, cw);
507+ const pos = textLayout.offsetToPosition(cursorOffset);
555508 const screenRow = pos.line - scrollOffset;
556509 if (screenRow >= 0 && screenRow < ch && pos.col < cw) {
557510 const absX = cx + pos.col;
@@ -581,43 +534,6 @@ function paintTextInput(
581534 }
582535 }
583536
584-/**
585- * Map a cursor offset in the raw value to a (line, col) position
586- * in the word-wrapped output.
587- */
588-function offsetToWrappedPos(
589- value: string,
590- cursor: number,
591- width: number,
592-): { line: number; col: number } {
593- const rawLines = value.split("\n");
594- let offset = 0;
595- let wrappedLine = 0;
596-
597- for (const rawLine of rawLines) {
598- if (cursor <= offset + rawLine.length) {
599- // Cursor is in this raw line
600- const colInRaw = cursor - offset;
601- if (width <= 0) return { line: wrappedLine, col: colInRaw };
602- // Compute visible width of text before cursor
603- const textBeforeCursor = rawLine.slice(0, colInRaw);
604- const vw = visibleWidth(textBeforeCursor);
605- const extraLines = Math.floor(vw / width);
606- return { line: wrappedLine + extraLines, col: vw % width };
607- }
608- // Count wrapped lines for this raw line
609- const lineVW = visibleWidth(rawLine);
610- if (lineVW <= width || width <= 0) {
611- wrappedLine += 1;
612- } else {
613- wrappedLine += Math.ceil(lineVW / width);
614- }
615- offset += rawLine.length + 1; // +1 for \n
616- }
617-
618- return { line: wrappedLine, col: 0 };
619-}
620-
621537 // --- Framework-managed state ---
622538
623539 import type { ContainerProps } from "@cel-tui/types";
@@ -653,7 +569,8 @@ export function getTextInputCursorScreenPos(
653569 if (cw <= 0 || ch <= 0) return null;
654570
655571 const cursorOffset = getTextInputCursor(props);
656- const pos = offsetToWrappedPos(props.value, cursorOffset, cw);
572+ const textLayout = layoutText(props.value, cw, "word");
573+ const pos = textLayout.offsetToPosition(cursorOffset);
657574 const scrollOffset = getTextInputScroll(props);
658575 const screenRow = pos.line - scrollOffset;
659576
@@ -692,58 +609,3 @@ export function setTextInputScroll(
692609 ): void {
693610 textInputScrolls.set(props.onChange, scroll);
694611 }
695-
696-/**
697- * Simple word-wrap: break a line into multiple lines at word boundaries.
698- */
699-function wrapLine(line: string, width: number, out: string[]): void {
700- if (width <= 0) return;
701-
702- let current = "";
703- let currentW = 0;
704- const words = line.split(" ");
705-
706- for (const word of words) {
707- const wordW = visibleWidth(word);
708- if (currentW === 0) {
709- current = word;
710- currentW = wordW;
711- } else if (currentW + 1 + wordW <= width) {
712- current += " " + word;
713- currentW += 1 + wordW;
714- } else {
715- out.push(current);
716- current = word;
717- currentW = wordW;
718- }
719-
720- // Handle words longer than width (break by grapheme)
721- while (currentW > width) {
722- let taken = "";
723- let takenW = 0;
724- let rest = "";
725- let inRest = false;
726- for (const { segment } of segmenter.segment(current)) {
727- if (inRest) {
728- rest += segment;
729- continue;
730- }
731- const gw = visibleWidth(segment);
732- if (takenW + gw > width) {
733- rest += segment;
734- inRest = true;
735- } else {
736- taken += segment;
737- takenW += gw;
738- }
739- }
740- out.push(taken);
741- current = rest;
742- currentW = visibleWidth(rest);
743- }
744- }
745-
746- if (current.length > 0) {
747- out.push(current);
748- }
749-}