• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "core/components_ng/pattern/rich_editor/rich_editor_event_hub.h"
16 
17 #include "core/components_ng/base/frame_node.h"
18 #include "core/components_ng/pattern/rich_editor/rich_editor_pattern.h"
19 
20 namespace OHOS::Ace::NG {
SetInsertOffset(int32_t insertOffset)21 void RichEditorInsertValue::SetInsertOffset(int32_t insertOffset)
22 {
23     insertOffset_ = insertOffset;
24 }
25 
GetInsertOffset() const26 int32_t RichEditorInsertValue::GetInsertOffset() const
27 {
28     return insertOffset_;
29 }
30 
SetInsertValue(const std::string & insertValue)31 void RichEditorInsertValue::SetInsertValue(const std::string& insertValue)
32 {
33     insertValue_ = insertValue;
34 }
35 
GetInsertValue() const36 const std::string& RichEditorInsertValue::GetInsertValue() const
37 {
38     return insertValue_;
39 }
40 
SetSpanIndex(int32_t spanIndex)41 void RichEditorAbstractSpanResult::SetSpanIndex(int32_t spanIndex)
42 {
43     spanIndex_ = spanIndex;
44 }
45 
GetSpanIndex() const46 int32_t RichEditorAbstractSpanResult::GetSpanIndex() const
47 {
48     return spanIndex_;
49 }
50 
SetSpanRangeStart(int32_t spanRangeStart)51 void RichEditorAbstractSpanResult::SetSpanRangeStart(int32_t spanRangeStart)
52 {
53     spanRangeStart_ = spanRangeStart;
54 }
55 
GetSpanRangeStart() const56 int32_t RichEditorAbstractSpanResult::GetSpanRangeStart() const
57 {
58     return spanRangeStart_;
59 }
60 
SetSpanRangeEnd(int32_t spanRangeEnd)61 void RichEditorAbstractSpanResult::SetSpanRangeEnd(int32_t spanRangeEnd)
62 {
63     spanRangeEnd_ = spanRangeEnd;
64 }
65 
GetSpanRangeEnd() const66 int32_t RichEditorAbstractSpanResult::GetSpanRangeEnd() const
67 {
68     return spanRangeEnd_;
69 }
70 
SetSpanType(SpanResultType spanType)71 void RichEditorAbstractSpanResult::SetSpanType(SpanResultType spanType)
72 {
73     spanType_ = spanType;
74 }
75 
GetType() const76 SpanResultType RichEditorAbstractSpanResult::GetType() const
77 {
78     return spanType_;
79 }
80 
SetOffsetInSpan(int32_t offsetInSpan)81 void RichEditorAbstractSpanResult::SetOffsetInSpan(int32_t offsetInSpan)
82 {
83     offsetInSpan_ = offsetInSpan;
84 }
85 
OffsetInSpan() const86 int32_t RichEditorAbstractSpanResult::OffsetInSpan() const
87 {
88     return offsetInSpan_;
89 }
90 
SetEraseLength(int32_t eraseLength)91 void RichEditorAbstractSpanResult::SetEraseLength(int32_t eraseLength)
92 {
93     eraseLength_ = eraseLength;
94 }
95 
GetEraseLength() const96 int32_t RichEditorAbstractSpanResult::GetEraseLength() const
97 {
98     return eraseLength_;
99 }
100 
SetValue(const std::string & value)101 void RichEditorAbstractSpanResult::SetValue(const std::string& value)
102 {
103     value_ = value;
104 }
105 
GetValue() const106 const std::string& RichEditorAbstractSpanResult::GetValue() const
107 {
108     return value_;
109 }
110 
SetFontColor(const std::string & fontColor)111 void RichEditorAbstractSpanResult::SetFontColor(const std::string& fontColor)
112 {
113     fontColor_ = fontColor;
114 }
115 
GetFontColor() const116 const std::string& RichEditorAbstractSpanResult::GetFontColor() const
117 {
118     return fontColor_;
119 }
120 
SetFontSize(double fontSize)121 void RichEditorAbstractSpanResult::SetFontSize(double fontSize)
122 {
123     fontSize_ = fontSize;
124 }
125 
GetFontSize() const126 double RichEditorAbstractSpanResult::GetFontSize() const
127 {
128     return fontSize_;
129 }
130 
SetFontWeight(int32_t fontWeigth)131 void RichEditorAbstractSpanResult::SetFontWeight(int32_t fontWeigth)
132 {
133     fontWeigth_ = fontWeigth;
134 }
135 
GetFontWeight() const136 int32_t RichEditorAbstractSpanResult::GetFontWeight() const
137 {
138     return fontWeigth_;
139 }
140 
SetFontFamily(const std::string & fontFamily)141 void RichEditorAbstractSpanResult::SetFontFamily(const std::string& fontFamily)
142 {
143     fontFamily_ = fontFamily;
144 }
145 
GetFontFamily() const146 const std::string& RichEditorAbstractSpanResult::GetFontFamily() const
147 {
148     return fontFamily_;
149 }
150 
SetTextDecoration(TextDecoration textDecoration)151 void RichEditorAbstractSpanResult::SetTextDecoration(TextDecoration textDecoration)
152 {
153     textDecoration_ = textDecoration;
154 }
155 
GetTextDecoration() const156 TextDecoration RichEditorAbstractSpanResult::GetTextDecoration() const
157 {
158     return textDecoration_;
159 }
160 
SetColor(const std::string & color)161 void RichEditorAbstractSpanResult::SetColor(const std::string& color)
162 {
163     color_ = color;
164 }
165 
GetColor() const166 const std::string& RichEditorAbstractSpanResult::GetColor() const
167 {
168     return color_;
169 }
170 
SetValuePixelMap(const RefPtr<PixelMap> & valuePixelMap)171 void RichEditorAbstractSpanResult::SetValuePixelMap(const RefPtr<PixelMap>& valuePixelMap)
172 {
173     valuePixelMap_ = valuePixelMap;
174 }
175 
GetValuePixelMap() const176 const RefPtr<PixelMap>& RichEditorAbstractSpanResult::GetValuePixelMap() const
177 {
178     return valuePixelMap_;
179 }
180 
SetValueResourceStr(const std::string valueResourceStr)181 void RichEditorAbstractSpanResult::SetValueResourceStr(const std::string valueResourceStr)
182 {
183     valueResourceStr_ = valueResourceStr;
184 }
185 
GetValueResourceStr() const186 const std::string& RichEditorAbstractSpanResult::GetValueResourceStr() const
187 {
188     return valueResourceStr_;
189 }
190 
SetSizeWidth(int32_t width)191 void RichEditorAbstractSpanResult::SetSizeWidth(int32_t width)
192 {
193     width_ = width;
194 }
195 
GetSizeWidth() const196 int32_t RichEditorAbstractSpanResult::GetSizeWidth() const
197 {
198     return width_;
199 }
200 
SetSizeHeight(int32_t height)201 void RichEditorAbstractSpanResult::SetSizeHeight(int32_t height)
202 {
203     height_ = height;
204 }
205 
GetSizeHeight() const206 int32_t RichEditorAbstractSpanResult::GetSizeHeight() const
207 {
208     return height_;
209 }
210 
SetVerticalAlign(VerticalAlign verticalAlign)211 void RichEditorAbstractSpanResult::SetVerticalAlign(VerticalAlign verticalAlign)
212 {
213     verticalAlign_ = verticalAlign;
214 }
215 
GetVerticalAlign() const216 VerticalAlign RichEditorAbstractSpanResult::GetVerticalAlign() const
217 {
218     return verticalAlign_;
219 }
220 
SetImageFit(ImageFit objectFit)221 void RichEditorAbstractSpanResult::SetImageFit(ImageFit objectFit)
222 {
223     objectFit_ = objectFit;
224 }
225 
GetObjectFit() const226 ImageFit RichEditorAbstractSpanResult::GetObjectFit() const
227 {
228     return objectFit_;
229 }
230 
SetOffset(int32_t offset)231 void RichEditorDeleteValue::SetOffset(int32_t offset)
232 {
233     offset_ = offset;
234 }
235 
GetOffset() const236 int32_t RichEditorDeleteValue::GetOffset() const
237 {
238     return offset_;
239 }
240 
SetRichEditorDeleteDirection(RichEditorDeleteDirection direction)241 void RichEditorDeleteValue::SetRichEditorDeleteDirection(RichEditorDeleteDirection direction)
242 {
243     direction_ = direction;
244 }
245 
GetRichEditorDeleteDirection() const246 RichEditorDeleteDirection RichEditorDeleteValue::GetRichEditorDeleteDirection() const
247 {
248     return direction_;
249 }
250 
SetLength(int32_t length)251 void RichEditorDeleteValue::SetLength(int32_t length)
252 {
253     length_ = length;
254 }
255 
GetLength() const256 int32_t RichEditorDeleteValue::GetLength() const
257 {
258     return length_;
259 }
260 
SetRichEditorDeleteSpans(const RichEditorAbstractSpanResult & deleteSpan)261 void RichEditorDeleteValue::SetRichEditorDeleteSpans(const RichEditorAbstractSpanResult& deleteSpan)
262 {
263     richEditorDeleteSpans_.emplace_back(deleteSpan);
264 }
265 
GetRichEditorDeleteSpans() const266 const std::list<RichEditorAbstractSpanResult>& RichEditorDeleteValue::GetRichEditorDeleteSpans() const
267 {
268     return richEditorDeleteSpans_;
269 }
270 
SetOnReady(std::function<void ()> && func)271 void RichEditorEventHub::SetOnReady(std::function<void()>&& func)
272 {
273     onReady_ = std::move(func);
274 }
275 
FireOnReady()276 void RichEditorEventHub::FireOnReady()
277 {
278     if (onReady_) {
279         onReady_();
280         auto host = GetFrameNode();
281         CHECK_NULL_VOID(host);
282         host->PostTask([host]() { host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE); });
283     }
284 }
285 
SetAboutToIMEInput(std::function<bool (const RichEditorInsertValue &)> && func)286 void RichEditorEventHub::SetAboutToIMEInput(std::function<bool(const RichEditorInsertValue&)>&& func)
287 {
288     aboutToIMEInput_ = std::move(func);
289 }
290 
FireAboutToIMEInput(const RichEditorInsertValue & info)291 bool RichEditorEventHub::FireAboutToIMEInput(const RichEditorInsertValue& info)
292 {
293     return aboutToIMEInput_ ? aboutToIMEInput_(info) : true;
294 }
295 
SetOnIMEInputComplete(std::function<void (const RichEditorAbstractSpanResult &)> && func)296 void RichEditorEventHub::SetOnIMEInputComplete(std::function<void(const RichEditorAbstractSpanResult&)>&& func)
297 {
298     onIMEIputComplete_ = std::move(func);
299 }
300 
FireOnIMEInputComplete(const RichEditorAbstractSpanResult & info)301 void RichEditorEventHub::FireOnIMEInputComplete(const RichEditorAbstractSpanResult& info)
302 {
303     if (onIMEIputComplete_)
304         onIMEIputComplete_(info);
305 }
306 
SetAboutToDelete(std::function<bool (const RichEditorDeleteValue &)> && func)307 void RichEditorEventHub::SetAboutToDelete(std::function<bool(const RichEditorDeleteValue&)>&& func)
308 {
309     aboutToDelete_ = std::move(func);
310 }
311 
FireAboutToDelete(const RichEditorDeleteValue & info)312 bool RichEditorEventHub::FireAboutToDelete(const RichEditorDeleteValue& info)
313 {
314     return aboutToDelete_ ? aboutToDelete_(info) : true;
315 }
316 
SetOnDeleteComplete(std::function<void ()> && func)317 void RichEditorEventHub::SetOnDeleteComplete(std::function<void()>&& func)
318 {
319     onDeleteComplete_ = std::move(func);
320 }
FireOndeleteComplete()321 void RichEditorEventHub::FireOndeleteComplete()
322 {
323     if (onDeleteComplete_)
324         onDeleteComplete_();
325 }
326 
GetDragExtraParams(const std::string & extraInfo,const Point & point,DragEventType type)327 std::string RichEditorEventHub::GetDragExtraParams(const std::string& extraInfo, const Point& point, DragEventType type)
328 {
329     auto host = GetFrameNode();
330     CHECK_NULL_RETURN(host, extraInfo);
331     auto pattern = host->GetPattern<RichEditorPattern>();
332     CHECK_NULL_RETURN(host, extraInfo);
333 
334     auto json = JsonUtil::Create(true);
335     if (type == DragEventType::DROP && pattern->GetTimestamp() == timestamp_) {
336         json->Put("isInComponent", true);
337     }
338     if (!extraInfo.empty()) {
339         json->Put("extraInfo", extraInfo.c_str());
340     }
341     return json->ToString();
342 }
343 } // namespace OHOS::Ace::NG
344