1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_PATTERN_H 18 19 #include <cstdint> 20 #include <map> 21 #include <optional> 22 #include <string> 23 24 #include "core/common/ai/ai_write_adapter.h" 25 #include "core/common/ime/text_edit_controller.h" 26 #include "core/common/ime/text_input_action.h" 27 #include "core/common/ime/text_input_client.h" 28 #include "core/common/ime/text_input_configuration.h" 29 #include "core/common/ime/text_input_connection.h" 30 #include "core/common/ime/text_input_formatter.h" 31 #include "core/common/ime/text_input_proxy.h" 32 #include "core/common/ime/text_input_type.h" 33 #include "core/common/ime/text_selection.h" 34 #include "core/components/common/properties/text_layout_info.h" 35 #include "core/components_ng/pattern/rich_editor/paragraph_manager.h" 36 #include "core/components_ng/pattern/rich_editor/rich_editor_accessibility_property.h" 37 #include "core/components_ng/pattern/rich_editor/rich_editor_content_modifier.h" 38 #include "core/components_ng/pattern/rich_editor/rich_editor_controller.h" 39 #include "core/components_ng/pattern/rich_editor/rich_editor_event_hub.h" 40 #include "core/components_ng/pattern/rich_editor/rich_editor_layout_algorithm.h" 41 #include "core/components_ng/pattern/rich_editor/rich_editor_layout_property.h" 42 #include "core/components_ng/pattern/rich_editor/rich_editor_overlay_modifier.h" 43 #include "core/components_ng/pattern/rich_editor/rich_editor_paint_method.h" 44 #include "core/components_ng/pattern/rich_editor/rich_editor_select_overlay.h" 45 #include "core/components_ng/pattern/rich_editor/rich_editor_styled_string_controller.h" 46 #include "core/components_ng/pattern/rich_editor/selection_info.h" 47 #include "core/components_ng/pattern/scrollable/scrollable_pattern.h" 48 #include "core/components_ng/pattern/select_overlay/magnifier.h" 49 #include "core/components_ng/pattern/select_overlay/magnifier_controller.h" 50 #include "core/components_ng/pattern/text/layout_info_interface.h" 51 #include "core/components_ng/pattern/text/span_node.h" 52 #include "core/components_ng/pattern/text/text_base.h" 53 #include "core/components_ng/pattern/text/text_pattern.h" 54 #include "core/components_ng/pattern/text_field/text_field_model.h" 55 #include "core/text/text_emoji_processor.h" 56 57 #ifndef ACE_UNITTEST 58 #ifdef ENABLE_STANDARD_INPUT 59 #include "commonlibrary/c_utils/base/include/refbase.h" 60 61 namespace OHOS::MiscServices { 62 class OnTextChangedListener; 63 struct TextConfig; 64 } // namespace OHOS::MiscServices 65 #endif 66 #endif 67 68 #define COPY_SPAN_STYLE_IF_PRESENT(sourceNode, targetNode, styleType, propertyInfo) \ 69 do { \ 70 if ((sourceNode)->Has##styleType()) { \ 71 (targetNode)->Update##styleType(*((sourceNode)->Get##styleType())); \ 72 (targetNode)->AddPropertyInfo(propertyInfo); \ 73 } \ 74 } while (false) 75 #define CONTENT_MODIFY_LOCK(patternPtr) ContentModifyLock contentModifyLock(patternPtr) 76 77 #define IF_TRUE(cond, func) \ 78 do { \ 79 if (cond) { \ 80 func; \ 81 } \ 82 } while (false) 83 84 #define IF_PRESENT(opt, func) \ 85 do { \ 86 if (opt) { \ 87 (opt)->func; \ 88 } \ 89 } while (false) 90 91 namespace OHOS::Ace::NG { 92 class InspectorFilter; 93 94 // TextPattern is the base class for text render node to perform paint text. 95 enum class MoveDirection { FORWARD, BACKWARD }; 96 97 enum class AutoScrollEvent { HANDLE, DRAG, MOUSE, NONE }; 98 enum class EdgeDetectionStrategy { OUT_BOUNDARY, IN_BOUNDARY, DISABLE }; 99 struct AutoScrollParam { 100 AutoScrollEvent autoScrollEvent = AutoScrollEvent::NONE; 101 RectF handleRect; 102 bool isFirstHandle = false; 103 float offset = 0.0f; 104 bool showScrollbar = false; 105 Offset eventOffset; 106 bool isFirstRun_ = true; 107 }; 108 enum class RecordType { DEL_FORWARD = 0, DEL_BACKWARD = 1, INSERT = 2, UNDO = 3, REDO = 4, DRAG = 5 }; 109 enum class SelectorAdjustPolicy { INCLUDE = 0, EXCLUDE }; 110 enum class HandleType { FIRST = 0, SECOND }; 111 enum class SelectType { SELECT_FORWARD = 0, SELECT_BACKWARD, SELECT_NOTHING }; 112 enum class CaretAffinityPolicy { DEFAULT = 0, UPSTREAM_FIRST, DOWNSTREAM_FIRST }; 113 enum class OperationType { DEFAULT = 0, DRAG, IME }; 114 const std::map<std::pair<HandleType, SelectorAdjustPolicy>, MoveDirection> SELECTOR_ADJUST_DIR_MAP = { 115 {{ HandleType::FIRST, SelectorAdjustPolicy::INCLUDE }, MoveDirection::BACKWARD }, 116 {{ HandleType::FIRST, SelectorAdjustPolicy::EXCLUDE }, MoveDirection::FORWARD }, 117 {{ HandleType::SECOND, SelectorAdjustPolicy::INCLUDE }, MoveDirection::FORWARD }, 118 {{ HandleType::SECOND, SelectorAdjustPolicy::EXCLUDE }, MoveDirection::BACKWARD } 119 }; 120 struct CaretOffsetInfo { 121 // caret front offset info 122 OffsetF caretOffsetUp; 123 // caret end offset info 124 OffsetF caretOffsetDown; 125 // caret position offset info 126 OffsetF caretOffsetLine; 127 float caretHeightUp = 0.0f; 128 float caretHeightDown = 0.0f; 129 float caretHeightLine = 0.0f; 130 }; 131 enum class PositionType { DEFAULT, PARAGRAPH_START, PARAGRAPH_END, LINE_START, LINE_END }; 132 133 134 class RichEditorPattern 135 : public TextPattern, public ScrollablePattern, public TextInputClient, public SpanWatcher { 136 DECLARE_ACE_TYPE(RichEditorPattern, TextPattern, ScrollablePattern, TextInputClient, SpanWatcher); 137 138 public: 139 RichEditorPattern(); 140 ~RichEditorPattern() override; 141 142 struct OperationRecord { OperationRecordOperationRecord143 OperationRecord() : beforeCaretPosition(-1), afterCaretPosition(-1), deleteCaretPostion(-1) {} 144 std::optional<std::string> addText; 145 std::optional<std::string> deleteText; 146 int32_t beforeCaretPosition; 147 int32_t afterCaretPosition; 148 int32_t deleteCaretPostion; 149 }; 150 151 struct PreviewTextRecord { 152 int32_t startOffset = INVALID_VALUE; 153 int32_t endOffset = INVALID_VALUE; 154 bool isPreviewTextInputting = false; 155 std::string previewContent; 156 std::string newPreviewContent; 157 bool hasDiff = false; 158 PreviewRange replacedRange; 159 ToStringPreviewTextRecord160 std::string ToString() const 161 { 162 auto jsonValue = JsonUtil::Create(true); 163 JSON_STRING_PUT_STRING(jsonValue, previewContent); 164 JSON_STRING_PUT_BOOL(jsonValue, isPreviewTextInputting); 165 JSON_STRING_PUT_INT(jsonValue, startOffset); 166 JSON_STRING_PUT_INT(jsonValue, endOffset); 167 168 return jsonValue->ToString(); 169 } 170 ResetPreviewTextRecord171 void Reset() 172 { 173 startOffset = INVALID_VALUE; 174 endOffset = INVALID_VALUE; 175 previewContent.clear(); 176 isPreviewTextInputting = false; 177 hasDiff = false; 178 replacedRange.Set(INVALID_VALUE, INVALID_VALUE); 179 } 180 IsValidPreviewTextRecord181 bool IsValid() const 182 { 183 return !previewContent.empty() && isPreviewTextInputting && startOffset >= 0 && endOffset >= startOffset; 184 } 185 }; 186 187 struct TouchAndMoveCaretState { 188 bool isTouchCaret = false; 189 bool isMoveCaret = false; 190 Offset touchDownOffset; 191 const Dimension minDistance = 5.0_vp; 192 ResetTouchAndMoveCaretState193 void Reset() 194 { 195 isTouchCaret = false; 196 isMoveCaret = false; 197 touchDownOffset.Reset(); 198 } 199 }; 200 201 class ContentModifyLock { 202 public: 203 ContentModifyLock(); ContentModifyLock(RichEditorPattern * pattern)204 ContentModifyLock(RichEditorPattern* pattern) 205 { 206 pattern->isModifyingContent_ = true; 207 pattern_ = WeakClaim(pattern); 208 } ~ContentModifyLock()209 ~ContentModifyLock() 210 { 211 auto pattern = pattern_.Upgrade(); 212 if (!pattern) { 213 TAG_LOGE(AceLogTag::ACE_RICH_TEXT, "pattern is null, unlock failed"); 214 return; 215 } 216 pattern->isModifyingContent_ = false; 217 auto host = pattern->GetHost(); 218 CHECK_NULL_VOID(host); 219 host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE); 220 } 221 private: 222 WeakPtr<RichEditorPattern> pattern_; 223 }; 224 225 struct OneStepDragParam { 226 std::function<void()> menuBuilder = nullptr; 227 std::function<void()> previewBuilder = nullptr; 228 std::function<void(int32_t, int32_t)> onAppear = nullptr; 229 MenuParam menuParam; 230 OneStepDragParam(const std::function<void()>& builder, const SelectMenuParam& selectMenuParam); 231 MenuParam GetMenuParam(const RefPtr<ImageSpanNode>& imageNode); 232 }; 233 234 int32_t SetPreviewText(const std::string& previewTextValue, const PreviewRange range) override; 235 236 bool InitPreviewText(const std::string& previewTextValue, const PreviewRange range); 237 238 bool ReplacePreviewText(const std::string& previewTextValue, const PreviewRange& range); 239 240 bool UpdatePreviewText(const std::string& previewTextValue, const PreviewRange range); 241 242 const PreviewTextInfo GetPreviewTextInfo() const; 243 244 void FinishTextPreview() override; 245 ReceivePreviewTextStyle(const std::string & style)246 void ReceivePreviewTextStyle(const std::string& style) override 247 { 248 ACE_UPDATE_LAYOUT_PROPERTY(RichEditorLayoutProperty, PreviewTextStyle, style); 249 } 250 251 const Color& GetPreviewTextDecorationColor() const; 252 IsPreviewTextInputting()253 bool IsPreviewTextInputting() 254 { 255 return previewTextRecord_.IsValid(); 256 } 257 258 std::vector<RectF> GetPreviewTextRects(); 259 260 float GetPreviewTextUnderlineWidth() const; 261 262 PreviewTextStyle GetPreviewTextStyle() const; 263 SetSupportPreviewText(bool isTextPreviewSupported)264 void SetSupportPreviewText(bool isTextPreviewSupported) 265 { 266 isTextPreviewSupported_ = isTextPreviewSupported; 267 } 268 269 ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(TextInputAction, TextInputAction) 270 TextInputAction GetDefaultTextInputAction() const; 271 CreateEventHub()272 RefPtr<EventHub> CreateEventHub() override 273 { 274 return MakeRefPtr<RichEditorEventHub>(); 275 } 276 CreateLayoutProperty()277 RefPtr<LayoutProperty> CreateLayoutProperty() override 278 { 279 return MakeRefPtr<RichEditorLayoutProperty>(); 280 } 281 CreateLayoutAlgorithm()282 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 283 { 284 return MakeRefPtr<RichEditorLayoutAlgorithm>(spans_, ¶graphs_, typingTextStyle_); 285 } 286 GetFocusPattern()287 FocusPattern GetFocusPattern() const override 288 { 289 FocusPattern focusPattern = { FocusType::NODE, true, FocusStyleType::INNER_BORDER }; 290 focusPattern.SetIsFocusActiveWhenFocused(true); 291 return focusPattern; 292 } 293 294 RefPtr<NodePaintMethod> CreateNodePaintMethod() override; 295 GetRichEditorController()296 const RefPtr<RichEditorController>& GetRichEditorController() 297 { 298 return richEditorController_; 299 } 300 SetRichEditorController(const RefPtr<RichEditorController> & controller)301 void SetRichEditorController(const RefPtr<RichEditorController>& controller) 302 { 303 richEditorController_ = controller; 304 } 305 GetRichEditorStyledStringController()306 const RefPtr<RichEditorStyledStringController>& GetRichEditorStyledStringController() 307 { 308 return richEditorStyledStringController_; 309 } 310 SetRichEditorStyledStringController(const RefPtr<RichEditorStyledStringController> & controller)311 void SetRichEditorStyledStringController(const RefPtr<RichEditorStyledStringController>& controller) 312 { 313 richEditorStyledStringController_ = controller; 314 } 315 GetTimestamp()316 long long GetTimestamp() const 317 { 318 return timestamp_; 319 } 320 321 // RichEditor needs softkeyboard, override function. NeedSoftKeyboard()322 bool NeedSoftKeyboard() const override 323 { 324 return true; 325 } 326 UpdateSpanPosition()327 void UpdateSpanPosition() 328 { 329 uint32_t spanTextLength = 0; 330 for (auto& span : spans_) { 331 span->rangeStart = static_cast<int32_t>(spanTextLength); 332 spanTextLength += StringUtils::ToWstring(span->content).length(); 333 span->position = static_cast<int32_t>(spanTextLength); 334 } 335 } 336 337 BlurReason GetBlurReason(); 338 339 uint32_t GetSCBSystemWindowId(); 340 341 void RegisiterCaretChangeListener(std::function<void(int32_t)>&& listener); 342 void SetStyledString(const RefPtr<SpanString>& value); 343 GetStyledString()344 RefPtr<MutableSpanString> GetStyledString() const 345 { 346 return styledString_; 347 } 348 349 void UpdateSpanItems(const std::list<RefPtr<NG::SpanItem>>& spanItems) override; 350 void ProcessStyledString(); 351 void MountImageNode(const RefPtr<ImageSpanItem>& imageItem); 352 void SetImageLayoutProperty(RefPtr<ImageSpanNode> imageNode, const ImageSpanOptions& options); 353 void InsertValueInStyledString(const std::string& insertValue); 354 RefPtr<SpanString> CreateStyledStringByTextStyle( 355 const std::string& insertValue, const struct UpdateSpanStyle& updateSpanStyle, const TextStyle& textStyle); 356 RefPtr<FontSpan> CreateFontSpanByTextStyle( 357 const struct UpdateSpanStyle& updateSpanStyle, const TextStyle& textStyle, int32_t length); 358 RefPtr<DecorationSpan> CreateDecorationSpanByTextStyle( 359 const struct UpdateSpanStyle& updateSpanStyle, const TextStyle& textStyle, int32_t length); 360 void DeleteBackwardInStyledString(int32_t length); 361 void DeleteForwardInStyledString(int32_t length, bool isIME = true); 362 void DeleteValueInStyledString(int32_t start, int32_t length, bool isIME = true, bool isUpdateCaret = true); 363 364 bool BeforeStyledStringChange(int32_t start, int32_t length, const std::string& string); 365 bool BeforeStyledStringChange(int32_t start, int32_t length, const RefPtr<SpanString>& styledString); 366 void AfterStyledStringChange(int32_t start, int32_t length, const std::string& string); 367 void ResetBeforePaste(); 368 void ResetAfterPaste(); 369 370 void OnVisibleChange(bool isVisible) override; 371 void OnModifyDone() override; 372 void BeforeCreateLayoutWrapper() override; 373 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 374 void HandleSelectOverlayOnLayoutSwap(); 375 void FireOnReady(); 376 void SupplementIdealSizeWidth(const RefPtr<FrameNode>& frameNode); 377 void MoveCaretOnLayoutSwap(bool isReduceSize); 378 379 void UpdateEditingValue(const std::shared_ptr<TextEditingValue>& value, bool needFireChangeEvent = true) override; 380 void PerformAction(TextInputAction action, bool forceCloseKeyboard = true) override; 381 bool IsIMEOperation(OperationType operationType); 382 void InsertValue(const std::string& insertValue, bool isIME = false) override; 383 void InsertValueByOperationType(const std::string& insertValue, 384 OperationType operationType = OperationType::DEFAULT); 385 void InsertValueOperation(const std::string& insertValue, OperationRecord* const record = nullptr, 386 OperationType operationType = OperationType::IME); 387 void DeleteSelectOperation(OperationRecord* const record); 388 void DeleteByRange(OperationRecord* const record, int32_t start, int32_t end); 389 void InsertDiffStyleValueInSpan( 390 RefPtr<SpanNode>& spanNode, const TextInsertValueInfo& info, const std::string& insertValue, bool isIME = true); 391 void InsertValueByPaste(const std::string& insertValue); 392 bool IsLineSeparatorInLast(RefPtr<SpanNode>& spanNode); 393 void InsertValueToSpanNode( 394 RefPtr<SpanNode>& spanNode, const std::string& insertValue, const TextInsertValueInfo& info); 395 void SpanNodeFission(RefPtr<SpanNode>& spanNode); 396 void CreateTextSpanNode( 397 RefPtr<SpanNode>& spanNode, const TextInsertValueInfo& info, const std::string& insertValue, bool isIME = true); 398 void SetDefaultColor(RefPtr<SpanNode>& spanNode); 399 void HandleOnDelete(bool backward) override; 400 std::pair<bool, bool> IsEmojiOnCaretPosition(int32_t& emojiLength, bool isBackward, int32_t length); 401 int32_t CalculateDeleteLength(int32_t length, bool isBackward); 402 void DeleteBackward(int32_t length = 1) override; 403 std::wstring DeleteBackwardOperation(int32_t length); 404 void DeleteForward(int32_t length = 1) override; 405 int32_t HandleOnDragDeleteForward(); 406 std::wstring DeleteForwardOperation(int32_t length); 407 void SetInputMethodStatus(bool keyboardShown) override; 408 bool ClickAISpan(const PointF& textOffset, const AISpan& aiSpan) override; 409 WindowMode GetWindowMode(); NotifyKeyboardClosedByUser()410 void NotifyKeyboardClosedByUser() override 411 { 412 TAG_LOGI(AceLogTag::ACE_RICH_TEXT, "KeyboardClosedByUser"); 413 FocusHub::LostFocusToViewRoot(); 414 } NotifyKeyboardClosed()415 void NotifyKeyboardClosed() override 416 { 417 TAG_LOGI(AceLogTag::ACE_RICH_TEXT, "KeyboardClosed"); 418 CHECK_NULL_VOID(HasFocus()); 419 420 // lost focus in floating window mode 421 auto windowMode = GetWindowMode(); 422 if (windowMode == WindowMode::WINDOW_MODE_FLOATING) { 423 FocusHub::LostFocusToViewRoot(); 424 } 425 } 426 void ClearOperationRecords(); 427 void ClearRedoOperationRecords(); 428 void AddOperationRecord(const OperationRecord& record); 429 bool HandleOnEscape() override; 430 void HandleOnUndoAction() override; 431 void HandleOnRedoAction() override; 432 void CursorMove(CaretMoveIntent direction) override; 433 bool BeforeStatusCursorMove(bool isLeft); 434 bool CursorMoveLeft(); 435 bool CursorMoveRight(); 436 bool CursorMoveUp(); 437 bool CursorMoveDown(); 438 bool CursorMoveLeftWord(); 439 bool CursorMoveRightWord(); 440 bool CursorMoveToParagraphBegin(); 441 bool CursorMoveToParagraphEnd(); 442 bool CursorMoveHome(); 443 bool CursorMoveEnd(); 444 void CalcLineSidesIndexByPosition(int32_t& startIndex, int32_t& endIndex); 445 RectF CalcLineInfoByPosition(); 446 CaretOffsetInfo GetCaretOffsetInfoByPosition(int32_t position = -1); 447 int32_t CalcMoveUpPos(float& leadingMarginOffset); 448 int32_t CalcMoveDownPos(float& leadingMarginOffset); 449 int32_t CalcLineBeginPosition(); 450 float GetTextThemeFontSize(); 451 int32_t CalcLineEndPosition(int32_t index = -1); 452 bool CursorMoveLineBegin(); 453 bool CursorMoveLineEnd(); 454 void HandleSelectFontStyle(KeyCode code) override; 455 void HandleSelectFontStyleWrapper(KeyCode code, TextStyle& spanStyle); 456 void HandleOnShowMenu() override; 457 int32_t HandleSelectPosition(bool isForward); 458 int32_t HandleSelectParagraghPos(bool direction); 459 PositionType GetPositionTypeFromLine(); 460 int32_t HandleSelectWrapper(CaretMoveIntent direction, int32_t fixedPos); 461 void AIDeleteComb(int32_t start, int32_t end, int32_t& aiPosition, bool direction); 462 bool HandleOnDeleteComb(bool backward) override; 463 int32_t GetLeftWordPosition(int32_t caretPosition); 464 int32_t GetRightWordPosition(int32_t caretPosition); 465 int32_t GetParagraphBeginPosition(int32_t caretPosition); 466 int32_t GetParagraphEndPosition(int32_t caretPosition); 467 int32_t CaretPositionSelectEmoji(CaretMoveIntent direction); 468 void HandleSelect(CaretMoveIntent direction) override; 469 void SetCaretPositionWithAffinity(PositionWithAffinity positionWithAffinity); 470 bool SetCaretPosition(int32_t pos, bool needNotifyImf = true); 471 int32_t GetCaretPosition(); 472 int32_t GetTextContentLength() override; 473 bool GetCaretVisible() const; 474 OffsetF CalcCursorOffsetByPosition( 475 int32_t position, float& selectLineHeight, bool downStreamFirst = false, bool needLineHighest = true); 476 bool IsCustomSpanInCaretPos(int32_t position, bool downStreamFirst); 477 void CopyTextSpanStyle(RefPtr<SpanNode>& source, RefPtr<SpanNode>& target, bool needLeadingMargin = false); 478 void CopyTextSpanFontStyle(RefPtr<SpanNode>& source, RefPtr<SpanNode>& target); 479 void CopyTextSpanLineStyle(RefPtr<SpanNode>& source, RefPtr<SpanNode>& target, bool needLeadingMargin = false); 480 void CopyGestureOption(const RefPtr<SpanNode>& source, RefPtr<SpanNode>& target); 481 int32_t TextSpanSplit(int32_t position, bool needLeadingMargin = false); 482 SpanPositionInfo GetSpanPositionInfo(int32_t position); 483 std::function<ImageSourceInfo()> CreateImageSourceInfo(const ImageSpanOptions& options); 484 void DeleteSpans(const RangeOptions& options); 485 void DeleteSpansOperation(int32_t start, int32_t end); 486 void DeleteSpanByRange(int32_t start, int32_t end, SpanPositionInfo info); 487 void DeleteSpansByRange(int32_t start, int32_t end, SpanPositionInfo startInfo, SpanPositionInfo endInfo); 488 void ClearContent(const RefPtr<UINode>& child); 489 void CloseSelectionMenu(); 490 bool SetCaretOffset(int32_t caretPosition); 491 void ResetFirstNodeStyle(); 492 bool DoDeleteActions(int32_t currentPosition, int32_t length, RichEditorDeleteValue& info); 493 494 void UpdateSpanStyle(int32_t start, int32_t end, const TextStyle& textStyle, const ImageSpanAttribute& imageStyle); 495 std::string GetContentBySpans(); 496 ResultObject TextEmojiSplit(int32_t& start, int32_t end, std::string& content); 497 SelectionInfo GetEmojisBySelect(int32_t start, int32_t end); 498 void MixTextEmojiUpdateStyle(int32_t start, int32_t end, TextStyle textStyle, ImageSpanAttribute imageStyle); 499 void SetSelectSpanStyle(int32_t start, int32_t end, KeyCode code, bool isStart); 500 void GetSelectSpansPositionInfo( 501 int32_t& start, int32_t& end, SpanPositionInfo& startPositionSpanInfo, SpanPositionInfo& endPositionSpanInfo); 502 std::list<RefPtr<UINode>>::const_iterator GetSpanNodeIter(int32_t index); 503 std::list<SpanPosition> GetSelectSpanSplit( 504 SpanPositionInfo& startPositionSpanInfo, SpanPositionInfo& endPositionSpanInfo); 505 std::list<SpanPosition> GetSelectSpanInfo(int32_t start, int32_t end); 506 bool IsTextSpanFromResult(int32_t& start, int32_t& end, KeyCode code); 507 void UpdateSelectSpanStyle(int32_t start, int32_t end, KeyCode code); 508 bool SymbolSpanUpdateStyle(RefPtr<SpanNode>& spanNode, struct UpdateSpanStyle updateSpanStyle, TextStyle textStyle); 509 void SetUpdateSpanStyle(struct UpdateSpanStyle updateSpanStyle); 510 struct UpdateSpanStyle GetUpdateSpanStyle(); 511 void UpdateParagraphStyle(int32_t start, int32_t end, const struct UpdateParagraphStyle& style); 512 void UpdateParagraphStyle(RefPtr<SpanNode> spanNode, const struct UpdateParagraphStyle& style); 513 std::vector<ParagraphInfo> GetParagraphInfo(int32_t start, int32_t end); 514 void SetTypingStyle(std::optional<struct UpdateSpanStyle> typingStyle, std::optional<TextStyle> textStyle); 515 std::optional<struct UpdateSpanStyle> GetTypingStyle(); 516 int32_t AddImageSpan(const ImageSpanOptions& options, bool isPaste = false, int32_t index = -1, 517 bool updateCaret = true); 518 int32_t AddTextSpan(TextSpanOptions options, bool isPaste = false, int32_t index = -1); 519 int32_t AddTextSpanOperation(const TextSpanOptions& options, bool isPaste = false, int32_t index = -1, 520 bool needLeadingMargin = false, bool updateCaretPosition = true); 521 int32_t AddSymbolSpan(const SymbolSpanOptions& options, bool isPaste = false, int32_t index = -1); 522 int32_t AddSymbolSpanOperation(const SymbolSpanOptions& options, bool isPaste = false, int32_t index = -1); 523 void AddSpanItem(const RefPtr<SpanItem>& item, int32_t offset); 524 int32_t AddPlaceholderSpan(const RefPtr<UINode>& customNode, const SpanOptionBase& options); 525 void HandleSelectOverlayWithOptions(const SelectionOptions& options); 526 void SetSelection(int32_t start, int32_t end, const std::optional<SelectionOptions>& options = std::nullopt, 527 bool isForward = false); 528 bool ResetOnInvalidSelection(int32_t start, int32_t end); 529 void RefreshSelectOverlay(bool isMousePressed, bool selectedTypeChange); 530 bool IsShowHandle(); 531 void UpdateSelectionInfo(int32_t start, int32_t end); 532 bool IsEditing(); 533 std::u16string GetLeftTextOfCursor(int32_t number) override; 534 std::u16string GetRightTextOfCursor(int32_t number) override; 535 int32_t GetTextIndexAtCursor() override; 536 void ShowSelectOverlay(const RectF& firstHandle, const RectF& secondHandle, bool isCopyAll = false, 537 TextResponseType responseType = TextResponseType::LONG_PRESS, bool handlReverse = false); 538 void CheckEditorTypeChange(); 539 int32_t GetHandleIndex(const Offset& offset) const override; 540 void OnAreaChangedInner() override; 541 void CreateHandles() override; 542 void ShowHandles(const bool isNeedShowHandles) override; 543 void ShowHandles() override; 544 void HandleMenuCallbackOnSelectAll(); 545 void HandleOnSelectAll() override; 546 void OnCopyOperation(bool isUsingExternalKeyboard = false); 547 void HandleOnCopy(bool isUsingExternalKeyboard = false) override; 548 void HandleDraggableFlag(bool isTouchSelectArea); 549 void SetIsTextDraggable(bool isTextDraggable = true) override; 550 bool JudgeContentDraggable(); 551 std::pair<OffsetF, float> CalculateCaretOffsetAndHeight(); 552 std::pair<OffsetF, float> CalculateEmptyValueCaretRect(); 553 TextAlign GetTextAlignByDirection(); 554 void RemoveEmptySpan(std::set<int32_t, std::greater<int32_t>>& deleteSpanIndexs); 555 void RemoveEmptySpanItems(); 556 void RemoveEmptySpanNodes(); 557 void RemoveEmptySpans(); 558 RefPtr<GestureEventHub> GetGestureEventHub(); 559 float GetSelectedMaxWidth(); 560 void AfterAddImage(RichEditorChangeValue& changeValue); 561 void OnWindowHide() override; 562 bool BeforeAddImage(RichEditorChangeValue& changeValue, const ImageSpanOptions& options, int32_t insertIndex); 563 RefPtr<SpanString> ToStyledString(int32_t start, int32_t end); 564 SelectionInfo FromStyledString(const RefPtr<SpanString>& spanString); 565 bool BeforeAddSymbol(RichEditorChangeValue& changeValue, const SymbolSpanOptions& options); 566 void AfterAddSymbol(RichEditorChangeValue& changeValue); 567 IsUsingMouse()568 bool IsUsingMouse() const 569 { 570 return isMousePressed_; 571 } 572 ResetIsMousePressed()573 void ResetIsMousePressed() 574 { 575 isMousePressed_ = false; 576 } 577 GetSelectionMenuOffset()578 OffsetF GetSelectionMenuOffset() const 579 { 580 return selectionMenuOffsetByMouse_; 581 } 582 SetLastClickOffset(const OffsetF & lastClickOffset)583 void SetLastClickOffset(const OffsetF& lastClickOffset) 584 { 585 lastClickOffset_ = lastClickOffset; 586 } 587 ResetLastClickOffset()588 void ResetLastClickOffset() 589 { 590 lastClickOffset_.SetX(-1); 591 lastClickOffset_.SetY(-1); 592 } 593 GetCaretSpanIndex()594 int32_t GetCaretSpanIndex() 595 { 596 return caretSpanIndex_; 597 } 598 GetParagraphs()599 std::list<ParagraphManager::ParagraphInfo> GetParagraphs() const override 600 { 601 return paragraphs_.GetParagraphs(); 602 } 603 GetFirstHandleInfo()604 std::optional<SelectHandleInfo> GetFirstHandleInfo() const 605 { 606 return selectOverlay_->GetFirstHandleInfo(); 607 } 608 GetSecondHandleInfo()609 std::optional<SelectHandleInfo> GetSecondHandleInfo() const 610 { 611 return selectOverlay_->GetSecondHandleInfo(); 612 } 613 614 RectF GetCaretRect() const override; 615 void CloseSelectOverlay() override; 616 void CloseHandleAndSelect() override; 617 void CalculateHandleOffsetAndShowOverlay(bool isUsingMouse = false); 618 void CalculateDefaultHandleHeight(float& height) override; 619 bool IsSingleHandle(); 620 bool IsHandlesShow() override; 621 void CopySelectionMenuParams(SelectOverlayInfo& selectInfo, TextResponseType responseType); 622 std::function<void(Offset)> GetThumbnailCallback() override; 623 void HandleOnDragStatusCallback( 624 const DragEventType& dragEventType, const RefPtr<NotifyDragEvent>& notifyDragEvent) override; 625 void ResetSelection(); 626 bool BetweenSelection(const Offset& globalOffset); 627 bool BetweenSelectedPosition(const Offset& globalOffset) override; 628 void HandleSurfaceChanged(int32_t newWidth, int32_t newHeight, int32_t prevWidth, int32_t prevHeight) override; 629 void HandleSurfacePositionChanged(int32_t posX, int32_t posY) override; 630 bool RequestCustomKeyboard(); 631 bool CloseCustomKeyboard(); GetPasteStr()632 const std::string& GetPasteStr() const 633 { 634 return pasteStr_; 635 } AddPasteStr(const std::string & addedStr)636 void AddPasteStr(const std::string& addedStr) 637 { 638 pasteStr_.append(addedStr); 639 } ClearPasteStr()640 void ClearPasteStr() 641 { 642 pasteStr_.clear(); 643 } SetCustomKeyboard(const std::function<void ()> && keyboardBuilder)644 void SetCustomKeyboard(const std::function<void()>&& keyboardBuilder) 645 { 646 if (customKeyboardBuilder_ && isCustomKeyboardAttached_ && !keyboardBuilder) { 647 // close customKeyboard and request system keyboard 648 CloseCustomKeyboard(); 649 customKeyboardBuilder_ = keyboardBuilder; // refresh current keyboard 650 RequestKeyboard(false, true, true); 651 return; 652 } 653 if (!customKeyboardBuilder_ && keyboardBuilder) { 654 // close system keyboard and request custom keyboard 655 #if defined(OHOS_STANDARD_SYSTEM) && !defined(PREVIEW) 656 if (imeShown_) { 657 CloseKeyboard(true); 658 customKeyboardBuilder_ = keyboardBuilder; // refresh current keyboard 659 RequestKeyboard(false, true, true); 660 return; 661 } 662 #endif 663 } 664 customKeyboardBuilder_ = keyboardBuilder; 665 } 666 void BindSelectionMenu(TextResponseType type, TextSpanType richEditorType, std::function<void()>& menuBuilder, 667 std::function<void(int32_t, int32_t)>& onAppear, std::function<void()>& onDisappear); ClearSelectionMenu()668 void ClearSelectionMenu() 669 { 670 selectionMenuMap_.clear(); 671 } 672 void DumpInfo() override; 673 void MouseDoubleClickParagraphEnd(int32_t& index); 674 void AdjustSelectionExcludeSymbol(int32_t& start, int32_t& end); 675 void InitSelection(const Offset& pos); 676 bool HasFocus() const; 677 void OnColorConfigurationUpdate() override; 678 bool IsDisabled() const; 679 float GetLineHeight() const override; 680 size_t GetLineCount() const override; 681 TextLineMetrics GetLineMetrics(int32_t lineNumber) override; 682 float GetLetterSpacing() const; 683 std::vector<RectF> GetTextBoxes() override; 684 bool OnBackPressed() override; 685 bool IsShowSelectMenuUsingMouse(); 686 687 // Add for Scroll 688 689 void OnAttachToFrameNode() override; 690 OnDetachFromFrameNode(FrameNode * node)691 void OnDetachFromFrameNode(FrameNode* node) override 692 { 693 TextPattern::OnDetachFromFrameNode(node); 694 ScrollablePattern::OnDetachFromFrameNode(node); 695 } 696 IsAtBottom()697 bool IsAtBottom() const override 698 { 699 return true; 700 } 701 IsAtTop()702 bool IsAtTop() const override 703 { 704 return true; 705 } 706 UpdateCurrentOffset(float offset,int32_t source)707 bool UpdateCurrentOffset(float offset, int32_t source) override 708 { 709 return true; 710 } 711 GetTextRect()712 const RectF& GetTextRect() override 713 { 714 return richTextRect_; 715 } 716 GetScrollOffset()717 float GetScrollOffset() const 718 { 719 return scrollOffset_; 720 } 721 GetScrollControllerBar()722 RefPtr<ScrollBar> GetScrollControllerBar() 723 { 724 return GetScrollBar(); 725 } 726 727 bool OnScrollCallback(float offset, int32_t source) override; 728 void OnScrollEndCallback() override; IsScrollable()729 bool IsScrollable() const override 730 { 731 return scrollable_; 732 } 733 IsAtomicNode()734 bool IsAtomicNode() const override 735 { 736 return true; 737 } 738 GetClientHost()739 RefPtr<FrameNode> GetClientHost() const override 740 { 741 return GetHost(); 742 } 743 744 bool IsSelectAreaVisible(); 745 746 void ResetDragOption() override; 747 bool NeedShowAIDetect() override; 748 GetEditorType()749 TextSpanType GetEditorType() const 750 { 751 return selectedType_.value_or(TextSpanType::NONE); 752 } 753 754 std::string GetPlaceHolder() const; 755 756 void HandleOnCameraInput() override; 757 void HandleOnAIWrite(); 758 void GetAIWriteInfo(AIWriteInfo& info); 759 void HandleAIWriteResult(int32_t start, int32_t end, std::vector<uint8_t>& buffer); 760 void InsertSpanByBackData(RefPtr<SpanString>& spanString); 761 void AddSpansAndReplacePlaceholder(RefPtr<SpanString>& spanString); 762 void ReplacePlaceholderWithRawSpans(const RefPtr<SpanItem>& spanItem, size_t& index, size_t& textIndex); 763 void SetSubSpansWithAIWrite(RefPtr<SpanString>& spanString, int32_t start, int32_t end); 764 SymbolSpanOptions GetSymbolSpanOptions(const RefPtr<SpanItem>& spanItem); 765 bool IsShowAIWrite(); 766 RefPtr<FocusHub> GetFocusHub() const; 767 SetShowSelect(bool isShowSelect)768 void SetShowSelect(bool isShowSelect) 769 { 770 showSelect_ = isShowSelect; 771 } 772 void GetCaretMetrics(CaretMetricsF& caretCaretMetric) override; 773 774 const std::list<RefPtr<UINode>>& GetAllChildren() const override; 775 776 void OnVirtualKeyboardAreaChanged() override; 777 IsShowPlaceholder()778 bool IsShowPlaceholder() const 779 { 780 return isShowPlaceholder_; 781 } 782 783 bool SetPlaceholder(std::vector<std::list<RefPtr<SpanItem>>>& spanItemList); 784 SetCaretColor(const Color & caretColor)785 void SetCaretColor(const Color& caretColor) 786 { 787 caretColor_ = caretColor; 788 IF_TRUE(SelectOverlayIsOn(), selectOverlay_->UpdateHandleColor()); 789 } 790 791 Color GetCaretColor(); 792 SetSelectedBackgroundColor(const Color & selectedBackgroundColor)793 void SetSelectedBackgroundColor(const Color& selectedBackgroundColor) 794 { 795 selectedBackgroundColor_ = selectedBackgroundColor; 796 } 797 798 Color GetSelectedBackgroundColor(); 799 800 void SetCustomKeyboardOption(bool supportAvoidance); 801 802 void StopEditing(); 803 void ResetKeyboardIfNeed(); 804 HandleOnEnter()805 void HandleOnEnter() override 806 { 807 PerformAction(GetTextInputActionValue(GetDefaultTextInputAction()), false); 808 } 809 GetCaretIndex()810 int32_t GetCaretIndex() const override 811 { 812 return caretPosition_; 813 } 814 GetContentWideTextLength()815 int32_t GetContentWideTextLength() override 816 { 817 return GetTextContentLength(); 818 } 819 GetCaretOffset()820 OffsetF GetCaretOffset() const override 821 { 822 // only used in magnifier, return position of the handle that is currently moving 823 return movingHandleOffset_; 824 } 825 SetMovingHandleOffset(const OffsetF & handleOffset)826 void SetMovingHandleOffset(const OffsetF& handleOffset) 827 { 828 movingHandleOffset_ = handleOffset; 829 } 830 GetParentGlobalOffset()831 OffsetF GetParentGlobalOffset() const override 832 { 833 return parentGlobalOffset_; 834 } 835 GetFirstHandleOffset()836 OffsetF GetFirstHandleOffset() const override 837 { 838 return textSelector_.firstHandle.GetOffset(); 839 } 840 GetSecondHandleOffset()841 OffsetF GetSecondHandleOffset() const override 842 { 843 return textSelector_.secondHandle.GetOffset(); 844 } 845 846 OffsetF GetTextPaintOffset() const override; 847 OffsetF GetPaintRectGlobalOffset() const; 848 // original local point to transformed global point. 849 void HandlePointWithTransform(OffsetF& point); 850 851 float GetCrossOverHeight() const; 852 CreateAccessibilityProperty()853 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 854 { 855 return MakeRefPtr<RichEditorAccessibilityProperty>(); 856 } 857 858 void AdjustSelector(int32_t& index, HandleType handleType, 859 SelectorAdjustPolicy policy = SelectorAdjustPolicy::INCLUDE); 860 void AdjustSelector(int32_t& start, int32_t& end, SelectorAdjustPolicy policy = SelectorAdjustPolicy::INCLUDE); 861 bool AdjustSelectorForSymbol(int32_t& index, HandleType handleType, SelectorAdjustPolicy policy); 862 bool AdjustSelectorForEmoji(int32_t& index, HandleType handleType, SelectorAdjustPolicy policy); 863 EmojiRelation GetEmojiRelation(int index); 864 void UpdateSelector(int32_t start, int32_t end); 865 void UpdateSelectionType(const SelectionInfo& textSelectInfo); 866 std::list<RefPtr<SpanItem>>::iterator GetSpanIter(int32_t index); 867 DumpAdvanceInfo()868 void DumpAdvanceInfo() override {} 869 SetContentChange(bool onChange)870 void SetContentChange(bool onChange) 871 { 872 contentChange_ = onChange; 873 } 874 GetClipboard()875 RefPtr<Clipboard> GetClipboard() override 876 { 877 return clipboard_; 878 } 879 880 PositionWithAffinity GetGlyphPositionAtCoordinate(int32_t x, int32_t y) override; 881 void OnSelectionMenuOptionsUpdate( 882 const NG::OnCreateMenuCallback&& onCreateMenuCallback, const NG::OnMenuItemClickCallback&& onMenuItemClick); 883 RectF GetTextContentRect(bool isActualText = false) const override 884 { 885 return contentRect_; 886 } 887 888 void PreferredParagraph(); 889 GetPresetParagraph()890 const RefPtr<Paragraph>& GetPresetParagraph() 891 { 892 return presetParagraph_; 893 } 894 IsMoveCaretAnywhere()895 bool IsMoveCaretAnywhere() const 896 { 897 return isMoveCaretAnywhere_; 898 } 899 OnFrameNodeChanged(FrameNodeChangeInfoFlag flag)900 void OnFrameNodeChanged(FrameNodeChangeInfoFlag flag) override 901 { 902 selectOverlay_->OnAncestorNodeChanged(flag); 903 } 904 905 bool IsResponseRegionExpandingNeededForStylus(const TouchEvent& touchEvent) const override; 906 907 RectF ExpandDefaultResponseRegion(RectF& rect) override; ConsumeChildrenAdjustment(const OffsetF &)908 bool ConsumeChildrenAdjustment(const OffsetF& /* offset */) override 909 { 910 return true; 911 } 912 913 TextStyle GetDefaultTextStyle(); SetEnableHapticFeedback(bool isEnabled)914 void SetEnableHapticFeedback(bool isEnabled) 915 { 916 isEnableHapticFeedback_ = isEnabled; 917 } 918 SetRequestKeyboardOnFocus(bool needToRequest)919 void SetRequestKeyboardOnFocus(bool needToRequest) 920 { 921 needToRequestKeyboardOnFocus_ = needToRequest; 922 } 923 924 bool IsTextEditableForStylus() const override; 925 926 NG::DragDropInfo HandleDragStart(const RefPtr<Ace::DragEvent>& event, const std::string& extraParams); 927 RequestFocusWhenSelected()928 void RequestFocusWhenSelected() 929 { 930 CHECK_NULL_VOID(!HasFocus()); 931 CHECK_NULL_VOID(IsSelected()); 932 auto focusHub = GetFocusHub(); 933 CHECK_NULL_VOID(focusHub); 934 focusHub->RequestFocusImmediately(); 935 isOnlyRequestFocus_ = true; 936 } 937 GetBarDisplayMode()938 DisplayMode GetBarDisplayMode() 939 { 940 return barDisplayMode_.value_or(DisplayMode::AUTO); 941 } 942 943 void SetImagePreviewMenuParam(std::function<void()>& builder, const SelectMenuParam& menuParam); 944 945 protected: 946 bool CanStartAITask() override; 947 948 template<typename T> GetTheme()949 RefPtr<T> GetTheme() 950 { 951 auto pipelineContext = GetContext(); 952 CHECK_NULL_RETURN(pipelineContext, {}); 953 return pipelineContext->GetTheme<T>(); 954 } 955 956 private: 957 friend class RichEditorSelectOverlay; 958 RefPtr<RichEditorSelectOverlay> selectOverlay_; 959 Offset ConvertGlobalToLocalOffset(const Offset& globalOffset); 960 void UpdateSelectMenuInfo(SelectMenuInfo& selectInfo); 961 void HandleOnPaste() override; 962 void HandleOnCut() override; 963 void InitClickEvent(const RefPtr<GestureEventHub>& gestureHub) override; 964 void InitFocusEvent(const RefPtr<FocusHub>& focusHub); 965 void HandleBlurEvent(); 966 void HandleFocusEvent(); 967 void HandleClickEvent(GestureEvent& info); 968 void HandleSingleClickEvent(GestureEvent& info); 969 bool HandleClickSelection(const OHOS::Ace::GestureEvent& info); 970 bool IsClickEventOnlyForMenuToggle(const OHOS::Ace::GestureEvent& info); 971 Offset ConvertTouchOffsetToTextOffset(const Offset& touchOffset); 972 bool IsShowSingleHandleByClick(const OHOS::Ace::GestureEvent& info, int32_t lastCaretPosition, 973 const RectF& lastCaretRect, bool isCaretTwinkling); 974 bool RepeatClickCaret(const Offset& offset, int32_t lastCaretPosition, const RectF& lastCaretRect); 975 bool RepeatClickCaret(const Offset& offset, const RectF& lastCaretRect); 976 void CreateAndShowSingleHandle(); 977 void MoveCaretAndStartFocus(const Offset& offset); 978 void HandleDoubleClickEvent(GestureEvent& info); 979 bool HandleUserClickEvent(GestureEvent& info); 980 bool HandleUserLongPressEvent(GestureEvent& info); 981 bool HandleUserGestureEvent( 982 GestureEvent& info, std::function<bool(RefPtr<SpanItem> item, GestureEvent& info)>&& gestureFunc); 983 void HandleOnlyImageSelected(const Offset& globalOffset, const SourceTool sourceTool); 984 void CalcCaretInfoByClick(const Offset& touchOffset); 985 std::pair<OffsetF, float> CalcAndRecordLastClickCaretInfo(const Offset& textOffset); 986 void HandleEnabled(); 987 void InitMouseEvent(); 988 void ScheduleCaretTwinkling(); 989 void OnCaretTwinkling(); 990 void StartTwinkling(); 991 void ShowCaretWithoutTwinkling(); 992 void StopTwinkling(); 993 void UpdateFontFeatureTextStyle( 994 RefPtr<SpanNode>& spanNode, struct UpdateSpanStyle& updateSpanStyle, TextStyle& textStyle); 995 void UpdateDecoration(RefPtr<SpanNode>& spanNode, struct UpdateSpanStyle& updateSpanStyle, TextStyle& textStyle); 996 void UpdateTextStyle(RefPtr<SpanNode>& spanNode, struct UpdateSpanStyle updateSpanStyle, TextStyle textStyle); 997 void UpdateSymbolStyle(RefPtr<SpanNode>& spanNode, struct UpdateSpanStyle updateSpanStyle, TextStyle textStyle); 998 void UpdateImageStyle(RefPtr<FrameNode>& imageNode, const ImageSpanAttribute& imageStyle); 999 void UpdateImageAttribute(RefPtr<FrameNode>& imageNode, const ImageSpanAttribute& imageStyle); 1000 void InitTouchEvent(); 1001 void InitPanEvent(); 1002 bool SelectOverlayIsOn(); 1003 void HandleLongPress(GestureEvent& info); 1004 void HandleDoubleClickOrLongPress(GestureEvent& info); 1005 void HandleDoubleClickOrLongPress(GestureEvent& info, RefPtr<FrameNode> host); 1006 void StartVibratorByLongPress(); 1007 std::string GetPositionSpansText(int32_t position, int32_t& startSpan); 1008 void FireOnSelect(int32_t selectStart, int32_t selectEnd); 1009 void FireOnSelectionChange(const int32_t caretPosition); 1010 void FireOnSelectionChange(const TextSelector& selector); 1011 void FireOnSelectionChange(int32_t selectStart, int32_t selectEnd, bool isForced = false); 1012 void MouseRightFocus(const MouseInfo& info); 1013 bool IsScrollBarPressed(const MouseInfo& info); 1014 void HandleMouseLeftButtonMove(const MouseInfo& info); 1015 void HandleMouseLeftButtonPress(const MouseInfo& info); 1016 void HandleMouseLeftButtonRelease(const MouseInfo& info); 1017 void HandleMouseLeftButton(const MouseInfo& info); 1018 void HandleMouseRightButton(const MouseInfo& info); 1019 void HandleMouseEvent(const MouseInfo& info); 1020 void HandleTouchEvent(const TouchEventInfo& info); 1021 void HandleTouchDown(const TouchEventInfo& info); 1022 void HandleTouchUp(); 1023 void HandleTouchUpAfterLongPress(); 1024 void HandleTouchMove(const Offset& offset); 1025 void UpdateCaretByTouchMove(const Offset& offset); 1026 Offset AdjustLocalOffsetOnMoveEvent(const Offset& originalOffset); 1027 void StartVibratorByIndexChange(int32_t currentIndex, int32_t preIndex); 1028 void InitLongPressEvent(const RefPtr<GestureEventHub>& gestureHub); 1029 void UseHostToUpdateTextFieldManager(); 1030 void UpdateTextFieldManager(const Offset& offset, float height); 1031 void ScrollToSafeArea() const override; 1032 void InitDragDropEvent(); 1033 void OnDragStartAndEnd(); 1034 void onDragDropAndLeave(); 1035 void ClearDragDropEvent(); 1036 void OnDragMove(const RefPtr<OHOS::Ace::DragEvent>& event); 1037 void OnDragEnd(const RefPtr<Ace::DragEvent>& event); 1038 void ResetDragSpanItems(); 1039 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override; 1040 std::string GetPlaceHolderInJson() const; 1041 std::string GetTextColorInJson(const std::optional<Color>& value) const; 1042 void FillPreviewMenuInJson(const std::unique_ptr<JsonValue>& jsonValue) const override; 1043 void ResetSelectionAfterAddSpan(bool isPaste); 1044 void SetResultObjectText(ResultObject& resultObject, const RefPtr<SpanItem>& spanItem) override; 1045 SelectionInfo GetAdjustedSelectionInfo(const SelectionInfo& textSelectInfo); ResetAfterTextChange()1046 void ResetAfterTextChange() override {}; 1047 void AddOprationWhenAddImage(int32_t beforeCaretPos); 1048 void UpdateSpanNode(RefPtr<SpanNode> spanNode, const TextSpanOptions& options); 1049 void InitPlaceholderSpansMap( 1050 RefPtr<SpanItem>& newSpanItem, const RefPtr<SpanItem>& spanItem, size_t& index, size_t& placeholderGains); 1051 void ReplacePlaceholderWithCustomSpan(const RefPtr<SpanItem>& spanItem, size_t& index, size_t& textIndex); 1052 void ReplacePlaceholderWithSymbolSpan(const RefPtr<SpanItem>& spanItem, size_t& index, size_t& textIndex); 1053 void ReplacePlaceholderWithImageSpan(const RefPtr<SpanItem>& spanItem, size_t& index, size_t& textIndex); AddDragFrameNodeToManager(const RefPtr<FrameNode> & frameNode)1054 void AddDragFrameNodeToManager(const RefPtr<FrameNode>& frameNode) 1055 { 1056 auto host = GetHost(); 1057 CHECK_NULL_VOID(host); 1058 auto context = host->GetContext(); 1059 CHECK_NULL_VOID(context); 1060 auto dragDropManager = context->GetDragDropManager(); 1061 CHECK_NULL_VOID(dragDropManager); 1062 dragDropManager->AddDragFrameNode(frameNode->GetId(), AceType::WeakClaim(AceType::RawPtr(frameNode))); 1063 } 1064 RemoveDragFrameNodeFromManager(const RefPtr<FrameNode> & frameNode)1065 void RemoveDragFrameNodeFromManager(const RefPtr<FrameNode>& frameNode) 1066 { 1067 auto host = GetHost(); 1068 CHECK_NULL_VOID(host); 1069 auto context = host->GetContext(); 1070 CHECK_NULL_VOID(context); 1071 auto dragDropManager = context->GetDragDropManager(); 1072 CHECK_NULL_VOID(dragDropManager); 1073 dragDropManager->RemoveDragFrameNode(frameNode->GetId()); 1074 } 1075 1076 void HandleCursorOnDragMoved(const RefPtr<NotifyDragEvent>& notifyDragEvent); 1077 void HandleCursorOnDragLeaved(const RefPtr<NotifyDragEvent>& notifyDragEvent); 1078 void HandleCursorOnDragEnded(const RefPtr<NotifyDragEvent>& notifyDragEvent); 1079 1080 int32_t GetParagraphLength(const std::list<RefPtr<UINode>>& spans) const; 1081 // REQUIRES: 0 <= start < end 1082 std::vector<RefPtr<SpanNode>> GetParagraphNodes(int32_t start, int32_t end) const; 1083 void OnHover(bool isHover); 1084 bool RequestKeyboard(bool isFocusViewChanged, bool needStartTwinkling, bool needShowSoftKeyboard); 1085 void UpdateCaretInfoToController(); 1086 #if defined(ENABLE_STANDARD_INPUT) 1087 bool EnableStandardInput(bool needShowSoftKeyboard); 1088 std::optional<MiscServices::TextConfig> GetMiscTextConfig(); 1089 #else 1090 bool UnableStandardInput(bool isFocusViewChanged); 1091 #endif 1092 1093 bool HasConnection() const; 1094 bool CloseKeyboard(bool forceClose) override; 1095 void CalcInsertValueObj(TextInsertValueInfo& info); 1096 void CalcDeleteValueObj(int32_t currentPosition, int32_t length, RichEditorDeleteValue& info); 1097 RefPtr<SpanNode> GetSpanNodeBySpanItem(const RefPtr<SpanItem> spanItem); 1098 int32_t DeleteValueSetBuilderSpan(const RefPtr<SpanItem>& spanItem, RichEditorAbstractSpanResult& spanResult); 1099 int32_t DeleteValueSetImageSpan(const RefPtr<SpanItem>& spanItem, RichEditorAbstractSpanResult& spanResult); 1100 int32_t DeleteValueSetSymbolSpan(const RefPtr<SpanItem>& spanItem, RichEditorAbstractSpanResult& spanResult); 1101 int32_t DeleteValueSetTextSpan(const RefPtr<SpanItem>& spanItem, int32_t currentPosition, int32_t length, 1102 RichEditorAbstractSpanResult& spanResult); 1103 void DeleteByDeleteValueInfo(const RichEditorDeleteValue& info); 1104 int32_t ProcessDeleteNodes(std::list<RichEditorAbstractSpanResult>& deleteSpans); 1105 bool OnKeyEvent(const KeyEvent& keyEvent); 1106 void MoveCaretAfterTextChange(); 1107 bool BeforeIMEInsertValue(const std::string& insertValue); 1108 void AfterInsertValue( 1109 const RefPtr<SpanNode>& spanNode, int32_t insertValueLength, bool isCreate, bool isIme = true); 1110 bool AfterIMEInsertValue(const RefPtr<SpanNode>& spanNode, int32_t moveLength, bool isCreate); 1111 RefPtr<SpanNode> InsertValueToBeforeSpan(RefPtr<SpanNode>& spanNodeBefore, const std::string& insertValue); 1112 void SetCaretSpanIndex(int32_t index); 1113 bool HasSameTypingStyle(const RefPtr<SpanNode>& spanNode); 1114 void GetChangeSpanStyle(RichEditorChangeValue& changeValue, std::optional<TextStyle>& spanTextStyle, 1115 std::optional<struct UpdateParagraphStyle>& spanParaStyle, const RefPtr<SpanNode>& spanNode, int32_t spanIndex); 1116 void GetReplacedSpan(RichEditorChangeValue& changeValue, int32_t& innerPosition, const std::string& insertValue, 1117 int32_t textIndex, std::optional<TextStyle> textStyle, std::optional<struct UpdateParagraphStyle> paraStyle, 1118 bool isCreate = false, bool fixDel = true); 1119 void GetReplacedSpanFission(RichEditorChangeValue& changeValue, int32_t& innerPosition, std::string& content, 1120 int32_t startSpanIndex, int32_t offsetInSpan, std::optional<TextStyle> textStyle, 1121 std::optional<struct UpdateParagraphStyle> paraStyle); 1122 void CreateSpanResult(RichEditorChangeValue& changeValue, int32_t& innerPosition, int32_t spanIndex, 1123 int32_t offsetInSpan, int32_t endInSpan, std::string content, std::optional<TextStyle> textStyle, 1124 std::optional<struct UpdateParagraphStyle> paraStyle); 1125 void SetTextStyleToRet(RichEditorAbstractSpanResult& retInfo, const TextStyle& textStyle); 1126 void CalcInsertValueObj(TextInsertValueInfo& info, int textIndex, bool isCreate = false); 1127 void GetDeletedSpan(RichEditorChangeValue& changeValue, int32_t& innerPosition, int32_t length, 1128 RichEditorDeleteDirection direction = RichEditorDeleteDirection::FORWARD, bool isResetSelection = true); 1129 RefPtr<SpanItem> GetDelPartiallySpanItem( 1130 RichEditorChangeValue& changeValue, std::string& originalStr, int32_t& originalPos); 1131 void FixMoveDownChange(RichEditorChangeValue& changeValue, int32_t delLength); 1132 bool BeforeChangeText( 1133 RichEditorChangeValue& changeValue, const OperationRecord& record, RecordType type, int32_t delLength = 0); 1134 void BeforeUndo(RichEditorChangeValue& changeValue, int32_t& innerPosition, const OperationRecord& record); 1135 void BeforeRedo(RichEditorChangeValue& changeValue, int32_t& innerPosition, const OperationRecord& record); 1136 void BeforeDrag(RichEditorChangeValue& changeValue, int32_t& innerPosition, const OperationRecord& record); 1137 bool BeforeChangeText(RichEditorChangeValue& changeValue, const TextSpanOptions& options); 1138 void AfterChangeText(RichEditorChangeValue& changeValue); 1139 1140 // add for scroll. 1141 void UpdateChildrenOffset(); 1142 void MoveFirstHandle(float offset); 1143 void MoveSecondHandle(float offset); 1144 void InitScrollablePattern(); 1145 bool IsReachedBoundary(float offset); 1146 void UpdateScrollBarOffset() override; 1147 void CheckScrollable(); 1148 void UpdateMagnifierStateAfterLayout(bool frameSizeChange); 1149 void UpdateScrollStateAfterLayout(bool shouldDisappear); 1150 void ScheduleAutoScroll(AutoScrollParam param); 1151 void OnAutoScroll(AutoScrollParam param); 1152 void StopAutoScroll(bool hideBarImmediately = true); 1153 void AutoScrollByEdgeDetection(AutoScrollParam param, OffsetF offset, EdgeDetectionStrategy strategy); 1154 float CalcDragSpeed(float hotAreaStart, float hotAreaEnd, float point); 1155 float MoveTextRect(float offset); 1156 void SetNeedMoveCaretToContentRect(); 1157 void MoveCaretToContentRect(); 1158 void MoveCaretToContentRect(const OffsetF& caretOffset, float caretHeight); 1159 void MoveCaretToContentRect(float offset, int32_t source); 1160 bool IsCaretInContentArea(); IsTextArea()1161 bool IsTextArea() const override 1162 { 1163 return true; 1164 } 1165 void ProcessInnerPadding(); 1166 1167 // ai analysis fun 1168 bool NeedAiAnalysis( 1169 const CaretUpdateType targeType, const int32_t pos, const int32_t& spanStart, const std::string& content); 1170 bool IsIndexAfterOrInSymbolOrEmoji(int32_t index); 1171 void AdjustCursorPosition(int32_t& pos); 1172 void AdjustPlaceholderSelection(int32_t& start, int32_t& end, const Offset& pos); 1173 bool AdjustWordSelection(int32_t& start, int32_t& end); 1174 bool IsTouchAtLineEnd(int32_t caretPos, const Offset& textOffset); 1175 bool IsTouchBeforeCaret(int32_t caretPos, const Offset& textOffset); 1176 bool IsClickBoundary(const int32_t position); 1177 IsReachTop()1178 bool IsReachTop() 1179 { 1180 return NearEqual(richTextRect_.GetY(), contentRect_.GetY()); 1181 } 1182 IsReachBottom()1183 bool IsReachBottom() 1184 { 1185 return NearEqual(richTextRect_.Bottom(), contentRect_.Bottom()); 1186 } 1187 1188 void SetSelfAndChildDraggableFalse(const RefPtr<UINode>& customNode); 1189 1190 RectF GetSelectArea(); 1191 bool IsTouchInFrameArea(const PointF& touchPoint); 1192 void HandleOnDragDrop(const RefPtr<OHOS::Ace::DragEvent>& event); 1193 void DeleteForward(int32_t currentPosition, int32_t length); 1194 void HandleOnDragDropTextOperation(const std::string& insertValue, bool isDeleteSelect); 1195 void UndoDrag(const OperationRecord& record); 1196 void RedoDrag(const OperationRecord& record); 1197 void HandleOnDragInsertValueOperation(const std::string& insertValue); 1198 void HandleOnDragInsertValue(const std::string& str); 1199 void HandleOnEditChanged(bool isEditing); 1200 void OnTextInputActionUpdate(TextInputAction value); 1201 void CloseSystemMenu(); 1202 void SetAccessibilityAction() override; 1203 void SetAccessibilityEditAction(); 1204 void HandleTripleClickEvent(OHOS::Ace::GestureEvent& info); 1205 bool CheckTripClickEvent(GestureEvent& info); 1206 void HandleSelect(GestureEvent& info, int32_t selectStart, int32_t selectEnd); 1207 TextStyleResult GetTextStyleBySpanItem(const RefPtr<SpanItem>& spanItem); 1208 ImageStyleResult GetImageStyleBySpanItem(const RefPtr<SpanItem>& spanItem); 1209 void SetSubSpans(RefPtr<SpanString>& spanString, int32_t start, int32_t end); 1210 void SetSubMap(RefPtr<SpanString>& spanString); 1211 void OnCopyOperationExt(RefPtr<PasteDataMix>& pasteData); 1212 void AddSpanByPasteData(const RefPtr<SpanString>& spanString); 1213 void CompleteStyledString(RefPtr<SpanString>& spanString); 1214 void InsertStyledStringByPaste(const RefPtr<SpanString>& spanString); 1215 void HandleOnDragInsertStyledString(const RefPtr<SpanString>& spanString); 1216 void AddSpansByPaste(const std::list<RefPtr<NG::SpanItem>>& spans); 1217 TextSpanOptions GetTextSpanOptions(const RefPtr<SpanItem>& spanItem); 1218 void HandleOnCopyStyledString(); 1219 void HandleOnDragDropStyledString(const RefPtr<OHOS::Ace::DragEvent>& event); 1220 void NotifyExitTextPreview(); 1221 void ProcessInsertValue(const std::string& insertValue, OperationType operationType = OperationType::DEFAULT, 1222 bool calledbyImf = false); 1223 void FinishTextPreviewInner(); 1224 void UpdateSelectionByTouchMove(const Offset& offset); 1225 void MoveCaretAnywhere(const Offset& touchOffset); 1226 void ShowCaretNoTwinkling(const Offset& textOffset); 1227 void TripleClickSection(GestureEvent& info, int32_t start, int32_t end, int32_t pos); 1228 void ProcessOverlayOnSetSelection(const std::optional<SelectionOptions>& options); 1229 std::pair<int32_t, SelectType> JudgeSelectType(const Offset& pos); 1230 bool IsSelectEmpty(int32_t start, int32_t end); 1231 bool AdjustIndexSkipLineSeparator(int32_t& currentPosition); 1232 void RequestKeyboardToEdit(); HandleTasksOnLayoutSwap()1233 void HandleTasksOnLayoutSwap() 1234 { 1235 while (!tasks_.empty()) { 1236 const auto& task = tasks_.front(); 1237 if (task) { 1238 task(); 1239 } 1240 tasks_.pop(); 1241 } 1242 } PostTaskToLayutSwap(std::function<void ()> && task)1243 void PostTaskToLayutSwap(std::function<void()>&& task) 1244 { 1245 tasks_.emplace(std::forward<std::function<void()>>(task)); 1246 } 1247 1248 OffsetF GetGlobalOffset() const; 1249 void EnableImageDrag(const RefPtr<ImageSpanNode>& imageNode, bool isEnable); 1250 void DisableDrag(const RefPtr<ImageSpanNode>& imageNode); 1251 void EnableOneStepDrag(const RefPtr<ImageSpanNode>& imageNode); 1252 void SetImageSelfResponseEvent(bool isEnable); 1253 void CopyDragCallback(const RefPtr<ImageSpanNode>& imageNode); 1254 void SetGestureOptions(UserGestureOptions userGestureOptions, RefPtr<SpanItem> spanItem); 1255 void UpdateImagePreviewParam(); 1256 1257 #if defined(ENABLE_STANDARD_INPUT) 1258 sptr<OHOS::MiscServices::OnTextChangedListener> richEditTextChangeListener_; 1259 #else 1260 RefPtr<TextInputConnection> connection_ = nullptr; 1261 #endif 1262 bool isMouseSelect_ = false; 1263 bool isMousePressed_ = false; 1264 bool isFirstMouseSelect_ = true; 1265 bool leftMousePress_ = false; 1266 bool isLongPress_ = false; 1267 bool isDragging_ = false; 1268 #if defined(OHOS_STANDARD_SYSTEM) && !defined(PREVIEW) 1269 bool imeAttached_ = false; 1270 bool imeShown_ = false; 1271 #endif 1272 1273 bool isTextChange_ = false; 1274 bool caretVisible_ = false; 1275 bool caretTwinkling_ = false; 1276 bool isRichEditorInit_ = false; 1277 bool clickEventInitialized_ = false; 1278 bool focusEventInitialized_ = false; 1279 bool blockPress_ = false; 1280 bool isCustomKeyboardAttached_ = false; 1281 bool usingMouseRightButton_ = false; 1282 bool isCursorAlwaysDisplayed_ = false; 1283 bool isClickOnAISpan_ = false; 1284 bool isOnlyRequestFocus_ = false; 1285 1286 int32_t moveLength_ = 0; 1287 int32_t caretPosition_ = 0; 1288 int32_t caretSpanIndex_ = -1; 1289 long long timestamp_ = 0; 1290 CaretAffinityPolicy caretAffinityPolicy_ = CaretAffinityPolicy::DEFAULT; 1291 OffsetF selectionMenuOffsetByMouse_; 1292 OffsetF selectionMenuOffsetClick_; 1293 OffsetF lastClickOffset_; 1294 std::string pasteStr_; 1295 1296 // still in progress 1297 ParagraphManager paragraphs_; 1298 RefPtr<Paragraph> presetParagraph_; 1299 std::vector<OperationRecord> operationRecords_; 1300 std::vector<OperationRecord> redoOperationRecords_; 1301 1302 RefPtr<TouchEventImpl> touchListener_; 1303 RefPtr<PanEvent> panEvent_; 1304 struct UpdateSpanStyle updateSpanStyle_; 1305 CancelableCallback<void()> caretTwinklingTask_; 1306 RefPtr<RichEditorController> richEditorController_; 1307 RefPtr<RichEditorStyledStringController> richEditorStyledStringController_; 1308 MoveDirection moveDirection_ = MoveDirection::FORWARD; 1309 RectF frameRect_; 1310 std::optional<struct UpdateSpanStyle> typingStyle_; 1311 std::optional<TextStyle> typingTextStyle_; 1312 std::list<ResultObject> dragResultObjects_; 1313 std::optional<Color> caretColor_; 1314 std::optional<Color> selectedBackgroundColor_; 1315 std::function<void()> customKeyboardBuilder_; 1316 std::function<void(int32_t)> caretChangeListener_; 1317 RefPtr<OverlayManager> keyboardOverlay_; 1318 RefPtr<AIWriteAdapter> aiWriteAdapter_ = MakeRefPtr<AIWriteAdapter>(); 1319 Offset selectionMenuOffset_; 1320 // add for scroll 1321 RectF richTextRect_; 1322 float scrollOffset_ = 0.0f; 1323 bool isFirstCallOnReady_ = false; 1324 bool scrollable_ = true; 1325 CancelableCallback<void()> autoScrollTask_; 1326 OffsetF prevAutoScrollOffset_; 1327 // add for ai input analysis 1328 bool hasClicked_ = false; 1329 CaretUpdateType caretUpdateType_ = CaretUpdateType::NONE; 1330 TimeStamp lastClickTimeStamp_; 1331 TimeStamp lastAiPosTimeStamp_; 1332 bool adjusted_ = false; 1333 AutoScrollParam currentScrollParam_; 1334 bool isAutoScrollRunning_ = false; 1335 bool isShowMenu_ = true; 1336 bool isOnlyImageDrag_ = false; 1337 bool isShowPlaceholder_ = false; 1338 TouchAndMoveCaretState moveCaretState_; 1339 // Recorded when touch down or mouse left button press. 1340 OffsetF globalOffsetOnMoveStart_; 1341 SelectionRangeInfo lastSelectionRange_{-1, -1}; 1342 bool isDragSponsor_ = false; 1343 std::pair<int32_t, int32_t> dragRange_ { 0, 0 }; 1344 bool isEditing_ = false; 1345 int32_t dragPosition_ = 0; 1346 // Action when "enter" pressed. 1347 TextInputAction action_ = TextInputAction::NEW_LINE; 1348 // What the keyboard appears. 1349 TextInputType keyboard_ = TextInputType::UNSPECIFIED; 1350 ACE_DISALLOW_COPY_AND_MOVE(RichEditorPattern); 1351 1352 int32_t richEditorInstanceId_ = -1; 1353 int32_t frameId_ = -1; 1354 bool keyboardAvoidance_ = false; 1355 bool contentChange_ = false; 1356 PreviewTextRecord previewTextRecord_; 1357 bool isTextPreviewSupported_ = true; 1358 bool isTouchCaret_ = false; 1359 OffsetF movingHandleOffset_; 1360 float lastFontScale_ = -1; 1361 bool isCaretInContentArea_ = false; 1362 std::vector<TimeStamp> clickInfo_; 1363 std::pair<int32_t, int32_t> initSelector_ = { 0, 0 }; 1364 bool isMoveCaretAnywhere_ = false; 1365 bool previewLongPress_ = false; 1366 bool editingLongPress_ = false; 1367 bool needMoveCaretToContentRect_ = false; 1368 std::queue<std::function<void()>> tasks_; 1369 bool isModifyingContent_ = false; 1370 bool needToRequestKeyboardOnFocus_ = true; 1371 bool isEnableHapticFeedback_ = true; 1372 std::optional<DisplayMode> barDisplayMode_ = std::nullopt; 1373 std::shared_ptr<OneStepDragParam> oneStepDragParam_ = nullptr; 1374 std::unordered_map<std::string, RefPtr<SpanItem>> placeholderSpansMap_; 1375 std::queue<WeakPtr<ImageSpanNode>> dirtyImageNodes; 1376 bool isImageSelfResponseEvent_ = true; 1377 }; 1378 } // namespace OHOS::Ace::NG 1379 1380 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_PATTERN_H 1381