mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-12-01 00:49:33 +01:00
fix(web): delete one grapheme per iOS Backspace, keep the recovery fallback (#830)
Count native edits and overlay deletions in grapheme clusters (Intl.Segmenter, code-point fallback), so one Backspace on a multi-code-point emoji sends one DEL. Toolbar Enter with echo off tells the codex predictor about the CR, clearing its predictions as typed Enter does. Fall back to the keyCode-229 recovery when the iOS owner refuses to start, in the main pane and tiles.
This commit is contained in:
1 parent
05fc2f990b
commit
71c7d01d02
8 files changed
+184
-24
No files matched your search
@@ -19,8 +19,33 @@ const DEFAULT_BG = '#0d0d0d';
|
||||
const DEFAULT_FG = '#eeeeee';
|
||||
const DEFAULT_CURSOR = '#e0e0e0';
|
||||
|
||||
/** UTF-16 width of the final code point (one for an empty legacy flushed string). */
|
||||
function lastCodePointWidth(text: string): number {
|
||||
type GraphemeSegmenter = {
|
||||
segment(input: string): Iterable<{ segment: string }>;
|
||||
};
|
||||
|
||||
const GRAPHEME_SEGMENTER: GraphemeSegmenter | null = (() => {
|
||||
try {
|
||||
const Segmenter = (
|
||||
Intl as typeof Intl & {
|
||||
Segmenter?: new (locale?: string, options?: { granularity: 'grapheme' }) => GraphemeSegmenter;
|
||||
}
|
||||
).Segmenter;
|
||||
return Segmenter ? new Segmenter(undefined, { granularity: 'grapheme' }) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
|
||||
/** UTF-16 width of the final grapheme cluster (one for an empty legacy flushed string). */
|
||||
function lastGraphemeWidth(text: string): number {
|
||||
if (GRAPHEME_SEGMENTER) {
|
||||
let width = 1;
|
||||
for (const { segment } of GRAPHEME_SEGMENTER.segment(text)) {
|
||||
width = segment.length;
|
||||
}
|
||||
return width;
|
||||
}
|
||||
|
||||
const last = text.charCodeAt(text.length - 1);
|
||||
const previous = text.charCodeAt(text.length - 2);
|
||||
return last >= 0xdc00 && last <= 0xdfff && previous >= 0xd800 && previous <= 0xdbff ? 2 : 1;
|
||||
@@ -209,7 +234,7 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the last Unicode code point from the overlay.
|
||||
* Remove the last grapheme cluster (one user-perceived character).
|
||||
*
|
||||
* Cascade order:
|
||||
* 1. Remove from `pendingText` if non-empty → returns `'pending'`
|
||||
@@ -232,7 +257,7 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
const droppedComposition = this._composition.length > 0;
|
||||
this._composition = '';
|
||||
if (this._pendingText.length > 0) {
|
||||
this._pendingText = this._pendingText.slice(0, -lastCodePointWidth(this._pendingText));
|
||||
this._pendingText = this._pendingText.slice(0, -lastGraphemeWidth(this._pendingText));
|
||||
if (this._pendingText.length > 0 || this._flushedOffset > 0) {
|
||||
this._render();
|
||||
} else {
|
||||
@@ -245,7 +270,7 @@ export class ZerolagInputAddon implements XtermAddon {
|
||||
// (handles tab completion, arrow-key edits, etc.).
|
||||
if (this._flushedOffset === 0) this._detectBufferText();
|
||||
if (this._flushedOffset > 0) {
|
||||
const width = lastCodePointWidth(this._flushedText);
|
||||
const width = lastGraphemeWidth(this._flushedText);
|
||||
this._flushedOffset -= width;
|
||||
this._flushedText = this._flushedText.slice(0, -width);
|
||||
if (this._flushedOffset > 0) {
|
||||
|
||||
@@ -127,6 +127,28 @@ describe('ZerolagInputAddon', () => {
|
||||
expect(addon.state.visible).toBe(false);
|
||||
});
|
||||
|
||||
const graphemeCases = [
|
||||
['red heart', '\u2764\uFE0F'],
|
||||
['skin-tone thumbs up', '\u{1F44D}\u{1F3FD}'],
|
||||
['flag', '\u{1F1EB}\u{1F1F7}'],
|
||||
['ZWJ family', '\u{1F468}\u200D\u{1F469}\u200D\u{1F467}\u200D\u{1F466}'],
|
||||
];
|
||||
|
||||
it.each(graphemeCases)('removes a whole pending %s grapheme', (_name, emoji) => {
|
||||
const { addon } = tracked();
|
||||
addon.appendText('ok' + emoji);
|
||||
expect(addon.removeChar()).toBe('pending');
|
||||
expect(addon.pendingText).toBe('ok');
|
||||
});
|
||||
|
||||
it.each(graphemeCases)('removes a whole flushed %s grapheme and subtracts its UTF-16 width', (_name, emoji) => {
|
||||
const { addon } = tracked();
|
||||
const text = 'ok' + emoji;
|
||||
addon.setFlushed(text.length, text);
|
||||
expect(addon.removeChar()).toBe('flushed');
|
||||
expect(addon.getFlushed()).toEqual({ count: 2, text: 'ok' });
|
||||
});
|
||||
|
||||
it('returns false when nothing to remove', () => {
|
||||
const { addon } = tracked();
|
||||
expect(addon.removeChar()).toBe(false);
|
||||
|
||||
Reference in new issue
Block a user