mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-12-01 00:49:33 +01:00
fix(web): exact iOS caret, review fixes and round-eight items (#830)
Toolbar Enter spends one-shot Ctrl; Backspace finds the last grapheme with Segments.containing(); accessory Shift+Tab keeps the draft like a typed one. Pass the captured session to Compose uploads, voice compose and held CJK drafts; clear Ctrl on deletion-only native edits; path picker clears predictions. Clear xterm dead-key state on native commits; restore a borrowed clipboard field before classifying a key; overlay removeChar spans the pending/flushed boundary. Place the trackpad caret from the overlay layout (getInputAnchor, committedText) or validated xterm cells, hiding when unproven; no end-minus-width arithmetic. Grapheme-aware replay Backspace; docs, README rows, changeset and comment updates.
This commit is contained in:
1 parent
414dd51eb2
commit
cda5d96451
26 files changed
+1072
-170
No files matched your search
@@ -88,6 +88,13 @@ function holdsOnlyRendered(container: HTMLDivElement, state: RenderedLines): boo
|
||||
return !state.cursor || children[state.lines.length] === state.cursor;
|
||||
}
|
||||
|
||||
interface OverlayPlacement {
|
||||
end: InputEndAnchor | null;
|
||||
firstVisible: number;
|
||||
topRow: number;
|
||||
keepsPromptLine: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the overlay content into the container element.
|
||||
*
|
||||
@@ -102,7 +109,7 @@ function holdsOnlyRendered(container: HTMLDivElement, state: RenderedLines): boo
|
||||
* DOM work, layout and paint, which a long prompt on a phone paid for in
|
||||
* dropped frames.
|
||||
*/
|
||||
export function renderOverlay(container: HTMLDivElement, params: RenderParams): InputEndAnchor | null {
|
||||
export function renderOverlay(container: HTMLDivElement, params: RenderParams): OverlayPlacement {
|
||||
const {
|
||||
lines,
|
||||
compositionStart,
|
||||
@@ -252,7 +259,12 @@ export function renderOverlay(container: HTMLDivElement, params: RenderParams):
|
||||
}
|
||||
|
||||
container.style.display = '';
|
||||
return lastLine === undefined ? null : { col: cursorCol, row: topRow + visibleLines.length - 1 };
|
||||
return {
|
||||
end: lastLine === undefined ? null : { col: cursorCol, row: topRow + visibleLines.length - 1 },
|
||||
firstVisible,
|
||||
topRow,
|
||||
keepsPromptLine,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,8 +19,27 @@ const DEFAULT_BG = '#0d0d0d';
|
||||
const DEFAULT_FG = '#eeeeee';
|
||||
const DEFAULT_CURSOR = '#e0e0e0';
|
||||
|
||||
interface InputLayoutLine {
|
||||
start: number;
|
||||
end: number;
|
||||
startCol: number;
|
||||
cols: number[];
|
||||
codeUnits: number[];
|
||||
}
|
||||
|
||||
interface InputLayout {
|
||||
text: string;
|
||||
committedLength: number;
|
||||
lines: InputLayoutLine[];
|
||||
firstVisible: number;
|
||||
topRow: number;
|
||||
keepsPromptLine: boolean;
|
||||
startCol: number;
|
||||
totalCols: number;
|
||||
}
|
||||
|
||||
type GraphemeSegmenter = {
|
||||
segment(input: string): Iterable<{ segment: string }>;
|
||||
segment(input: string): { containing(index: number): { segment: string } | undefined };
|
||||
};
|
||||
|
||||
const GRAPHEME_SEGMENTER: GraphemeSegmenter | null = (() => {
|
||||
@@ -36,15 +55,15 @@ const GRAPHEME_SEGMENTER: GraphemeSegmenter | null = (() => {
|
||||
}
|
||||
})();
|
||||
|
||||
/** UTF-16 width of the final grapheme cluster (one for an empty legacy flushed string). */
|
||||
/**
|
||||
* UTF-16 width of the final grapheme cluster (one for an empty legacy flushed string).
|
||||
* `containing()` finds the cluster at the end without walking the whole string (measured
|
||||
* 0.09 ms at 60,000 characters, against 41 ms for a full scan), and segments with the
|
||||
* whole text as context, so a cluster of any length and an odd run of flags come out right.
|
||||
*/
|
||||
function lastGraphemeWidth(text: string): number {
|
||||
if (GRAPHEME_SEGMENTER) {
|
||||
let width = 1;
|
||||
for (const { segment } of GRAPHEME_SEGMENTER.segment(text)) {
|
||||
width = segment.length;
|
||||
}
|
||||
return width;
|
||||
}
|
||||
if (!text) return 1;
|
||||
if (GRAPHEME_SEGMENTER) return GRAPHEME_SEGMENTER.segment(text).containing(text.length - 1)?.segment.length ?? 1;
|
||||
|
||||
const last = text.charCodeAt(text.length - 1);
|
||||
const previous = text.charCodeAt(text.length - 2);
|
||||
@@ -110,6 +129,7 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
private _lastRenderKey = '';
|
||||
private _lastPromptPos: PromptPosition | null = null;
|
||||
private _inputEndAnchor: InputEndAnchor | null = null;
|
||||
private _layout: InputLayout | null = null;
|
||||
private _layoutListeners = new Set<() => void>();
|
||||
|
||||
// Font cache
|
||||
@@ -166,6 +186,7 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
if (!promptRowInViewport(this._terminal!)) {
|
||||
this._overlay!.style.display = 'none';
|
||||
this._inputEndAnchor = null;
|
||||
this._layout = null;
|
||||
this._notifyLayout();
|
||||
if (this._scrollTimer) {
|
||||
clearTimeout(this._scrollTimer);
|
||||
@@ -237,7 +258,9 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
* Remove the last grapheme cluster (one user-perceived character).
|
||||
*
|
||||
* Cascade order:
|
||||
* 1. Remove from `pendingText` if non-empty → returns `'pending'`
|
||||
* 1. Remove the last visible cluster from pending text; if it crosses into
|
||||
* trustworthy flushed text, remove that tail too → returns `'flushed'`,
|
||||
* otherwise returns `'pending'`
|
||||
* 2. Decrement `flushedOffset` if pending is empty but flushed exists → returns `'flushed'`
|
||||
* 3. Try `detectBufferText()` if both are empty, then decrement → returns `'flushed'`
|
||||
*
|
||||
@@ -245,8 +268,8 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
*
|
||||
* - `'pending'`: A character was removed from unsent text. The consumer
|
||||
* should NOT send backspace to the PTY (the text was never transmitted).
|
||||
* - `'flushed'`: A character was removed from text already sent to the PTY.
|
||||
* The consumer SHOULD send backspace to the PTY.
|
||||
* - `'flushed'`: A character was removed wholly or partly from text already
|
||||
* sent to the PTY. The consumer SHOULD send one backspace to the PTY.
|
||||
* - `false`: Nothing to remove. The consumer should NOT send backspace.
|
||||
*
|
||||
* Any IME composition is dropped in every case, and the overlay is repainted
|
||||
@@ -257,13 +280,21 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
const droppedComposition = this._composition.length > 0;
|
||||
this._composition = '';
|
||||
if (this._pendingText.length > 0) {
|
||||
this._pendingText = this._pendingText.slice(0, -lastGraphemeWidth(this._pendingText));
|
||||
const pendingLength = this._pendingText.length;
|
||||
const hasFlushedText = this._flushedOffset > 0 && this._flushedText.length === this._flushedOffset;
|
||||
const width = lastGraphemeWidth(hasFlushedText ? this._flushedText + this._pendingText : this._pendingText);
|
||||
const flushedWidth = Math.max(0, width - pendingLength);
|
||||
this._pendingText = this._pendingText.slice(0, -width);
|
||||
if (flushedWidth > 0) {
|
||||
this._flushedOffset -= flushedWidth;
|
||||
this._flushedText = this._flushedText.slice(0, -flushedWidth);
|
||||
}
|
||||
if (this._pendingText.length > 0 || this._flushedOffset > 0) {
|
||||
this._render();
|
||||
} else {
|
||||
this._hide();
|
||||
}
|
||||
return 'pending';
|
||||
return flushedWidth > 0 ? 'flushed' : 'pending';
|
||||
}
|
||||
|
||||
// Both empty — try detecting text already on the prompt line
|
||||
@@ -353,6 +384,29 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
return this._inputEndAnchor ? { ...this._inputEndAnchor } : null;
|
||||
}
|
||||
|
||||
/** Resolve a UTF-16 offset in the last displayed text, with forward wrap affinity. */
|
||||
getInputAnchor(offset: number): InputEndAnchor | null {
|
||||
const layout = this._layout;
|
||||
if (!layout || !Number.isInteger(offset) || offset < 0 || offset > layout.text.length) return null;
|
||||
if (offset === layout.text.length) return this.getInputEndAnchor();
|
||||
for (let i = layout.lines.length - 1; i >= layout.firstVisible; i--) {
|
||||
const line = layout.lines[i];
|
||||
if (offset < line.start || offset >= line.end) continue;
|
||||
const index = line.codeUnits.indexOf(offset - line.start);
|
||||
if (index < 0) return null;
|
||||
return {
|
||||
col: (i === 0 && layout.keepsPromptLine ? layout.startCol : 0) + line.cols[index],
|
||||
row: layout.topRow + i - layout.firstVisible,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Last displayed flushed + pending text, not a delivery acknowledgement. */
|
||||
get committedText(): string {
|
||||
return this._layout ? this._layout.text.slice(0, this._layout.committedLength) : '';
|
||||
}
|
||||
|
||||
/** Subscribe after each layout, including hide. Returns an unsubscribe function. */
|
||||
onLayout(cb: () => void): () => void {
|
||||
this._layoutListeners.add(cb);
|
||||
@@ -636,6 +690,7 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
this._overlay.innerHTML = '';
|
||||
this._overlay.style.display = 'none';
|
||||
this._inputEndAnchor = null;
|
||||
this._layout = null;
|
||||
this._notifyLayout();
|
||||
}
|
||||
|
||||
@@ -700,6 +755,7 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
}
|
||||
|
||||
// The composition is a styled tail after everything the user has typed.
|
||||
const committedLength = displayText.length;
|
||||
const compositionStart = [...displayText].length;
|
||||
displayText += this._composition;
|
||||
|
||||
@@ -721,37 +777,51 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
const firstLineCols = Math.max(1, totalCols - startCol);
|
||||
const chars = [...displayText]; // proper Unicode iteration
|
||||
const lines: string[] = [];
|
||||
const layoutLines: InputLayoutLine[] = [];
|
||||
let codeUnit = 0;
|
||||
let ci = 0;
|
||||
// First line: remaining columns after prompt
|
||||
{
|
||||
let lineStr = '';
|
||||
let lineCols = 0;
|
||||
const line: InputLayoutLine = { start: codeUnit, end: codeUnit, startCol, cols: [], codeUnits: [] };
|
||||
while (ci < chars.length) {
|
||||
const cw = charCellWidth(this._terminal, chars[ci]);
|
||||
if (lineCols + cw > firstLineCols) break;
|
||||
line.cols.push(lineCols);
|
||||
line.codeUnits.push(codeUnit - line.start);
|
||||
codeUnit += chars[ci].length;
|
||||
lineStr += chars[ci];
|
||||
lineCols += cw;
|
||||
ci++;
|
||||
}
|
||||
lines.push(lineStr);
|
||||
line.end = codeUnit;
|
||||
layoutLines.push(line);
|
||||
}
|
||||
// Subsequent lines: full terminal width
|
||||
while (ci < chars.length) {
|
||||
let lineStr = '';
|
||||
let lineCols = 0;
|
||||
const line: InputLayoutLine = { start: codeUnit, end: codeUnit, startCol: 0, cols: [], codeUnits: [] };
|
||||
while (ci < chars.length) {
|
||||
const cw = charCellWidth(this._terminal, chars[ci]);
|
||||
if (lineCols + cw > totalCols) break;
|
||||
line.cols.push(lineCols);
|
||||
line.codeUnits.push(codeUnit - line.start);
|
||||
codeUnit += chars[ci].length;
|
||||
lineStr += chars[ci];
|
||||
lineCols += cw;
|
||||
ci++;
|
||||
}
|
||||
lines.push(lineStr);
|
||||
line.end = codeUnit;
|
||||
layoutLines.push(line);
|
||||
}
|
||||
|
||||
const cursorColor = this._options.cursorColor ?? this._terminal.options.theme?.cursor ?? DEFAULT_CURSOR;
|
||||
|
||||
this._inputEndAnchor = renderOverlay(this._overlay, {
|
||||
const placement = renderOverlay(this._overlay, {
|
||||
lines,
|
||||
compositionStart: this._composition ? compositionStart : undefined,
|
||||
startCol,
|
||||
@@ -767,6 +837,17 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
cursorColor,
|
||||
terminal: this._terminal,
|
||||
});
|
||||
this._inputEndAnchor = placement.end;
|
||||
this._layout = {
|
||||
text: displayText,
|
||||
committedLength,
|
||||
lines: layoutLines,
|
||||
firstVisible: placement.firstVisible,
|
||||
topRow: placement.topRow,
|
||||
keepsPromptLine: placement.keepsPromptLine,
|
||||
startCol,
|
||||
totalCols,
|
||||
};
|
||||
} catch {
|
||||
// Hide on render error but preserve pendingText —
|
||||
// next rerender() will retry when terminal is ready.
|
||||
@@ -775,7 +856,10 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
this._overlay.style.display = 'none';
|
||||
}
|
||||
} finally {
|
||||
if (this._overlay.style.display === 'none') this._inputEndAnchor = null;
|
||||
if (this._overlay.style.display === 'none') {
|
||||
this._inputEndAnchor = null;
|
||||
this._layout = null;
|
||||
}
|
||||
if (layoutChanged) this._notifyLayout();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user