• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "frameworks/bridge/declarative_frontend/jsview/js_textinput.h"
17 
18 #include <optional>
19 #include <string>
20 #include <vector>
21 
22 #include "core/common/container.h"
23 #include "frameworks/bridge/common/utils/utils.h"
24 #include "frameworks/bridge/declarative_frontend/engine/functions/js_clipboard_function.h"
25 #include "frameworks/bridge/declarative_frontend/engine/functions/js_function.h"
26 #include "frameworks/bridge/declarative_frontend/jsview/js_textfield.h"
27 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
28 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
29 #include "frameworks/core/common/ime/text_input_action.h"
30 #include "frameworks/core/common/ime/text_input_type.h"
31 #include "frameworks/core/components/text_field/text_field_component.h"
32 #include "frameworks/core/components/text_field/textfield_theme.h"
33 
34 namespace OHOS::Ace::Framework {
35 
JSBind(BindingTarget globalObj)36 void JSTextInput::JSBind(BindingTarget globalObj)
37 {
38     JSClass<JSTextInput>::Declare("TextInput");
39     MethodOptions opt = MethodOptions::NONE;
40     JSClass<JSTextInput>::StaticMethod("create", &JSTextInput::Create, opt);
41     JSClass<JSTextInput>::StaticMethod("type", &JSTextField::SetType);
42     JSClass<JSTextInput>::StaticMethod("placeholderColor", &JSTextField::SetPlaceholderColor);
43     JSClass<JSTextInput>::StaticMethod("placeholderFont", &JSTextField::SetPlaceholderFont);
44     JSClass<JSTextInput>::StaticMethod("backgroundColor", &JSTextField::SetBackgroundColor);
45     JSClass<JSTextInput>::StaticMethod("enterKeyType", &JSTextField::SetEnterKeyType);
46     JSClass<JSTextInput>::StaticMethod("caretColor", &JSTextField::SetCaretColor);
47     JSClass<JSTextInput>::StaticMethod("maxLength", &JSTextField::SetMaxLength);
48     JSClass<JSTextInput>::StaticMethod("width", &JSTextField::JsWidth);
49     JSClass<JSTextInput>::StaticMethod("height", &JSTextField::JsHeight);
50     JSClass<JSTextInput>::StaticMethod("padding", &JSTextField::JsPadding);
51     JSClass<JSTextInput>::StaticMethod("border", &JSTextField::JsBorder);
52     JSClass<JSTextInput>::StaticMethod("borderWidth", &JSTextField::JsBorderWidth);
53     JSClass<JSTextInput>::StaticMethod("borderColor", &JSTextField::JsBorderColor);
54     JSClass<JSTextInput>::StaticMethod("borderStyle", &JSTextField::JsBorderStyle);
55     JSClass<JSTextInput>::StaticMethod("borderRadius", &JSTextField::JsBorderRadius);
56     JSClass<JSTextInput>::StaticMethod("fontSize", &JSTextField::SetFontSize);
57     JSClass<JSTextInput>::StaticMethod("fontColor", &JSTextField::SetTextColor);
58     JSClass<JSTextInput>::StaticMethod("fontWeight", &JSTextField::SetFontWeight);
59     JSClass<JSTextInput>::StaticMethod("fontStyle", &JSTextField::SetFontStyle);
60     JSClass<JSTextInput>::StaticMethod("fontFamily", &JSTextField::SetFontFamily);
61     JSClass<JSTextInput>::StaticMethod("inputFilter", &JSTextField::SetInputFilter);
62     JSClass<JSTextInput>::StaticMethod("showPasswordIcon", &JSTextField::SetShowPasswordIcon);
63     JSClass<JSTextInput>::StaticMethod("textAlign", &JSTextField::SetTextAlign);
64     JSClass<JSTextInput>::StaticMethod("style", &JSTextField::SetInputStyle);
65     JSClass<JSTextInput>::StaticMethod("hoverEffect", &JSTextField::JsHoverEffect);
66     JSClass<JSTextInput>::StaticMethod("copyOption", &JSTextField::SetCopyOption);
67     // API7 onEditChanged deprecated
68     JSClass<JSTextInput>::StaticMethod("onEditChanged", &JSTextField::SetOnEditChanged);
69     JSClass<JSTextInput>::StaticMethod("onEditChange", &JSTextField::SetOnEditChanged);
70     JSClass<JSTextInput>::StaticMethod("onSubmit", &JSTextField::SetOnSubmit);
71     JSClass<JSTextInput>::StaticMethod("onChange", &JSTextField::SetOnChange);
72     JSClass<JSTextInput>::StaticMethod("onCopy", &JSTextField::SetOnCopy);
73     JSClass<JSTextInput>::StaticMethod("onCut", &JSTextField::SetOnCut);
74     JSClass<JSTextInput>::StaticMethod("onPaste", &JSTextField::SetOnPaste);
75     JSClass<JSTextInput>::StaticMethod("onClick", &JSTextField::SetOnClick);
76     JSClass<JSTextInput>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
77     JSClass<JSTextInput>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
78     JSClass<JSTextInput>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
79     JSClass<JSTextInput>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
80     JSClass<JSTextInput>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
81     JSClass<JSTextInput>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
82     JSClass<JSTextInput>::Inherit<JSViewAbstract>();
83     JSClass<JSTextInput>::Bind(globalObj);
84 }
85 
Create(const JSCallbackInfo & info)86 void JSTextInput::Create(const JSCallbackInfo& info)
87 {
88     JSTextField::CreateTextInput(info);
89 }
90 
JSBind(BindingTarget globalObj)91 void JSTextInputController::JSBind(BindingTarget globalObj)
92 {
93     JSClass<JSTextInputController>::Declare("TextInputController");
94     JSClass<JSTextInputController>::Method("caretPosition", &JSTextInputController::CaretPosition);
95     JSClass<JSTextInputController>::Bind(
96         globalObj, JSTextInputController::Constructor, JSTextInputController::Destructor);
97 }
98 
Constructor(const JSCallbackInfo & args)99 void JSTextInputController::Constructor(const JSCallbackInfo& args)
100 {
101     auto scroller = Referenced::MakeRefPtr<JSTextInputController>();
102     scroller->IncRefCount();
103     args.SetReturnValue(Referenced::RawPtr(scroller));
104 }
105 
Destructor(JSTextInputController * scroller)106 void JSTextInputController::Destructor(JSTextInputController* scroller)
107 {
108     if (scroller != nullptr) {
109         scroller->DecRefCount();
110     }
111 }
112 
CaretPosition(int32_t caretPosition)113 void JSTextInputController::CaretPosition(int32_t caretPosition)
114 {
115     auto controller = controllerWeak_.Upgrade();
116     if (controller) {
117         controller->CaretPosition(caretPosition);
118     }
119 }
120 
121 } // namespace OHOS::Ace::Framework