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