• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 constexpr uint32_t MIN_LINES = 1;
35 constexpr uint32_t MAX_LINES_MODE_CLIP = 0;
36 constexpr uint32_t MAX_LINES_MODE_SCROLL = 1;
37 constexpr uint32_t TWO_ARGS = 2;
38 }
39 
JSBind(BindingTarget globalObj)40 void JSTextArea::JSBind(BindingTarget globalObj)
41 {
42     JSClass<JSTextArea>::Declare("TextArea");
43     MethodOptions opt = MethodOptions::NONE;
44     JSClass<JSTextArea>::StaticMethod("create", &JSTextArea::Create, opt);
45     JSClass<JSTextArea>::StaticMethod("placeholderColor", &JSTextField::SetPlaceholderColor);
46     JSClass<JSTextArea>::StaticMethod("placeholderFont", &JSTextField::SetPlaceholderFont);
47     JSClass<JSTextArea>::StaticMethod("backgroundColor", &JSTextField::SetBackgroundColor);
48     JSClass<JSTextArea>::StaticMethod("textAlign", &JSTextField::SetTextAlign);
49     JSClass<JSTextArea>::StaticMethod("caretColor", &JSTextField::SetCaretColor);
50     JSClass<JSTextArea>::StaticMethod("height", &JSTextField::JsHeight);
51     JSClass<JSTextArea>::StaticMethod("width", &JSTextField::JsWidth);
52     JSClass<JSTextArea>::StaticMethod("padding", &JSTextField::JsPadding);
53     JSClass<JSTextArea>::StaticMethod("margin", &JSTextField::JsMargin);
54     JSClass<JSTextArea>::StaticMethod("border", &JSTextField::JsBorder);
55     JSClass<JSTextArea>::StaticMethod("borderWidth", &JSTextField::JsBorderWidth);
56     JSClass<JSTextArea>::StaticMethod("borderColor", &JSTextField::JsBorderColor);
57     JSClass<JSTextArea>::StaticMethod("borderStyle", &JSTextField::JsBorderStyle);
58     JSClass<JSTextArea>::StaticMethod("borderRadius", &JSTextField::JsBorderRadius);
59     JSClass<JSTextArea>::StaticMethod("fontSize", &JSTextField::SetFontSize);
60     JSClass<JSTextArea>::StaticMethod("fontColor", &JSTextField::SetTextColor);
61     JSClass<JSTextArea>::StaticMethod("fontWeight", &JSTextField::SetFontWeight);
62     JSClass<JSTextArea>::StaticMethod("fontStyle", &JSTextField::SetFontStyle);
63     JSClass<JSTextArea>::StaticMethod("fontFamily", &JSTextField::SetFontFamily);
64     JSClass<JSTextArea>::StaticMethod("minFontScale", &JSTextField::SetMinFontScale);
65     JSClass<JSTextArea>::StaticMethod("maxFontScale", &JSTextField::SetMaxFontScale);
66     JSClass<JSTextArea>::StaticMethod("inputFilter", &JSTextField::SetInputFilter);
67     JSClass<JSTextArea>::StaticMethod("hoverEffect", &JSTextField::JsHoverEffect);
68     JSClass<JSTextArea>::StaticMethod("maxLength", &JSTextField::SetMaxLength);
69     JSClass<JSTextArea>::StaticMethod("showCounter", &JSTextField::SetShowCounter);
70     JSClass<JSTextArea>::StaticMethod("barState", &JSTextField::SetBarState);
71     JSClass<JSTextArea>::StaticMethod("maxLines", &JSTextArea::SetMaxLines);
72     JSClass<JSTextArea>::StaticMethod("minLines", &JSTextArea::SetMinLines);
73     JSClass<JSTextArea>::StaticMethod("style", &JSTextField::SetInputStyle);
74     JSClass<JSTextArea>::StaticMethod("onWillChange", &JSTextField::SetOnWillChange);
75     JSClass<JSTextArea>::StaticMethod("onChange", &JSTextField::SetOnChange);
76     JSClass<JSTextArea>::StaticMethod("onTextSelectionChange", &JSTextField::SetOnTextSelectionChange);
77     JSClass<JSTextArea>::StaticMethod("onContentScroll", &JSTextField::SetOnContentScroll);
78     JSClass<JSTextArea>::StaticMethod("onCopy", &JSTextField::SetOnCopy);
79     JSClass<JSTextArea>::StaticMethod("onCut", &JSTextField::SetOnCut);
80     JSClass<JSTextArea>::StaticMethod("onPaste", &JSTextField::SetOnPaste);
81     JSClass<JSTextArea>::StaticMethod("onClick", &JSTextField::SetOnClick);
82     JSClass<JSTextArea>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
83     JSClass<JSTextArea>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
84     JSClass<JSTextArea>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
85     JSClass<JSTextArea>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
86     JSClass<JSTextArea>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
87     JSClass<JSTextArea>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
88     JSClass<JSTextArea>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
89     JSClass<JSTextArea>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
90     JSClass<JSTextArea>::StaticMethod("onEditChange", &JSTextField::SetOnEditChanged);
91     JSClass<JSTextArea>::StaticMethod("copyOption", &JSTextField::SetCopyOption);
92     JSClass<JSTextArea>::StaticMethod("foregroundColor", &JSTextField::SetForegroundColor);
93     JSClass<JSTextArea>::StaticMethod("enableKeyboardOnFocus", &JSTextField::SetEnableKeyboardOnFocus);
94     JSClass<JSTextArea>::StaticMethod("selectionMenuHidden", &JSTextField::SetSelectionMenuHidden);
95     JSClass<JSTextArea>::StaticMethod("customKeyboard", &JSTextField::SetCustomKeyboard);
96     JSClass<JSTextArea>::StaticMethod("onSubmit", &JSTextField::SetOnSubmit);
97     JSClass<JSTextArea>::StaticMethod("enterKeyType", &JSTextField::SetEnterKeyType);
98     JSClass<JSTextArea>::StaticMethod("type", &JSTextField::SetType);
99     JSClass<JSTextArea>::StaticMethod("fontFeature", &JSTextField::SetFontFeature);
100     JSClass<JSTextArea>::StaticMethod("lineBreakStrategy", &JSTextField::SetLineBreakStrategy);
101     JSClass<JSTextArea>::StaticMethod("decoration", &JSTextField::SetDecoration);
102     JSClass<JSTextArea>::StaticMethod("minFontSize", &JSTextField::SetMinFontSize);
103     JSClass<JSTextArea>::StaticMethod("maxFontSize", &JSTextField::SetMaxFontSize);
104     JSClass<JSTextArea>::StaticMethod("heightAdaptivePolicy", &JSTextField::SetHeightAdaptivePolicy);
105     JSClass<JSTextArea>::StaticMethod("letterSpacing", &JSTextField::SetLetterSpacing);
106     JSClass<JSTextArea>::StaticMethod("lineHeight", &JSTextField::SetLineHeight);
107     JSClass<JSTextArea>::StaticMethod("halfLeading", &JSTextField::SetHalfLeading);
108     JSClass<JSTextArea>::StaticMethod("lineSpacing", &JSTextField::SetLineSpacing);
109     JSClass<JSTextArea>::StaticMethod("wordBreak", &JSTextField::SetWordBreak);
110     JSClass<JSTextArea>::StaticMethod("contentType", &JSTextField::SetContentType);
111     JSClass<JSTextArea>::StaticMethod("enableAutoFill", &JSTextField::SetEnableAutoFill);
112     JSClass<JSTextArea>::StaticMethod("selectedBackgroundColor", &JSTextField::SetSelectedBackgroundColor);
113     JSClass<JSTextArea>::StaticMethod("caretStyle", &JSTextField::SetCaretStyle);
114     JSClass<JSTextArea>::StaticMethod("textIndent", &JSTextField::SetTextIndent);
115     JSClass<JSTextArea>::StaticMethod("textOverflow", &JSTextField::SetTextOverflow);
116     JSClass<JSTextArea>::StaticMethod("onWillInsert", &JSTextField::OnWillInsertValue);
117     JSClass<JSTextArea>::StaticMethod("onDidInsert", &JSTextField::OnDidInsertValue);
118     JSClass<JSTextArea>::StaticMethod("onWillDelete", &JSTextField::OnWillDelete);
119     JSClass<JSTextArea>::StaticMethod("onDidDelete", &JSTextField::OnDidDelete);
120     JSClass<JSTextArea>::StaticMethod("editMenuOptions", &JSTextField::EditMenuOptions);
121     JSClass<JSTextArea>::StaticMethod("enablePreviewText", &JSTextField::SetEnablePreviewText);
122     JSClass<JSTextArea>::StaticMethod("enableHapticFeedback", &JSTextField::SetEnableHapticFeedback);
123     JSClass<JSTextArea>::StaticMethod("autoCapitalizationMode", &JSTextField::SetCapitalizationMode);
124     JSClass<JSTextArea>::StaticMethod("ellipsisMode", &JSTextField::SetEllipsisMode);
125     JSClass<JSTextArea>::StaticMethod("stopBackPress", &JSTextField::SetStopBackPress);
126     JSClass<JSTextArea>::StaticMethod("keyboardAppearance", &JSTextField::SetKeyboardAppearance);
127     JSClass<JSTextArea>::StaticMethod("strokeWidth", &JSTextField::SetStrokeWidth);
128     JSClass<JSTextArea>::StaticMethod("strokeColor", &JSTextField::SetStrokeColor);
129     JSClass<JSTextArea>::StaticMethod("enableAutoSpacing", &JSTextField::SetEnableAutoSpacing);
130     JSClass<JSTextArea>::InheritAndBind<JSViewAbstract>(globalObj);
131 }
132 
Create(const JSCallbackInfo & info)133 void JSTextArea::Create(const JSCallbackInfo& info)
134 {
135     JSTextField::CreateTextArea(info);
136 }
137 
SetMaxLines(const JSCallbackInfo & info)138 void JSTextArea::SetMaxLines(const JSCallbackInfo& info)
139 {
140     auto normalMaxViewLines = Infinity<uint32_t>();
141     auto inlineMaxViewLines = MAX_LINES;
142     auto isValid = !(info.Length() < 1 || !info[0]->IsNumber() || info[0]->ToNumber<int32_t>() <= 0);
143     if (isValid) {
144         inlineMaxViewLines = info[0]->ToNumber<uint32_t>();
145         normalMaxViewLines = info[0]->ToNumber<uint32_t>();
146     }
147     TextFieldModel::GetInstance()->SetNormalMaxViewLines(normalMaxViewLines);
148     TextFieldModel::GetInstance()->SetMaxViewLines(inlineMaxViewLines);
149 
150     auto overflow = MAX_LINES_MODE_CLIP;
151     if (info.Length() == TWO_ARGS && info[1]->IsObject()) {
152         auto paramObject = JSRef<JSObject>::Cast(info[1]);
153         auto overflowMode = paramObject->GetProperty("overflowMode");
154         auto modeValue = overflowMode->IsNumber() ? overflowMode->ToNumber<int32_t>() : -1;
155         if (modeValue >= 0 && (modeValue == MAX_LINES_MODE_CLIP || modeValue == MAX_LINES_MODE_SCROLL)) {
156             overflow = static_cast<uint32_t>(modeValue);
157         }
158     }
159     TextFieldModel::GetInstance()->SetOverflowMode(OVERFLOWS_MODE[overflow]);
160 }
161 
SetMinLines(const JSCallbackInfo & info)162 void JSTextArea::SetMinLines(const JSCallbackInfo& info)
163 {
164     auto minLines = MIN_LINES;
165     if (info.Length() == 1) {
166         auto tmpInfo = info[0];
167         if (tmpInfo->IsNumber() && tmpInfo->ToNumber<int32_t>() > 0) {
168             minLines = tmpInfo->ToNumber<uint32_t>();
169         }
170     }
171     TextFieldModel::GetInstance()->SetMinLines(minLines);
172 }
173 
JSBind(BindingTarget globalObj)174 void JSTextAreaController::JSBind(BindingTarget globalObj)
175 {
176     JSClass<JSTextEditableController>::Declare("TextAreaController");
177     JSTextEditableController::JSBind(globalObj);
178 }
179 } // namespace OHOS::Ace::Framework
180