fix: cursor navigation across pages #229

pr675
Hufe921 3 years ago
parent 94247c324b
commit a96a77a237

@ -205,86 +205,101 @@ export function keydown(evt: KeyboardEvent, host: CanvasEvent) {
} }
} }
const { const {
rowNo,
index, index,
pageNo, rowNo,
coordinate: { leftTop, rightTop } rowIndex,
coordinate: {
leftTop: [curLeftX],
rightTop: [curRightX]
}
} = anchorPosition } = anchorPosition
if ((isUp && rowNo !== 0) || (!isUp && rowNo !== draw.getRowCount())) { // 向上时在首行、向下时再最尾则忽略
// 下一个光标点所在行位置集合 if (
const probablePosition = isUp (isUp && rowIndex === 0) ||
? positionList (!isUp && rowIndex === draw.getRowCount() - 1)
.slice(0, index) ) {
.filter(p => p.rowNo === rowNo - 1 && pageNo === p.pageNo) return
: positionList }
.slice(index, positionList.length - 1) // 查找下一行信息
.filter(p => p.rowNo === rowNo + 1 && pageNo === p.pageNo) const probablePosition: IElementPosition[] = []
// 查找与当前位置元素点交叉最多的位置 if (isUp) {
let maxIndex = 0 let p = index - 1
let maxDistance = 0 while (p > 0) {
for (let p = 0; p < probablePosition.length; p++) { const position = positionList[p]
const position = probablePosition[p] p--
// 当前光标在前 if (position.rowNo === rowNo) continue
if ( if (
position.coordinate.leftTop[0] >= leftTop[0] && probablePosition[0] &&
position.coordinate.leftTop[0] <= rightTop[0] probablePosition[0].rowNo !== position.rowNo
) { ) {
const curDistance = rightTop[0] - position.coordinate.leftTop[0] break
if (curDistance > maxDistance) {
maxIndex = position.index
maxDistance = curDistance
break
}
} }
// 当前光标在后 probablePosition.unshift(position)
else if ( }
position.coordinate.leftTop[0] <= leftTop[0] && } else {
position.coordinate.rightTop[0] >= leftTop[0] let p = index + 1
while (p < positionList.length) {
const position = positionList[p]
p++
if (position.rowNo === rowNo) continue
if (
probablePosition[0] &&
probablePosition[0].rowNo !== position.rowNo
) { ) {
const curDistance = position.coordinate.rightTop[0] - leftTop[0] break
if (curDistance > maxDistance) {
maxIndex = position.index
maxDistance = curDistance
break
}
} }
// 匹配不到 probablePosition.push(position)
if (p === probablePosition.length - 1 && maxIndex === 0) { }
maxIndex = position.index }
// 查找下一行位置:第一个存在交叉宽度的元素位置
let nextIndex = 0
for (let p = 0; p < probablePosition.length; p++) {
const nextPosition = probablePosition[p]
const {
coordinate: {
leftTop: [nextLeftX],
rightTop: [nextRightX]
} }
} = nextPosition
if (p === probablePosition.length - 1) {
nextIndex = nextPosition.index
} }
const curIndex = maxIndex if (curRightX <= nextLeftX || curLeftX >= nextRightX) continue
// shift则缩放选区 nextIndex = nextPosition.index
let anchorStartIndex = curIndex break
let anchorEndIndex = curIndex }
if (evt.shiftKey) { if (!nextIndex) return
if (startIndex !== endIndex) { const curIndex = nextIndex
if (startIndex === cursorPosition.index) { // shift则缩放选区
anchorStartIndex = startIndex let anchorStartIndex = curIndex
} else { let anchorEndIndex = curIndex
anchorEndIndex = endIndex if (evt.shiftKey) {
} if (startIndex !== endIndex) {
if (startIndex === cursorPosition.index) {
anchorStartIndex = startIndex
} else { } else {
if (isUp) { anchorEndIndex = endIndex
anchorEndIndex = endIndex }
} else { } else {
anchorStartIndex = startIndex if (isUp) {
} anchorEndIndex = endIndex
} else {
anchorStartIndex = startIndex
} }
} }
if (anchorStartIndex > anchorEndIndex) {
// prettier-ignore
[anchorStartIndex, anchorEndIndex] = [anchorEndIndex, anchorStartIndex]
}
rangeManager.setRange(anchorStartIndex, anchorEndIndex)
const isCollapsed = anchorStartIndex === anchorEndIndex
draw.render({
curIndex: isCollapsed ? anchorStartIndex : undefined,
isSetCursor: isCollapsed,
isSubmitHistory: false,
isCompute: false
})
} }
if (anchorStartIndex > anchorEndIndex) {
// prettier-ignore
[anchorStartIndex, anchorEndIndex] = [anchorEndIndex, anchorStartIndex]
}
rangeManager.setRange(anchorStartIndex, anchorEndIndex)
const isCollapsed = anchorStartIndex === anchorEndIndex
draw.render({
curIndex: isCollapsed ? anchorStartIndex : undefined,
isSetCursor: isCollapsed,
isSubmitHistory: false,
isCompute: false
})
} else if (isMod(evt) && evt.key === KeyMap.Z) { } else if (isMod(evt) && evt.key === KeyMap.Z) {
if (isReadonly) return if (isReadonly) return
historyManager.undo() historyManager.undo()

Loading…
Cancel
Save