feat: move the cursor the entire word #281

pr675
Hufe921 3 years ago
parent 0e6739522d
commit b38e4ed0dd

@ -125,17 +125,38 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) {
} else if (evt.key === KeyMap.Left) { } else if (evt.key === KeyMap.Left) {
if (isReadonly) return if (isReadonly) return
if (index > 0) { 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则缩放选区 // shift则缩放选区
let anchorStartIndex = curIndex let anchorStartIndex = curIndex
let anchorEndIndex = curIndex let anchorEndIndex = curIndex
const cursorPosition = position.getCursorPosition()
if (evt.shiftKey && cursorPosition) { if (evt.shiftKey && cursorPosition) {
if (startIndex !== endIndex) { if (startIndex !== endIndex) {
if (startIndex === cursorPosition.index) { if (startIndex === cursorPosition.index) {
// 减小选区 // 减小选区
anchorStartIndex = startIndex anchorStartIndex = startIndex
anchorEndIndex = endIndex - 1 anchorEndIndex = endIndex - moveCount
} else { } else {
anchorStartIndex = curIndex anchorStartIndex = curIndex
anchorEndIndex = endIndex anchorEndIndex = endIndex
@ -146,10 +167,10 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) {
} }
if (!~anchorStartIndex || !~anchorEndIndex) return if (!~anchorStartIndex || !~anchorEndIndex) return
rangeManager.setRange(anchorStartIndex, anchorEndIndex) rangeManager.setRange(anchorStartIndex, anchorEndIndex)
const isCollapsed = anchorStartIndex === anchorEndIndex const isAnchorCollapsed = anchorStartIndex === anchorEndIndex
draw.render({ draw.render({
curIndex: isCollapsed ? anchorStartIndex : undefined, curIndex: isAnchorCollapsed ? anchorStartIndex : undefined,
isSetCursor: isCollapsed, isSetCursor: isAnchorCollapsed,
isSubmitHistory: false, isSubmitHistory: false,
isCompute: false isCompute: false
}) })
@ -158,11 +179,32 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) {
} else if (evt.key === KeyMap.Right) { } else if (evt.key === KeyMap.Right) {
if (isReadonly) return if (isReadonly) return
if (index < positionList.length) { 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则缩放选区 // shift则缩放选区
let anchorStartIndex = curIndex let anchorStartIndex = curIndex
let anchorEndIndex = curIndex let anchorEndIndex = curIndex
const cursorPosition = position.getCursorPosition()
if (evt.shiftKey && cursorPosition) { if (evt.shiftKey && cursorPosition) {
if (startIndex !== endIndex) { if (startIndex !== endIndex) {
if (startIndex === cursorPosition.index) { if (startIndex === cursorPosition.index) {
@ -170,7 +212,7 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) {
anchorStartIndex = startIndex anchorStartIndex = startIndex
anchorEndIndex = curIndex anchorEndIndex = curIndex
} else { } else {
anchorStartIndex = startIndex + 1 anchorStartIndex = startIndex + moveCount
anchorEndIndex = endIndex anchorEndIndex = endIndex
} }
} else { } else {
@ -185,10 +227,10 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) {
return return
} }
rangeManager.setRange(anchorStartIndex, anchorEndIndex) rangeManager.setRange(anchorStartIndex, anchorEndIndex)
const isCollapsed = anchorStartIndex === anchorEndIndex const isAnchorCollapsed = anchorStartIndex === anchorEndIndex
draw.render({ draw.render({
curIndex: isCollapsed ? anchorStartIndex : undefined, curIndex: isAnchorCollapsed ? anchorStartIndex : undefined,
isSetCursor: isCollapsed, isSetCursor: isAnchorCollapsed,
isSubmitHistory: false, isSubmitHistory: false,
isCompute: false isCompute: false
}) })

Loading…
Cancel
Save