From a19a0a1126f5d8521cde7d53d1042d4fa67ade89 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Fri, 20 Jan 2023 20:46:35 +0800 Subject: [PATCH] fix:adjust selection by shortcut #111 --- src/editor/core/event/handlers/keydown.ts | 55 +++++++++++++++++++---- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/src/editor/core/event/handlers/keydown.ts b/src/editor/core/event/handlers/keydown.ts index cdf3a70..5e69dcc 100644 --- a/src/editor/core/event/handlers/keydown.ts +++ b/src/editor/core/event/handlers/keydown.ts @@ -89,11 +89,30 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) { if (isReadonly) return if (index > 0) { const curIndex = startIndex - 1 - // shift则向左扩大选区 - const anchorIndex = evt.shiftKey ? endIndex : curIndex - rangeManager.setRange(curIndex, anchorIndex) + // 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 + } else { + anchorStartIndex = curIndex + anchorEndIndex = endIndex + } + } else { + anchorEndIndex = endIndex + } + } + if (!~anchorStartIndex || !~anchorEndIndex) return + rangeManager.setRange(anchorStartIndex, anchorEndIndex) + const isCollapsed = anchorStartIndex === anchorEndIndex draw.render({ - curIndex, + curIndex: isCollapsed ? anchorStartIndex : undefined, + isSetCursor: isCollapsed, isSubmitHistory: false, isComputeRowList: false }) @@ -103,11 +122,31 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) { if (isReadonly) return if (index < positionList.length - 1) { const curIndex = endIndex + 1 - // shift则向右扩大选区 - const anchorIndex = evt.shiftKey ? startIndex : curIndex - rangeManager.setRange(anchorIndex, curIndex) + // 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 = curIndex + } else { + anchorStartIndex = startIndex + 1 + anchorEndIndex = endIndex + } + } else { + anchorStartIndex = startIndex + } + } + const maxElementListIndex = elementList.length - 1 + if (anchorStartIndex > maxElementListIndex || anchorEndIndex > maxElementListIndex) return + rangeManager.setRange(anchorStartIndex, anchorEndIndex) + const isCollapsed = anchorStartIndex === anchorEndIndex draw.render({ - curIndex, + curIndex: isCollapsed ? anchorStartIndex : undefined, + isSetCursor: isCollapsed, isSubmitHistory: false, isComputeRowList: false })