From d16b0f46facc7155e74cd06bf019725c5cac2a82 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Tue, 28 May 2024 21:26:19 +0800 Subject: [PATCH] fix: cursor position outside the margin of the page #609 --- src/editor/core/position/Position.ts | 48 +++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/editor/core/position/Position.ts b/src/editor/core/position/Position.ts index d6a35e5..3156929 100644 --- a/src/editor/core/position/Position.ts +++ b/src/editor/core/position/Position.ts @@ -543,7 +543,8 @@ export class Position { if (!isLastArea) { // 页眉底部距离页面顶部距离 const header = this.draw.getHeader() - const headerBottomY = header.getHeaderTop() + header.getHeight() + const headerHeight = header.getHeight() + const headerBottomY = header.getHeaderTop() + headerHeight // 页脚上部距离页面顶部距离 const footer = this.draw.getFooter() const pageHeight = this.draw.getHeight() @@ -574,6 +575,51 @@ export class Position { } } } + // 正文上-循环首行 + const margins = this.draw.getMargins() + if (y <= margins[1]) { + for (let p = 0; p < positionList.length; p++) { + const position = positionList[p] + if (position.pageNo !== positionNo || position.rowNo !== 0) continue + const { leftTop, rightTop } = position.coordinate + // 小于左页边距 || 命中文字 || 首行最后元素 + if ( + x <= margins[3] || + (x >= leftTop[0] && x <= rightTop[0]) || + positionList[p + 1]?.rowNo !== 0 + ) { + return { + index: position.index + } + } + } + } else { + // 正文下-循环尾行 + const lastLetter = lastLetterList[lastLetterList.length - 1] + if (lastLetter) { + const lastRowNo = lastLetter.rowNo + for (let p = 0; p < positionList.length; p++) { + const position = positionList[p] + if ( + position.pageNo !== positionNo || + position.rowNo !== lastRowNo + ) { + continue + } + const { leftTop, rightTop } = position.coordinate + // 小于左页边距 || 命中文字 || 尾行最后元素 + if ( + x <= margins[3] || + (x >= leftTop[0] && x <= rightTop[0]) || + positionList[p + 1]?.rowNo !== lastRowNo + ) { + return { + index: position.index + } + } + } + } + } // 当前页最后一行 return { index: