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 
21 #include "interfaces/inner_api/ace/ai/data_detector_interface.h"
22 
23 #include "base/memory/ace_type.h"
24 #include "base/thread/cancelable_callback.h"
25 #include "core/components_ng/base/frame_node.h"
26 #include "core/components_ng/property/property.h"
27 #include "core/components_v2/inspector/utils.h"
28 
29 namespace OHOS::AAFwk {
30 class Want;
31 class WantParams;
32 } // namespace OHOS::AAFwk
33 
34 namespace OHOS::Ace {
35 
36 namespace NG {
37 class TextPattern;
38 class RichEditorPattern;
39 }
40 
41 struct AISpan {
42     int32_t start = 0;
43     int32_t end = 0;
44     std::string content = "";
45     TextDataDetectType type = TextDataDetectType::PHONE_NUMBER;
46 };
47 class DataDetectorAdapter : public AceType {
48     DECLARE_ACE_TYPE(DataDetectorAdapter, AceType);
49 
50 public:
51     DataDetectorAdapter() = default;
52     ~DataDetectorAdapter() override = default;
53 
GetHost()54     RefPtr<NG::FrameNode> GetHost() const
55     {
56         return frameNode_.Upgrade();
57     }
SetTextDetectResult(const TextDataDetectResult result)58     void SetTextDetectResult(const TextDataDetectResult result)
59     {
60         textDetectResult_ = result;
61     }
FireOnResult(const std::string & result)62     void FireOnResult(const std::string& result)
63     {
64         if (onResult_) {
65             TAG_LOGD(AceLogTag::ACE_TEXT, "Data detect result: %{public}s.", result.c_str());
66             onResult_(result);
67         }
68     }
69     bool ParseOriText(const std::unique_ptr<JsonValue>& entityJson, std::string& text);
70     void InitTextDetect(int32_t startPos, std::string detectText);
71     void SetTextDetectTypes(const std::string& types);
72     void ParseAIResult(const TextDataDetectResult& result, int32_t startPos);
73     void ParseAIJson(const std::unique_ptr<JsonValue>& jsonValue, TextDataDetectType type, int32_t startPos);
74     void StartAITask();
CancelAITask()75     void CancelAITask()
76     {
77         if (aiDetectDelayTask_) {
78             aiDetectDelayTask_.Cancel();
79         }
80         aiDetectInitialized_ = false;
81     }
82     bool ShowAIEntityMenu(const AISpan& aiSpan, const NG::RectF& aiRect, const RefPtr<NG::FrameNode>& targetNode,
83         bool isShowCopy = true, bool isShowSelectText = true);
84     void ResponseBestMatchItem(const AISpan& aiSpan);
85     void GetAIEntityMenu();
GetCloseMenuForAISpanFlag()86     bool GetCloseMenuForAISpanFlag()
87     {
88         return closeMenuForAISpanFlag_;
89     }
SetCloseMenuForAISpanFlag(bool flag)90     void SetCloseMenuForAISpanFlag(bool flag)
91     {
92         closeMenuForAISpanFlag_ = flag;
93     }
94 
95 private:
96     friend class NG::TextPattern;
97     friend class NG::RichEditorPattern;
98 
99     std::function<void()> GetDetectDelayTask(const std::map<int32_t, AISpan>& aiSpanMap);
100     void OnClickAIMenuOption(const AISpan& aiSpan, const std::pair<std::string, FuncVariant>& menuOption,
101         const RefPtr<NG::FrameNode>& targetNode = nullptr);
102 
103     WeakPtr<NG::FrameNode> frameNode_;
104     bool aiDetectInitialized_ = false;
105     bool hasClickedAISpan_ = false;
106     bool closeMenuForAISpanFlag_ = false;
107     bool pressedByLeftMouse_ = false;
108     bool typeChanged_ = false;
109     bool hasClickedMenuOption_ = false;
110     AISpan clickedAISpan_;
111     std::string textDetectTypes_;
112     std::string textForAI_;
113     std::string lastTextForAI_;
114     std::set<std::string> textDetectTypesSet_;
115     TextDataDetectResult textDetectResult_;
116     std::function<void(const std::string&)> onResult_;
117     std::function<void(const std::string&)> onClickMenu_;
118     std::map<int32_t, AISpan> aiSpanMap_;
119     CancelableCallback<void()> aiDetectDelayTask_;
120     std::unordered_map<int32_t, std::string> entityJson_;
121     TimeStamp startDetectorTimeStamp_;
122     std::vector<std::string> detectTexts_;
123     int32_t mainContainerId_ = -1;
124     std::optional<Color> entityColor_;
125     std::optional<TextDecoration> entityDecorationType_;
126     std::optional<Color> entityDecorationColor_;
127     std::optional<TextDecorationStyle> entityDecorationStyle_;
128     std::string textDetectConfigStr_;
129 };
130 } // namespace OHOS::Ace
131 
132 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_AI_PROPERTIES_H
133