1 /* 2 * Copyright (c) 2024 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_COMMON_AI_PROPERTIES_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_AI_PROPERTIES_H 18 19 #include <set> 20 #include <unordered_map> 21 22 #include "interfaces/inner_api/ace/ai/data_detector_interface.h" 23 #include "interfaces/inner_api/ace/ai/data_url_analyzer.h" 24 25 #include "base/memory/ace_type.h" 26 #include "base/thread/cancelable_callback.h" 27 #include "core/components_ng/base/frame_node.h" 28 #include "core/components_ng/property/property.h" 29 #include "core/components_v2/inspector/utils.h" 30 31 #include "frameworks/core/components/common/layout/constants.h" 32 namespace OHOS::AAFwk { 33 class Want; 34 class WantParams; 35 } // namespace OHOS::AAFwk 36 37 namespace OHOS::Ace { 38 39 namespace NG { 40 class TextPattern; 41 class RichEditorPattern; 42 } 43 44 struct AISpan { 45 int32_t start = 0; 46 int32_t end = 0; 47 std::string content = ""; 48 TextDataDetectType type = TextDataDetectType::INVALID; 49 std::map<std::string, std::string> params; 50 bool operator==(const AISpan& span) const 51 { 52 return start == span.start && end == span.end && content == span.content && type == span.type; 53 } 54 }; 55 class DataDetectorAdapter : public AceType { 56 DECLARE_ACE_TYPE(DataDetectorAdapter, AceType); 57 58 public: 59 DataDetectorAdapter() = default; 60 ~DataDetectorAdapter() override = default; 61 GetHost()62 RefPtr<NG::FrameNode> GetHost() const 63 { 64 return frameNode_.Upgrade(); 65 } SetTextDetectResult(const TextDataDetectResult result)66 void SetTextDetectResult(const TextDataDetectResult result) 67 { 68 textDetectResult_ = result; 69 } 70 void FireFinalResult(); FireOnResult(const std::string & result)71 void FireOnResult(const std::string& result) 72 { 73 if (onResult_) { 74 onResult_(result); 75 } 76 } 77 bool ParseOriText(const std::unique_ptr<JsonValue>& entityJson, std::u16string& text); 78 void PreprocessTextDetect(); 79 void InitTextDetect(int32_t startPos, std::string detectText); 80 void HandleTextUrlDetect(); 81 void HandleUrlResult(std::vector<UrlEntity> urlEntities); 82 void SetTextDetectTypes(const std::string& types); 83 void ParseAIResult(const TextDataDetectResult& result, int32_t startPos); 84 void ParseAIJson(const std::unique_ptr<JsonValue>& jsonValue, TextDataDetectType type, int32_t startPos); 85 void StartAITask(); CancelAITask()86 void CancelAITask() 87 { 88 if (aiDetectDelayTask_) { 89 aiDetectDelayTask_.Cancel(); 90 } 91 aiDetectInitialized_ = false; 92 } 93 struct AIMenuInfo { 94 bool isShowCopy = true; 95 bool isShowSelectText = true; 96 }; 97 98 bool ShowAIEntityMenu( 99 const AISpan& aiSpan, const NG::RectF& aiRect, const RefPtr<NG::FrameNode>& targetNode, AIMenuInfo info); 100 bool GetAiEntityMenuOptions(const AISpan& aiSpan, const RefPtr<NG::FrameNode>& targetNode, AIMenuInfo info, 101 std::vector<std::pair<std::string, std::function<void()>>>& menuOptions); 102 RefPtr<NG::FrameNode> CreateAIEntityMenu( 103 const AISpan& aiSpan, const RefPtr<NG::FrameNode>& targetNode, AIMenuInfo info); 104 void ResponseBestMatchItem(const AISpan& aiSpan); 105 void GetAIEntityMenu(); 106 void MarkDirtyNode() const; 107 private: 108 friend class NG::TextPattern; 109 friend class NG::RichEditorPattern; 110 111 std::function<void()> GetDetectDelayTask(const std::map<int32_t, AISpan>& aiSpanMap); 112 std::function<void()> GetPreviewMenuOptionCallback(TextDataDetectType type, const std::string& content); 113 void OnClickAIMenuOption(const AISpan& aiSpan, const std::pair<std::string, FuncVariant>& menuOption, 114 const RefPtr<NG::FrameNode>& targetNode = nullptr); 115 116 WeakPtr<NG::FrameNode> frameNode_; 117 bool aiDetectInitialized_ = false; 118 bool hasClickedAISpan_ = false; 119 bool pressedByLeftMouse_ = false; 120 bool typeChanged_ = false; 121 bool hasClickedMenuOption_ = false; 122 bool hasUrlType_ = false; 123 bool enablePreviewMenu_ = false; 124 uint8_t aiDetectFlag_ = 0; 125 std::vector<NG::RectF> aiSpanRects_; 126 AISpan clickedAISpan_; 127 std::string textDetectTypes_; 128 std::u16string textForAI_; 129 std::u16string lastTextForAI_; 130 std::set<std::string> textDetectTypesSet_; 131 TextDataDetectResult textDetectResult_; 132 std::function<void(const std::string&)> onResult_; 133 std::function<void(const std::string&)> onClickMenu_; 134 std::map<int32_t, AISpan> aiSpanMap_; 135 CancelableCallback<void()> aiDetectDelayTask_; 136 std::unordered_map<int32_t, std::string> entityJson_; 137 TimeStamp startDetectorTimeStamp_; 138 std::vector<std::string> detectTexts_; 139 int32_t mainContainerId_ = -1; 140 std::optional<Color> entityColor_; 141 std::optional<TextDecoration> entityDecorationType_; 142 std::optional<Color> entityDecorationColor_; 143 std::optional<TextDecorationStyle> entityDecorationStyle_; 144 std::string textDetectConfigStr_; 145 }; 146 } // namespace OHOS::Ace 147 148 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_AI_PROPERTIES_H 149