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 #include "frameworks/bridge/declarative_frontend/jsview/js_container_span.h"
17
18 #include "core/components_ng/pattern/text/span_model.h"
19 #include "core/components_ng/pattern/text/span_model_ng.h"
20
21 namespace OHOS::Ace::Framework {
SetTextBackgroundStyle(const JSCallbackInfo & info)22 void JSContainerSpan::SetTextBackgroundStyle(const JSCallbackInfo& info)
23 {
24 auto textBackgroundStyle = JSContainerSpan::ParseTextBackgroundStyle(info);
25 SpanModel::GetInstance()->SetTextBackgroundStyle(textBackgroundStyle);
26 }
27
ParseTextBackgroundStyle(const JSCallbackInfo & info)28 TextBackgroundStyle JSContainerSpan::ParseTextBackgroundStyle(const JSCallbackInfo& info)
29 {
30 TextBackgroundStyle textBackgroundStyle;
31 auto jsValue = info[0];
32 if (!jsValue->IsObject()) {
33 return textBackgroundStyle;
34 }
35 JSRef<JSObject> obj = JSRef<JSObject>::Cast(jsValue);
36 JSRef<JSVal> colorValue = obj->GetProperty("color");
37 Color colorVal;
38 if (ParseJsColor(colorValue, colorVal)) {
39 textBackgroundStyle.backgroundColor = colorVal;
40 }
41 JSRef<JSVal> radiusValue = obj->GetProperty("radius");
42 if (!radiusValue->IsString() && !radiusValue->IsNumber() && !radiusValue->IsObject()) {
43 return textBackgroundStyle;
44 }
45 CalcDimension radius;
46 if (ParseJsDimensionVp(radiusValue, radius)) {
47 if (radius.Unit() == DimensionUnit::PERCENT) {
48 radius.Reset();
49 }
50 textBackgroundStyle.backgroundRadius = { radius, radius, radius, radius };
51 textBackgroundStyle.backgroundRadius->multiValued = false;
52 } else if (radiusValue->IsObject()) {
53 JSRef<JSObject> object = JSRef<JSObject>::Cast(radiusValue);
54 CalcDimension topLeft;
55 CalcDimension topRight;
56 CalcDimension bottomLeft;
57 CalcDimension bottomRight;
58 ParseAllBorderRadiuses(object, topLeft, topRight, bottomLeft, bottomRight);
59 textBackgroundStyle.backgroundRadius = { topLeft, topRight, bottomRight, bottomLeft };
60 textBackgroundStyle.backgroundRadius->multiValued = true;
61 }
62 return textBackgroundStyle;
63 }
64
JSBind(BindingTarget globalObj)65 void JSContainerSpan::JSBind(BindingTarget globalObj)
66 {
67 JSClass<JSContainerSpan>::Declare("ContainerSpan");
68 MethodOptions opt = MethodOptions::NONE;
69 JSClass<JSContainerSpan>::StaticMethod("create", &JSContainerSpan::Create, opt);
70 JSClass<JSContainerSpan>::StaticMethod("textBackgroundStyle", &JSContainerSpan::SetTextBackgroundStyle, opt);
71 JSClass<JSContainerSpan>::InheritAndBind<JSContainerBase>(globalObj);
72 }
73
Create(const JSCallbackInfo & info)74 void JSContainerSpan::Create(const JSCallbackInfo& info)
75 {
76 SpanModel::GetInstance()->CreateContainSpan();
77 }
78 } // namespace OHOS::Ace::Framework
79