1 /*
2  * Copyright (c) 2021-2022 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 "core/components_v2/inspector/textinput_composed_element.h"
17 
18 #include "base/log/dump_log.h"
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/text_field/text_field_element.h"
21 #include "core/components_v2/inspector/utils.h"
22 
23 namespace OHOS::Ace::V2 {
24 
25 namespace {
26 
27 const std::unordered_map<std::string, std::function<std::string(const TextInputComposedElement&)>> CREATE_JSON_MAP {
__anonc2e9b3510202(const TextInputComposedElement& inspector) 28     { "placeholder", [](const TextInputComposedElement& inspector) { return inspector.GetPlaceholder(); } },
__anonc2e9b3510302(const TextInputComposedElement& inspector) 29     { "text", [](const TextInputComposedElement& inspector) { return inspector.GetText(); } },
__anonc2e9b3510402(const TextInputComposedElement& inspector) 30     { "type", [](const TextInputComposedElement& inspector) { return inspector.GetTextinputType(); } },
__anonc2e9b3510502(const TextInputComposedElement& inspector) 31     { "placeholderColor", [](const TextInputComposedElement& inspector) { return inspector.GetPlaceholderColor(); } },
__anonc2e9b3510602(const TextInputComposedElement& inspector) 32     { "placeholderFont", [](const TextInputComposedElement& inspector) { return inspector.GetPlaceholderFont(); } },
__anonc2e9b3510702(const TextInputComposedElement& inspector) 33     { "enterKeyType", [](const TextInputComposedElement& inspector) { return inspector.GetEnterKeyType(); } },
__anonc2e9b3510802(const TextInputComposedElement& inspector) 34     { "caretColor", [](const TextInputComposedElement& inspector) { return inspector.GetCaretColor(); } },
__anonc2e9b3510902(const TextInputComposedElement& inspector) 35     { "fontColor", [](const TextInputComposedElement& inspector) { return inspector.GetTextInputFontColor(); } },
__anonc2e9b3510a02(const TextInputComposedElement& inspector) 36     { "fontSize", [](const TextInputComposedElement& inspector) { return inspector.GetTextInputFontSize(); } },
__anonc2e9b3510b02(const TextInputComposedElement& inspector) 37     { "fontStyle", [](const TextInputComposedElement& inspector) { return inspector.GetTextInputFontStyle(); } },
__anonc2e9b3510c02(const TextInputComposedElement& inspector) 38     { "fontWeight", [](const TextInputComposedElement& inspector) { return inspector.GetTextInputFontWeight(); } },
__anonc2e9b3510d02(const TextInputComposedElement& inspector) 39     { "fontFamily", [](const TextInputComposedElement& inspector) { return inspector.GetTextInputFontFamily(); } },
__anonc2e9b3510e02(const TextInputComposedElement& inspector) 40     { "maxLength", [](const TextInputComposedElement& inspector) { return inspector.GetTextMaxLength(); } },
__anonc2e9b3510f02(const TextInputComposedElement& inspector) 41     { "inputFilter", [](const TextInputComposedElement& inspector) { return inspector.GetTextInputFilter(); } },
42 };
43 
44 } // namespace
45 
Dump()46 void TextInputComposedElement::Dump()
47 {
48     InspectorComposedElement::Dump();
49     DumpLog::GetInstance().AddDesc(std::string("placeholder: ").append(GetPlaceholder()));
50     DumpLog::GetInstance().AddDesc(std::string("text: ").append(GetText()));
51     DumpLog::GetInstance().AddDesc(std::string("type: ").append(GetTextinputType()));
52     DumpLog::GetInstance().AddDesc(std::string("placeholderColor: ").append(GetPlaceholderColor()));
53     DumpLog::GetInstance().AddDesc(std::string("placeholderFont: ").append(GetPlaceholderFont()));
54     DumpLog::GetInstance().AddDesc(std::string("enterKeyType: ").append(GetEnterKeyType()));
55     DumpLog::GetInstance().AddDesc(std::string("caretColor: ").append(GetCaretColor()));
56     DumpLog::GetInstance().AddDesc(std::string("fontColor: ").append(GetTextInputFontColor()));
57     DumpLog::GetInstance().AddDesc(std::string("fontSize: ").append(GetTextInputFontSize()));
58     DumpLog::GetInstance().AddDesc(std::string("fontStyle: ").append(GetTextInputFontStyle()));
59     DumpLog::GetInstance().AddDesc(std::string("fontWeight: ").append(GetTextInputFontWeight()));
60     DumpLog::GetInstance().AddDesc(std::string("fontFamily: ").append(GetTextInputFontFamily()));
61     DumpLog::GetInstance().AddDesc(std::string("maxLength: ").append(GetTextMaxLength()));
62     DumpLog::GetInstance().AddDesc(std::string("inputFilter: ").append(GetTextInputFilter()));
63 }
64 
ToJsonObject() const65 std::unique_ptr<JsonValue> TextInputComposedElement::ToJsonObject() const
66 {
67     auto resultJson = InspectorComposedElement::ToJsonObject();
68     for (const auto& value : CREATE_JSON_MAP) {
69         resultJson->Put(value.first.c_str(), value.second(*this).c_str());
70     }
71     return resultJson;
72 }
73 
GetPlaceholder() const74 std::string TextInputComposedElement::GetPlaceholder() const
75 {
76     auto render = GetRenderTextField();
77     if (render) {
78         return render->GetPlaceholder();
79     }
80     return "";
81 }
82 
GetText() const83 std::string TextInputComposedElement::GetText() const
84 {
85     auto render = GetRenderTextField();
86     if (render) {
87         return render->GetValue();
88     }
89     return "";
90 }
91 
GetPlaceholderColor() const92 std::string TextInputComposedElement::GetPlaceholderColor() const
93 {
94     auto render = GetRenderTextField();
95     if (render) {
96         return ConvertColorToString(render->GetInactivePlaceholderColor());
97     }
98     return "";
99 }
100 
GetPlaceholderFont() const101 std::string TextInputComposedElement::GetPlaceholderFont() const
102 {
103     auto render = GetRenderTextField();
104     auto jsonValue = JsonUtil::Create(true);
105     if (render) {
106         auto  placeHoldStyle = render->GetPlaceHoldStyle();
107         jsonValue->Put("size", placeHoldStyle.GetFontSize().ToString().c_str());
108         auto weight = placeHoldStyle.GetFontWeight();
109         if (weight == FontWeight::W100) {
110             jsonValue->Put("weight", "100");
111         } else if (weight == FontWeight::W200) {
112             jsonValue->Put("weight", "200");
113         } else if (weight == FontWeight::W300) {
114             jsonValue->Put("weight", "300");
115         } else if (weight == FontWeight::W400) {
116             jsonValue->Put("weight", "400");
117         } else if (weight == FontWeight::W500) {
118             jsonValue->Put("weight", "500");
119         } else if (weight == FontWeight::W600) {
120             jsonValue->Put("weight", "600");
121         } else if (weight == FontWeight::W700) {
122             jsonValue->Put("weight", "700");
123         } else if (weight == FontWeight::W800) {
124             jsonValue->Put("weight", "800");
125         } else if (weight == FontWeight::W900) {
126             jsonValue->Put("weight", "900");
127         } else {
128             jsonValue->Put("weight", ConvertWrapFontWeightToStirng(weight).c_str());
129         }
130         auto family = placeHoldStyle.GetFontFamilies();
131         std::string jsonFamily = ConvertFontFamily(family);
132         jsonValue->Put("family", jsonFamily.c_str());
133         auto fontStyle = placeHoldStyle.GetFontStyle();
134         jsonValue->Put("style", ConvertWrapFontStyleToStirng(fontStyle).c_str());
135         return jsonValue->ToString();
136     }
137     return "";
138 }
139 
GetCaretColor() const140 std::string TextInputComposedElement::GetCaretColor() const
141 {
142     auto render = GetRenderTextField();
143     if (render) {
144         return ConvertColorToString(render->GetCursorColor());
145     }
146     return "";
147 }
148 
GetTextInputFontColor() const149 std::string TextInputComposedElement::GetTextInputFontColor() const
150 {
151     auto render = GetRenderTextField();
152     if (render) {
153         return ConvertColorToString(render->GetEditingStyle().GetTextColor());
154     }
155     return "";
156 }
157 
GetTextInputFontSize() const158 std::string TextInputComposedElement::GetTextInputFontSize() const
159 {
160     auto render = GetRenderTextField();
161     if (render) {
162         return render->GetEditingStyle().GetFontSize().ToString().c_str();
163     }
164     return "";
165 }
166 
GetTextInputFontStyle() const167 std::string TextInputComposedElement::GetTextInputFontStyle() const
168 {
169     auto render = GetRenderTextField();
170     auto fontStyle =
171         render ? render->GetEditingStyle().GetFontStyle() : FontStyle::NORMAL;
172     return ConvertWrapFontStyleToStirng(fontStyle);
173 }
174 
GetTextInputFontWeight() const175 std::string TextInputComposedElement::GetTextInputFontWeight() const
176 {
177     auto render = GetRenderTextField();
178     auto weight =
179         render ? render->GetEditingStyle().GetFontWeight() : FontWeight::NORMAL;
180     if (weight == FontWeight::W100) {
181             return "100";
182         } else if (weight == FontWeight::W200) {
183             return "200";
184         } else if (weight == FontWeight::W300) {
185             return "300";
186         } else if (weight == FontWeight::W400) {
187             return "400";
188         } else if (weight == FontWeight::W500) {
189             return "500";
190         } else if (weight == FontWeight::W600) {
191             return "600";
192         } else if (weight == FontWeight::W700) {
193             return "700";
194         } else if (weight == FontWeight::W800) {
195             return "800";
196         } else if (weight == FontWeight::W900) {
197             return "900";
198         } else {
199             return ConvertWrapFontWeightToStirng(weight);
200         }
201 }
202 
GetTextInputFontFamily() const203 std::string TextInputComposedElement::GetTextInputFontFamily() const
204 {
205     auto render = GetRenderTextField();
206     auto fontFamily = render ? render->GetEditingStyle().GetFontFamilies() : std::vector<std::string>();
207     return ConvertFontFamily(fontFamily);
208 }
209 
GetTextinputType() const210 std::string TextInputComposedElement::GetTextinputType() const
211 {
212     auto render = GetRenderTextField();
213     if (!render) {
214         return "InputType.Normal";
215     }
216     auto keyboard = render->GetKeyboard();
217     if (keyboard == TextInputType::NUMBER) {
218         return "InputType.Number";
219     } else if (keyboard == TextInputType::VISIBLE_PASSWORD) {
220         return "InputType.Password";
221     } else if (keyboard == TextInputType::EMAIL_ADDRESS) {
222         return "InputType.Email";
223     } else if (keyboard == TextInputType::USER_NAME) {
224         return "InputType.USER_NAME";
225     } else if (keyboard == TextInputType::NEW_PASSWORD) {
226         return "InputType.NEW_PASSWORD";
227     }
228     return "InputType.Normal";
229 }
230 
GetEnterKeyType() const231 std::string TextInputComposedElement::GetEnterKeyType() const
232 {
233     auto render = GetRenderTextField();
234     if (!render) {
235         return "EnterKeyType.Done";
236     }
237     auto action = render->GetAction();
238     if (action == TextInputAction::GO) {
239         return "EnterKeyType.Go";
240     } else if (action == TextInputAction::SEARCH) {
241         return "EnterKeyType.Search";
242     } else if (action == TextInputAction::SEND) {
243         return "EnterKeyType.Send";
244     } else if (action == TextInputAction::NEXT) {
245         return "EnterKeyType.Next";
246     }
247     return "EnterKeyType.Done";
248 }
249 
GetTextMaxLength() const250 std::string TextInputComposedElement::GetTextMaxLength() const
251 {
252     auto render = GetRenderTextField();
253     if (render) {
254         return std::to_string(render->GetMaxLength()).c_str();
255     }
256     return "";
257 }
258 
GetRenderTextField() const259 RefPtr<RenderTextField> TextInputComposedElement::GetRenderTextField() const
260 {
261     auto node = GetInspectorNode(TextFieldElement::TypeId());
262     if (node) {
263         return AceType::DynamicCast<RenderTextField>(node);
264     }
265     return nullptr;
266 }
267 
ConvertFontFamily(const std::vector<std::string> & fontFamily) const268 std::string TextInputComposedElement::ConvertFontFamily(const std::vector<std::string>& fontFamily) const
269 {
270     std::string result = "";
271     for (const auto& item : fontFamily) {
272         result += item;
273         result += ",";
274     }
275     result = result.substr(0, result.size() - 1);
276     return result;
277 }
278 
GetTextInputFilter() const279 std::string TextInputComposedElement::GetTextInputFilter() const
280 {
281     auto render = GetRenderTextField();
282     if (render) {
283         return render->GetTextInputFilter();
284     }
285     return "";
286 }
287 
288 } // namespace OHOS::Ace::V2
289