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