• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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_PATTERN_TEXT_FIELD_PATTERN_CONTENT_CONTROLLER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_PATTERN_CONTENT_CONTROLLER_H
18 
19 #include <string>
20 #include <utility>
21 
22 #include "base/memory/ace_type.h"
23 #include "base/memory/referenced.h"
24 #include "base/utils/string_utils.h"
25 #include "core/common/ime/text_input_type.h"
26 #include "core/components_ng/pattern/pattern.h"
27 
28 namespace OHOS::Ace::NG {
29 class ContentController : public virtual AceType {
30     DECLARE_ACE_TYPE(ContentController, AceType);
31 
32 public:
ContentController(const WeakPtr<Pattern> & pattern)33     explicit ContentController(const WeakPtr<Pattern>& pattern) : pattern_(pattern) {}
34     ~ContentController() override = default;
35     bool InsertValue(int32_t index, const std::string& value);
36     bool ReplaceSelectedValue(int32_t startIndex, int32_t endIndex, const std::string& value);
37     std::string GetSelectedValue(int32_t startIndex, int32_t endIndex);
38     std::string GetValueBeforeIndex(int32_t index);
39     std::string GetValueAfterIndex(int32_t index);
40     void erase(int32_t startIndex, int32_t length);
41     int32_t Delete(int32_t startIndex, int32_t length, bool isBackward);
42     int32_t GetDeleteLength(int32_t startIndex, int32_t length, bool isBackward);
43     bool IsIndexBeforeOrInEmoji(int32_t index);
44     void FilterValue();
45     void FilterValueType(std::string& value);
46     std::string GetSelectedLimitValue(int32_t& index, int32_t& startIndex);
47     void FilterTextInputStyle(bool& textChanged, std::string& result);
48 
GetWideText()49     std::wstring GetWideText()
50     {
51         return StringUtils::ToWstring(content_);
52     }
53 
GetTextValue()54     std::string GetTextValue()
55     {
56         return content_;
57     }
58 
IsEmpty()59     bool IsEmpty()
60     {
61         return content_.empty();
62     }
63 
SetTextValue(std::string && value)64     void SetTextValue(std::string&& value)
65     {
66         content_ = value;
67         FilterValue();
68     }
69 
SetTextValue(const std::string & value)70     void SetTextValue(const std::string& value)
71     {
72         content_ = value;
73         FilterValue();
74     }
75 
SetTextValueOnly(std::string && value)76     void SetTextValueOnly(std::string&& value)
77     {
78         content_ = value;
79     }
80 
SetTextValueOnly(const std::string & value)81     void SetTextValueOnly(const std::string& value)
82     {
83         content_ = value;
84     }
85 
Reset()86     void Reset()
87     {
88         content_ = "";
89     }
90 
GetInsertValue()91     std::string GetInsertValue()
92     {
93         return insertValue_;
94     }
95 
96 private:
97     void FormatIndex(int32_t& startIndex, int32_t& endIndex);
98     bool FilterWithEvent(const std::string& filter, std::string& result);
99     std::string PreprocessString(int32_t startIndex, int32_t endIndex, const std::string& value);
100     static std::string RemoveErrorTextFromValue(const std::string& value, const std::string& errorText);
101     static std::string FilterWithRegex(const std::string& filter, std::string& result);
102     static bool FilterWithEmail(std::string& result);
103     static bool FilterWithAscii(std::string& result);
104     static bool FilterWithDecimal(std::string& result);
105 
106     std::string insertValue_;
107     std::string content_;
108     WeakPtr<Pattern> pattern_;
109 };
110 } // namespace OHOS::Ace::NG
111 
112 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_PATTERN_CONTENT_CONTROLLER_H