-
-
Notifications
You must be signed in to change notification settings - Fork 141
Feat/vipshop add intersection observer #698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guopf
wants to merge
8
commits into
main
Choose a base branch
from
feat/vipshop_add_intersection_observer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9b9a702
add IntersectionObserver;
96870df
fix: fix native intersection observer entry.
andycall 03cabce
优化IntersectionObserver,支持thresholds;
200aedf
Committing clang-format changes
17437c3
resolve specification;
98be7a8
Merge branch 'main' into feat/vipshop_add_intersection_observer
guopf 6c593e2
Committing clang-format changes
f5e85b5
test(intersectionobserver): add test case
ChrisCindy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// Copyright 2016 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Copyright (C) 2024-present The WebF authors. All rights reserved. | ||
|
||
#include "core/dom/intersection_observer.h" | ||
|
||
#include <algorithm> | ||
#include <limits> | ||
|
||
#include <native_value_converter.h> | ||
#include "bindings/qjs/converter_impl.h" | ||
#include "core/dom/element.h" | ||
#include "core/dom/intersection_observer_entry.h" | ||
#include "core/dom/node.h" | ||
#include "core/executing_context.h" | ||
#include "foundation/logging.h" | ||
#include "qjs_intersection_observer_init.h" | ||
|
||
namespace webf { | ||
|
||
IntersectionObserver* IntersectionObserver::Create(ExecutingContext* context, | ||
const std::shared_ptr<QJSFunction>& function, | ||
ExceptionState& exception_state) { | ||
return MakeGarbageCollected<IntersectionObserver>(context, function); | ||
} | ||
|
||
IntersectionObserver* IntersectionObserver::Create(ExecutingContext* context, | ||
const std::shared_ptr<QJSFunction>& function, | ||
const std::shared_ptr<IntersectionObserverInit>& observer_init, | ||
ExceptionState& exception_state) { | ||
return MakeGarbageCollected<IntersectionObserver>(context, function, observer_init); | ||
} | ||
|
||
IntersectionObserver::IntersectionObserver(ExecutingContext* context, const std::shared_ptr<QJSFunction>& function) | ||
: BindingObject(context->ctx()), function_(function) { | ||
GetExecutingContext()->dartMethodPtr()->createBindingObject( | ||
GetExecutingContext()->isDedicated(), GetExecutingContext()->contextId(), bindingObject(), | ||
CreateBindingObjectType::kCreateIntersectionObserver, nullptr, 0); | ||
} | ||
|
||
IntersectionObserver::IntersectionObserver(ExecutingContext* context, | ||
const std::shared_ptr<QJSFunction>& function, | ||
const std::shared_ptr<IntersectionObserverInit>& observer_init) | ||
: BindingObject(context->ctx()), function_(function) { | ||
if (observer_init && observer_init->hasRoot()) { | ||
root_ = observer_init->root(); | ||
} | ||
NativeValue arguments[1]; | ||
if (observer_init && observer_init->hasThreshold()) { | ||
#if ENABLE_LOG | ||
WEBF_LOG(DEBUG) << "[IntersectionObserver]: Constructor threshold.size = " << observer_init->threshold().size() | ||
<< std::endl; | ||
#endif | ||
thresholds_ = std::move(observer_init->threshold()); | ||
std::sort(thresholds_.begin(), thresholds_.end()); | ||
arguments[0] = NativeValueConverter<NativeTypeArray<NativeTypeDouble>>::ToNativeValue(thresholds_); | ||
} | ||
GetExecutingContext()->dartMethodPtr()->createBindingObject( | ||
GetExecutingContext()->isDedicated(), GetExecutingContext()->contextId(), bindingObject(), | ||
CreateBindingObjectType::kCreateIntersectionObserver, arguments, 1); | ||
} | ||
|
||
bool IntersectionObserver::RootIsValid() const { | ||
return RootIsImplicit() || root(); | ||
} | ||
|
||
void IntersectionObserver::observe(Element* target, ExceptionState& exception_state) { | ||
if (!RootIsValid() || !target) { | ||
WEBF_LOG(ERROR) << "[IntersectionObserver]: observe valid:" << std::endl; | ||
return; | ||
} | ||
|
||
#if ENABLE_LOG | ||
WEBF_LOG(DEBUG) << "[IntersectionObserver]: observe target=" << target << ",tagName=" << target->nodeName() | ||
<< std::endl; | ||
#endif | ||
GetExecutingContext()->uiCommandBuffer()->AddCommand(UICommand::kAddIntersectionObserver, nullptr, bindingObject(), | ||
target->bindingObject()); | ||
} | ||
|
||
void IntersectionObserver::unobserve(Element* target, ExceptionState& exception_state) { | ||
if (!target) { | ||
WEBF_LOG(ERROR) << "[IntersectionObserver]: unobserve valid:" << std::endl; | ||
return; | ||
} | ||
|
||
GetExecutingContext()->uiCommandBuffer()->AddCommand(UICommand::kRemoveIntersectionObserver, nullptr, bindingObject(), | ||
target->bindingObject()); | ||
} | ||
|
||
void IntersectionObserver::disconnect(ExceptionState& exception_state) { | ||
GetExecutingContext()->uiCommandBuffer()->AddCommand(UICommand::kDisconnectIntersectionObserver, nullptr, | ||
bindingObject(), nullptr); | ||
} | ||
|
||
// std::vector<Member<IntersectionObserverEntry>> IntersectionObserver::takeRecords(ExceptionState& exception_state) { | ||
// std::vector<Member<IntersectionObserverEntry>> entries; | ||
// for (auto& observation : observations_) | ||
// observation->TakeRecords(entries); | ||
// active_observations_.clear(); | ||
// return entries; | ||
// } | ||
|
||
// AtomicString IntersectionObserver::rootMargin() const { | ||
// return StringifyMargin(RootMargin()); | ||
// } | ||
|
||
// AtomicString IntersectionObserver::scrollMargin() const { | ||
// return StringifyMargin(ScrollMargin()); | ||
// } | ||
|
||
NativeValue IntersectionObserver::HandleCallFromDartSide(const AtomicString& method, | ||
int32_t argc, | ||
const NativeValue* argv, | ||
Dart_Handle dart_object) { | ||
if (!GetExecutingContext() || !GetExecutingContext()->IsContextValid()) { | ||
WEBF_LOG(ERROR) << "[IntersectionObserver]: HandleCallFromDartSide Context Valid" << std::endl; | ||
return Native_NewNull(); | ||
} | ||
|
||
MemberMutationScope scope{GetExecutingContext()}; | ||
|
||
NativeIntersectionObserverEntry* native_entry = | ||
NativeValueConverter<NativeTypePointer<NativeIntersectionObserverEntry>>::FromNativeValue(argv[0]); | ||
size_t length = NativeValueConverter<NativeTypeInt64>::FromNativeValue(argv[1]); | ||
|
||
if (length > 0) { | ||
assert(function_ != nullptr); | ||
JSValue js_array = JS_NewArray(ctx()); | ||
for (int i = 0; i < length; i++) { | ||
auto* entry = MakeGarbageCollected<IntersectionObserverEntry>( | ||
GetExecutingContext(), native_entry[i].is_intersecting, native_entry[i].intersectionRatio, | ||
DynamicTo<Element>(BindingObject::From(native_entry[i].target))); | ||
JS_SetPropertyUint32(ctx(), js_array, i, entry->ToQuickJS()); | ||
} | ||
ScriptValue arguments[] = {ScriptValue(ctx(), js_array), ToValue()}; | ||
|
||
#if ENABLE_LOG | ||
WEBF_LOG(DEBUG) << "[IntersectionObserver]: HandleCallFromDartSide length=" << length << ",JS function_ Invoke" | ||
<< std::endl; | ||
#endif | ||
|
||
function_->Invoke(ctx(), ToValue(), 2, arguments); | ||
|
||
JS_FreeValue(ctx(), js_array); | ||
} else { | ||
WEBF_LOG(ERROR) << "[IntersectionObserver]: HandleCallFromDartSide entries is empty"; | ||
} | ||
|
||
return Native_NewNull(); | ||
} | ||
|
||
void IntersectionObserver::Trace(GCVisitor* visitor) const { | ||
BindingObject::Trace(visitor); | ||
|
||
function_->Trace(visitor); | ||
} | ||
|
||
} // namespace webf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2016 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// https://www.w3.org/TR/intersection-observer/#intersection-observer-interface | ||
|
||
// Copyright (C) 2024-present The WebF authors. All rights reserved. | ||
|
||
import {IntersectionObserverInit} from "./intersection_observer_init"; | ||
import {IntersectionObserverEntry} from "./intersection_observer_entry"; | ||
import {Node} from "./node"; | ||
import {Element} from "./element"; | ||
|
||
interface IntersectionObserver { | ||
new(callback: Function, options?: IntersectionObserverInit): IntersectionObserver; | ||
|
||
//readonly root: Node | null; | ||
//readonly rootMargin: string; | ||
//readonly scrollMargin: string; | ||
//readonly thresholds: number[]; | ||
//readonly delay: number; | ||
//readonly trackVisibility: boolean; | ||
|
||
observe(target: Element): void; | ||
unobserve(target: Element): void; | ||
disconnect(): void; | ||
//takeRecords(): IntersectionObserverEntry[]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.