1 /*
2  * Copyright (c) 2021-2024 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_textarea.h"
17 
18 #include <vector>
19 
20 #include "frameworks/bridge/common/utils/utils.h"
21 #include "frameworks/bridge/declarative_frontend/engine/functions/js_clipboard_function.h"
22 #include "frameworks/bridge/declarative_frontend/engine/functions/js_function.h"
23 #include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h"
24 #include "frameworks/bridge/declarative_frontend/jsview/js_interactable_view.h"
25 #include "frameworks/bridge/declarative_frontend/jsview/js_text_editable_controller.h"
26 #include "frameworks/bridge/declarative_frontend/jsview/js_textfield.h"
27 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h"
28 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
29 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
30 
31 namespace OHOS::Ace::Framework {
32 namespace {
33 constexpr uint32_t MAX_LINES = 3;
34 }
35 
JSBind(BindingTarget globalObj)36 void JSTextArea::JSBind(BindingTarget globalObj)
37 {
38     JSClass<JSTextArea>::Declare("TextArea");
39     MethodOptions opt = MethodOptions::NONE;
40     JSClass<JSTextArea>::StaticMethod("create", &JSTextArea::Create, opt);
41     JSClass<JSTextArea>::StaticMethod("placeholderColor", &JSTextField::SetPlaceholderColor);
42     JSClass<JSTextArea>::StaticMethod("placeholderFont", &JSTextField::SetPlaceholderFont);
43     JSClass<JSTextArea>::StaticMethod("backgroundColor", &JSTextField::SetBackgroundColor);
44     JSClass<JSTextArea>::StaticMethod("textAlign", &JSTextField::SetTextAlign);
45     JSClass<JSTextArea>::StaticMethod("caretColor", &JSTextField::SetCaretColor);
46     JSClass<JSTextArea>::StaticMethod("height", &JSTextField::JsHeight);
47     JSClass<JSTextArea>::StaticMethod("width", &JSTextField::JsWidth);
48     JSClass<JSTextArea>::StaticMethod("padding", &JSTextField::JsPadding);
49     JSClass<JSTextArea>::StaticMethod("margin", &JSTextField::JsMargin);
50     JSClass<JSTextArea>::StaticMethod("border", &JSTextField::JsBorder);
51     JSClass<JSTextArea>::StaticMethod("borderWidth", &JSTextField::JsBorderWidth);
52     JSClass<JSTextArea>::StaticMethod("borderColor", &JSTextField::JsBorderColor);
53     JSClass<JSTextArea>::StaticMethod("borderStyle", &JSTextField::JsBorderStyle);
54     JSClass<JSTextArea>::StaticMethod("borderRadius", &JSTextField::JsBorderRadius);
55     JSClass<JSTextArea>::StaticMethod("fontSize", &JSTextField::SetFontSize);
56     JSClass<JSTextArea>::StaticMethod("fontColor", &JSTextField::SetTextColor);
57     JSClass<JSTextArea>::StaticMethod("fontWeight", &JSTextField::SetFontWeight);
58     JSClass<JSTextArea>::StaticMethod("fontStyle", &JSTextField::SetFontStyle);
59     JSClass<JSTextArea>::StaticMethod("fontFamily", &JSTextField::SetFontFamily);
60     JSClass<JSTextArea>::StaticMethod("inputFilter", &JSTextField::SetInputFilter);
61     JSClass<JSTextArea>::StaticMethod("hoverEffect", &JSTextField::JsHoverEffect);
62     JSClass<JSTextArea>::StaticMethod("maxLength", &JSTextField::SetMaxLength);
63     JSClass<JSTextArea>::StaticMethod("showCounter", &JSTextField::SetShowCounter);
64     JSClass<JSTextArea>::StaticMethod("barState", &JSTextField::SetBarState);
65     JSClass<JSTextArea>::StaticMethod("maxLines", &JSTextArea::SetMaxLines);
66     JSClass<JSTextArea>::StaticMethod("style", &JSTextField::SetInputStyle);
67     JSClass<JSTextArea>::StaticMethod("onChange", &JSTextField::SetOnChange);
68     JSClass<JSTextArea>::StaticMethod("onTextSelectionChange", &JSTextField::SetOnTextSelectionChange);
69     JSClass<JSTextArea>::StaticMethod("onContentScroll", &JSTextField::SetOnContentScroll);
70     JSClass<JSTextArea>::StaticMethod("onCopy", &JSTextField::SetOnCopy);
71     JSClass<JSTextArea>::StaticMethod("onCut", &JSTextField::SetOnCut);
72     JSClass<JSTextArea>::StaticMethod("onPaste", &JSTextField::SetOnPaste);
73     JSClass<JSTextArea>::StaticMethod("onClick", &JSTextField::SetOnClick);
74     JSClass<JSTextArea>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
75     JSClass<JSTextArea>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
76     JSClass<JSTextArea>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
77     JSClass<JSTextArea>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
78     JSClass<JSTextArea>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
79     JSClass<JSTextArea>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
80     JSClass<JSTextArea>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
81     JSClass<JSTextArea>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
82     JSClass<JSTextArea>::StaticMethod("onEditChange", &JSTextField::SetOnEditChanged);
83     JSClass<JSTextArea>::StaticMethod("copyOption", &JSTextField::SetCopyOption);
84     JSClass<JSTextArea>::StaticMethod("foregroundColor", &JSTextField::SetForegroundColor);
85     JSClass<JSTextArea>::StaticMethod("enableKeyboardOnFocus", &JSTextField::SetEnableKeyboardOnFocus);
86     JSClass<JSTextArea>::StaticMethod("selectionMenuHidden", &JSTextField::SetSelectionMenuHidden);
87     JSClass<JSTextArea>::StaticMethod("customKeyboard", &JSTextField::SetCustomKeyboard);
88     JSClass<JSTextArea>::StaticMethod("onSubmit", &JSTextField::SetOnSubmit);
89     JSClass<JSTextArea>::StaticMethod("enterKeyType", &JSTextField::SetEnterKeyType);
90     JSClass<JSTextArea>::StaticMethod("type", &JSTextField::SetType);
91     JSClass<JSTextArea>::StaticMethod("fontFeature", &JSTextField::SetFontFeature);
92     JSClass<JSTextArea>::StaticMethod("lineBreakStrategy", &JSTextField::SetLineBreakStrategy);
93     JSClass<JSTextArea>::StaticMethod("decoration", &JSTextField::SetDecoration);
94     JSClass<JSTextArea>::StaticMethod("minFontSize", &JSTextField::SetMinFontSize);
95     JSClass<JSTextArea>::StaticMethod("maxFontSize", &JSTextField::SetMaxFontSize);
96     JSClass<JSTextArea>::StaticMethod("heightAdaptivePolicy", &JSTextField::SetHeightAdaptivePolicy);
97     JSClass<JSTextArea>::StaticMethod("letterSpacing", &JSTextField::SetLetterSpacing);
98     JSClass<JSTextArea>::StaticMethod("lineHeight", &JSTextField::SetLineHeight);
99     JSClass<JSTextArea>::StaticMethod("lineSpacing", &JSTextField::SetLineSpacing);
100     JSClass<JSTextArea>::StaticMethod("wordBreak", &JSTextField::SetWordBreak);
101     JSClass<JSTextArea>::StaticMethod("contentType", &JSTextField::SetContentType);
102     JSClass<JSTextArea>::StaticMethod("enableAutoFill", &JSTextField::SetEnableAutoFill);
103     JSClass<JSTextArea>::StaticMethod("selectedBackgroundColor", &JSTextField::SetSelectedBackgroundColor);
104     JSClass<JSTextArea>::StaticMethod("caretStyle", &JSTextField::SetCaretStyle);
105     JSClass<JSTextArea>::StaticMethod("textIndent", &JSTextField::SetTextIndent);
106     JSClass<JSTextArea>::StaticMethod("textOverflow", &JSTextField::SetTextOverflow);
107     JSClass<JSTextArea>::StaticMethod("onWillInsert", &JSTextField::OnWillInsertValue);
108     JSClass<JSTextArea>::StaticMethod("onDidInsert", &JSTextField::OnDidInsertValue);
109     JSClass<JSTextArea>::StaticMethod("onWillDelete", &JSTextField::OnWillDelete);
110     JSClass<JSTextArea>::StaticMethod("onDidDelete", &JSTextField::OnDidDelete);
111     JSClass<JSTextArea>::StaticMethod("editMenuOptions", &JSTextField::EditMenuOptions);
112     JSClass<JSTextArea>::StaticMethod("enablePreviewText", &JSTextField::SetEnablePreviewText);
113     JSClass<JSTextArea>::StaticMethod("enableHapticFeedback", &JSTextField::SetEnableHapticFeedback);
114     JSClass<JSTextArea>::InheritAndBind<JSViewAbstract>(globalObj);
115 }
116 
Create(const JSCallbackInfo & info)117 void JSTextArea::Create(const JSCallbackInfo& info)
118 {
119     JSTextField::CreateTextArea(info);
120 }
121 
SetMaxLines(const JSCallbackInfo & info)122 void JSTextArea::SetMaxLines(const JSCallbackInfo& info)
123 {
124     auto normalMaxViewLines = Infinity<uint32_t>();
125     auto inlineMaxViewLines = MAX_LINES;
126     auto isValid = !(info.Length() < 1 || !info[0]->IsNumber() || info[0]->ToNumber<int32_t>() <= 0);
127     if (isValid) {
128         inlineMaxViewLines = info[0]->ToNumber<uint32_t>();
129         normalMaxViewLines = info[0]->ToNumber<uint32_t>();
130     }
131     TextFieldModel::GetInstance()->SetNormalMaxViewLines(normalMaxViewLines);
132     TextFieldModel::GetInstance()->SetMaxViewLines(inlineMaxViewLines);
133 }
134 
JSBind(BindingTarget globalObj)135 void JSTextAreaController::JSBind(BindingTarget globalObj)
136 {
137     JSClass<JSTextEditableController>::Declare("TextAreaController");
138     JSTextEditableController::JSBind(globalObj);
139 }
140 } // namespace OHOS::Ace::Framework
141