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/interfaces/native/node/rich_editor_modifier.h"
16 
17 #include "core/pipeline/base/element_register.h"
18 #include "core/components_ng/base/frame_node.h"
19 #include "core/components_ng/base/view_abstract.h"
20 #include "core/components/common/layout/constants.h"
21 #include "core/components_ng/pattern/rich_editor/rich_editor_model_ng.h"
22 #include "core/components_ng/pattern/rich_editor/rich_editor_theme.h"
23 
24 namespace OHOS::Ace::NG {
25 constexpr bool DEFAULT_ENABLE_TEXT_DETECTOR = false;
26 
SetRichEditorDetectEnable(ArkUINodeHandle node,ArkUI_Uint32 value)27 void SetRichEditorDetectEnable(ArkUINodeHandle node, ArkUI_Uint32 value)
28 {
29     auto* frameNode = reinterpret_cast<FrameNode*>(node);
30     CHECK_NULL_VOID(frameNode);
31     RichEditorModelNG::SetTextDetectEnable(frameNode, static_cast<bool>(value));
32 }
33 
ResetRichEditorDetectEnable(ArkUINodeHandle node)34 void ResetRichEditorDetectEnable(ArkUINodeHandle node)
35 {
36     auto* frameNode = reinterpret_cast<FrameNode*>(node);
37     CHECK_NULL_VOID(frameNode);
38     RichEditorModelNG::SetTextDetectEnable(frameNode, DEFAULT_ENABLE_TEXT_DETECTOR);
39 }
40 
SetRichEditorDataDetectorConfigWithEvent(ArkUINodeHandle node,const struct ArkUITextDetectConfigStruct * arkUITextDetectConfig)41 void SetRichEditorDataDetectorConfigWithEvent(
42     ArkUINodeHandle node, const struct ArkUITextDetectConfigStruct* arkUITextDetectConfig)
43 {
44     auto* frameNode = reinterpret_cast<FrameNode*>(node);
45     CHECK_NULL_VOID(frameNode);
46     TextDetectConfig textDetectConfig;
47     textDetectConfig.types = arkUITextDetectConfig->types;
48     if (arkUITextDetectConfig->onResult) {
49         textDetectConfig.onResult =
50             std::move(*(reinterpret_cast<std::function<void(const std::string&)>*>(arkUITextDetectConfig->onResult)));
51     }
52     textDetectConfig.entityColor = Color(arkUITextDetectConfig->entityColor);
53     textDetectConfig.entityDecorationType = TextDecoration(arkUITextDetectConfig->entityDecorationType);
54     textDetectConfig.entityDecorationColor = Color(arkUITextDetectConfig->entityDecorationColor);
55     textDetectConfig.entityDecorationStyle = TextDecorationStyle(arkUITextDetectConfig->entityDecorationStyle);
56     RichEditorModelNG::SetTextDetectConfig(frameNode, textDetectConfig);
57 }
58 
ResetRichEditorDataDetectorConfigWithEvent(ArkUINodeHandle node)59 void ResetRichEditorDataDetectorConfigWithEvent(ArkUINodeHandle node)
60 {
61     auto* frameNode = reinterpret_cast<FrameNode*>(node);
62     CHECK_NULL_VOID(frameNode);
63     TextDetectConfig textDetectConfig;
64     RichEditorModelNG::SetTextDetectConfig(frameNode, textDetectConfig);
65 }
66 
SetRichEditorOnIMEInputComplete(ArkUINodeHandle node,void * callback)67 void SetRichEditorOnIMEInputComplete(ArkUINodeHandle node, void* callback)
68 {
69     auto* frameNode = reinterpret_cast<FrameNode*>(node);
70     CHECK_NULL_VOID(frameNode);
71     if (callback) {
72         auto onIMEInputComplete = reinterpret_cast<std::function<void(RichEditorAbstractSpanResult)>*>(callback);
73         RichEditorModelNG::SetOnIMEInputComplete(frameNode, std::move(*onIMEInputComplete));
74     } else {
75         RichEditorModelNG::SetOnIMEInputComplete(frameNode, nullptr);
76     }
77 }
78 
ResetRichEditorOnIMEInputComplete(ArkUINodeHandle node)79 void ResetRichEditorOnIMEInputComplete(ArkUINodeHandle node)
80 {
81     auto* frameNode = reinterpret_cast<FrameNode*>(node);
82     CHECK_NULL_VOID(frameNode);
83     RichEditorModelNG::SetOnIMEInputComplete(frameNode, nullptr);
84 }
85 
SetRichEditorCopyOptions(ArkUINodeHandle node,ArkUI_Int32 copyOptionsValue)86 void SetRichEditorCopyOptions(ArkUINodeHandle node, ArkUI_Int32 copyOptionsValue)
87 {
88     auto* frameNode = reinterpret_cast<FrameNode*>(node);
89     CopyOptions copyOptions = static_cast<CopyOptions>(copyOptionsValue);
90     CHECK_NULL_VOID(frameNode);
91     RichEditorModelNG::SetCopyOption(frameNode, copyOptions);
92 }
93 
ResetRichEditorCopyOptions(ArkUINodeHandle node)94 void ResetRichEditorCopyOptions(ArkUINodeHandle node)
95 {
96     auto* frameNode = reinterpret_cast<FrameNode*>(node);
97     CHECK_NULL_VOID(frameNode);
98     CopyOptions defaultCopyOptions = CopyOptions::Distributed;
99     RichEditorModelNG::SetCopyOption(frameNode, defaultCopyOptions);
100 }
101 
SetRichEditorOnSelectionChange(ArkUINodeHandle node,void * callback)102 void SetRichEditorOnSelectionChange(ArkUINodeHandle node, void* callback)
103 {
104     auto* frameNode = reinterpret_cast<FrameNode*>(node);
105     CHECK_NULL_VOID(frameNode);
106     if (callback) {
107         auto onSelectionChange = reinterpret_cast<std::function<void(const BaseEventInfo*)>*>(callback);
108         RichEditorModelNG::SetOnSelectionChange(frameNode, std::move(*onSelectionChange));
109     } else {
110         RichEditorModelNG::SetOnSelectionChange(frameNode, nullptr);
111     }
112 }
113 
ResetRichEditorOnSelectionChange(ArkUINodeHandle node)114 void ResetRichEditorOnSelectionChange(ArkUINodeHandle node)
115 {
116     auto* frameNode = reinterpret_cast<FrameNode*>(node);
117     CHECK_NULL_VOID(frameNode);
118     RichEditorModelNG::SetOnSelectionChange(frameNode, nullptr);
119 }
120 
SetRichEditorCaretColor(ArkUINodeHandle node,ArkUI_Uint32 color)121 void SetRichEditorCaretColor(ArkUINodeHandle node, ArkUI_Uint32 color)
122 {
123     auto* frameNode = reinterpret_cast<FrameNode*>(node);
124     CHECK_NULL_VOID(frameNode);
125     RichEditorModelNG::SetCaretColor(frameNode, Color(color));
126 }
127 
ResetRichEditorCaretColor(ArkUINodeHandle node)128 void ResetRichEditorCaretColor(ArkUINodeHandle node)
129 {
130     auto pipeline = PipelineContext::GetCurrentContext();
131     CHECK_NULL_VOID(pipeline);
132     auto richEditorTheme = pipeline->GetTheme<RichEditorTheme>();
133     CHECK_NULL_VOID(richEditorTheme);
134     auto caretColor = richEditorTheme->GetCaretColor();
135     auto* frameNode = reinterpret_cast<FrameNode*>(node);
136     CHECK_NULL_VOID(frameNode);
137     RichEditorModelNG::SetCaretColor(frameNode, caretColor);
138 }
139 
SetRichEditorOnSelect(ArkUINodeHandle node,void * callback)140 void SetRichEditorOnSelect(ArkUINodeHandle node, void* callback)
141 {
142     auto* frameNode = reinterpret_cast<FrameNode*>(node);
143     CHECK_NULL_VOID(frameNode);
144     if (callback) {
145         auto onSelect = reinterpret_cast<std::function<void(const BaseEventInfo*)>*>(callback);
146         RichEditorModelNG::SetOnSelect(frameNode, std::move(*onSelect));
147     } else {
148         RichEditorModelNG::SetOnSelect(frameNode, nullptr);
149     }
150 }
151 
ResetRichEditorOnSelect(ArkUINodeHandle node)152 void ResetRichEditorOnSelect(ArkUINodeHandle node)
153 {
154     auto* frameNode = reinterpret_cast<FrameNode*>(node);
155     CHECK_NULL_VOID(frameNode);
156     RichEditorModelNG::SetOnSelect(frameNode, nullptr);
157 }
158 
SetRichEditorOnSubmit(ArkUINodeHandle node,void * callback)159 void SetRichEditorOnSubmit(ArkUINodeHandle node, void* callback)
160 {
161     auto* frameNode = reinterpret_cast<FrameNode*>(node);
162     CHECK_NULL_VOID(frameNode);
163     if (callback) {
164         auto onSubmit = reinterpret_cast<std::function<void(int32_t, NG::TextFieldCommonEvent&)>*>(callback);
165         RichEditorModelNG::SetOnSubmit(frameNode, std::move(*onSubmit));
166     } else {
167         RichEditorModelNG::SetOnSubmit(frameNode, nullptr);
168     }
169 }
170 
ResetRichEditorOnSubmit(ArkUINodeHandle node)171 void ResetRichEditorOnSubmit(ArkUINodeHandle node)
172 {
173     auto* frameNode = reinterpret_cast<FrameNode*>(node);
174     CHECK_NULL_VOID(frameNode);
175     RichEditorModelNG::SetOnSubmit(frameNode, nullptr);
176 }
177 
SetRichEditorAboutToIMEInput(ArkUINodeHandle node,void * callback)178 void SetRichEditorAboutToIMEInput(ArkUINodeHandle node, void* callback)
179 {
180     auto* frameNode = reinterpret_cast<FrameNode*>(node);
181     CHECK_NULL_VOID(frameNode);
182     if (callback) {
183         auto aboutToIMEInput = reinterpret_cast<std::function<bool(const RichEditorInsertValue&)>*>(callback);
184         RichEditorModelNG::SetAboutToIMEInput(frameNode, std::move(*aboutToIMEInput));
185     } else {
186         RichEditorModelNG::SetAboutToIMEInput(frameNode, nullptr);
187     }
188 }
189 
ResetRichEditorAboutToIMEInput(ArkUINodeHandle node)190 void ResetRichEditorAboutToIMEInput(ArkUINodeHandle node)
191 {
192     auto* frameNode = reinterpret_cast<FrameNode*>(node);
193     CHECK_NULL_VOID(frameNode);
194     RichEditorModelNG::SetAboutToIMEInput(frameNode, nullptr);
195 }
196 
SetRichEditorOnReady(ArkUINodeHandle node,void * callback)197 void SetRichEditorOnReady(ArkUINodeHandle node, void* callback)
198 {
199     auto* frameNode = reinterpret_cast<FrameNode*>(node);
200     CHECK_NULL_VOID(frameNode);
201     if (callback) {
202         auto onReady = reinterpret_cast<std::function<void(void)>*>(callback);
203         RichEditorModelNG::SetOnReady(frameNode, std::move(*onReady));
204     } else {
205         RichEditorModelNG::SetOnReady(frameNode, nullptr);
206     }
207 }
208 
ResetRichEditorOnReady(ArkUINodeHandle node)209 void ResetRichEditorOnReady(ArkUINodeHandle node)
210 {
211     auto* frameNode = reinterpret_cast<FrameNode*>(node);
212     CHECK_NULL_VOID(frameNode);
213     RichEditorModelNG::SetOnReady(frameNode, nullptr);
214 }
215 
SetRichEditorOnDeleteComplete(ArkUINodeHandle node,void * callback)216 void SetRichEditorOnDeleteComplete(ArkUINodeHandle node, void* callback)
217 {
218     auto* frameNode = reinterpret_cast<FrameNode*>(node);
219     CHECK_NULL_VOID(frameNode);
220     if (callback) {
221         auto onDeleteComplete = reinterpret_cast<std::function<void(void)>*>(callback);
222         RichEditorModelNG::SetOnDeleteComplete(frameNode, std::move(*onDeleteComplete));
223     } else {
224         RichEditorModelNG::SetOnDeleteComplete(frameNode, nullptr);
225     }
226 }
227 
ResetRichEditorOnDeleteComplete(ArkUINodeHandle node)228 void ResetRichEditorOnDeleteComplete(ArkUINodeHandle node)
229 {
230     auto* frameNode = reinterpret_cast<FrameNode*>(node);
231     CHECK_NULL_VOID(frameNode);
232     RichEditorModelNG::SetOnDeleteComplete(frameNode, nullptr);
233 }
234 
SetRichEditorOnEditingChange(ArkUINodeHandle node,void * callback)235 void SetRichEditorOnEditingChange(ArkUINodeHandle node, void* callback)
236 {
237     auto* frameNode = reinterpret_cast<FrameNode*>(node);
238     CHECK_NULL_VOID(frameNode);
239     if (callback) {
240         auto onEditingChange = reinterpret_cast<std::function<void(bool)>*>(callback);
241         RichEditorModelNG::SetOnEditingChange(frameNode, std::move(*onEditingChange));
242     } else {
243         RichEditorModelNG::SetOnEditingChange(frameNode, nullptr);
244     }
245 }
246 
ResetRichEditorOnEditingChange(ArkUINodeHandle node)247 void ResetRichEditorOnEditingChange(ArkUINodeHandle node)
248 {
249     auto* frameNode = reinterpret_cast<FrameNode*>(node);
250     CHECK_NULL_VOID(frameNode);
251     RichEditorModelNG::SetOnEditingChange(frameNode, nullptr);
252 }
253 
SetRichEditorSelectedBackgroundColor(ArkUINodeHandle node,ArkUI_Uint32 color)254 void SetRichEditorSelectedBackgroundColor(ArkUINodeHandle node, ArkUI_Uint32 color)
255 {
256     auto* frameNode = reinterpret_cast<FrameNode*>(node);
257     CHECK_NULL_VOID(frameNode);
258     RichEditorModelNG::SetSelectedBackgroundColor(frameNode, Color(color));
259 }
260 
ResetRichEditorSelectedBackgroundColor(ArkUINodeHandle node)261 void ResetRichEditorSelectedBackgroundColor(ArkUINodeHandle node)
262 {
263     auto pipeline = PipelineContext::GetCurrentContext();
264     CHECK_NULL_VOID(pipeline);
265     auto richEditorTheme = pipeline->GetTheme<RichEditorTheme>();
266     CHECK_NULL_VOID(richEditorTheme);
267     auto selectedBackgroundColor = richEditorTheme->GetSelectedBackgroundColor();
268     auto* frameNode = reinterpret_cast<FrameNode*>(node);
269     CHECK_NULL_VOID(frameNode);
270     RichEditorModelNG::SetSelectedBackgroundColor(frameNode, selectedBackgroundColor);
271 }
272 
SetRichEditorOnPaste(ArkUINodeHandle node,void * callback)273 void SetRichEditorOnPaste(ArkUINodeHandle node, void* callback)
274 {
275     auto* frameNode = reinterpret_cast<FrameNode*>(node);
276     CHECK_NULL_VOID(frameNode);
277     if (callback) {
278         auto onPaste = reinterpret_cast<std::function<void(NG::TextCommonEvent&)>*>(callback);
279         RichEditorModelNG::SetOnPaste(frameNode, std::move(*onPaste));
280     } else {
281         RichEditorModelNG::SetOnPaste(frameNode, nullptr);
282     }
283 }
284 
ResetRichEditorOnPaste(ArkUINodeHandle node)285 void ResetRichEditorOnPaste(ArkUINodeHandle node)
286 {
287     auto* frameNode = reinterpret_cast<FrameNode*>(node);
288     CHECK_NULL_VOID(frameNode);
289     RichEditorModelNG::SetOnPaste(frameNode, nullptr);
290 }
291 
SetRichEditorOnCut(ArkUINodeHandle node,void * callback)292 void SetRichEditorOnCut(ArkUINodeHandle node, void* callback)
293 {
294     auto* frameNode = reinterpret_cast<FrameNode*>(node);
295     CHECK_NULL_VOID(frameNode);
296     if (callback) {
297         auto onCut = reinterpret_cast<std::function<void(NG::TextCommonEvent&)>*>(callback);
298         RichEditorModelNG::SetOnCut(frameNode, std::move(*onCut));
299     } else {
300         RichEditorModelNG::SetOnCut(frameNode, nullptr);
301     }
302 }
303 
ResetRichEditorOnCut(ArkUINodeHandle node)304 void ResetRichEditorOnCut(ArkUINodeHandle node)
305 {
306     auto* frameNode = reinterpret_cast<FrameNode*>(node);
307     CHECK_NULL_VOID(frameNode);
308     RichEditorModelNG::SetOnCut(frameNode, nullptr);
309 }
310 
SetRichEditorOnCopy(ArkUINodeHandle node,void * callback)311 void SetRichEditorOnCopy(ArkUINodeHandle node, void* callback)
312 {
313     auto* frameNode = reinterpret_cast<FrameNode*>(node);
314     CHECK_NULL_VOID(frameNode);
315     if (callback) {
316         auto onCopy = reinterpret_cast<std::function<void(NG::TextCommonEvent&)>*>(callback);
317         RichEditorModelNG::SetOnCopy(frameNode, std::move(*onCopy));
318     } else {
319         RichEditorModelNG::SetOnCopy(frameNode, nullptr);
320     }
321 }
322 
ResetRichEditorOnCopy(ArkUINodeHandle node)323 void ResetRichEditorOnCopy(ArkUINodeHandle node)
324 {
325     auto* frameNode = reinterpret_cast<FrameNode*>(node);
326     CHECK_NULL_VOID(frameNode);
327     RichEditorModelNG::SetOnCopy(frameNode, nullptr);
328 }
329 
SetRichEditorEnterKeyType(ArkUINodeHandle node,ArkUI_Uint32 enterKeyType)330 void SetRichEditorEnterKeyType(ArkUINodeHandle node, ArkUI_Uint32 enterKeyType)
331 {
332     auto* frameNode = reinterpret_cast<FrameNode*>(node);
333     CHECK_NULL_VOID(frameNode);
334     RichEditorModelNG::SetEnterKeyType(frameNode, TextInputAction(enterKeyType));
335 }
336 
ResetRichEditorEnterKeyType(ArkUINodeHandle node)337 void ResetRichEditorEnterKeyType(ArkUINodeHandle node)
338 {
339     auto* frameNode = reinterpret_cast<FrameNode*>(node);
340     CHECK_NULL_VOID(frameNode);
341     auto defaultEnterKeyType = TextInputAction::NEW_LINE;
342     RichEditorModelNG::SetEnterKeyType(frameNode, defaultEnterKeyType);
343 }
344 
345 namespace NodeModifier {
GetRichEditorModifier()346 const ArkUIRichEditorModifier* GetRichEditorModifier()
347 {
348     static const ArkUIRichEditorModifier modifier = { SetRichEditorDetectEnable, ResetRichEditorDetectEnable,
349         SetRichEditorDataDetectorConfigWithEvent, ResetRichEditorDataDetectorConfigWithEvent,
350         SetRichEditorOnIMEInputComplete, ResetRichEditorOnIMEInputComplete,
351         SetRichEditorCopyOptions, ResetRichEditorCopyOptions, SetRichEditorOnSelectionChange,
352         ResetRichEditorOnSelectionChange, SetRichEditorCaretColor, ResetRichEditorCaretColor,
353         SetRichEditorOnSelect, ResetRichEditorOnSelect,
354         SetRichEditorOnSubmit, ResetRichEditorOnSubmit, SetRichEditorAboutToIMEInput, ResetRichEditorAboutToIMEInput,
355         SetRichEditorOnReady, ResetRichEditorOnReady, SetRichEditorOnDeleteComplete, ResetRichEditorOnDeleteComplete,
356         SetRichEditorOnEditingChange, ResetRichEditorOnEditingChange,
357         SetRichEditorSelectedBackgroundColor, ResetRichEditorSelectedBackgroundColor, SetRichEditorOnPaste,
358         ResetRichEditorOnPaste, SetRichEditorOnCut, ResetRichEditorOnCut, SetRichEditorOnCopy, ResetRichEditorOnCopy,
359         SetRichEditorEnterKeyType, ResetRichEditorEnterKeyType };
360     return &modifier;
361 }
362 
GetCJUIRichEditorModifier()363 const CJUIRichEditorModifier* GetCJUIRichEditorModifier()
364 {
365     static const CJUIRichEditorModifier modifier = { SetRichEditorDetectEnable, ResetRichEditorDetectEnable,
366         SetRichEditorCopyOptions, ResetRichEditorCopyOptions, SetRichEditorCaretColor, ResetRichEditorCaretColor,
367         SetRichEditorOnReady, ResetRichEditorOnReady, SetRichEditorOnDeleteComplete, ResetRichEditorOnDeleteComplete,
368         SetRichEditorOnEditingChange, ResetRichEditorOnEditingChange,
369         SetRichEditorSelectedBackgroundColor, ResetRichEditorSelectedBackgroundColor, SetRichEditorEnterKeyType,
370         ResetRichEditorEnterKeyType };
371     return &modifier;
372 }
373 }
374 }