0 works both ways now

This commit is contained in:
2025-10-19 15:09:41 +01:00
parent 47aea08d49
commit 07f218f4dd
4 changed files with 99 additions and 89 deletions
+1
View File
@@ -92,6 +92,7 @@ export type CursorManager = {
repeatLastMotion: () => void; repeatLastMotion: () => void;
resetCount: () => void; resetCount: () => void;
getCount: () => string; getCount: () => string;
hasCount: () => boolean;
setCount: (digit: string) => void; setCount: (digit: string) => void;
setLastKey: (key: string) => void; setLastKey: (key: string) => void;
getLastKey: () => string; getLastKey: () => string;
+5
View File
@@ -160,6 +160,10 @@ export function useCursor(board: BoardManager, gameStatusManager: GameStatusMana
return String(vimMotions.getCount()); return String(vimMotions.getCount());
} }
function hasCount(): boolean {
return vimMotions.hasCount();
}
function setCount(digit: string): void { function setCount(digit: string): void {
vimMotions.processKey(digit); vimMotions.processKey(digit);
} }
@@ -187,6 +191,7 @@ export function useCursor(board: BoardManager, gameStatusManager: GameStatusMana
repeatLastMotion, repeatLastMotion,
resetCount, resetCount,
getCount, getCount,
hasCount,
setCount, setCount,
setLastKey, setLastKey,
getLastKey, getLastKey,
+1 -3
View File
@@ -34,8 +34,7 @@ export const useGameKeys = (props: UseGameKeysParams) => {
const handler = (ev: KeyboardEvent) => { const handler = (ev: KeyboardEvent) => {
if (["waiting", "game-over", "game-won"].includes(gameStatus)) return; if (["waiting", "game-over", "game-won"].includes(gameStatus)) return;
// "0" behaves like Vim: beginning of line if (ev.key === "0" && !cursor.hasCount()) {
if (ev.key === "0" && cursor.getCount() === "") {
recordKey("0"); recordKey("0");
cursor.resetCount(); cursor.resetCount();
cursor.moveToStart(); cursor.moveToStart();
@@ -44,7 +43,6 @@ export const useGameKeys = (props: UseGameKeysParams) => {
const isDigit = ev.key >= "0" && ev.key <= "9"; const isDigit = ev.key >= "0" && ev.key <= "9";
if (isDigit) { if (isDigit) {
if (ev.key === "0" && cursor.getCount() === "") return;
recordKey(ev.key); recordKey(ev.key);
cursor.setCount(ev.key); cursor.setCount(ev.key);
return; return;
+6
View File
@@ -6,6 +6,7 @@ export type VimMotionSystem = {
processKey: (key: string) => { type: 'move'|'anchor'|'repeat'|'count', data: any } | null; processKey: (key: string) => { type: 'move'|'anchor'|'repeat'|'count', data: any } | null;
resetCount: () => void; resetCount: () => void;
getCount: () => number; getCount: () => number;
hasCount: () => boolean;
setLastMotion: (motion: Motion) => void; setLastMotion: (motion: Motion) => void;
repeatLastMotion: () => Motion | null; repeatLastMotion: () => Motion | null;
}; };
@@ -98,10 +99,15 @@ export function useVimMotions(): VimMotionSystem {
return lastMotionRef.current; return lastMotionRef.current;
} }
function hasCount(): boolean {
return countRef.current !== '';
}
return { return {
processKey, processKey,
resetCount, resetCount,
getCount, getCount,
hasCount,
setLastMotion, setLastMotion,
repeatLastMotion repeatLastMotion
}; };