• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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/js_frontend/engine/v8/v8_list_bridge.h"
17 
18 #include "frameworks/bridge/common/dom/dom_list.h"
19 
20 namespace OHOS::Ace::Framework {
21 
JsGetCurrentOffset(v8::Isolate * isolate,NodeId nodeId)22 v8::Local<v8::Object> V8ListBridge::JsGetCurrentOffset(v8::Isolate* isolate, NodeId nodeId)
23 {
24     if (!isolate) {
25         return v8::Local<v8::Object>();
26     }
27     double offsetX = 0.0;
28     double offsetY = 0.0;
29     auto page = static_cast<RefPtr<JsAcePage>*>(isolate->GetData(V8EngineInstance::RUNNING_PAGE));
30     auto task = [nodeId, page, &offsetX, &offsetY]() {
31         auto domDoc = (*page)->GetDomDocument();
32         if (!domDoc) {
33             return;
34         }
35 
36         auto domList = AceType::DynamicCast<DOMList>(domDoc->GetDOMNodeById(nodeId));
37         if (!domList) {
38             return;
39         }
40         auto offset = domList->GetCurrentOffset();
41         offsetX = offset.GetX();
42         offsetY = offset.GetY();
43     };
44 
45     auto delegate = static_cast<RefPtr<FrontendDelegate>*>(isolate->GetData(V8EngineInstance::FRONTEND_DELEGATE));
46     (*delegate)->PostSyncTaskToPage(task);
47 
48     auto ctx = isolate->GetCurrentContext();
49     v8::Local<v8::Object> offsetContext = v8::Object::New(ctx->GetIsolate());
50     offsetContext
51         ->Set(ctx, v8::String::NewFromUtf8(ctx->GetIsolate(), "x").ToLocalChecked(),
52             v8::Number::New(ctx->GetIsolate(), offsetX))
53         .ToChecked();
54     offsetContext
55         ->Set(ctx, v8::String::NewFromUtf8(ctx->GetIsolate(), "y").ToLocalChecked(),
56             v8::Number::New(ctx->GetIsolate(), offsetY))
57         .ToChecked();
58     return offsetContext;
59 }
60 
61 } // namespace OHOS::Ace::Framework
62