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 #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 {
DECLARE_ACE_TYPE(RichEditorInsertValue,BaseEventInfo)60     DECLARE_ACE_TYPE(RichEditorInsertValue, BaseEventInfo)
61 public:
62     RichEditorInsertValue() : BaseEventInfo("RichEditorInsertValue") {}
63     ~RichEditorInsertValue() override = default;
64     void SetInsertOffset(int32_t insertOffset);
65     int32_t GetInsertOffset() const;
66     void SetInsertValue(const std::string& insertValue);
67     void SetPreviewText(const std::string& previewText);
68     const std::string& GetInsertValue() const;
69     const std::string& GetPreviewText() const;
70 
71 private:
72     int32_t insertOffset_ = 0;
73     std::string insertValue_;
74     std::string 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::string& value);
96     const std::string& GetValue() const;
97     void SetPreviewText(const std::string& previewText);
98     const std::string& 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 SetLetterspacing(double letterSpacing);
114     double GetLetterspacing() const;
115     void SetTextStyle(TextStyleResult textStyle);
116     TextStyleResult GetTextStyle() const;
117     void SetFontWeight(int32_t fontWeigth);
118     int32_t GetFontWeight() const;
119     void SetFontFamily(const std::string& fontFamily);
120     const std::string& GetFontFamily() const;
121     void SetTextDecoration(TextDecoration textDecoration);
122     TextDecoration GetTextDecoration() const;
123     void SetColor(const std::string& color);
124     const std::string& GetColor() const;
125     void SetTextDecorationStyle(TextDecorationStyle textDecorationStyle);
126     TextDecorationStyle GetTextDecorationStyle() const;
127     void SetValuePixelMap(const RefPtr<PixelMap>& valuePixelMap);
128     const RefPtr<PixelMap>& GetValuePixelMap() const;
129     void SetValueResourceStr(const std::string valueResourceStr);
130     const std::string& GetValueResourceStr() const;
131     void SetSizeWidth(int32_t width);
132     int32_t GetSizeWidth() const;
133     void SetSizeHeight(int32_t height);
134     int32_t GetSizeHeight() const;
135     void SetVerticalAlign(VerticalAlign verticalAlign);
136     VerticalAlign GetVerticalAlign() const;
137     void SetImageFit(ImageFit objectFit);
138     ImageFit GetObjectFit() const;
SetBorderRadius(const std::string & borderRadius)139     void SetBorderRadius(const std::string& borderRadius)
140     {
141         borderRadius_ = borderRadius;
142     }
GetBorderRadius()143     const std::string& GetBorderRadius() const
144     {
145         return borderRadius_;
146     }
SetMargin(const std::string & margin)147     void SetMargin(const std::string& margin)
148     {
149         margin_ = margin;
150     }
GetMargin()151     const std::string& GetMargin() const
152     {
153         return margin_;
154     }
SetFontStyle(OHOS::Ace::FontStyle fontStyle)155     void SetFontStyle(OHOS::Ace::FontStyle fontStyle)
156     {
157         fontStyle_ = fontStyle;
158     }
159 
GetFontStyle()160     OHOS::Ace::FontStyle GetFontStyle() const
161     {
162         return fontStyle_;
163     }
164 
165 private:
166     TextStyleResult textStyle_;
167     double lineHeight_ = 0.0;
168     double letterSpacing_ = 0.0;
169     int32_t spanIndex_ = 0;
170     int32_t spanRangeStart_ = 0;
171     int32_t spanRangeEnd_ = 0;
172     SpanResultType spanType_;
173     int32_t offsetInSpan_ = 0;
174     int32_t eraseLength_ = 0;
175     std::string value_;
176     std::string previewText_;
177     std::string fontColor_;
178     FONT_FEATURES_LIST fontFeature_;
179     double fontSize_ = 0.0;
180     OHOS::Ace::FontStyle fontStyle_;
181     int32_t fontWeigth_ = 0;
182     std::string fontFamily_;
183     TextDecoration textDecoration_;
184     std::string color_;
185     TextDecorationStyle textDecorationStyle_;
186     RefPtr<PixelMap> valuePixelMap_;
187     std::string valueResourceStr_;
188     int32_t width_ = 0;
189     int32_t height_ = 0;
190     VerticalAlign verticalAlign_;
191     ImageFit objectFit_;
192     std::string borderRadius_;
193     std::string margin_;
194     std::string valueString_;
195     SymbolSpanStyle symbolSpanStyle_;
196     RefPtr<ResourceObject> valueResource_;
197 };
198 
199 enum class RichEditorDeleteDirection { BACKWARD = 0, FORWARD };
200 
201 class ACE_FORCE_EXPORT RichEditorDeleteValue : public BaseEventInfo {
DECLARE_ACE_TYPE(RichEditorDeleteValue,BaseEventInfo)202     DECLARE_ACE_TYPE(RichEditorDeleteValue, BaseEventInfo)
203 public:
204     RichEditorDeleteValue() : BaseEventInfo("RichEditorDeleteValue") {}
205     ~RichEditorDeleteValue() = default;
206     void SetOffset(int32_t offset);
207     int32_t GetOffset() const;
208     void SetRichEditorDeleteDirection(RichEditorDeleteDirection direction);
209     RichEditorDeleteDirection GetRichEditorDeleteDirection() const;
210     void SetLength(int32_t length);
211     int32_t GetLength() const;
212     void SetRichEditorDeleteSpans(const RichEditorAbstractSpanResult& deleteSpan);
213     void ResetRichEditorDeleteSpans();
214     const std::list<RichEditorAbstractSpanResult>& GetRichEditorDeleteSpans() const;
215 
216 private:
217     int32_t offset_ = 0;
218     RichEditorDeleteDirection direction_ = RichEditorDeleteDirection::BACKWARD;
219     int32_t length_ = 0;
220     std::list<RichEditorAbstractSpanResult> richEditorDeleteSpans_;
221 };
222 
223 class ACE_FORCE_EXPORT RichEditorChangeValue : public BaseEventInfo {
DECLARE_ACE_TYPE(RichEditorChangeValue,BaseEventInfo)224     DECLARE_ACE_TYPE(RichEditorChangeValue, BaseEventInfo)
225 public:
226     RichEditorChangeValue() : BaseEventInfo("RichEditorChangeValue") {}
227     ~RichEditorChangeValue() = default;
228 
229     void SetRichEditorOriginalSpans(const RichEditorAbstractSpanResult& span);
230     const std::vector<RichEditorAbstractSpanResult>& GetRichEditorOriginalSpans() const;
231 
232     void SetRichEditorReplacedSpans(const RichEditorAbstractSpanResult& span);
233     const std::vector<RichEditorAbstractSpanResult>& GetRichEditorReplacedSpans() const;
234 
235     void SetRichEditorReplacedImageSpans(const RichEditorAbstractSpanResult& span);
236     const std::vector<RichEditorAbstractSpanResult>& GetRichEditorReplacedImageSpans() const;
237 
238     void SetRichEditorReplacedSymbolSpans(const RichEditorAbstractSpanResult& span);
239     const std::vector<RichEditorAbstractSpanResult>& GetRichEditorReplacedSymbolSpans() const;
240 
241     void SetRangeBefore(const TextRange& rangeBefore);
242     TextRange GetRangeBefore() const;
243 
244     void SetRangeAfter(const TextRange& rangeAfter);
245     TextRange GetRangeAfter() const;
246 
reset()247     void reset()
248     {
249         originalSpans_.clear();
250         replacedSpans_.clear();
251         replacedImageSpans_.clear();
252         replacedSymbolSpans_.clear();
253         rangeBefore_ = TextRange();
254         rangeAfter_ = TextRange();
255     }
256 
257 private:
258     std::vector<RichEditorAbstractSpanResult> originalSpans_;
259     std::vector<RichEditorAbstractSpanResult> replacedSpans_;
260     std::vector<RichEditorAbstractSpanResult> replacedImageSpans_;
261     std::vector<RichEditorAbstractSpanResult> replacedSymbolSpans_;
262     TextRange rangeBefore_;
263     TextRange rangeAfter_;
264 };
265 
266 class StyledStringChangeValue : public BaseEventInfo {
DECLARE_ACE_TYPE(StyledStringChangeValue,BaseEventInfo)267     DECLARE_ACE_TYPE(StyledStringChangeValue, BaseEventInfo)
268 public:
269     StyledStringChangeValue() : BaseEventInfo("StyledStringChangeValue") {}
270     ~StyledStringChangeValue() = default;
271 
272     void SetRangeBefore(const TextRange& range);
273     TextRange GetRangeBefore() const;
274 
275     void SetRangeAfter(const TextRange& range);
276     TextRange GetRangeAfter() const;
277 
278     void SetReplacementString(const RefPtr<SpanStringBase>& styledString);
279     const RefPtr<SpanStringBase> GetReplacementString() const;
280 
281 private:
282     TextRange rangeBefore_;
283     TextRange rangeAfter_;
284     RefPtr<SpanStringBase> replacementString_;
285 };
286 
287 class RichEditorEventHub : public EventHub {
288     DECLARE_ACE_TYPE(RichEditorEventHub, EventHub)
289 
290 public:
291     RichEditorEventHub() = default;
292     ~RichEditorEventHub() override = default;
293     void SetOnReady(std::function<void()>&& func);
294     void FireOnReady();
295     void SetAboutToIMEInput(std::function<bool(const RichEditorInsertValue&)>&& func);
296     bool FireAboutToIMEInput(const RichEditorInsertValue& info);
297     void SetOnIMEInputComplete(std::function<void(const RichEditorAbstractSpanResult&)>&& func);
298     void SetOnDidIMEInput(std::function<void(const TextRange&)>&& func);
299     void FireOnIMEInputComplete(const RichEditorAbstractSpanResult& info);
300     void FireOnDidIMEInput(const TextRange& info);
301     void SetAboutToDelete(std::function<bool(const RichEditorDeleteValue&)>&& func);
302     bool FireAboutToDelete(const RichEditorDeleteValue& info);
303     void SetOnDeleteComplete(std::function<void()>&& func);
304     void FireOnDeleteComplete();
305     std::string GetDragExtraParams(const std::string& extraInfo, const Point& point, DragEventType type) override;
306     void SetOnEditingChange(std::function<void(const bool&)>&& func);
307     void FireOnEditingChange(bool isEditing);
SetOnSelect(std::function<void (const BaseEventInfo *)> && func)308     void SetOnSelect(std::function<void(const BaseEventInfo*)>&& func)
309     {
310         onSelect_ = std::move(func);
311     }
312 
FireOnSelect(BaseEventInfo * value)313     void FireOnSelect(BaseEventInfo* value)
314     {
315         if (onSelect_) {
316             onSelect_(value);
317         }
318     }
319 
SetOnSelectionChange(std::function<void (const BaseEventInfo *)> && func)320     void SetOnSelectionChange(std::function<void(const BaseEventInfo*)>&& func)
321     {
322         OnSelectionChange_ = std::move(func);
323     }
324 
FireOnSelectionChange(BaseEventInfo * value)325     void FireOnSelectionChange(BaseEventInfo* value)
326     {
327         if (OnSelectionChange_) {
328             OnSelectionChange_(value);
329         }
330     }
331 
SetTimestamp(long long timestamp)332     void SetTimestamp(long long timestamp)
333     {
334         timestamp_ = timestamp;
335     }
336 
SetOnPaste(std::function<void (NG::TextCommonEvent &)> && func)337     void SetOnPaste(std::function<void(NG::TextCommonEvent&)>&& func)
338     {
339         onPaste_ = std::move(func);
340     }
341 
FireOnPaste(NG::TextCommonEvent & value)342     void FireOnPaste(NG::TextCommonEvent& value)
343     {
344         if (onPaste_) {
345             onPaste_(value);
346         }
347     }
348 
SetOnSubmit(std::function<void (int32_t,NG::TextFieldCommonEvent &)> && func)349     void SetOnSubmit(std::function<void(int32_t, NG::TextFieldCommonEvent&)>&& func)
350     {
351         onSubmit_ = std::move(func);
352     }
353 
FireOnSubmit(int32_t value,NG::TextFieldCommonEvent & event)354     void FireOnSubmit(int32_t value, NG::TextFieldCommonEvent& event)
355     {
356         if (onSubmit_) {
357             onSubmit_(value, event);
358         }
359     }
360 
361     void SetOnWillChange(std::function<bool(const RichEditorChangeValue&)>&& func);
362     bool FireOnWillChange(const RichEditorChangeValue& info);
363     bool HasOnWillChange() const;
364     void SetOnDidChange(std::function<void(const RichEditorChangeValue&)>&& func);
365     void FireOnDidChange(const RichEditorChangeValue& info);
366     bool HasOnDidChange() const;
367     void SetOnCut(std::function<void(NG::TextCommonEvent&)>&& func);
368     void FireOnCut(NG::TextCommonEvent& value);
369     void SetOnCopy(std::function<void(NG::TextCommonEvent&)>&& func);
370     void FireOnCopy(NG::TextCommonEvent& value);
371     void SetOnStyledStringWillChange(std::function<bool(const StyledStringChangeValue&)>&& func);
372     bool FireOnStyledStringWillChange(const StyledStringChangeValue& info);
373     bool HasOnStyledStringWillChange() const;
374     void SetOnStyledStringDidChange(std::function<void(const StyledStringChangeValue&)>&& func);
375     void FireOnStyledStringDidChange(const StyledStringChangeValue& info);
376     bool HasOnStyledStringDidChange() const;
377 
378 private:
379     long long timestamp_ = 0;
380     std::function<void(NG::TextCommonEvent&)> onPaste_;
381     std::function<void()> onReady_;
382     std::function<void(const BaseEventInfo*)> onSelect_;
383     std::function<void(const BaseEventInfo*)> OnSelectionChange_;
384     std::function<bool(const RichEditorInsertValue&)> aboutToIMEInput_;
385     std::function<void(const RichEditorAbstractSpanResult&)> onIMEInputComplete_;
386     std::function<void(const TextRange&)> onDidIMEInput_;
387     std::function<bool(const RichEditorDeleteValue&)> aboutToDelete_;
388     std::function<void()> onDeleteComplete_;
389     std::function<void(int32_t, NG::TextFieldCommonEvent&)> onSubmit_;
390     std::function<void(const bool&)> onEditingChange_;
391     std::function<bool(const RichEditorChangeValue&)> onWillChange_;
392     std::function<void(const RichEditorChangeValue&)> onDidChange_;
393     std::function<void(NG::TextCommonEvent&)> onCut_;
394     std::function<void(NG::TextCommonEvent&)> onCopy_;
395     std::function<bool(const StyledStringChangeValue&)> onStyledStringWillChange_;
396     std::function<void(const StyledStringChangeValue&)> onStyledStringDidChange_;
397     ACE_DISALLOW_COPY_AND_MOVE(RichEditorEventHub);
398 };
399 } // namespace OHOS::Ace::NG
400 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_EVENT_HUB_H
401