1 /*
2 * Copyright (c) 2021-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 #include "frameworks/bridge/declarative_frontend/jsview/js_image.h"
17
18 #if !defined(PREVIEW)
19 #include <dlfcn.h>
20 #endif
21
22 #include "base/image/pixel_map.h"
23 #include "base/log/ace_scoring_log.h"
24 #include "base/log/ace_trace.h"
25 #include "bridge/declarative_frontend/engine/functions/js_drag_function.h"
26 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
27 #include "bridge/declarative_frontend/jsview/models/image_model_impl.h"
28 #include "core/common/container.h"
29 #include "core/components/image/image_event.h"
30 #include "core/components_ng/event/gesture_event_hub.h"
31 #include "core/components_ng/pattern/image/image_model.h"
32 #include "core/components_ng/pattern/image/image_model_ng.h"
33 #include "core/image/image_source_info.h"
34
35 namespace OHOS::Ace {
36
37 std::unique_ptr<ImageModel> ImageModel::instance_ = nullptr;
38
GetInstance()39 ImageModel* ImageModel::GetInstance()
40 {
41 if (!instance_) {
42 #ifdef NG_BUILD
43 instance_.reset(new NG::ImageModelNG());
44 #else
45 if (Container::IsCurrentUseNewPipeline()) {
46 instance_.reset(new NG::ImageModelNG());
47 } else {
48 instance_.reset(new Framework::ImageModelImpl());
49 }
50 #endif
51 }
52 return instance_.get();
53 }
54
55 } // namespace OHOS::Ace
56
57 namespace OHOS::Ace::Framework {
58
LoadImageSuccEventToJSValue(const LoadImageSuccessEvent & eventInfo)59 JSRef<JSVal> LoadImageSuccEventToJSValue(const LoadImageSuccessEvent& eventInfo)
60 {
61 JSRef<JSObject> obj = JSRef<JSObject>::New();
62 obj->SetProperty("width", eventInfo.GetWidth());
63 obj->SetProperty("height", eventInfo.GetHeight());
64 obj->SetProperty("componentWidth", eventInfo.GetComponentWidth());
65 obj->SetProperty("componentHeight", eventInfo.GetComponentHeight());
66 obj->SetProperty("loadingStatus", eventInfo.GetLoadingStatus());
67 return JSRef<JSVal>::Cast(obj);
68 }
69
LoadImageFailEventToJSValue(const LoadImageFailEvent & eventInfo)70 JSRef<JSVal> LoadImageFailEventToJSValue(const LoadImageFailEvent& eventInfo)
71 {
72 JSRef<JSObject> obj = JSRef<JSObject>::New();
73 obj->SetProperty("componentWidth", eventInfo.GetComponentWidth());
74 obj->SetProperty("componentHeight", eventInfo.GetComponentHeight());
75 obj->SetProperty("message", eventInfo.GetErrorMessage());
76 return JSRef<JSVal>::Cast(obj);
77 }
78
SetAlt(const JSCallbackInfo & args)79 void JSImage::SetAlt(const JSCallbackInfo& args)
80 {
81 if (args.Length() < 1) {
82 LOGE("The argv is wrong, it it supposed to have at least 1 argument");
83 return;
84 }
85
86 std::string src;
87 if (!ParseJsMedia(args[0], src)) {
88 return;
89 }
90 ImageModel::GetInstance()->SetAlt(src);
91 }
92
SetObjectFit(int32_t value)93 void JSImage::SetObjectFit(int32_t value)
94 {
95 auto fit = static_cast<ImageFit>(value);
96 if (fit < ImageFit::FILL || fit > ImageFit::SCALE_DOWN) {
97 LOGW("The value of objectFit is out of range %{public}d", value);
98 fit = ImageFit::COVER;
99 }
100 ImageModel::GetInstance()->SetImageFit(fit);
101 }
102
SetMatchTextDirection(bool value)103 void JSImage::SetMatchTextDirection(bool value)
104 {
105 ImageModel::GetInstance()->SetMatchTextDirection(value);
106 }
107
SetFitOriginalSize(bool value)108 void JSImage::SetFitOriginalSize(bool value)
109 {
110 ImageModel::GetInstance()->SetFitOriginSize(value);
111 }
112
SetBorder(const Border & border)113 void JSImage::SetBorder(const Border& border)
114 {
115 ImageModel::GetInstance()->SetBorder(border);
116 }
117
OnComplete(const JSCallbackInfo & args)118 void JSImage::OnComplete(const JSCallbackInfo& args)
119 {
120 LOGD("JSImage V8OnComplete");
121 if (args[0]->IsFunction()) {
122 auto jsLoadSuccFunc = AceType::MakeRefPtr<JsEventFunction<LoadImageSuccessEvent, 1>>(
123 JSRef<JSFunc>::Cast(args[0]), LoadImageSuccEventToJSValue);
124
125 auto onComplete = [execCtx = args.GetExecutionContext(), func = std::move(jsLoadSuccFunc)](
126 const LoadImageSuccessEvent& info) {
127 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
128 ACE_SCORING_EVENT("Image.onComplete");
129 func->Execute(info);
130 };
131 ImageModel::GetInstance()->SetOnComplete(std::move(onComplete));
132 } else {
133 LOGE("args not function");
134 }
135 }
136
OnError(const JSCallbackInfo & args)137 void JSImage::OnError(const JSCallbackInfo& args)
138 {
139 LOGD("JSImage V8OnError");
140 if (args[0]->IsFunction()) {
141 auto jsLoadFailFunc = AceType::MakeRefPtr<JsEventFunction<LoadImageFailEvent, 1>>(
142 JSRef<JSFunc>::Cast(args[0]), LoadImageFailEventToJSValue);
143 auto onError = [execCtx = args.GetExecutionContext(), func = std::move(jsLoadFailFunc)](
144 const LoadImageFailEvent& info) {
145 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
146 ACE_SCORING_EVENT("Image.onError");
147 func->Execute(info);
148 };
149
150 ImageModel::GetInstance()->SetOnError(onError);
151 } else {
152 LOGE("args not function");
153 }
154 }
155
OnFinish(const JSCallbackInfo & info)156 void JSImage::OnFinish(const JSCallbackInfo& info)
157 {
158 LOGD("JSImage V8OnFinish");
159 if (!info[0]->IsFunction()) {
160 LOGE("info[0] is not a function.");
161 return;
162 }
163 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
164 auto onFinish = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)]() {
165 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
166 ACE_SCORING_EVENT("Image.onFinish");
167 func->Execute();
168 };
169 ImageModel::GetInstance()->SetSvgAnimatorFinishEvent(onFinish);
170 }
171
Create(const JSCallbackInfo & info)172 void JSImage::Create(const JSCallbackInfo& info)
173 {
174 if (info.Length() < 1) {
175 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
176 return;
177 }
178 auto context = PipelineBase::GetCurrentContext();
179 CHECK_NULL_VOID(context);
180
181 std::string src;
182 bool noPixMap = true;
183 RefPtr<PixelMap> pixMap = nullptr;
184 if (info[0]->IsString()) {
185 src = info[0]->ToString();
186 if (context->IsFormRender()) {
187 SrcType srcType = ImageSourceInfo::ResolveURIType(src);
188 bool notSupport = (
189 srcType == SrcType::NETWORK || srcType == SrcType::FILE || srcType == SrcType::DATA_ABILITY);
190 if (notSupport) {
191 LOGE("Not supported src : %{public}s when form render", src.c_str());
192 src.clear();
193 }
194 }
195 } else {
196 noPixMap = ParseJsMedia(info[0], src);
197 #if defined(PIXEL_MAP_SUPPORTED)
198 if (!noPixMap) {
199 if (context->IsFormRender()) {
200 LOGE("Not supported pixMap when form render");
201 } else {
202 pixMap = CreatePixelMapFromNapiValue(info[0]);
203 }
204 }
205 #endif
206 }
207
208 ImageModel::GetInstance()->Create(src, noPixMap, pixMap);
209 }
210
JsBorder(const JSCallbackInfo & info)211 void JSImage::JsBorder(const JSCallbackInfo& info)
212 {
213 JSViewAbstract::JsBorder(info);
214 ImageModel::GetInstance()->SetBackBorder();
215 }
216
JsBorderRadius(const JSCallbackInfo & info)217 void JSImage::JsBorderRadius(const JSCallbackInfo& info)
218 {
219 JSViewAbstract::JsBorderRadius(info);
220 ImageModel::GetInstance()->SetBackBorder();
221 }
222
SetSourceSize(const JSCallbackInfo & info)223 void JSImage::SetSourceSize(const JSCallbackInfo& info)
224 {
225 ImageModel::GetInstance()->SetImageSourceSize(JSViewAbstract::ParseSize(info));
226 }
227
SetImageFill(const JSCallbackInfo & info)228 void JSImage::SetImageFill(const JSCallbackInfo& info)
229 {
230 if (info.Length() < 1) {
231 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
232 return;
233 }
234
235 Color color;
236 if (!ParseJsColor(info[0], color)) {
237 return;
238 }
239 ImageModel::GetInstance()->SetImageFill(color);
240 }
241
SetImageRenderMode(int32_t imageRenderMode)242 void JSImage::SetImageRenderMode(int32_t imageRenderMode)
243 {
244 auto renderMode = static_cast<ImageRenderMode>(imageRenderMode);
245 if (renderMode < ImageRenderMode::ORIGINAL || renderMode > ImageRenderMode::TEMPLATE) {
246 LOGW("invalid imageRenderMode value %{public}d", imageRenderMode);
247 renderMode = ImageRenderMode::ORIGINAL;
248 }
249 ImageModel::GetInstance()->SetImageRenderMode(renderMode);
250 }
251
SetImageInterpolation(int32_t imageInterpolation)252 void JSImage::SetImageInterpolation(int32_t imageInterpolation)
253 {
254 auto interpolation = static_cast<ImageInterpolation>(imageInterpolation);
255 if (interpolation < ImageInterpolation::NONE || interpolation > ImageInterpolation::HIGH) {
256 LOGW("invalid imageInterpolation value %{public}d", imageInterpolation);
257 interpolation = ImageInterpolation::NONE;
258 }
259 ImageModel::GetInstance()->SetImageInterpolation(interpolation);
260 }
261
SetImageRepeat(int32_t imageRepeat)262 void JSImage::SetImageRepeat(int32_t imageRepeat)
263 {
264 auto repeat = static_cast<ImageRepeat>(imageRepeat);
265 if (repeat < ImageRepeat::NO_REPEAT || repeat > ImageRepeat::REPEAT) {
266 LOGW("invalid imageRepeat value %{public}d", imageRepeat);
267 repeat = ImageRepeat::NO_REPEAT;
268 }
269 ImageModel::GetInstance()->SetImageRepeat(repeat);
270 }
271
JsTransition(const JSCallbackInfo & info)272 void JSImage::JsTransition(const JSCallbackInfo& info)
273 {
274 if (ImageModel::GetInstance()->IsSrcSvgImage()) {
275 JSViewAbstract::JsTransition(info);
276 } else {
277 JSViewAbstract::JsTransitionPassThrough(info);
278 }
279 }
280
JsOpacity(const JSCallbackInfo & info)281 void JSImage::JsOpacity(const JSCallbackInfo& info)
282 {
283 if (ImageModel::GetInstance()->IsSrcSvgImage()) {
284 JSViewAbstract::JsOpacity(info);
285 } else {
286 JSViewAbstract::JsOpacityPassThrough(info);
287 }
288 }
289
JsBlur(const JSCallbackInfo & info)290 void JSImage::JsBlur(const JSCallbackInfo& info)
291 {
292 // only flutter runs special image blur
293 #ifdef ENABLE_ROSEN_BACKEND
294 JSViewAbstract::JsBlur(info);
295 #else
296 if (info.Length() < 1) {
297 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
298 return;
299 }
300 double blur = 0.0;
301 if (ParseJsDouble(info[0], blur)) {
302 ImageModel::GetInstance()->SetBlur(blur);
303 }
304 #endif
305 }
306
SetAutoResize(bool autoResize)307 void JSImage::SetAutoResize(bool autoResize)
308 {
309 ImageModel::GetInstance()->SetAutoResize(autoResize);
310 }
311
SetSyncLoad(const JSCallbackInfo & info)312 void JSImage::SetSyncLoad(const JSCallbackInfo& info)
313 {
314 ImageModel::GetInstance()->SetSyncMode(info[0]->ToBoolean());
315 }
316
ConstructorCallback(const JSCallbackInfo & args)317 void JSColorFilter::ConstructorCallback(const JSCallbackInfo& args)
318 {
319 if (args.Length() < 1) {
320 LOGE("The argv is wrong, it it supposed to have at least 1 argument");
321 return;
322 }
323 if (!args[0]->IsArray()) {
324 LOGE("jscallback is not object or array");
325 return;
326 }
327 JSRef<JSArray> array = JSRef<JSArray>::Cast(args[0]);
328 if (array->Length() != COLOR_FILTER_MATRIX_SIZE) {
329 LOGE("arg length illegal");
330 return;
331 }
332 auto jscolorfilter = Referenced::MakeRefPtr<JSColorFilter>();
333 if (jscolorfilter == nullptr) {
334 LOGE("make jscolorfilter object failed");
335 return;
336 }
337 std::vector<float> colorfilter;
338 for (size_t i = 0; i < array->Length(); i++) {
339 JSRef<JSVal> value = array->GetValueAt(i);
340 if (value->IsNumber()) {
341 colorfilter.emplace_back(value->ToNumber<float>());
342 }
343 }
344 if (colorfilter.size() != COLOR_FILTER_MATRIX_SIZE) {
345 LOGE("colorfilter length illegal");
346 return;
347 }
348 jscolorfilter->SetColorFilterMatrix(std::move(colorfilter));
349 jscolorfilter->IncRefCount();
350 args.SetReturnValue(Referenced::RawPtr(jscolorfilter));
351 }
352
DestructorCallback(JSColorFilter * obj)353 void JSColorFilter::DestructorCallback(JSColorFilter* obj)
354 {
355 if (obj != nullptr) {
356 obj->DecRefCount();
357 }
358 }
359
SetColorFilter(const JSCallbackInfo & info)360 void JSImage::SetColorFilter(const JSCallbackInfo& info)
361 {
362 if (info.Length() != 1 || !info[0]->IsObject()) {
363 LOGE("The arg is wrong, it is supposed to have 1 arguments");
364 return;
365 }
366 JSRef<JSArray> array = JSRef<JSArray>::Cast(info[0]);
367 if (array->Length() != COLOR_FILTER_MATRIX_SIZE) {
368 LOGE("arg length illegal");
369 return;
370 }
371 std::vector<float> colorfilter;
372 for (size_t i = 0; i < array->Length(); i++) {
373 JSRef<JSVal> value = array->GetValueAt(i);
374 if (value->IsNumber()) {
375 colorfilter.emplace_back(value->ToNumber<float>());
376 }
377 }
378 if (colorfilter.size() != COLOR_FILTER_MATRIX_SIZE) {
379 LOGE("colorfilter length illegal");
380 return;
381 }
382 ImageModel::GetInstance()->SetColorFilterMatrix(colorfilter);
383 }
384
JSBind(BindingTarget globalObj)385 void JSImage::JSBind(BindingTarget globalObj)
386 {
387 JSClass<JSImage>::Declare("Image");
388 MethodOptions opt = MethodOptions::NONE;
389 JSClass<JSImage>::StaticMethod("create", &JSImage::Create, opt);
390 JSClass<JSImage>::StaticMethod("alt", &JSImage::SetAlt, opt);
391 JSClass<JSImage>::StaticMethod("objectFit", &JSImage::SetObjectFit, opt);
392 JSClass<JSImage>::StaticMethod("matchTextDirection", &JSImage::SetMatchTextDirection, opt);
393 JSClass<JSImage>::StaticMethod("fitOriginalSize", &JSImage::SetFitOriginalSize, opt);
394 JSClass<JSImage>::StaticMethod("sourceSize", &JSImage::SetSourceSize, opt);
395 JSClass<JSImage>::StaticMethod("fillColor", &JSImage::SetImageFill, opt);
396 JSClass<JSImage>::StaticMethod("renderMode", &JSImage::SetImageRenderMode, opt);
397 JSClass<JSImage>::StaticMethod("objectRepeat", &JSImage::SetImageRepeat, opt);
398 JSClass<JSImage>::StaticMethod("interpolation", &JSImage::SetImageInterpolation, opt);
399 JSClass<JSImage>::StaticMethod("colorFilter", &JSImage::SetColorFilter, opt);
400
401 JSClass<JSImage>::StaticMethod("border", &JSImage::JsBorder);
402 JSClass<JSImage>::StaticMethod("borderRadius", &JSImage::JsBorderRadius);
403 JSClass<JSImage>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
404 JSClass<JSImage>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
405 JSClass<JSImage>::StaticMethod("autoResize", &JSImage::SetAutoResize);
406
407 JSClass<JSImage>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
408 JSClass<JSImage>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
409 JSClass<JSImage>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
410 JSClass<JSImage>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
411 JSClass<JSImage>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
412 JSClass<JSImage>::StaticMethod("onComplete", &JSImage::OnComplete);
413 JSClass<JSImage>::StaticMethod("onError", &JSImage::OnError);
414 JSClass<JSImage>::StaticMethod("onFinish", &JSImage::OnFinish);
415 JSClass<JSImage>::StaticMethod("syncLoad", &JSImage::SetSyncLoad);
416 JSClass<JSImage>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
417 JSClass<JSImage>::StaticMethod("draggable", &JSImage::JsSetDraggable);
418 JSClass<JSImage>::StaticMethod("onDragStart", &JSImage::JsOnDragStart);
419 JSClass<JSImage>::StaticMethod("onDragEnter", &JSImage::JsOnDragEnter);
420 JSClass<JSImage>::StaticMethod("onDragMove", &JSImage::JsOnDragMove);
421 JSClass<JSImage>::StaticMethod("onDragLeave", &JSImage::JsOnDragLeave);
422 JSClass<JSImage>::StaticMethod("onDrop", &JSImage::JsOnDrop);
423 JSClass<JSImage>::StaticMethod("copyOption", &JSImage::SetCopyOption);
424 // override method
425 JSClass<JSImage>::StaticMethod("opacity", &JSImage::JsOpacity);
426 JSClass<JSImage>::StaticMethod("blur", &JSImage::JsBlur);
427 JSClass<JSImage>::StaticMethod("transition", &JSImage::JsTransition);
428 JSClass<JSImage>::Inherit<JSViewAbstract>();
429 JSClass<JSImage>::Bind<>(globalObj);
430
431 JSClass<JSColorFilter>::Declare("ColorFilter");
432 JSClass<JSColorFilter>::Bind(globalObj, JSColorFilter::ConstructorCallback, JSColorFilter::DestructorCallback);
433 }
434
JsSetDraggable(bool draggable)435 void JSImage::JsSetDraggable(bool draggable)
436 {
437 ImageModel::GetInstance()->SetDraggable(draggable);
438 }
439
JsOnDragStart(const JSCallbackInfo & info)440 void JSImage::JsOnDragStart(const JSCallbackInfo& info)
441 {
442 RefPtr<JsDragFunction> jsOnDragStartFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
443 auto onDragStartId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragStartFunc)](
444 const RefPtr<DragEvent>& info, const std::string& extraParams) -> NG::DragDropBaseInfo {
445 NG::DragDropBaseInfo itemInfo;
446 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, itemInfo);
447
448 auto ret = func->Execute(info, extraParams);
449 if (!ret->IsObject()) {
450 LOGE("builder param is not an object.");
451 return itemInfo;
452 }
453 if (ParseAndUpdateDragItemInfo(ret, itemInfo)) {
454 return itemInfo;
455 }
456
457 auto builderObj = JSRef<JSObject>::Cast(ret);
458 #if defined(PIXEL_MAP_SUPPORTED)
459 auto pixmap = builderObj->GetProperty("pixelMap");
460 itemInfo.pixelMap = CreatePixelMapFromNapiValue(pixmap);
461 #endif
462 auto extraInfo = builderObj->GetProperty("extraInfo");
463 ParseJsString(extraInfo, itemInfo.extraInfo);
464 ParseAndUpdateDragItemInfo(builderObj->GetProperty("builder"), itemInfo);
465 return itemInfo;
466 };
467 ImageModel::GetInstance()->SetOnDragStart(std::move(onDragStartId));
468 }
469
JsOnDragEnter(const JSCallbackInfo & info)470 void JSImage::JsOnDragEnter(const JSCallbackInfo& info)
471 {
472 RefPtr<JsDragFunction> jsOnDragEnterFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
473 auto onDragEnterId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragEnterFunc)](
474 const RefPtr<DragEvent>& info, const std::string& extraParams) {
475 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
476 ACE_SCORING_EVENT("onDragEnter");
477 func->Execute(info, extraParams);
478 };
479 ImageModel::GetInstance()->SetOnDragEnter(std::move(onDragEnterId));
480 }
481
JsOnDragMove(const JSCallbackInfo & info)482 void JSImage::JsOnDragMove(const JSCallbackInfo& info)
483 {
484 RefPtr<JsDragFunction> jsOnDragMoveFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
485 auto onDragMoveId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragMoveFunc)](
486 const RefPtr<DragEvent>& info, const std::string& extraParams) {
487 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
488 ACE_SCORING_EVENT("onDragMove");
489 func->Execute(info, extraParams);
490 };
491 ImageModel::GetInstance()->SetOnDragMove(std::move(onDragMoveId));
492 }
493
JsOnDragLeave(const JSCallbackInfo & info)494 void JSImage::JsOnDragLeave(const JSCallbackInfo& info)
495 {
496 RefPtr<JsDragFunction> jsOnDragLeaveFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
497 auto onDragLeaveId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragLeaveFunc)](
498 const RefPtr<DragEvent>& info, const std::string& extraParams) {
499 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
500 ACE_SCORING_EVENT("onDragLeave");
501 func->Execute(info, extraParams);
502 };
503 ImageModel::GetInstance()->SetOnDragLeave(std::move(onDragLeaveId));
504 }
505
JsOnDrop(const JSCallbackInfo & info)506 void JSImage::JsOnDrop(const JSCallbackInfo& info)
507 {
508 RefPtr<JsDragFunction> jsOnDropFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
509 auto onDropId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDropFunc)](
510 const RefPtr<DragEvent>& info, const std::string& extraParams) {
511 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
512 ACE_SCORING_EVENT("onDrop");
513 func->Execute(info, extraParams);
514 };
515 ImageModel::GetInstance()->SetOnDrop(std::move(onDropId));
516 }
517
SetCopyOption(const JSCallbackInfo & info)518 void JSImage::SetCopyOption(const JSCallbackInfo& info)
519 {
520 if (info.Length() == 0) {
521 return;
522 }
523 auto copyOptions = CopyOptions::None;
524 if (info[0]->IsNumber()) {
525 auto enumNumber = info[0]->ToNumber<int>();
526 copyOptions = static_cast<CopyOptions>(enumNumber);
527 if (copyOptions < CopyOptions::None || copyOptions > CopyOptions::Distributed) {
528 LOGW("copy option is invalid %{public}d", copyOptions);
529 copyOptions = CopyOptions::None;
530 }
531 }
532 LOGI("copy option: %{public}d", copyOptions);
533 ImageModel::GetInstance()->SetCopyOption(copyOptions);
534 }
535
536 } // namespace OHOS::Ace::Framework
537