1 /*
2 * Copyright (c) 20212022 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_textfield.h"
26 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h"
27 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
28 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
29
30 namespace OHOS::Ace::Framework {
31
JSBind(BindingTarget globalObj)32 void JSTextArea::JSBind(BindingTarget globalObj)
33 {
34 JSClass<JSTextArea>::Declare("TextArea");
35 MethodOptions opt = MethodOptions::NONE;
36 JSClass<JSTextArea>::StaticMethod("create", &JSTextArea::Create, opt);
37 JSClass<JSTextArea>::StaticMethod("placeholderColor", &JSTextField::SetPlaceholderColor);
38 JSClass<JSTextArea>::StaticMethod("placeholderFont", &JSTextField::SetPlaceholderFont);
39 JSClass<JSTextArea>::StaticMethod("backgroundColor", &JSTextField::SetBackgroundColor);
40 JSClass<JSTextArea>::StaticMethod("textAlign", &JSTextField::SetTextAlign);
41 JSClass<JSTextArea>::StaticMethod("caretColor", &JSTextField::SetCaretColor);
42 JSClass<JSTextArea>::StaticMethod("height", &JSTextField::JsHeight);
43 JSClass<JSTextArea>::StaticMethod("padding", &JSTextField::JsPadding);
44 JSClass<JSTextArea>::StaticMethod("border", &JSTextField::JsBorder);
45 JSClass<JSTextArea>::StaticMethod("borderWidth", &JSTextField::JsBorderWidth);
46 JSClass<JSTextArea>::StaticMethod("borderColor", &JSTextField::JsBorderColor);
47 JSClass<JSTextArea>::StaticMethod("borderStyle", &JSTextField::JsBorderStyle);
48 JSClass<JSTextArea>::StaticMethod("borderRadius", &JSTextField::JsBorderRadius);
49 JSClass<JSTextArea>::StaticMethod("fontSize", &JSTextField::SetFontSize);
50 JSClass<JSTextArea>::StaticMethod("fontColor", &JSTextField::SetTextColor);
51 JSClass<JSTextArea>::StaticMethod("fontWeight", &JSTextField::SetFontWeight);
52 JSClass<JSTextArea>::StaticMethod("fontStyle", &JSTextField::SetFontStyle);
53 JSClass<JSTextArea>::StaticMethod("fontFamily", &JSTextField::SetFontFamily);
54 JSClass<JSTextArea>::StaticMethod("inputFilter", &JSTextField::SetInputFilter);
55 JSClass<JSTextArea>::StaticMethod("hoverEffect", &JSTextField::JsHoverEffect);
56 JSClass<JSTextArea>::StaticMethod("maxLength", &JSTextField::SetMaxLength);
57 JSClass<JSTextArea>::StaticMethod("showCounter", &JSTextField::SetShowCounter);
58 JSClass<JSTextArea>::StaticMethod("barState", &JSTextField::SetBarState);
59 JSClass<JSTextArea>::StaticMethod("maxLines", &JSTextField::SetMaxLines);
60 JSClass<JSTextArea>::StaticMethod("style", &JSTextField::SetInputStyle);
61 JSClass<JSTextArea>::StaticMethod("onChange", &JSTextField::SetOnChange);
62 JSClass<JSTextArea>::StaticMethod("onTextSelectionChange", &JSTextField::SetOnTextSelectionChange);
63 JSClass<JSTextArea>::StaticMethod("onContentScroll", &JSTextField::SetOnContentScroll);
64 JSClass<JSTextArea>::StaticMethod("onCopy", &JSTextField::SetOnCopy);
65 JSClass<JSTextArea>::StaticMethod("onCut", &JSTextField::SetOnCut);
66 JSClass<JSTextArea>::StaticMethod("onPaste", &JSTextField::SetOnPaste);
67 JSClass<JSTextArea>::StaticMethod("onClick", &JSTextField::SetOnClick);
68 JSClass<JSTextArea>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
69 JSClass<JSTextArea>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
70 JSClass<JSTextArea>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
71 JSClass<JSTextArea>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
72 JSClass<JSTextArea>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
73 JSClass<JSTextArea>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
74 JSClass<JSTextArea>::StaticMethod("onEditChange", &JSTextField::SetOnEditChanged);
75 JSClass<JSTextArea>::StaticMethod("copyOption", &JSTextField::SetCopyOption);
76 JSClass<JSTextArea>::StaticMethod("textMenuOptions", &JSTextField::JsMenuOptionsExtension);
77 JSClass<JSTextArea>::StaticMethod("foregroundColor", &JSTextField::SetForegroundColor);
78 JSClass<JSTextArea>::StaticMethod("enableKeyboardOnFocus", &JSTextField::SetEnableKeyboardOnFocus);
79 JSClass<JSTextArea>::StaticMethod("selectionMenuHidden", &JSTextField::SetSelectionMenuHidden);
80 JSClass<JSTextArea>::StaticMethod("customKeyboard", &JSTextField::SetCustomKeyboard);
81 JSClass<JSTextArea>::InheritAndBind<JSViewAbstract>(globalObj);
82 }
83
Create(const JSCallbackInfo & info)84 void JSTextArea::Create(const JSCallbackInfo& info)
85 {
86 JSTextField::CreateTextArea(info);
87 }
88
JSBind(BindingTarget globalObj)89 void JSTextAreaController::JSBind(BindingTarget globalObj)
90 {
91 JSClass<JSTextAreaController>::Declare("TextAreaController");
92 JSClass<JSTextAreaController>::Method("caretPosition", &JSTextAreaController::CaretPosition);
93 JSClass<JSTextAreaController>::Method("setTextSelection", &JSTextAreaController::SetTextSelection);
94 JSClass<JSTextAreaController>::CustomMethod("getTextContentRect", &JSTextAreaController::GetTextContentRect);
95 JSClass<JSTextAreaController>::CustomMethod("getTextContentLineCount",
96 &JSTextAreaController::GetTextContentLinesNum);
97 JSClass<JSTextAreaController>::Method("stopEditing", &JSTextAreaController::StopEditing);
98 JSClass<JSTextAreaController>::Bind(globalObj, JSTextAreaController::Constructor, JSTextAreaController::Destructor);
99 }
100
Constructor(const JSCallbackInfo & args)101 void JSTextAreaController::Constructor(const JSCallbackInfo& args)
102 {
103 auto scroller = Referenced::MakeRefPtr<JSTextAreaController>();
104 scroller->IncRefCount();
105 args.SetReturnValue(Referenced::RawPtr(scroller));
106 }
107
Destructor(JSTextAreaController * scroller)108 void JSTextAreaController::Destructor(JSTextAreaController* scroller)
109 {
110 if (scroller != nullptr) {
111 scroller->DecRefCount();
112 }
113 }
114
CaretPosition(int32_t caretPosition)115 void JSTextAreaController::CaretPosition(int32_t caretPosition)
116 {
117 auto controller = controllerWeak_.Upgrade();
118 if (controller) {
119 controller->CaretPosition(caretPosition);
120 }
121 }
122
SetTextSelection(int32_t selectionStart,int32_t selectionEnd)123 void JSTextAreaController::SetTextSelection(int32_t selectionStart, int32_t selectionEnd)
124 {
125 auto controller = controllerWeak_.Upgrade();
126 if (controller) {
127 controller->SetTextSelection(selectionStart, selectionEnd);
128 }
129 }
130
CreateRectangle(const Rect & info)131 JSRef<JSObject> JSTextAreaController::CreateRectangle(const Rect& info)
132 {
133 JSRef<JSObject> rectObj = JSRef<JSObject>::New();
134 rectObj->SetProperty<double>("x", info.Left());
135 rectObj->SetProperty<double>("y", info.Top());
136 rectObj->SetProperty<double>("width", info.Width());
137 rectObj->SetProperty<double>("height", info.Height());
138 return rectObj;
139 }
140
GetTextContentRect(const JSCallbackInfo & info)141 void JSTextAreaController::GetTextContentRect(const JSCallbackInfo& info)
142 {
143 auto controller = controllerWeak_.Upgrade();
144 if (controller) {
145 auto rectObj = CreateRectangle(controller->GetTextContentRect());
146 JSRef<JSVal> rect = JSRef<JSObject>::Cast(rectObj);
147 info.SetReturnValue(rect);
148 } else {
149 LOGE("GetTextContentRect: The JSTextAreaController is NULL");
150 }
151 }
152
GetTextContentLinesNum(const JSCallbackInfo & info)153 void JSTextAreaController::GetTextContentLinesNum(const JSCallbackInfo& info)
154 {
155 auto controller = controllerWeak_.Upgrade();
156 if (controller) {
157 auto lines = controller->GetTextContentLinesNum();
158 auto linesNum = JSVal(ToJSValue(lines));
159 auto textLines = JSRef<JSVal>::Make(linesNum);
160 info.SetReturnValue(textLines);
161 } else {
162 LOGE("GetTextContentRect: The JSTextAreaController is NULL");
163 }
164 }
165
StopEditing()166 void JSTextAreaController::StopEditing()
167 {
168 auto controller = controllerWeak_.Upgrade();
169 if (controller) {
170 controller->StopEditing();
171 }
172 }
173 } // namespace OHOS::Ace::Framework
174