• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_textinput.h"
17 
18 #include <cstdint>
19 #include <optional>
20 #include <string>
21 #include <vector>
22 
23 #include "core/common/container.h"
24 #include "frameworks/bridge/common/utils/utils.h"
25 #include "frameworks/bridge/declarative_frontend/engine/functions/js_clipboard_function.h"
26 #include "frameworks/bridge/declarative_frontend/engine/functions/js_function.h"
27 #include "frameworks/bridge/declarative_frontend/jsview/js_textfield.h"
28 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
29 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
30 #include "frameworks/core/common/ime/text_input_action.h"
31 #include "frameworks/core/common/ime/text_input_type.h"
32 #include "frameworks/core/components/text_field/text_field_component.h"
33 #include "frameworks/core/components/text_field/textfield_theme.h"
34 
35 namespace OHOS::Ace::Framework {
36 
JSBind(BindingTarget globalObj)37 void JSTextInput::JSBind(BindingTarget globalObj)
38 {
39     JSClass<JSTextInput>::Declare("TextInput");
40     MethodOptions opt = MethodOptions::NONE;
41     JSClass<JSTextInput>::StaticMethod("create", &JSTextInput::Create, opt);
42     JSClass<JSTextInput>::StaticMethod("type", &JSTextField::SetType);
43     JSClass<JSTextInput>::StaticMethod("placeholderColor", &JSTextField::SetPlaceholderColor);
44     JSClass<JSTextInput>::StaticMethod("placeholderFont", &JSTextField::SetPlaceholderFont);
45     JSClass<JSTextInput>::StaticMethod("backgroundColor", &JSTextField::SetBackgroundColor);
46     JSClass<JSTextInput>::StaticMethod("enterKeyType", &JSTextField::SetEnterKeyType);
47     JSClass<JSTextInput>::StaticMethod("caretColor", &JSTextField::SetCaretColor);
48     JSClass<JSTextInput>::StaticMethod("caretPosition", &JSTextField::SetCaretPosition);
49     JSClass<JSTextInput>::StaticMethod("selectedBackgroundColor", &JSTextField::SetSelectedBackgroundColor);
50     JSClass<JSTextInput>::StaticMethod("caretStyle", &JSTextField::SetCaretStyle);
51     JSClass<JSTextInput>::StaticMethod("maxLength", &JSTextField::SetMaxLength);
52     JSClass<JSTextInput>::StaticMethod("width", &JSTextField::JsWidth);
53     JSClass<JSTextInput>::StaticMethod("height", &JSTextField::JsHeight);
54     JSClass<JSTextInput>::StaticMethod("padding", &JSTextField::JsPadding);
55     JSClass<JSTextInput>::StaticMethod("border", &JSTextField::JsBorder);
56     JSClass<JSTextInput>::StaticMethod("borderWidth", &JSTextField::JsBorderWidth);
57     JSClass<JSTextInput>::StaticMethod("borderColor", &JSTextField::JsBorderColor);
58     JSClass<JSTextInput>::StaticMethod("borderStyle", &JSTextField::JsBorderStyle);
59     JSClass<JSTextInput>::StaticMethod("borderRadius", &JSTextField::JsBorderRadius);
60     JSClass<JSTextInput>::StaticMethod("fontSize", &JSTextField::SetFontSize);
61     JSClass<JSTextInput>::StaticMethod("fontColor", &JSTextField::SetTextColor);
62     JSClass<JSTextInput>::StaticMethod("fontWeight", &JSTextField::SetFontWeight);
63     JSClass<JSTextInput>::StaticMethod("fontStyle", &JSTextField::SetFontStyle);
64     JSClass<JSTextInput>::StaticMethod("fontFamily", &JSTextField::SetFontFamily);
65     JSClass<JSTextInput>::StaticMethod("inputFilter", &JSTextField::SetInputFilter);
66     JSClass<JSTextInput>::StaticMethod("showPasswordIcon", &JSTextField::SetShowPasswordIcon);
67     JSClass<JSTextInput>::StaticMethod("textAlign", &JSTextField::SetTextAlign);
68     JSClass<JSTextInput>::StaticMethod("style", &JSTextField::SetInputStyle);
69     JSClass<JSTextInput>::StaticMethod("hoverEffect", &JSTextField::JsHoverEffect);
70     JSClass<JSTextInput>::StaticMethod("copyOption", &JSTextField::SetCopyOption);
71     JSClass<JSTextInput>::StaticMethod("textMenuOptions", &JSTextField::JsMenuOptionsExtension);
72     JSClass<JSTextInput>::StaticMethod("foregroundColor", &JSTextField::SetForegroundColor);
73     JSClass<JSTextInput>::StaticMethod("showUnit", &JSTextField::SetShowUnit);
74     JSClass<JSTextInput>::StaticMethod("showError", &JSTextField::SetShowError);
75     JSClass<JSTextInput>::StaticMethod("barState", &JSTextField::SetBarState);
76     JSClass<JSTextInput>::StaticMethod("maxLines", &JSTextField::SetMaxLines);
77     // API7 onEditChanged deprecated
78     JSClass<JSTextInput>::StaticMethod("onEditChanged", &JSTextField::SetOnEditChanged);
79     JSClass<JSTextInput>::StaticMethod("onEditChange", &JSTextField::SetOnEditChanged);
80     JSClass<JSTextInput>::StaticMethod("onSubmit", &JSTextField::SetOnSubmit);
81     JSClass<JSTextInput>::StaticMethod("onChange", &JSTextField::SetOnChange);
82     JSClass<JSTextInput>::StaticMethod("onTextSelectionChange", &JSTextField::SetOnTextSelectionChange);
83     JSClass<JSTextInput>::StaticMethod("onContentScroll", &JSTextField::SetOnContentScroll);
84     JSClass<JSTextInput>::StaticMethod("onCopy", &JSTextField::SetOnCopy);
85     JSClass<JSTextInput>::StaticMethod("onCut", &JSTextField::SetOnCut);
86     JSClass<JSTextInput>::StaticMethod("onPaste", &JSTextField::SetOnPaste);
87     JSClass<JSTextInput>::StaticMethod("onClick", &JSTextField::SetOnClick);
88     JSClass<JSTextInput>::StaticMethod("requestKeyboardOnFocus", &JSTextField::SetEnableKeyboardOnFocus);
89     JSClass<JSTextInput>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
90     JSClass<JSTextInput>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
91     JSClass<JSTextInput>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
92     JSClass<JSTextInput>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
93     JSClass<JSTextInput>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
94     JSClass<JSTextInput>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
95     JSClass<JSTextInput>::StaticMethod("passwordIcon", &JSTextField::SetPasswordIcon);
96     JSClass<JSTextInput>::StaticMethod("showUnderline", &JSTextField::SetShowUnderline);
97     JSClass<JSTextInput>::StaticMethod("enableKeyboardOnFocus", &JSTextField::SetEnableKeyboardOnFocus);
98     JSClass<JSTextInput>::StaticMethod("selectionMenuHidden", &JSTextField::SetSelectionMenuHidden);
99     JSClass<JSTextInput>::StaticMethod("customKeyboard", &JSTextField::SetCustomKeyboard);
100 
101     JSClass<JSTextInput>::InheritAndBind<JSViewAbstract>(globalObj);
102 }
103 
Create(const JSCallbackInfo & info)104 void JSTextInput::Create(const JSCallbackInfo& info)
105 {
106     JSTextField::CreateTextInput(info);
107 }
108 
JSBind(BindingTarget globalObj)109 void JSTextInputController::JSBind(BindingTarget globalObj)
110 {
111     JSClass<JSTextInputController>::Declare("TextInputController");
112     JSClass<JSTextInputController>::Method("caretPosition", &JSTextInputController::CaretPosition);
113     JSClass<JSTextInputController>::Method("setTextSelection", &JSTextInputController::SetTextSelection);
114     JSClass<JSTextInputController>::CustomMethod("getTextContentRect", &JSTextInputController::GetTextContentRect);
115     JSClass<JSTextInputController>::CustomMethod("getTextContentLineCount",
116         &JSTextInputController::GetTextContentLinesNum);
117     JSClass<JSTextInputController>::Method("stopEditing", &JSTextInputController::StopEditing);
118     JSClass<JSTextInputController>::Bind(
119         globalObj, JSTextInputController::Constructor, JSTextInputController::Destructor);
120 }
121 
Constructor(const JSCallbackInfo & args)122 void JSTextInputController::Constructor(const JSCallbackInfo& args)
123 {
124     auto scroller = Referenced::MakeRefPtr<JSTextInputController>();
125     scroller->IncRefCount();
126     args.SetReturnValue(Referenced::RawPtr(scroller));
127 }
128 
Destructor(JSTextInputController * scroller)129 void JSTextInputController::Destructor(JSTextInputController* scroller)
130 {
131     if (scroller != nullptr) {
132         scroller->DecRefCount();
133     }
134 }
135 
CaretPosition(int32_t caretPosition)136 void JSTextInputController::CaretPosition(int32_t caretPosition)
137 {
138     auto controller = controllerWeak_.Upgrade();
139     if (controller) {
140         controller->CaretPosition(caretPosition);
141     }
142 }
143 
SetTextSelection(int32_t selectionStart,int32_t selectionEnd)144 void JSTextInputController::SetTextSelection(int32_t selectionStart, int32_t selectionEnd)
145 {
146     auto controller = controllerWeak_.Upgrade();
147     if (controller) {
148         controller->SetTextSelection(selectionStart, selectionEnd);
149     }
150 }
151 
CreateRectangle(const Rect & info)152 JSRef<JSObject> JSTextInputController::CreateRectangle(const Rect& info)
153 {
154     JSRef<JSObject> rectObj = JSRef<JSObject>::New();
155     rectObj->SetProperty<double>("x", info.Left());
156     rectObj->SetProperty<double>("y", info.Top());
157     rectObj->SetProperty<double>("width", info.Width());
158     rectObj->SetProperty<double>("height", info.Height());
159     return rectObj;
160 }
161 
GetTextContentRect(const JSCallbackInfo & info)162 void JSTextInputController::GetTextContentRect(const JSCallbackInfo& info)
163 {
164     auto controller = controllerWeak_.Upgrade();
165     if (controller) {
166         auto rectObj = CreateRectangle(controller->GetTextContentRect());
167         JSRef<JSVal> rect = JSRef<JSObject>::Cast(rectObj);
168         info.SetReturnValue(rect);
169     } else {
170         LOGE("GetTextContentRect: The JSTextInputController is NULL");
171     }
172 }
173 
GetTextContentLinesNum(const JSCallbackInfo & info)174 void JSTextInputController::GetTextContentLinesNum(const JSCallbackInfo& info)
175 {
176     auto controller = controllerWeak_.Upgrade();
177     if (controller) {
178         auto lines = controller->GetTextContentLinesNum();
179         auto linesNum = JSVal(ToJSValue(lines));
180         auto textLines = JSRef<JSVal>::Make(linesNum);
181         info.SetReturnValue(textLines);
182     } else {
183         LOGE("GetTextContentRect: The JSTextInputController is NULL");
184     }
185 }
186 
StopEditing()187 void JSTextInputController::StopEditing()
188 {
189     auto controller = controllerWeak_.Upgrade();
190     if (controller) {
191         controller->StopEditing();
192     }
193 }
194 } // namespace OHOS::Ace::Framework
195