1 /*
2 * Copyright (c) 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_text_editable_controller.h"
17
18 namespace OHOS::Ace::Framework {
19
JSBind(BindingTarget globalObj)20 void JSTextEditableController::JSBind(BindingTarget globalObj)
21 {
22 JSClass<JSTextEditableController>::Method("caretPosition", &JSTextEditableController::CaretPosition);
23 JSClass<JSTextEditableController>::CustomMethod("getCaretOffset", &JSTextEditableController::GetCaretOffset);
24 JSClass<JSTextEditableController>::Method("setTextSelection", &JSTextEditableController::SetTextSelection);
25 JSClass<JSTextEditableController>::CustomMethod(
26 "getTextContentRect", &JSTextEditableController::GetTextContentRect);
27 JSClass<JSTextEditableController>::CustomMethod(
28 "getTextContentLineCount", &JSTextEditableController::GetTextContentLinesNum);
29 JSClass<JSTextEditableController>::Method("stopEditing", &JSTextEditableController::StopEditing);
30 JSClass<JSTextEditableController>::Bind(
31 globalObj, JSTextEditableController::Constructor, JSTextEditableController::Destructor);
32 }
33
Constructor(const JSCallbackInfo & args)34 void JSTextEditableController::Constructor(const JSCallbackInfo& args)
35 {
36 auto controller = Referenced::MakeRefPtr<JSTextEditableController>();
37 controller->IncRefCount();
38 args.SetReturnValue(Referenced::RawPtr(controller));
39 }
40
Destructor(JSTextEditableController * controller)41 void JSTextEditableController::Destructor(JSTextEditableController* controller)
42 {
43 if (controller != nullptr) {
44 controller->DecRefCount();
45 }
46 }
47
CaretPosition(int32_t caretPosition)48 void JSTextEditableController::CaretPosition(int32_t caretPosition)
49 {
50 auto controller = controllerWeak_.Upgrade();
51 if (controller) {
52 controller->CaretPosition(caretPosition);
53 } else {
54 TAG_LOGI(AceLogTag::ACE_TEXT_FIELD, "CaretPosition: The JSTextEditableController is NULL");
55 }
56 }
57
SetTextSelection(int32_t selectionStart,int32_t selectionEnd)58 void JSTextEditableController::SetTextSelection(int32_t selectionStart, int32_t selectionEnd)
59 {
60 auto controller = controllerWeak_.Upgrade();
61 if (controller) {
62 controller->SetTextSelection(selectionStart, selectionEnd);
63 } else {
64 TAG_LOGI(AceLogTag::ACE_TEXT_FIELD, "SetTextSelection: The JSTextEditableController is NULL");
65 }
66 }
67
CreateRectangle(const Rect & info)68 JSRef<JSObject> JSTextEditableController::CreateRectangle(const Rect& info)
69 {
70 JSRef<JSObject> rectObj = JSRef<JSObject>::New();
71 rectObj->SetProperty<double>("x", info.Left());
72 rectObj->SetProperty<double>("y", info.Top());
73 rectObj->SetProperty<double>("width", info.Width());
74 rectObj->SetProperty<double>("height", info.Height());
75 return rectObj;
76 }
77
GetTextContentRect(const JSCallbackInfo & info)78 void JSTextEditableController::GetTextContentRect(const JSCallbackInfo& info)
79 {
80 auto controller = controllerWeak_.Upgrade();
81 if (controller) {
82 auto rectObj = CreateRectangle(controller->GetTextContentRect());
83 JSRef<JSVal> rect = JSRef<JSObject>::Cast(rectObj);
84 info.SetReturnValue(rect);
85 } else {
86 TAG_LOGI(AceLogTag::ACE_TEXT_FIELD, "GetTextContentRect: The JSTextEditableController is NULL");
87 }
88 }
89
GetTextContentLinesNum(const JSCallbackInfo & info)90 void JSTextEditableController::GetTextContentLinesNum(const JSCallbackInfo& info)
91 {
92 auto controller = controllerWeak_.Upgrade();
93 if (controller) {
94 auto lines = controller->GetTextContentLinesNum();
95 auto linesNum = JSVal(ToJSValue(lines));
96 auto textLines = JSRef<JSVal>::Make(linesNum);
97 info.SetReturnValue(textLines);
98 } else {
99 TAG_LOGI(AceLogTag::ACE_TEXT_FIELD, "GetTextContentLinesNum: The JSTextEditableController is NULL");
100 }
101 }
102
GetCaretOffset(const JSCallbackInfo & info)103 void JSTextEditableController::GetCaretOffset(const JSCallbackInfo& info)
104 {
105 auto controller = controllerWeak_.Upgrade();
106 if (controller) {
107 JSRef<JSObject> caretObj = JSRef<JSObject>::New();
108 NG::OffsetF caretOffset = controller->GetCaretPosition();
109 caretObj->SetProperty<int32_t>("index", controller->GetCaretIndex());
110 caretObj->SetProperty<float>("x", caretOffset.GetX());
111 caretObj->SetProperty<float>("y", caretOffset.GetY());
112 JSRef<JSVal> ret = JSRef<JSObject>::Cast(caretObj);
113 info.SetReturnValue(ret);
114 } else {
115 TAG_LOGI(AceLogTag::ACE_TEXT_FIELD, "GetCaretOffset: The JSTextEditableController is NULL");
116 }
117 }
118
StopEditing()119 void JSTextEditableController::StopEditing()
120 {
121 auto controller = controllerWeak_.Upgrade();
122 if (controller) {
123 controller->StopEditing();
124 } else {
125 TAG_LOGI(AceLogTag::ACE_TEXT_FIELD, "StopEditing: The JSTextEditableController is NULL");
126 }
127 }
128 } // namespace OHOS::Ace::Framework
129