!37784 RichEditor shift+end/home键做成只选中到本行

Merge pull request !37784 from Zhang Jinyu/0715richEditor
This commit is contained in:
openharmony_ci 2024-07-19 09:24:54 +00:00 committed by Gitee
commit 76beb2cf22
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 71 additions and 6 deletions

View File

@ -5296,7 +5296,7 @@ void RichEditorPattern::HandleSelect(CaretMoveIntent direction)
fixedPos = (caretPosition_ == textSelector_.GetTextStart() ? textSelector_.GetTextEnd()
: textSelector_.GetTextStart());
}
newPos = HandleSelectWrapper(direction);
newPos = HandleSelectWrapper(direction, fixedPos);
if (newPos == -1) {
return;
}
@ -9053,6 +9053,68 @@ int32_t RichEditorPattern::CalcLineEndPosition()
return position;
}
int32_t RichEditorPattern::CalcSingleLineBeginPosition(int32_t fixedPos)
{
float caretHeightDown = 0.0f;
OffsetF caretOffsetDown = CalcCursorOffsetByPosition(fixedPos, caretHeightDown, true, false);
float caretHeightUp = 0.0f;
OffsetF caretOffsetUp = CalcCursorOffsetByPosition(fixedPos, caretHeightUp, false, false);
bool isCaretPosInLineEnd = !NearEqual(caretOffsetDown.GetX(), caretOffsetUp.GetX(), 0.5f);
auto overlayMod = DynamicCast<RichEditorOverlayModifier>(overlayMod_);
CHECK_NULL_RETURN(overlayMod_, false);
auto caretOffsetOverlay = overlayMod->GetCaretOffset();
bool cursorNotAtLineStart = NearEqual(caretOffsetOverlay.GetX(), caretOffsetUp.GetX(), 0.5f);
Offset textOffset;
if (!cursorNotAtLineStart && !lastClickOffset_.IsNegative()) {
return fixedPos;
} else if (isCaretPosInLineEnd && lastClickOffset_.IsNegative()) {
return lastSelectionRange_.start_;
} else {
float caretHeight = 0.0f;
OffsetF caretOffsetFixed = CalcCursorOffsetByPosition(fixedPos, caretHeight, false, false);
textOffset = { 0, caretOffsetFixed.GetY() };
}
auto position = paragraphs_.GetIndex(textOffset);
return position;
}
int32_t RichEditorPattern::CalcSingleLineEndPosition(int32_t fixedPos)
{
auto rectLineInfo = CalcLineInfoByPosition();
float textWidth = richTextRect_.Width() + rectLineInfo.GetX();
float caretHeightDown = 0.0f;
OffsetF caretOffsetDown = CalcCursorOffsetByPosition(fixedPos, caretHeightDown, true, false);
float caretHeightUp = 0.0f;
OffsetF caretOffsetUp = CalcCursorOffsetByPosition(fixedPos, caretHeightUp, false, false);
bool isCaretPosInLineEnd = !NearEqual(caretOffsetDown.GetX(), caretOffsetUp.GetX(), 0.5f);
auto overlayMod = DynamicCast<RichEditorOverlayModifier>(overlayMod_);
CHECK_NULL_RETURN(overlayMod_, false);
auto caretOffsetOverlay = overlayMod->GetCaretOffset();
bool cursorNotAtLineStart = NearEqual(caretOffsetOverlay.GetX(), caretOffsetUp.GetX(), 0.5f);
Offset textOffset;
if (isCaretPosInLineEnd && lastClickOffset_.IsNegative() && lastSelectionRange_.end_ > fixedPos) {
return lastSelectionRange_.end_;
} else if (!cursorNotAtLineStart && isCaretPosInLineEnd && fixedPos == caretPosition_) {
textOffset = { textWidth, caretOffsetOverlay.GetY() };
} else if (isCaretPosInLineEnd && lastSelectionRange_.end_ <= fixedPos) {
CursorMoveLineEnd();
return fixedPos;
} else if (cursorNotAtLineStart && isCaretPosInLineEnd) {
return fixedPos;
} else {
float caretHeight = 0.0f;
OffsetF caretOffsetFixed = CalcCursorOffsetByPosition(fixedPos, caretHeight, false, false);
textOffset = { textWidth, caretOffsetFixed.GetY() };
}
auto position = paragraphs_.GetIndex(textOffset);
return position;
}
bool RichEditorPattern::CursorMoveLineBegin()
{
CloseSelectOverlay();
@ -9130,7 +9192,7 @@ void RichEditorPattern::HandleOnShowMenu()
ShowSelectOverlay(RectF(), RectF(), IsSelectAll(), TextResponseType::RIGHT_CLICK);
}
int32_t RichEditorPattern::HandleSelectWrapper(CaretMoveIntent direction)
int32_t RichEditorPattern::HandleSelectWrapper(CaretMoveIntent direction, int32_t fixedPos)
{
int32_t index = GetCaretPosition();
switch (direction) {
@ -9161,9 +9223,9 @@ int32_t RichEditorPattern::HandleSelectWrapper(CaretMoveIntent direction)
case CaretMoveIntent::ParagraghEnd:
return HandleSelectParagraghPos(false);
case CaretMoveIntent::LineBegin:
return CalcLineBeginPosition();
return CalcSingleLineBeginPosition(fixedPos);
case CaretMoveIntent::LineEnd:
return CalcLineEndPosition();
return CalcSingleLineEndPosition(fixedPos);
default:
return NONE_SELECT_TYPE;
}

View File

@ -379,6 +379,8 @@ public:
int32_t CalcLineBeginPosition();
float GetTextThemeFontSize();
int32_t CalcLineEndPosition();
int32_t CalcSingleLineBeginPosition(int32_t fixedPos);
int32_t CalcSingleLineEndPosition(int32_t fixedPos);
bool CursorMoveLineBegin();
bool CursorMoveLineEnd();
void HandleSelectFontStyle(KeyCode code) override;
@ -386,7 +388,7 @@ public:
void HandleOnShowMenu() override;
int32_t HandleSelectPosition(bool isForward);
int32_t HandleSelectParagraghPos(bool direction);
int32_t HandleSelectWrapper(CaretMoveIntent direction);
int32_t HandleSelectWrapper(CaretMoveIntent direction, int32_t fixedPos);
void AIDeleteComb(int32_t start, int32_t end, int32_t& aiPosition, bool direction);
bool HandleOnDeleteComb(bool backward) override;
int32_t GetLeftWordPosition(int32_t caretPosition);

View File

@ -527,7 +527,8 @@ HWTEST_F(RichEditorKeyboardShortcutTestNg, HandleSelectWrapper101, TestSize.Leve
auto richEditorPattern = richEditorNode_->GetPattern<RichEditorPattern>();
ASSERT_NE(richEditorPattern, nullptr);
OHOS::Ace::CaretMoveIntent direction = OHOS::Ace::CaretMoveIntent::Home;
auto ret = richEditorPattern->HandleSelectWrapper(direction);
int32_t fixedPos = 0;
auto ret = richEditorPattern->HandleSelectWrapper(direction, fixedPos);
EXPECT_EQ(ret, -1);
}