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/components_ng/pattern/rich_editor/rich_editor_model_ng.h"
18 #include "core/components_ng/pattern/rich_editor/rich_editor_theme.h"
19
20 namespace OHOS::Ace::NG {
21 namespace {
22 constexpr uint32_t NORMAL_VALUE_ARRAY_STEP = 2;
23 constexpr DisplayMode DEFAULT_BAR_STATE_VALUE = DisplayMode::AUTO;
24 }
25
26 constexpr bool DEFAULT_ENABLE_TEXT_DETECTOR = false;
27
SetRichEditorDetectEnable(ArkUINodeHandle node,ArkUI_Uint32 value)28 void SetRichEditorDetectEnable(ArkUINodeHandle node, ArkUI_Uint32 value)
29 {
30 auto* frameNode = reinterpret_cast<FrameNode*>(node);
31 CHECK_NULL_VOID(frameNode);
32 RichEditorModelNG::SetTextDetectEnable(frameNode, static_cast<bool>(value));
33 }
34
ResetRichEditorDetectEnable(ArkUINodeHandle node)35 void ResetRichEditorDetectEnable(ArkUINodeHandle node)
36 {
37 auto* frameNode = reinterpret_cast<FrameNode*>(node);
38 CHECK_NULL_VOID(frameNode);
39 RichEditorModelNG::SetTextDetectEnable(frameNode, DEFAULT_ENABLE_TEXT_DETECTOR);
40 }
41
SetRichEditorDataDetectorConfigWithEvent(ArkUINodeHandle node,const struct ArkUITextDetectConfigStruct * arkUITextDetectConfig)42 void SetRichEditorDataDetectorConfigWithEvent(
43 ArkUINodeHandle node, const struct ArkUITextDetectConfigStruct* arkUITextDetectConfig)
44 {
45 auto* frameNode = reinterpret_cast<FrameNode*>(node);
46 CHECK_NULL_VOID(frameNode);
47 TextDetectConfig textDetectConfig;
48 textDetectConfig.types = arkUITextDetectConfig->types;
49 if (arkUITextDetectConfig->onResult) {
50 textDetectConfig.onResult =
51 std::move(*(reinterpret_cast<std::function<void(const std::string&)>*>(arkUITextDetectConfig->onResult)));
52 }
53 textDetectConfig.entityColor = Color(arkUITextDetectConfig->entityColor);
54 textDetectConfig.entityDecorationType = TextDecoration(arkUITextDetectConfig->entityDecorationType);
55 textDetectConfig.entityDecorationColor = Color(arkUITextDetectConfig->entityDecorationColor);
56 textDetectConfig.entityDecorationStyle = TextDecorationStyle(arkUITextDetectConfig->entityDecorationStyle);
57 RichEditorModelNG::SetTextDetectConfig(frameNode, textDetectConfig);
58 }
59
ResetRichEditorDataDetectorConfigWithEvent(ArkUINodeHandle node)60 void ResetRichEditorDataDetectorConfigWithEvent(ArkUINodeHandle node)
61 {
62 auto* frameNode = reinterpret_cast<FrameNode*>(node);
63 CHECK_NULL_VOID(frameNode);
64 TextDetectConfig textDetectConfig;
65 RichEditorModelNG::SetTextDetectConfig(frameNode, textDetectConfig);
66 }
67
SetRichEditorOnIMEInputComplete(ArkUINodeHandle node,void * callback)68 void SetRichEditorOnIMEInputComplete(ArkUINodeHandle node, void* callback)
69 {
70 auto* frameNode = reinterpret_cast<FrameNode*>(node);
71 CHECK_NULL_VOID(frameNode);
72 if (callback) {
73 auto onIMEInputComplete = reinterpret_cast<std::function<void(RichEditorAbstractSpanResult)>*>(callback);
74 RichEditorModelNG::SetOnIMEInputComplete(frameNode, std::move(*onIMEInputComplete));
75 } else {
76 RichEditorModelNG::SetOnIMEInputComplete(frameNode, nullptr);
77 }
78 }
79
ResetRichEditorOnIMEInputComplete(ArkUINodeHandle node)80 void ResetRichEditorOnIMEInputComplete(ArkUINodeHandle node)
81 {
82 auto* frameNode = reinterpret_cast<FrameNode*>(node);
83 CHECK_NULL_VOID(frameNode);
84 RichEditorModelNG::SetOnIMEInputComplete(frameNode, nullptr);
85 }
86
SetRichEditorCopyOptions(ArkUINodeHandle node,ArkUI_Int32 copyOptionsValue)87 void SetRichEditorCopyOptions(ArkUINodeHandle node, ArkUI_Int32 copyOptionsValue)
88 {
89 auto* frameNode = reinterpret_cast<FrameNode*>(node);
90 CopyOptions copyOptions = static_cast<CopyOptions>(copyOptionsValue);
91 CHECK_NULL_VOID(frameNode);
92 RichEditorModelNG::SetCopyOption(frameNode, copyOptions);
93 }
94
ResetRichEditorCopyOptions(ArkUINodeHandle node)95 void ResetRichEditorCopyOptions(ArkUINodeHandle node)
96 {
97 auto* frameNode = reinterpret_cast<FrameNode*>(node);
98 CHECK_NULL_VOID(frameNode);
99 CopyOptions defaultCopyOptions = CopyOptions::Distributed;
100 RichEditorModelNG::SetCopyOption(frameNode, defaultCopyOptions);
101 }
102
SetRichEditorOnSelectionChange(ArkUINodeHandle node,void * callback)103 void SetRichEditorOnSelectionChange(ArkUINodeHandle node, void* callback)
104 {
105 auto* frameNode = reinterpret_cast<FrameNode*>(node);
106 CHECK_NULL_VOID(frameNode);
107 if (callback) {
108 auto onSelectionChange = reinterpret_cast<std::function<void(const BaseEventInfo*)>*>(callback);
109 RichEditorModelNG::SetOnSelectionChange(frameNode, std::move(*onSelectionChange));
110 } else {
111 RichEditorModelNG::SetOnSelectionChange(frameNode, nullptr);
112 }
113 }
114
ResetRichEditorOnSelectionChange(ArkUINodeHandle node)115 void ResetRichEditorOnSelectionChange(ArkUINodeHandle node)
116 {
117 auto* frameNode = reinterpret_cast<FrameNode*>(node);
118 CHECK_NULL_VOID(frameNode);
119 RichEditorModelNG::SetOnSelectionChange(frameNode, nullptr);
120 }
121
SetRichEditorCaretColor(ArkUINodeHandle node,ArkUI_Uint32 color)122 void SetRichEditorCaretColor(ArkUINodeHandle node, ArkUI_Uint32 color)
123 {
124 auto* frameNode = reinterpret_cast<FrameNode*>(node);
125 CHECK_NULL_VOID(frameNode);
126 RichEditorModelNG::SetCaretColor(frameNode, Color(color));
127 }
128
ResetRichEditorCaretColor(ArkUINodeHandle node)129 void ResetRichEditorCaretColor(ArkUINodeHandle node)
130 {
131 auto pipeline = PipelineContext::GetCurrentContext();
132 CHECK_NULL_VOID(pipeline);
133 auto richEditorTheme = pipeline->GetTheme<RichEditorTheme>();
134 CHECK_NULL_VOID(richEditorTheme);
135 auto caretColor = richEditorTheme->GetCaretColor();
136 auto* frameNode = reinterpret_cast<FrameNode*>(node);
137 CHECK_NULL_VOID(frameNode);
138 RichEditorModelNG::SetCaretColor(frameNode, caretColor);
139 }
140
SetRichEditorOnSelect(ArkUINodeHandle node,void * callback)141 void SetRichEditorOnSelect(ArkUINodeHandle node, void* callback)
142 {
143 auto* frameNode = reinterpret_cast<FrameNode*>(node);
144 CHECK_NULL_VOID(frameNode);
145 if (callback) {
146 auto onSelect = reinterpret_cast<std::function<void(const BaseEventInfo*)>*>(callback);
147 RichEditorModelNG::SetOnSelect(frameNode, std::move(*onSelect));
148 } else {
149 RichEditorModelNG::SetOnSelect(frameNode, nullptr);
150 }
151 }
152
ResetRichEditorOnSelect(ArkUINodeHandle node)153 void ResetRichEditorOnSelect(ArkUINodeHandle node)
154 {
155 auto* frameNode = reinterpret_cast<FrameNode*>(node);
156 CHECK_NULL_VOID(frameNode);
157 RichEditorModelNG::SetOnSelect(frameNode, nullptr);
158 }
159
SetRichEditorOnSubmit(ArkUINodeHandle node,void * callback)160 void SetRichEditorOnSubmit(ArkUINodeHandle node, void* callback)
161 {
162 auto* frameNode = reinterpret_cast<FrameNode*>(node);
163 CHECK_NULL_VOID(frameNode);
164 if (callback) {
165 auto onSubmit = reinterpret_cast<std::function<void(int32_t, NG::TextFieldCommonEvent&)>*>(callback);
166 RichEditorModelNG::SetOnSubmit(frameNode, std::move(*onSubmit));
167 } else {
168 RichEditorModelNG::SetOnSubmit(frameNode, nullptr);
169 }
170 }
171
ResetRichEditorOnSubmit(ArkUINodeHandle node)172 void ResetRichEditorOnSubmit(ArkUINodeHandle node)
173 {
174 auto* frameNode = reinterpret_cast<FrameNode*>(node);
175 CHECK_NULL_VOID(frameNode);
176 RichEditorModelNG::SetOnSubmit(frameNode, nullptr);
177 }
178
SetRichEditorAboutToIMEInput(ArkUINodeHandle node,void * callback)179 void SetRichEditorAboutToIMEInput(ArkUINodeHandle node, void* callback)
180 {
181 auto* frameNode = reinterpret_cast<FrameNode*>(node);
182 CHECK_NULL_VOID(frameNode);
183 if (callback) {
184 auto aboutToIMEInput = reinterpret_cast<std::function<bool(const RichEditorInsertValue&)>*>(callback);
185 RichEditorModelNG::SetAboutToIMEInput(frameNode, std::move(*aboutToIMEInput));
186 } else {
187 RichEditorModelNG::SetAboutToIMEInput(frameNode, nullptr);
188 }
189 }
190
ResetRichEditorAboutToIMEInput(ArkUINodeHandle node)191 void ResetRichEditorAboutToIMEInput(ArkUINodeHandle node)
192 {
193 auto* frameNode = reinterpret_cast<FrameNode*>(node);
194 CHECK_NULL_VOID(frameNode);
195 RichEditorModelNG::SetAboutToIMEInput(frameNode, nullptr);
196 }
197
SetRichEditorOnReady(ArkUINodeHandle node,void * callback)198 void SetRichEditorOnReady(ArkUINodeHandle node, void* callback)
199 {
200 auto* frameNode = reinterpret_cast<FrameNode*>(node);
201 CHECK_NULL_VOID(frameNode);
202 if (callback) {
203 auto onReady = reinterpret_cast<std::function<void(void)>*>(callback);
204 RichEditorModelNG::SetOnReady(frameNode, std::move(*onReady));
205 } else {
206 RichEditorModelNG::SetOnReady(frameNode, nullptr);
207 }
208 }
209
ResetRichEditorOnReady(ArkUINodeHandle node)210 void ResetRichEditorOnReady(ArkUINodeHandle node)
211 {
212 auto* frameNode = reinterpret_cast<FrameNode*>(node);
213 CHECK_NULL_VOID(frameNode);
214 RichEditorModelNG::SetOnReady(frameNode, nullptr);
215 }
216
SetRichEditorOnDeleteComplete(ArkUINodeHandle node,void * callback)217 void SetRichEditorOnDeleteComplete(ArkUINodeHandle node, void* callback)
218 {
219 auto* frameNode = reinterpret_cast<FrameNode*>(node);
220 CHECK_NULL_VOID(frameNode);
221 if (callback) {
222 auto onDeleteComplete = reinterpret_cast<std::function<void(void)>*>(callback);
223 RichEditorModelNG::SetOnDeleteComplete(frameNode, std::move(*onDeleteComplete));
224 } else {
225 RichEditorModelNG::SetOnDeleteComplete(frameNode, nullptr);
226 }
227 }
228
ResetRichEditorOnDeleteComplete(ArkUINodeHandle node)229 void ResetRichEditorOnDeleteComplete(ArkUINodeHandle node)
230 {
231 auto* frameNode = reinterpret_cast<FrameNode*>(node);
232 CHECK_NULL_VOID(frameNode);
233 RichEditorModelNG::SetOnDeleteComplete(frameNode, nullptr);
234 }
235
SetRichEditorOnEditingChange(ArkUINodeHandle node,void * callback)236 void SetRichEditorOnEditingChange(ArkUINodeHandle node, void* callback)
237 {
238 auto* frameNode = reinterpret_cast<FrameNode*>(node);
239 CHECK_NULL_VOID(frameNode);
240 if (callback) {
241 auto onEditingChange = reinterpret_cast<std::function<void(bool)>*>(callback);
242 RichEditorModelNG::SetOnEditingChange(frameNode, std::move(*onEditingChange));
243 } else {
244 RichEditorModelNG::SetOnEditingChange(frameNode, nullptr);
245 }
246 }
247
ResetRichEditorOnEditingChange(ArkUINodeHandle node)248 void ResetRichEditorOnEditingChange(ArkUINodeHandle node)
249 {
250 auto* frameNode = reinterpret_cast<FrameNode*>(node);
251 CHECK_NULL_VOID(frameNode);
252 RichEditorModelNG::SetOnEditingChange(frameNode, nullptr);
253 }
254
SetRichEditorSelectedBackgroundColor(ArkUINodeHandle node,ArkUI_Uint32 color)255 void SetRichEditorSelectedBackgroundColor(ArkUINodeHandle node, ArkUI_Uint32 color)
256 {
257 auto* frameNode = reinterpret_cast<FrameNode*>(node);
258 CHECK_NULL_VOID(frameNode);
259 RichEditorModelNG::SetSelectedBackgroundColor(frameNode, Color(color));
260 }
261
ResetRichEditorSelectedBackgroundColor(ArkUINodeHandle node)262 void ResetRichEditorSelectedBackgroundColor(ArkUINodeHandle node)
263 {
264 auto pipeline = PipelineContext::GetCurrentContext();
265 CHECK_NULL_VOID(pipeline);
266 auto richEditorTheme = pipeline->GetTheme<RichEditorTheme>();
267 CHECK_NULL_VOID(richEditorTheme);
268 auto selectedBackgroundColor = richEditorTheme->GetSelectedBackgroundColor();
269 auto* frameNode = reinterpret_cast<FrameNode*>(node);
270 CHECK_NULL_VOID(frameNode);
271 RichEditorModelNG::SetSelectedBackgroundColor(frameNode, selectedBackgroundColor);
272 }
273
SetRichEditorOnPaste(ArkUINodeHandle node,void * callback)274 void SetRichEditorOnPaste(ArkUINodeHandle node, void* callback)
275 {
276 auto* frameNode = reinterpret_cast<FrameNode*>(node);
277 CHECK_NULL_VOID(frameNode);
278 if (callback) {
279 auto onPaste = reinterpret_cast<std::function<void(NG::TextCommonEvent&)>*>(callback);
280 RichEditorModelNG::SetOnPaste(frameNode, std::move(*onPaste));
281 } else {
282 RichEditorModelNG::SetOnPaste(frameNode, nullptr);
283 }
284 }
285
ResetRichEditorOnPaste(ArkUINodeHandle node)286 void ResetRichEditorOnPaste(ArkUINodeHandle node)
287 {
288 auto* frameNode = reinterpret_cast<FrameNode*>(node);
289 CHECK_NULL_VOID(frameNode);
290 RichEditorModelNG::SetOnPaste(frameNode, nullptr);
291 }
292
SetRichEditorOnCut(ArkUINodeHandle node,void * callback)293 void SetRichEditorOnCut(ArkUINodeHandle node, void* callback)
294 {
295 auto* frameNode = reinterpret_cast<FrameNode*>(node);
296 CHECK_NULL_VOID(frameNode);
297 if (callback) {
298 auto onCut = reinterpret_cast<std::function<void(NG::TextCommonEvent&)>*>(callback);
299 RichEditorModelNG::SetOnCut(frameNode, std::move(*onCut));
300 } else {
301 RichEditorModelNG::SetOnCut(frameNode, nullptr);
302 }
303 }
304
ResetRichEditorOnCut(ArkUINodeHandle node)305 void ResetRichEditorOnCut(ArkUINodeHandle node)
306 {
307 auto* frameNode = reinterpret_cast<FrameNode*>(node);
308 CHECK_NULL_VOID(frameNode);
309 RichEditorModelNG::SetOnCut(frameNode, nullptr);
310 }
311
SetRichEditorOnCopy(ArkUINodeHandle node,void * callback)312 void SetRichEditorOnCopy(ArkUINodeHandle node, void* callback)
313 {
314 auto* frameNode = reinterpret_cast<FrameNode*>(node);
315 CHECK_NULL_VOID(frameNode);
316 if (callback) {
317 auto onCopy = reinterpret_cast<std::function<void(NG::TextCommonEvent&)>*>(callback);
318 RichEditorModelNG::SetOnCopy(frameNode, std::move(*onCopy));
319 } else {
320 RichEditorModelNG::SetOnCopy(frameNode, nullptr);
321 }
322 }
323
ResetRichEditorOnCopy(ArkUINodeHandle node)324 void ResetRichEditorOnCopy(ArkUINodeHandle node)
325 {
326 auto* frameNode = reinterpret_cast<FrameNode*>(node);
327 CHECK_NULL_VOID(frameNode);
328 RichEditorModelNG::SetOnCopy(frameNode, nullptr);
329 }
330
SetRichEditorEnterKeyType(ArkUINodeHandle node,ArkUI_Uint32 enterKeyType)331 void SetRichEditorEnterKeyType(ArkUINodeHandle node, ArkUI_Uint32 enterKeyType)
332 {
333 auto* frameNode = reinterpret_cast<FrameNode*>(node);
334 CHECK_NULL_VOID(frameNode);
335 RichEditorModelNG::SetEnterKeyType(frameNode, TextInputAction(enterKeyType));
336 }
337
ResetRichEditorEnterKeyType(ArkUINodeHandle node)338 void ResetRichEditorEnterKeyType(ArkUINodeHandle node)
339 {
340 auto* frameNode = reinterpret_cast<FrameNode*>(node);
341 CHECK_NULL_VOID(frameNode);
342 auto defaultEnterKeyType = TextInputAction::NEW_LINE;
343 RichEditorModelNG::SetEnterKeyType(frameNode, defaultEnterKeyType);
344 }
345
SetRichEditorEnableKeyboardOnFocus(ArkUINodeHandle node,ArkUI_Bool value)346 void SetRichEditorEnableKeyboardOnFocus(ArkUINodeHandle node, ArkUI_Bool value)
347 {
348 auto* frameNode = reinterpret_cast<FrameNode*>(node);
349 CHECK_NULL_VOID(frameNode);
350 RichEditorModelNG::SetRequestKeyboardOnFocus(frameNode, value);
351 }
352
ResetRichEditorEnableKeyboardOnFocus(ArkUINodeHandle node)353 void ResetRichEditorEnableKeyboardOnFocus(ArkUINodeHandle node)
354 {
355 auto* frameNode = reinterpret_cast<FrameNode*>(node);
356 CHECK_NULL_VOID(frameNode);
357 RichEditorModelNG::SetRequestKeyboardOnFocus(frameNode, true);
358 }
359
SetRichEditorEnablePreviewText(ArkUINodeHandle node,ArkUI_Bool value)360 void SetRichEditorEnablePreviewText(ArkUINodeHandle node, ArkUI_Bool value)
361 {
362 auto* frameNode = reinterpret_cast<FrameNode*>(node);
363 CHECK_NULL_VOID(frameNode);
364 RichEditorModelNG::SetSupportPreviewText(frameNode, value);
365 }
366
ResetRichEditorEnablePreviewText(ArkUINodeHandle node)367 void ResetRichEditorEnablePreviewText(ArkUINodeHandle node)
368 {
369 auto* frameNode = reinterpret_cast<FrameNode*>(node);
370 CHECK_NULL_VOID(frameNode);
371 RichEditorModelNG::SetSupportPreviewText(frameNode, true);
372 }
373
374
SetRichEditorEditMenuOptions(ArkUINodeHandle node,void * onCreateMenuCallback,void * onMenuItemClickCallback)375 void SetRichEditorEditMenuOptions(ArkUINodeHandle node, void* onCreateMenuCallback, void* onMenuItemClickCallback)
376 {
377 auto* frameNode = reinterpret_cast<FrameNode*>(node);
378 CHECK_NULL_VOID(frameNode);
379 CHECK_NULL_VOID(onCreateMenuCallback && onMenuItemClickCallback);
380 NG::OnCreateMenuCallback* onCreateMenu = reinterpret_cast<NG::OnCreateMenuCallback*>(onCreateMenuCallback);
381 NG::OnMenuItemClickCallback* onMenuItemClick
382 = reinterpret_cast<NG::OnMenuItemClickCallback*>(onMenuItemClickCallback);
383 RichEditorModelNG::SetSelectionMenuOptions(frameNode, std::move(*onCreateMenu), std::move(*onMenuItemClick));
384 }
385
ResetRichEditorEditMenuOptions(ArkUINodeHandle node)386 void ResetRichEditorEditMenuOptions(ArkUINodeHandle node)
387 {
388 auto* frameNode = reinterpret_cast<FrameNode*>(node);
389 CHECK_NULL_VOID(frameNode);
390 NG::OnCreateMenuCallback onCreateMenuCallback;
391 NG::OnMenuItemClickCallback onMenuItemClick;
392 RichEditorModelNG::SetSelectionMenuOptions(frameNode, std::move(onCreateMenuCallback), std::move(onMenuItemClick));
393 }
394
SetRichEditorOnWillChange(ArkUINodeHandle node,void * callback)395 void SetRichEditorOnWillChange(ArkUINodeHandle node, void* callback)
396 {
397 auto* frameNode = reinterpret_cast<FrameNode*>(node);
398 CHECK_NULL_VOID(frameNode);
399 auto onWillChange = reinterpret_cast<std::function<bool(const RichEditorChangeValue&)>*>(callback);
400 RichEditorModelNG::SetOnWillChange(frameNode, std::move(*onWillChange));
401 }
402
ResetRichEditorOnWillChange(ArkUINodeHandle node)403 void ResetRichEditorOnWillChange(ArkUINodeHandle node)
404 {
405 auto* frameNode = reinterpret_cast<FrameNode*>(node);
406 CHECK_NULL_VOID(frameNode);
407 RichEditorModelNG::SetOnWillChange(frameNode, nullptr);
408 }
409
SetRichEditorOnDidChange(ArkUINodeHandle node,void * callback)410 void SetRichEditorOnDidChange(ArkUINodeHandle node, void* callback)
411 {
412 auto* frameNode = reinterpret_cast<FrameNode*>(node);
413 CHECK_NULL_VOID(frameNode);
414 auto onDidChange = reinterpret_cast<std::function<bool(const RichEditorChangeValue&)>*>(callback);
415 RichEditorModelNG::SetOnDidChange(frameNode, std::move(*onDidChange));
416 }
417
ResetRichEditorOnDidChange(ArkUINodeHandle node)418 void ResetRichEditorOnDidChange(ArkUINodeHandle node)
419 {
420 auto* frameNode = reinterpret_cast<FrameNode*>(node);
421 CHECK_NULL_VOID(frameNode);
422 RichEditorModelNG::SetOnDidChange(frameNode, nullptr);
423 }
424
SetRichEditorPlaceholderValue(const ArkUI_Float64 * valueArray,ArkUI_Uint32 index,const ArkUI_Uint32 arraySize,ArkUI_Float64 & result)425 bool SetRichEditorPlaceholderValue(
426 const ArkUI_Float64* valueArray, ArkUI_Uint32 index, const ArkUI_Uint32 arraySize, ArkUI_Float64& result)
427 {
428 CHECK_NULL_RETURN(valueArray, false);
429 ArkUI_Uint32 step = NORMAL_VALUE_ARRAY_STEP;
430 auto hasValueIndex = index * step;
431 auto valueIndex = hasValueIndex + 1;
432 if (valueIndex >= arraySize) {
433 return false;
434 }
435 if (static_cast<bool>(valueArray[hasValueIndex])) {
436 result = valueArray[valueIndex];
437 return true;
438 }
439 return false;
440 }
441
SetRichEditorPlaceholderDimension(const ArkUI_Float64 * dimensionArray,ArkUI_Uint32 offset,const ArkUI_Uint32 arraySize,std::optional<Dimension> & optDimension)442 bool SetRichEditorPlaceholderDimension(const ArkUI_Float64* dimensionArray, ArkUI_Uint32 offset,
443 const ArkUI_Uint32 arraySize, std::optional<Dimension>& optDimension)
444 {
445 CHECK_NULL_RETURN(dimensionArray, false);
446 auto hasValue = dimensionArray[offset];
447 if (!static_cast<bool>(hasValue)) {
448 return false;
449 }
450 auto valueIndex = offset + 1;
451 auto unitIndex = offset + 2;
452 if (unitIndex >= arraySize) {
453 return false;
454 }
455 auto value = dimensionArray[valueIndex];
456 auto unit = dimensionArray[unitIndex];
457 DimensionUnit unitValue = static_cast<DimensionUnit>(unit);
458 optDimension = Dimension(value, unitValue);
459 return true;
460 }
461
SetRichEditorPlaceholder(ArkUINodeHandle node,ArkUI_CharPtr * stringParameters,const ArkUI_Uint32 stringParametersCount,const ArkUI_Float64 * valuesArray,const ArkUI_Uint32 valuesCount)462 void SetRichEditorPlaceholder(ArkUINodeHandle node, ArkUI_CharPtr* stringParameters,
463 const ArkUI_Uint32 stringParametersCount, const ArkUI_Float64* valuesArray, const ArkUI_Uint32 valuesCount)
464 {
465 auto* frameNode = reinterpret_cast<FrameNode*>(node);
466 CHECK_NULL_VOID(frameNode);
467 double result = 0;
468 PlaceholderOptions options;
469 CHECK_NULL_VOID(stringParameters);
470 if (0 < stringParametersCount && stringParameters[0] != nullptr) { // 0: value
471 std::string value = stringParameters[0];
472 options.value = UtfUtils::Str8ToStr16(value);
473 }
474 for (ArkUI_Uint32 index = 1; index < stringParametersCount; index++) { // 1: value
475 options.fontFamilies.push_back(stringParameters[index]);
476 }
477 if (SetRichEditorPlaceholderValue(valuesArray, 0, valuesCount, result) && // 0: fontWeight
478 GreatOrEqual(result, 0.0) && result <= static_cast<double>(OHOS::Ace::FontWeight::REGULAR)) {
479 options.fontWeight = static_cast<OHOS::Ace::FontWeight>(result);
480 }
481 if (SetRichEditorPlaceholderValue(valuesArray, 1, valuesCount, result) && // 1: fontStyle
482 GreatOrEqual(result, 0.0) && result < static_cast<double>(OHOS::Ace::FontStyle::NONE)) {
483 options.fontStyle = static_cast<OHOS::Ace::FontStyle>(result);
484 }
485 if (SetRichEditorPlaceholderValue(valuesArray, 2, valuesCount, result)) { // 2: fontColor
486 double colorResourceId = -1.0;
487 Color fontColor;
488 if (SetRichEditorPlaceholderValue(valuesArray, 3, valuesCount, colorResourceId) && // 3: colorResourceId
489 GreatOrEqual(colorResourceId, 0.0)) {
490 fontColor.SetValue(static_cast<ArkUI_Uint32>(result));
491 options.fontColor = fontColor;
492 } else {
493 fontColor.SetValue(static_cast<ArkUI_Uint32>(result));
494 options.fontColor = fontColor;
495 }
496 }
497 std::optional<Dimension> fontSizeOptional = std::nullopt;
498 ArkUI_Uint32 step = NORMAL_VALUE_ARRAY_STEP;
499 ArkUI_Uint32 offset = 4 * step; // 4: fontSize
500 if (SetRichEditorPlaceholderDimension(valuesArray, offset, valuesCount, fontSizeOptional)) {
501 options.fontSize = fontSizeOptional;
502 }
503
504 RichEditorModelNG::SetPlaceholder(frameNode, options);
505 }
506
ResetRichEditorPlaceholder(ArkUINodeHandle node)507 void ResetRichEditorPlaceholder(ArkUINodeHandle node)
508 {
509 auto* frameNode = reinterpret_cast<FrameNode*>(node);
510 CHECK_NULL_VOID(frameNode);
511 PlaceholderOptions options;
512 auto pipeline = frameNode->GetContextWithCheck();
513 CHECK_NULL_VOID(pipeline);
514 auto textTheme = pipeline->GetTheme<TextTheme>();
515 CHECK_NULL_VOID(textTheme);
516 TextStyle textStyle = textTheme ? textTheme->GetTextStyle() : TextStyle();
517 options.value = u"";
518 options.fontSize = textStyle.GetFontSize();
519 options.fontFamilies = textStyle.GetFontFamilies();
520 options.fontWeight = textStyle.GetFontWeight();
521 options.fontStyle = textStyle.GetFontStyle();
522 Color fontColor;
523 auto richEditorTheme = pipeline->GetTheme<NG::RichEditorTheme>();
524 options.fontColor = richEditorTheme ? richEditorTheme->GetPlaceholderColor() : fontColor;
525 RichEditorModelNG::SetPlaceholder(frameNode, options);
526 }
527
SetRichEditorAboutToDelete(ArkUINodeHandle node,void * callback)528 void SetRichEditorAboutToDelete(ArkUINodeHandle node, void* callback)
529 {
530 auto* frameNode = reinterpret_cast<FrameNode*>(node);
531 CHECK_NULL_VOID(frameNode);
532 auto aboutToDelete = reinterpret_cast<std::function<bool(const RichEditorDeleteValue&)>*>(callback);
533 RichEditorModelNG::SetAboutToDelete(frameNode, std::move(*aboutToDelete));
534 }
535
ResetRichEditorAboutToDelete(ArkUINodeHandle node)536 void ResetRichEditorAboutToDelete(ArkUINodeHandle node)
537 {
538 auto* frameNode = reinterpret_cast<FrameNode*>(node);
539 CHECK_NULL_VOID(frameNode);
540 RichEditorModelNG::SetAboutToDelete(frameNode, nullptr);
541 }
542
SetRichEditorBarState(ArkUINodeHandle node,ArkUI_Uint32 barStateValue)543 void SetRichEditorBarState(ArkUINodeHandle node, ArkUI_Uint32 barStateValue)
544 {
545 auto *frameNode = reinterpret_cast<FrameNode*>(node);
546 CHECK_NULL_VOID(frameNode);
547 DisplayMode displayMode = static_cast<DisplayMode>(barStateValue);
548 RichEditorModelNG::SetBarState(frameNode, displayMode);
549 }
550
ResetRichEditorBarState(ArkUINodeHandle node)551 void ResetRichEditorBarState(ArkUINodeHandle node)
552 {
553 auto *frameNode = reinterpret_cast<FrameNode *>(node);
554 CHECK_NULL_VOID(frameNode);
555 RichEditorModelNG::SetBarState(frameNode, DEFAULT_BAR_STATE_VALUE);
556 }
557
SetRichEditorMaxLength(ArkUINodeHandle node,ArkUI_Uint32 value)558 void SetRichEditorMaxLength(ArkUINodeHandle node, ArkUI_Uint32 value)
559 {
560 auto *frameNode = reinterpret_cast<FrameNode*>(node);
561 CHECK_NULL_VOID(frameNode);
562 RichEditorModelNG::SetMaxLength(frameNode, value);
563 }
564
ResetRichEditorMaxLength(ArkUINodeHandle node)565 void ResetRichEditorMaxLength(ArkUINodeHandle node)
566 {
567 auto *frameNode = reinterpret_cast<FrameNode *>(node);
568 CHECK_NULL_VOID(frameNode);
569 RichEditorModelNG::SetMaxLength(frameNode, INT_MAX);
570 }
571
SetRichEditorMaxLines(ArkUINodeHandle node,ArkUI_Uint32 maxLine)572 void SetRichEditorMaxLines(ArkUINodeHandle node, ArkUI_Uint32 maxLine)
573 {
574 auto *frameNode = reinterpret_cast<FrameNode *>(node);
575 CHECK_NULL_VOID(frameNode);
576 if (maxLine <= 0) {
577 RichEditorModelNG::SetMaxLines(frameNode, INT_MAX);
578 return;
579 }
580 RichEditorModelNG::SetMaxLines(frameNode, maxLine);
581 }
582
ResetRichEditorMaxLines(ArkUINodeHandle node)583 void ResetRichEditorMaxLines(ArkUINodeHandle node)
584 {
585 auto *frameNode = reinterpret_cast<FrameNode *>(node);
586 CHECK_NULL_VOID(frameNode);
587 RichEditorModelNG::SetMaxLines(frameNode, INT_MAX);
588 }
589
SetRichEditorStopBackPress(ArkUINodeHandle node,ArkUI_Uint32 value)590 void SetRichEditorStopBackPress(ArkUINodeHandle node, ArkUI_Uint32 value)
591 {
592 auto* frameNode = reinterpret_cast<FrameNode*>(node);
593 CHECK_NULL_VOID(frameNode);
594 RichEditorModelNG::SetStopBackPress(frameNode, static_cast<bool>(value));
595 }
596
ResetRichEditorStopBackPress(ArkUINodeHandle node)597 void ResetRichEditorStopBackPress(ArkUINodeHandle node)
598 {
599 auto* frameNode = reinterpret_cast<FrameNode*>(node);
600 CHECK_NULL_VOID(frameNode);
601 RichEditorModelNG::SetStopBackPress(frameNode, true);
602 }
603
SetRichEditorKeyboardAppearance(ArkUINodeHandle node,ArkUI_Uint32 keyboardAppearance)604 void SetRichEditorKeyboardAppearance(ArkUINodeHandle node, ArkUI_Uint32 keyboardAppearance)
605 {
606 auto* frameNode = reinterpret_cast<FrameNode*>(node);
607 CHECK_NULL_VOID(frameNode);
608 auto value = static_cast<KeyboardAppearance>(keyboardAppearance);
609 if (value < KeyboardAppearance::NONE_IMMERSIVE || value > KeyboardAppearance::DARK_IMMERSIVE) {
610 RichEditorModelNG::SetKeyboardAppearance(frameNode, KeyboardAppearance::NONE_IMMERSIVE);
611 return;
612 }
613 RichEditorModelNG::SetKeyboardAppearance(frameNode, value);
614 }
615
ResetRichEditorKeyboardAppearance(ArkUINodeHandle node)616 void ResetRichEditorKeyboardAppearance(ArkUINodeHandle node)
617 {
618 auto* frameNode = reinterpret_cast<FrameNode*>(node);
619 CHECK_NULL_VOID(frameNode);
620 auto value = KeyboardAppearance::NONE_IMMERSIVE;
621 RichEditorModelNG::SetKeyboardAppearance(frameNode, value);
622 }
623
624 namespace NodeModifier {
GetRichEditorModifier()625 const ArkUIRichEditorModifier* GetRichEditorModifier()
626 {
627 CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
628 static const ArkUIRichEditorModifier modifier = {
629 .setRichEditorEnableDataDetector = SetRichEditorDetectEnable,
630 .resetRichEditorEnableDataDetector = ResetRichEditorDetectEnable,
631 .setRichEditorDataDetectorConfigWithEvent = SetRichEditorDataDetectorConfigWithEvent,
632 .resetRichEditorDataDetectorConfigWithEvent = ResetRichEditorDataDetectorConfigWithEvent,
633 .setRichEditorOnIMEInputComplete = SetRichEditorOnIMEInputComplete,
634 .resetRichEditorOnIMEInputComplete = ResetRichEditorOnIMEInputComplete,
635 .setRichEditorCopyOptions = SetRichEditorCopyOptions,
636 .resetRichEditorCopyOptions = ResetRichEditorCopyOptions,
637 .setRichEditorOnSelectionChange = SetRichEditorOnSelectionChange,
638 .resetRichEditorOnSelectionChange = ResetRichEditorOnSelectionChange,
639 .setRichEditorCaretColor = SetRichEditorCaretColor,
640 .resetRichEditorCaretColor = ResetRichEditorCaretColor,
641 .setRichEditorOnSelect = SetRichEditorOnSelect,
642 .resetRichEditorOnSelect = ResetRichEditorOnSelect,
643 .setRichEditorOnSubmit = SetRichEditorOnSubmit,
644 .resetRichEditorOnSubmit = ResetRichEditorOnSubmit,
645 .setRichEditorAboutToIMEInput = SetRichEditorAboutToIMEInput,
646 .resetRichEditorAboutToIMEInput = ResetRichEditorAboutToIMEInput,
647 .setOnReady = SetRichEditorOnReady,
648 .resetOnReady = ResetRichEditorOnReady,
649 .setOnDeleteComplete = SetRichEditorOnDeleteComplete,
650 .resetOnDeleteComplete = ResetRichEditorOnDeleteComplete,
651 .setOnEditingChange = SetRichEditorOnEditingChange,
652 .resetOnEditingChange = ResetRichEditorOnEditingChange,
653 .setRichEditorSelectedBackgroundColor = SetRichEditorSelectedBackgroundColor,
654 .resetRichEditorSelectedBackgroundColor = ResetRichEditorSelectedBackgroundColor,
655 .setRichEditorOnPaste = SetRichEditorOnPaste,
656 .resetRichEditorOnPaste = ResetRichEditorOnPaste,
657 .setRichEditorOnCut = SetRichEditorOnCut,
658 .resetRichEditorOnCut = ResetRichEditorOnCut,
659 .setRichEditorOnCopy = SetRichEditorOnCopy,
660 .resetRichEditorOnCopy = ResetRichEditorOnCopy,
661 .setRichEditorEnterKeyType = SetRichEditorEnterKeyType,
662 .resetRichEditorEnterKeyType = ResetRichEditorEnterKeyType,
663 .setRichEditorEnableKeyboardOnFocus = SetRichEditorEnableKeyboardOnFocus,
664 .resetRichEditorEnableKeyboardOnFocus = ResetRichEditorEnableKeyboardOnFocus,
665 .setRichEditorEnablePreviewText = SetRichEditorEnablePreviewText,
666 .resetRichEditorEnablePreviewText = ResetRichEditorEnablePreviewText,
667 .setRichEditorEditMenuOptions = SetRichEditorEditMenuOptions,
668 .resetRichEditorEditMenuOptions = ResetRichEditorEditMenuOptions,
669 .setRichEditorOnWillChange = SetRichEditorOnWillChange,
670 .resetRichEditorOnWillChange = ResetRichEditorOnWillChange,
671 .setRichEditorOnDidChange = SetRichEditorOnDidChange,
672 .resetRichEditorOnDidChange = ResetRichEditorOnDidChange,
673 .setRichEditorPlaceholder = SetRichEditorPlaceholder,
674 .resetRichEditorPlaceholder = ResetRichEditorPlaceholder,
675 .setRichEditorAboutToDelete = SetRichEditorAboutToDelete,
676 .resetRichEditorAboutToDelete = ResetRichEditorAboutToDelete,
677 .setRichEditorBarState = SetRichEditorBarState,
678 .resetRichEditorBarState = ResetRichEditorBarState,
679 .setRichEditorMaxLength = SetRichEditorMaxLength,
680 .resetRichEditorMaxLength = ResetRichEditorMaxLength,
681 .setRichEditorMaxLines = SetRichEditorMaxLines,
682 .resetRichEditorMaxLines = ResetRichEditorMaxLines,
683 .setRichEditorStopBackPress = SetRichEditorStopBackPress,
684 .resetRichEditorStopBackPress = ResetRichEditorStopBackPress,
685 .setRichEditorKeyboardAppearance = SetRichEditorKeyboardAppearance,
686 .resetRichEditorKeyboardAppearance = ResetRichEditorKeyboardAppearance,
687 };
688 CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
689 return &modifier;
690 }
691
GetCJUIRichEditorModifier()692 const CJUIRichEditorModifier* GetCJUIRichEditorModifier()
693 {
694 CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
695 static const CJUIRichEditorModifier modifier = {
696 .setRichEditorEnableDataDetector = SetRichEditorDetectEnable,
697 .resetRichEditorEnableDataDetector = ResetRichEditorDetectEnable,
698 .setRichEditorCopyOptions = SetRichEditorCopyOptions,
699 .resetRichEditorCopyOptions = ResetRichEditorCopyOptions,
700 .setRichEditorCaretColor = SetRichEditorCaretColor,
701 .resetRichEditorCaretColor = ResetRichEditorCaretColor,
702 .setOnReady = SetRichEditorOnReady,
703 .resetOnReady = ResetRichEditorOnReady,
704 .setOnDeleteComplete = SetRichEditorOnDeleteComplete,
705 .resetOnDeleteComplete = ResetRichEditorOnDeleteComplete,
706 .setOnEditingChange = SetRichEditorOnEditingChange,
707 .resetOnEditingChange = ResetRichEditorOnEditingChange,
708 .setRichEditorSelectedBackgroundColor = SetRichEditorSelectedBackgroundColor,
709 .resetRichEditorSelectedBackgroundColor = ResetRichEditorSelectedBackgroundColor,
710 .setRichEditorEnterKeyType = SetRichEditorEnterKeyType,
711 .resetRichEditorEnterKeyType = ResetRichEditorEnterKeyType,
712 .setRichEditorBarState = SetRichEditorBarState,
713 .resetRichEditorBarState = ResetRichEditorBarState,
714 };
715 CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
716 return &modifier;
717 }
718 }
719 }
720