• 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 
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     return ParseTextBackgroundStyle(obj);
37 }
38 
ParseTextBackgroundStyle(const JSRef<JSObject> & obj)39 TextBackgroundStyle JSContainerSpan::ParseTextBackgroundStyle(const JSRef<JSObject>& obj)
40 {
41     TextBackgroundStyle textBackgroundStyle;
42     JSRef<JSVal> colorValue = obj->GetProperty("color");
43     Color colorVal;
44     if (ParseJsColor(colorValue, colorVal)) {
45         textBackgroundStyle.backgroundColor = colorVal;
46     }
47     JSRef<JSVal> radiusValue = obj->GetProperty("radius");
48     if (!radiusValue->IsString() && !radiusValue->IsNumber() && !radiusValue->IsObject()) {
49         return textBackgroundStyle;
50     }
51     CalcDimension radius;
52     if (ParseJsDimensionVp(radiusValue, radius)) {
53         if (radius.Unit() == DimensionUnit::PERCENT) {
54             radius.Reset();
55         }
56         textBackgroundStyle.backgroundRadius = { radius, radius, radius, radius };
57         textBackgroundStyle.backgroundRadius->multiValued = false;
58     } else if (radiusValue->IsObject()) {
59         JSRef<JSObject> object = JSRef<JSObject>::Cast(radiusValue);
60         CalcDimension topLeft;
61         CalcDimension topRight;
62         CalcDimension bottomLeft;
63         CalcDimension bottomRight;
64         ParseAllBorderRadiuses(object, topLeft, topRight, bottomLeft, bottomRight);
65         textBackgroundStyle.backgroundRadius = { topLeft, topRight, bottomRight, bottomLeft };
66         textBackgroundStyle.backgroundRadius->multiValued = true;
67     }
68     return textBackgroundStyle;
69 }
70 
JSBind(BindingTarget globalObj)71 void JSContainerSpan::JSBind(BindingTarget globalObj)
72 {
73     JSClass<JSContainerSpan>::Declare("ContainerSpan");
74     MethodOptions opt = MethodOptions::NONE;
75     JSClass<JSContainerSpan>::StaticMethod("create", &JSContainerSpan::Create, opt);
76     JSClass<JSContainerSpan>::StaticMethod("textBackgroundStyle", &JSContainerSpan::SetTextBackgroundStyle, opt);
77     JSClass<JSContainerSpan>::InheritAndBind<JSContainerBase>(globalObj);
78 }
79 
Create(const JSCallbackInfo & info)80 void JSContainerSpan::Create(const JSCallbackInfo& info)
81 {
82     SpanModel::GetInstance()->CreateContainSpan();
83 }
84 } // namespace OHOS::Ace::Framework
85