From b38e4ed0ddaa020c0dcbb0336efa32cc605e281c Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Tue, 26 Sep 2023 22:51:03 +0800 Subject: [PATCH] feat: move the cursor the entire word #281 --- src/editor/core/event/handlers/keydown.ts | 66 ++++++++++++++++++----- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/src/editor/core/event/handlers/keydown.ts b/src/editor/core/event/handlers/keydown.ts index e0b2927..5bdf842 100644 --- a/src/editor/core/event/handlers/keydown.ts +++ b/src/editor/core/event/handlers/keydown.ts @@ -125,17 +125,38 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) { } else if (evt.key === KeyMap.Left) { if (isReadonly) return if (index > 0) { - const curIndex = startIndex - 1 + const cursorPosition = position.getCursorPosition() + // 单词整体移动 + let moveCount = 1 + if (isMod(evt)) { + const LETTER_REG = draw.getLetterReg() + // 起始位置 + const moveStartIndex = + evt.shiftKey && !isCollapsed && startIndex === cursorPosition?.index + ? endIndex + : startIndex + if (LETTER_REG.test(elementList[moveStartIndex]?.value)) { + let i = moveStartIndex - 1 + while (i > 0) { + const element = elementList[i] + if (!LETTER_REG.test(element.value)) { + break + } + moveCount++ + i-- + } + } + } + const curIndex = startIndex - moveCount // shift则缩放选区 let anchorStartIndex = curIndex let anchorEndIndex = curIndex - const cursorPosition = position.getCursorPosition() if (evt.shiftKey && cursorPosition) { if (startIndex !== endIndex) { if (startIndex === cursorPosition.index) { // 减小选区 anchorStartIndex = startIndex - anchorEndIndex = endIndex - 1 + anchorEndIndex = endIndex - moveCount } else { anchorStartIndex = curIndex anchorEndIndex = endIndex @@ -146,10 +167,10 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) { } if (!~anchorStartIndex || !~anchorEndIndex) return rangeManager.setRange(anchorStartIndex, anchorEndIndex) - const isCollapsed = anchorStartIndex === anchorEndIndex + const isAnchorCollapsed = anchorStartIndex === anchorEndIndex draw.render({ - curIndex: isCollapsed ? anchorStartIndex : undefined, - isSetCursor: isCollapsed, + curIndex: isAnchorCollapsed ? anchorStartIndex : undefined, + isSetCursor: isAnchorCollapsed, isSubmitHistory: false, isCompute: false }) @@ -158,11 +179,32 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) { } else if (evt.key === KeyMap.Right) { if (isReadonly) return if (index < positionList.length) { - const curIndex = endIndex + 1 + const cursorPosition = position.getCursorPosition() + let moveCount = 1 + // 单词整体移动 + if (isMod(evt)) { + const LETTER_REG = draw.getLetterReg() + // 起始位置 + const moveStartIndex = + evt.shiftKey && !isCollapsed && startIndex === cursorPosition?.index + ? endIndex + : startIndex + if (LETTER_REG.test(elementList[moveStartIndex + 1]?.value)) { + let i = moveStartIndex + 2 + while (i < elementList.length) { + const element = elementList[i] + if (!LETTER_REG.test(element.value)) { + break + } + moveCount++ + i++ + } + } + } + const curIndex = endIndex + moveCount // shift则缩放选区 let anchorStartIndex = curIndex let anchorEndIndex = curIndex - const cursorPosition = position.getCursorPosition() if (evt.shiftKey && cursorPosition) { if (startIndex !== endIndex) { if (startIndex === cursorPosition.index) { @@ -170,7 +212,7 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) { anchorStartIndex = startIndex anchorEndIndex = curIndex } else { - anchorStartIndex = startIndex + 1 + anchorStartIndex = startIndex + moveCount anchorEndIndex = endIndex } } else { @@ -185,10 +227,10 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) { return } rangeManager.setRange(anchorStartIndex, anchorEndIndex) - const isCollapsed = anchorStartIndex === anchorEndIndex + const isAnchorCollapsed = anchorStartIndex === anchorEndIndex draw.render({ - curIndex: isCollapsed ? anchorStartIndex : undefined, - isSetCursor: isCollapsed, + curIndex: isAnchorCollapsed ? anchorStartIndex : undefined, + isSetCursor: isAnchorCollapsed, isSubmitHistory: false, isCompute: false })