1 /* 2 * Copyright (c) 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_PATTERNS_RICH_EDITOR_RICH_EDITOR_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_EVENT_HUB_H 18 19 #include "core/components_ng/event/event_hub.h" 20 #include "core/components_ng/pattern/rich_editor/selection_info.h" 21 22 namespace OHOS::Ace::NG { 23 class TextInsertValueInfo { 24 public: 25 TextInsertValueInfo() = default; 26 ~TextInsertValueInfo() = default; SetSpanIndex(int32_t spanIndex)27 void SetSpanIndex(int32_t spanIndex) 28 { 29 spanIndex_ = spanIndex; 30 } 31 GetSpanIndex()32 int32_t GetSpanIndex() const 33 { 34 return spanIndex_; 35 } 36 SetOffsetInSpan(int32_t offsetInSpan)37 void SetOffsetInSpan(int32_t offsetInSpan) 38 { 39 offsetInSpan_ = offsetInSpan; 40 } 41 GetOffsetInSpan()42 int32_t GetOffsetInSpan() const 43 { 44 return offsetInSpan_; 45 } 46 ToString()47 std::string ToString() const 48 { 49 return "spanIndex_: " + std::to_string(spanIndex_) + ", offsetInSpan_" + std::to_string(offsetInSpan_); 50 } 51 52 private: 53 int32_t spanIndex_ = 0; 54 int32_t offsetInSpan_ = 0; 55 }; 56 57 class RichEditorInsertValue : public BaseEventInfo { DECLARE_ACE_TYPE(RichEditorInsertValue,BaseEventInfo)58 DECLARE_ACE_TYPE(RichEditorInsertValue, BaseEventInfo) 59 public: 60 RichEditorInsertValue() : BaseEventInfo("RichEditorInsertValue") {} 61 ~RichEditorInsertValue() override = default; 62 void SetInsertOffset(int32_t insertOffset); 63 int32_t GetInsertOffset() const; 64 void SetInsertValue(const std::string& insertValue); 65 const std::string& GetInsertValue() const; 66 67 private: 68 int32_t insertOffset_; 69 std::string insertValue_; 70 }; 71 72 enum class SpanResultType { TEXT, IMAGE, SYMBOL }; 73 74 class RichEditorAbstractSpanResult { 75 public: 76 RichEditorAbstractSpanResult() = default; 77 ~RichEditorAbstractSpanResult() = default; 78 void SetSpanIndex(int32_t spanIndex); 79 int32_t GetSpanIndex() const; 80 void SetSpanRangeStart(int32_t spanRangeStart); 81 int32_t GetSpanRangeStart() const; 82 void SetSpanRangeEnd(int32_t spanRangeEnd); 83 int32_t GetSpanRangeEnd() const; 84 void SetSpanType(SpanResultType spanType); 85 SpanResultType GetType() const; 86 void SetOffsetInSpan(int32_t offsetInSpan); 87 int32_t OffsetInSpan() const; 88 void SetEraseLength(int32_t eraseLength); 89 int32_t GetEraseLength() const; 90 void SetValue(const std::string& value); 91 const std::string& GetValue() const; 92 void SetFontColor(const std::string& fontColor); 93 const std::string& GetFontColor() const; 94 void SetFontSize(double fontSize); 95 double GetFontSize() const; 96 void SetFontWeight(int32_t fontWeigth); 97 int32_t GetFontWeight() const; 98 void SetFontFamily(const std::string& fontFamily); 99 const std::string& GetFontFamily() const; 100 void SetTextDecoration(TextDecoration textDecoration); 101 TextDecoration GetTextDecoration() const; 102 void SetColor(const std::string& color); 103 const std::string& GetColor() const; 104 void SetValuePixelMap(const RefPtr<PixelMap>& valuePixelMap); 105 const RefPtr<PixelMap>& GetValuePixelMap() const; 106 void SetValueResourceStr(const std::string valueResourceStr); 107 const std::string& GetValueResourceStr() const; 108 void SetSizeWidth(int32_t width); 109 int32_t GetSizeWidth() const; 110 void SetSizeHeight(int32_t height); 111 int32_t GetSizeHeight() const; 112 void SetVerticalAlign(VerticalAlign verticalAlign); 113 VerticalAlign GetVerticalAlign() const; 114 void SetImageFit(ImageFit objectFit); 115 ImageFit GetObjectFit() const; 116 SetFontStyle(OHOS::Ace::FontStyle fontStyle)117 void SetFontStyle(OHOS::Ace::FontStyle fontStyle) 118 { 119 fontStyle_ = fontStyle; 120 } 121 GetFontStyle()122 OHOS::Ace::FontStyle GetFontStyle() const 123 { 124 return fontStyle_; 125 } 126 127 private: 128 int32_t spanIndex_ = 0; 129 int32_t spanRangeStart_ = 0; 130 int32_t spanRangeEnd_ = 0; 131 SpanResultType spanType_; 132 int32_t offsetInSpan_ = 0; 133 int32_t eraseLength_ = 0; 134 std::string value_; 135 std::string fontColor_; 136 double fontSize_ = 0.0; 137 OHOS::Ace::FontStyle fontStyle_; 138 int32_t fontWeigth_ = 0; 139 std::string fontFamily_; 140 TextDecoration textDecoration_; 141 std::string color_; 142 RefPtr<PixelMap> valuePixelMap_; 143 std::string valueResourceStr_; 144 int32_t width_ = 0; 145 int32_t height_ = 0; 146 VerticalAlign verticalAlign_; 147 ImageFit objectFit_; 148 }; 149 150 enum class RichEditorDeleteDirection { BACKWARD = 0, FORWARD }; 151 152 class RichEditorDeleteValue : public BaseEventInfo { DECLARE_ACE_TYPE(RichEditorDeleteValue,BaseEventInfo)153 DECLARE_ACE_TYPE(RichEditorDeleteValue, BaseEventInfo) 154 public: 155 RichEditorDeleteValue() : BaseEventInfo("RichEditorDeleteValue") {} 156 ~RichEditorDeleteValue() = default; 157 void SetOffset(int32_t offset); 158 int32_t GetOffset() const; 159 void SetRichEditorDeleteDirection(RichEditorDeleteDirection direction); 160 RichEditorDeleteDirection GetRichEditorDeleteDirection() const; 161 void SetLength(int32_t length); 162 int32_t GetLength() const; 163 void SetRichEditorDeleteSpans(const RichEditorAbstractSpanResult& deleteSpan); 164 const std::list<RichEditorAbstractSpanResult>& GetRichEditorDeleteSpans() const; 165 166 private: 167 int32_t offset_ = 0; 168 RichEditorDeleteDirection direction_; 169 int32_t length_ = 0; 170 std::list<RichEditorAbstractSpanResult> richEditorDeleteSpans_; 171 }; 172 173 class RichEditorEventHub : public EventHub { 174 DECLARE_ACE_TYPE(RichEditorEventHub, EventHub) 175 176 public: 177 RichEditorEventHub() = default; 178 ~RichEditorEventHub() override = default; 179 void SetOnReady(std::function<void()>&& func); 180 void FireOnReady(); 181 void SetAboutToIMEInput(std::function<bool(const RichEditorInsertValue&)>&& func); 182 bool FireAboutToIMEInput(const RichEditorInsertValue& info); 183 void SetOnIMEInputComplete(std::function<void(const RichEditorAbstractSpanResult&)>&& func); 184 void FireOnIMEInputComplete(const RichEditorAbstractSpanResult& info); 185 void SetAboutToDelete(std::function<bool(const RichEditorDeleteValue&)>&& func); 186 bool FireAboutToDelete(const RichEditorDeleteValue& info); 187 void SetOnDeleteComplete(std::function<void()>&& func); 188 void FireOnDeleteComplete(); 189 std::string GetDragExtraParams(const std::string& extraInfo, const Point& point, DragEventType type) override; 190 SetOnSelect(std::function<void (const BaseEventInfo *)> && func)191 void SetOnSelect(std::function<void(const BaseEventInfo*)>&& func) 192 { 193 onSelect_ = std::move(func); 194 } 195 FireOnSelect(BaseEventInfo * value)196 void FireOnSelect(BaseEventInfo* value) 197 { 198 if (onSelect_) { 199 onSelect_(value); 200 } 201 } 202 SetOnSelectionChange(std::function<void (const BaseEventInfo *)> && func)203 void SetOnSelectionChange(std::function<void(const BaseEventInfo*)>&& func) 204 { 205 OnSelectionChange_ = std::move(func); 206 } 207 FireOnSelectionChange(BaseEventInfo * value)208 void FireOnSelectionChange(BaseEventInfo* value) 209 { 210 if (OnSelectionChange_) { 211 OnSelectionChange_(value); 212 } 213 } 214 SetTimestamp(long long timestamp)215 void SetTimestamp(long long timestamp) 216 { 217 timestamp_ = timestamp; 218 } 219 SetOnPaste(std::function<void (NG::TextCommonEvent &)> && func)220 void SetOnPaste(std::function<void(NG::TextCommonEvent&)>&& func) 221 { 222 onPaste_ = std::move(func); 223 } 224 FireOnPaste(NG::TextCommonEvent & value)225 void FireOnPaste(NG::TextCommonEvent& value) 226 { 227 if (onPaste_) { 228 onPaste_(value); 229 } 230 } 231 232 private: 233 long long timestamp_ = 0; 234 std::function<void(NG::TextCommonEvent&)> onPaste_; 235 std::function<void()> onReady_; 236 std::function<void(const BaseEventInfo*)> onSelect_; 237 std::function<void(const BaseEventInfo*)> OnSelectionChange_; 238 std::function<bool(const RichEditorInsertValue&)> aboutToIMEInput_; 239 std::function<void(const RichEditorAbstractSpanResult&)> onIMEIputComplete_; 240 std::function<bool(const RichEditorDeleteValue&)> aboutToDelete_; 241 std::function<void()> onDeleteComplete_; 242 ACE_DISALLOW_COPY_AND_MOVE(RichEditorEventHub); 243 }; 244 } // namespace OHOS::Ace::NG 245 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_EVENT_HUB_H 246