1 /*
2  * Copyright (c) 2021 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_IME_TEXT_EDITING_VALUE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_EDITING_VALUE_H
18 
19 #include <functional>
20 #include <string>
21 
22 #include "core/common/ime/text_selection.h"
23 
24 #if defined(IOS_PLATFORM)
25 #include "core/common/ime/text_compose.h"
26 #endif
27 
28 namespace OHOS::Ace {
29 
30 class JsonValue;
31 
32 using TextManipulation = std::function<void(std::wstring&)>;
33 
34 struct TextEditingValue {
35     void ParseFromJson(const JsonValue& json);
36     std::string ToJsonString() const;
37 
38     bool operator==(const TextEditingValue& other) const;
39     bool operator!=(const TextEditingValue& other) const;
40 
41     std::wstring GetWideText() const;
42 
43     /**
44      * @brief Selection offset of text is designed for 2 bytes per offset, so if glyphs out range of UTF-16 BMP,
45      * like emoji, will be counted as 2 offsets. For example, here is a string contains emoji [smile]:
46      *     abc[smile]
47      * The offset between 'c' and '[smile]' is 3, and the offset after '[smile]' will be 5.
48      *
49      * Internal implementation: We recognize this case by judging UTF-16 characters. If an code unit is out of
50      * UTF-16 BMP, then offset to move will for it will be two.
51      */
52     void MoveLeft();
53     void MoveRight();
54     void MoveToPosition(int32_t position);
55     void UpdateSelection(int32_t both);
56     void UpdateSelection(int32_t start, int32_t end);
57 
58 #if defined(IOS_PLATFORM)
59     void UpdateCompose(int32_t start, int32_t end);
60 #endif
61 
62     /**
63      * @brief Manipulate the text by [manipulation] func, meanwhile update the selection.
64      *
65      * @param[in] manipulation The function for manipulation.
66      */
67     void SelectionAwareTextManipulation(const TextManipulation& manipulation);
68     std::string GetBeforeSelection() const;
69     std::string GetSelectedText() const;
70     std::string GetSelectedText(const TextSelection& textSelection) const;
71     std::string GetAfterSelection() const;
72 
73     // Delete text of start to end.
74     void Delete(int32_t start, int32_t end);
75     void Append(const std::string& newText);
76 
77     std::string text;
78     std::string hint;
79     TextSelection selection;
80     bool isDelete;
81     std::string appendText;
82 
83 #if defined(IOS_PLATFORM)
84     TextCompose compose;
85 #endif
86 };
87 
88 } // namespace OHOS::Ace
89 
90 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_EDITING_VALUE_H
91