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