1 /* 2 * Copyright (c) 2023-2024 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 #include "core/components_ng/pattern/text_field/text_field_event_hub.h" 22 #include "core/components_ng/pattern/text/text_model.h" 23 #include "core/common/ime/text_range.h" 24 namespace OHOS::Ace::NG { 25 class TextInsertValueInfo { 26 public: 27 TextInsertValueInfo() = default; 28 ~TextInsertValueInfo() = default; SetSpanIndex(int32_t spanIndex)29 void SetSpanIndex(int32_t spanIndex) 30 { 31 spanIndex_ = spanIndex; 32 } 33 GetSpanIndex()34 int32_t GetSpanIndex() const 35 { 36 return spanIndex_; 37 } 38 SetOffsetInSpan(int32_t offsetInSpan)39 void SetOffsetInSpan(int32_t offsetInSpan) 40 { 41 offsetInSpan_ = offsetInSpan; 42 } 43 GetOffsetInSpan()44 int32_t GetOffsetInSpan() const 45 { 46 return offsetInSpan_; 47 } 48 ToString()49 std::string ToString() const 50 { 51 return "spanIndex_: " + std::to_string(spanIndex_) + ", offsetInSpan_" + std::to_string(offsetInSpan_); 52 } 53 54 private: 55 int32_t spanIndex_ = 0; 56 int32_t offsetInSpan_ = 0; 57 }; 58 59 class ACE_FORCE_EXPORT RichEditorInsertValue : public BaseEventInfo { 60 DECLARE_ACE_TYPE(RichEditorInsertValue, BaseEventInfo); 61 public: RichEditorInsertValue()62 RichEditorInsertValue() : BaseEventInfo("RichEditorInsertValue") {} 63 ~RichEditorInsertValue() override = default; 64 void SetInsertOffset(int32_t insertOffset); 65 int32_t GetInsertOffset() const; 66 void SetInsertValue(const std::u16string& insertValue); 67 void SetPreviewText(const std::u16string& previewText); 68 const std::u16string& GetInsertValue() const; 69 const std::u16string& GetPreviewText() const; 70 71 private: 72 int32_t insertOffset_ = 0; 73 std::u16string insertValue_; 74 std::u16string previewText_; 75 }; 76 77 enum class SpanResultType { TEXT, IMAGE, SYMBOL }; 78 79 class ACE_FORCE_EXPORT RichEditorAbstractSpanResult { 80 public: 81 RichEditorAbstractSpanResult() = default; 82 ~RichEditorAbstractSpanResult() = default; 83 void SetSpanIndex(int32_t spanIndex); 84 int32_t GetSpanIndex() const; 85 void SetSpanRangeStart(int32_t spanRangeStart); 86 int32_t GetSpanRangeStart() const; 87 void SetSpanRangeEnd(int32_t spanRangeEnd); 88 int32_t GetSpanRangeEnd() const; 89 void SetSpanType(SpanResultType spanType); 90 SpanResultType GetType() const; 91 void SetOffsetInSpan(int32_t offsetInSpan); 92 int32_t OffsetInSpan() const; 93 void SetEraseLength(int32_t eraseLength); 94 int32_t GetEraseLength() const; 95 void SetValue(const std::u16string& value); 96 const std::u16string& GetValue() const; 97 void SetPreviewText(const std::u16string& previewText); 98 const std::u16string& GetPreviewText() const; 99 void SetFontColor(const std::string& fontColor); 100 const std::string& GetFontColor() const; 101 void SetFontFeature(const FONT_FEATURES_LIST& fontFeature); 102 const FONT_FEATURES_LIST& GetFontFeatures() const; 103 void SetFontSize(double fontSize); 104 double GetFontSize() const; 105 void SetValueResource(const RefPtr<ResourceObject>&); 106 const RefPtr<ResourceObject> GetValueResource() const; 107 void SetValueString(const std::string& valueString); 108 const std::string GetValueString() const; 109 void SetSymbolSpanStyle(const SymbolSpanStyle& symbolSpanStyle); 110 const SymbolSpanStyle GetSymbolSpanStyle() const; 111 void SetLineHeight(double lineHeight); 112 double GetLineHeight() const; 113 void SetHalfLeading(bool halfLeading); 114 bool GetHalfLeading() const; 115 void SetLetterspacing(double letterSpacing); 116 double GetLetterspacing() const; 117 void SetTextStyle(TextStyleResult textStyle); 118 TextStyleResult GetTextStyle() const; 119 void SetFontWeight(int32_t fontWeigth); 120 int32_t GetFontWeight() const; 121 void SetFontFamily(const std::string& fontFamily); 122 const std::string& GetFontFamily() const; 123 void SetTextDecoration(TextDecoration textDecoration); 124 TextDecoration GetTextDecoration() const; 125 void SetColor(const std::string& color); 126 const std::string& GetColor() const; 127 void SetTextDecorationStyle(TextDecorationStyle textDecorationStyle); 128 TextDecorationStyle GetTextDecorationStyle() const; 129 void SetLineThicknessScale(float thicknessScale); 130 float GetLineThicknessScale() const; 131 void SetValuePixelMap(const RefPtr<PixelMap>& valuePixelMap); 132 const RefPtr<PixelMap>& GetValuePixelMap() const; 133 void SetValueResourceStr(const std::string valueResourceStr); 134 const std::string& GetValueResourceStr() const; 135 void SetSizeWidth(int32_t width); 136 int32_t GetSizeWidth() const; 137 void SetSizeHeight(int32_t height); 138 int32_t GetSizeHeight() const; 139 void SetVerticalAlign(VerticalAlign verticalAlign); 140 VerticalAlign GetVerticalAlign() const; 141 void SetImageFit(ImageFit objectFit); 142 ImageFit GetObjectFit() const; 143 void SetUrlAddress(const std::u16string& urlAddress); 144 const std::u16string& GetUrlAddress() const; SetBorderRadius(const std::string & borderRadius)145 void SetBorderRadius(const std::string& borderRadius) 146 { 147 borderRadius_ = borderRadius; 148 } GetBorderRadius()149 const std::string& GetBorderRadius() const 150 { 151 return borderRadius_; 152 } SetMargin(const std::string & margin)153 void SetMargin(const std::string& margin) 154 { 155 margin_ = margin; 156 } GetMargin()157 const std::string& GetMargin() const 158 { 159 return margin_; 160 } SetFontStyle(OHOS::Ace::FontStyle fontStyle)161 void SetFontStyle(OHOS::Ace::FontStyle fontStyle) 162 { 163 fontStyle_ = fontStyle; 164 } 165 GetFontStyle()166 OHOS::Ace::FontStyle GetFontStyle() const 167 { 168 return fontStyle_; 169 } 170 171 private: 172 TextStyleResult textStyle_; 173 double lineHeight_ = 0.0; 174 bool halfLeading_ = false; 175 double letterSpacing_ = 0.0; 176 int32_t spanIndex_ = 0; 177 int32_t spanRangeStart_ = 0; 178 int32_t spanRangeEnd_ = 0; 179 SpanResultType spanType_; 180 int32_t offsetInSpan_ = 0; 181 int32_t eraseLength_ = 0; 182 std::u16string value_; 183 std::u16string previewText_; 184 std::u16string urlAddress_; 185 std::string fontColor_; 186 FONT_FEATURES_LIST fontFeature_; 187 double fontSize_ = 0.0; 188 OHOS::Ace::FontStyle fontStyle_; 189 int32_t fontWeigth_ = 0; 190 std::string fontFamily_; 191 TextDecoration textDecoration_; 192 std::string color_; 193 TextDecorationStyle textDecorationStyle_; 194 float lineThicknessScale_ = 1.0f; 195 RefPtr<PixelMap> valuePixelMap_; 196 std::string valueResourceStr_; 197 int32_t width_ = 0; 198 int32_t height_ = 0; 199 VerticalAlign verticalAlign_; 200 ImageFit objectFit_; 201 std::string borderRadius_; 202 std::string margin_; 203 std::string valueString_; 204 SymbolSpanStyle symbolSpanStyle_; 205 RefPtr<ResourceObject> valueResource_; 206 }; 207 208 enum class RichEditorDeleteDirection { BACKWARD = 0, FORWARD }; 209 210 class ACE_FORCE_EXPORT RichEditorDeleteValue : public BaseEventInfo { 211 DECLARE_ACE_TYPE(RichEditorDeleteValue, BaseEventInfo); 212 public: RichEditorDeleteValue()213 RichEditorDeleteValue() : BaseEventInfo("RichEditorDeleteValue") {} 214 ~RichEditorDeleteValue() = default; 215 void SetOffset(int32_t offset); 216 int32_t GetOffset() const; 217 void SetRichEditorDeleteDirection(RichEditorDeleteDirection direction); 218 RichEditorDeleteDirection GetRichEditorDeleteDirection() const; 219 void SetLength(int32_t length); 220 int32_t GetLength() const; 221 void SetRichEditorDeleteSpans(const RichEditorAbstractSpanResult& deleteSpan); 222 void ResetRichEditorDeleteSpans(); 223 const std::list<RichEditorAbstractSpanResult>& GetRichEditorDeleteSpans() const; 224 225 private: 226 int32_t offset_ = 0; 227 RichEditorDeleteDirection direction_ = RichEditorDeleteDirection::BACKWARD; 228 int32_t length_ = 0; 229 std::list<RichEditorAbstractSpanResult> richEditorDeleteSpans_; 230 }; 231 232 class ACE_FORCE_EXPORT RichEditorChangeValue : public BaseEventInfo { 233 DECLARE_ACE_TYPE(RichEditorChangeValue, BaseEventInfo); 234 #ifndef ACE_UNITTEST 235 private: 236 #else 237 public: 238 #endif RichEditorChangeValue()239 RichEditorChangeValue() : BaseEventInfo("RichEditorChangeValue") {} 240 public: RichEditorChangeValue(TextChangeReason reason)241 RichEditorChangeValue(TextChangeReason reason) : RichEditorChangeValue() 242 { 243 changeReason_ = reason; 244 } 245 ~RichEditorChangeValue() = default; 246 247 void SetRichEditorOriginalSpans(const RichEditorAbstractSpanResult& span); 248 const std::vector<RichEditorAbstractSpanResult>& GetRichEditorOriginalSpans() const; 249 250 void SetRichEditorReplacedSpans(const RichEditorAbstractSpanResult& span); 251 const std::vector<RichEditorAbstractSpanResult>& GetRichEditorReplacedSpans() const; 252 253 void SetRichEditorReplacedImageSpans(const RichEditorAbstractSpanResult& span); 254 const std::vector<RichEditorAbstractSpanResult>& GetRichEditorReplacedImageSpans() const; 255 256 void SetRichEditorReplacedSymbolSpans(const RichEditorAbstractSpanResult& span); 257 const std::vector<RichEditorAbstractSpanResult>& GetRichEditorReplacedSymbolSpans() const; 258 259 void SetRangeBefore(const TextRange& rangeBefore); 260 TextRange GetRangeBefore() const; 261 262 void SetRangeAfter(const TextRange& rangeAfter); 263 TextRange GetRangeAfter() const; 264 265 TextChangeReason GetChangeReason() const; 266 reset()267 void reset() 268 { 269 originalSpans_.clear(); 270 replacedSpans_.clear(); 271 replacedImageSpans_.clear(); 272 replacedSymbolSpans_.clear(); 273 rangeBefore_ = TextRange(); 274 rangeAfter_ = TextRange(); 275 changeReason_ = TextChangeReason::UNKNOWN; 276 } 277 278 private: 279 std::vector<RichEditorAbstractSpanResult> originalSpans_; 280 std::vector<RichEditorAbstractSpanResult> replacedSpans_; 281 std::vector<RichEditorAbstractSpanResult> replacedImageSpans_; 282 std::vector<RichEditorAbstractSpanResult> replacedSymbolSpans_; 283 TextRange rangeBefore_; 284 TextRange rangeAfter_; 285 TextChangeReason changeReason_ = TextChangeReason::UNKNOWN; 286 }; 287 288 class StyledStringChangeValue : public BaseEventInfo { 289 DECLARE_ACE_TYPE(StyledStringChangeValue, BaseEventInfo); 290 public: StyledStringChangeValue()291 StyledStringChangeValue() : BaseEventInfo("StyledStringChangeValue") {} 292 ~StyledStringChangeValue() = default; 293 294 void SetRangeBefore(const TextRange& range); 295 TextRange GetRangeBefore() const; 296 297 void SetRangeAfter(const TextRange& range); 298 TextRange GetRangeAfter() const; 299 300 void SetReplacementString(const RefPtr<SpanStringBase>& styledString); 301 const RefPtr<SpanStringBase> GetReplacementString() const; 302 303 void SetPreviewText(const RefPtr<SpanStringBase>& previewText); 304 const RefPtr<SpanStringBase> GetPreviewText() const; 305 306 private: 307 TextRange rangeBefore_; 308 TextRange rangeAfter_; 309 RefPtr<SpanStringBase> replacementString_; 310 RefPtr<SpanStringBase> previewText_; 311 }; 312 313 class RichEditorEventHub : public EventHub { 314 DECLARE_ACE_TYPE(RichEditorEventHub, EventHub); 315 316 public: 317 RichEditorEventHub() = default; 318 ~RichEditorEventHub() override = default; 319 void SetOnReady(std::function<void()>&& func); 320 void FireOnReady(); 321 void SetAboutToIMEInput(std::function<bool(const RichEditorInsertValue&)>&& func); 322 bool FireAboutToIMEInput(const RichEditorInsertValue& info); 323 void SetOnIMEInputComplete(std::function<void(const RichEditorAbstractSpanResult&)>&& func); 324 void SetOnDidIMEInput(std::function<void(const TextRange&)>&& func); 325 void FireOnIMEInputComplete(const RichEditorAbstractSpanResult& info); 326 void FireOnDidIMEInput(const TextRange& info); 327 void SetAboutToDelete(std::function<bool(const RichEditorDeleteValue&)>&& func); 328 bool FireAboutToDelete(const RichEditorDeleteValue& info); 329 void SetOnDeleteComplete(std::function<void()>&& func); 330 void FireOnDeleteComplete(); 331 std::string GetDragExtraParams(const std::string& extraInfo, const Point& point, DragEventType type) override; 332 void SetOnEditingChange(std::function<void(const bool&)> && func); 333 void FireOnEditingChange(bool isEditing); SetOnSelect(std::function<void (const BaseEventInfo *)> && func)334 void SetOnSelect(std::function<void(const BaseEventInfo*)>&& func) 335 { 336 onSelect_ = std::move(func); 337 } 338 FireOnSelect(BaseEventInfo * value)339 void FireOnSelect(BaseEventInfo* value) 340 { 341 if (onSelect_) { 342 onSelect_(value); 343 } 344 } 345 SetOnSelectionChange(std::function<void (const BaseEventInfo *)> && func)346 void SetOnSelectionChange(std::function<void(const BaseEventInfo*)>&& func) 347 { 348 OnSelectionChange_ = std::move(func); 349 } 350 FireOnSelectionChange(BaseEventInfo * value)351 void FireOnSelectionChange(BaseEventInfo* value) 352 { 353 if (OnSelectionChange_) { 354 OnSelectionChange_(value); 355 } 356 } 357 SetTimestamp(long long timestamp)358 void SetTimestamp(long long timestamp) 359 { 360 timestamp_ = timestamp; 361 } 362 SetOnPaste(std::function<void (NG::TextCommonEvent &)> && func)363 void SetOnPaste(std::function<void(NG::TextCommonEvent&)>&& func) 364 { 365 onPaste_ = std::move(func); 366 } 367 FireOnPaste(NG::TextCommonEvent & value)368 void FireOnPaste(NG::TextCommonEvent& value) 369 { 370 if (onPaste_) { 371 onPaste_(value); 372 } 373 } 374 SetOnSubmit(std::function<void (int32_t,NG::TextFieldCommonEvent &)> && func)375 void SetOnSubmit(std::function<void(int32_t, NG::TextFieldCommonEvent&)>&& func) 376 { 377 onSubmit_ = std::move(func); 378 } 379 FireOnSubmit(int32_t value,NG::TextFieldCommonEvent & event)380 void FireOnSubmit(int32_t value, NG::TextFieldCommonEvent& event) 381 { 382 if (onSubmit_) { 383 onSubmit_(value, event); 384 } 385 } 386 387 void SetOnWillChange(std::function<bool(const RichEditorChangeValue&)> && func); 388 bool FireOnWillChange(const RichEditorChangeValue& info); 389 bool HasOnWillChange() const; 390 void SetOnDidChange(std::function<void(const RichEditorChangeValue&)> && func); 391 void FireOnDidChange(const RichEditorChangeValue& range); 392 bool HasOnDidChange() const; 393 void SetOnCut(std::function<void(NG::TextCommonEvent&)> && func); 394 void FireOnCut(NG::TextCommonEvent& value); 395 void SetOnCopy(std::function<void(NG::TextCommonEvent&)> && func); 396 void FireOnCopy(NG::TextCommonEvent& value); 397 void SetOnShare(std::function<void(NG::TextCommonEvent&)>&& func); 398 void FireOnShare(NG::TextCommonEvent& value); 399 void SetOnStyledStringWillChange(std::function<bool(const StyledStringChangeValue&)> && func); 400 bool FireOnStyledStringWillChange(const StyledStringChangeValue& info); 401 bool HasOnStyledStringWillChange() const; 402 void SetOnStyledStringDidChange(std::function<void(const StyledStringChangeValue&)> && func); 403 void FireOnStyledStringDidChange(const StyledStringChangeValue& info); 404 bool HasOnStyledStringDidChange() const; 405 406 private: 407 long long timestamp_ = 0; 408 std::function<void(NG::TextCommonEvent&)> onPaste_; 409 std::function<void()> onReady_; 410 std::function<void(const BaseEventInfo*)> onSelect_; 411 std::function<void(const BaseEventInfo*)> OnSelectionChange_; 412 std::function<bool(const RichEditorInsertValue&)> aboutToIMEInput_; 413 std::function<void(const RichEditorAbstractSpanResult&)> onIMEInputComplete_; 414 std::function<void(const TextRange&)> onDidIMEInput_; 415 std::function<bool(const RichEditorDeleteValue&)> aboutToDelete_; 416 std::function<void()> onDeleteComplete_; 417 std::function<void(const bool&)> onEditingChange_; 418 std::function<void(int32_t, NG::TextFieldCommonEvent&)> onSubmit_; 419 std::function<bool(const RichEditorChangeValue&)> onWillChange_; 420 std::function<void(const RichEditorChangeValue&)> onDidChange_; 421 std::function<void(NG::TextCommonEvent&)> onCut_; 422 std::function<void(NG::TextCommonEvent&)> onCopy_; 423 std::function<void(NG::TextCommonEvent&)> onShare_; 424 std::function<bool(const StyledStringChangeValue&)> onStyledStringWillChange_; 425 std::function<void(const StyledStringChangeValue&)> onStyledStringDidChange_; 426 ACE_DISALLOW_COPY_AND_MOVE(RichEditorEventHub); 427 }; 428 } // namespace OHOS::Ace::NG 429 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_EVENT_HUB_H 430