@rxdrag/website-lib-core 0.0.11 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/component-logic/modal.ts +11 -2
- package/src/controller/OpenableController.ts +5 -0
- package/src/controller/PageLoader.ts +1 -1
- package/src/entify/Entify.ts +56 -0
- package/src/entify/IEntify.ts +43 -3
- package/src/entify/lib/langFields.ts +2 -0
- package/src/entify/lib/newPageMetaOptions.ts +4 -6
- package/src/entify/lib/newQueryPageOptions.ts +14 -0
- package/src/entify/lib/queryAllProducts.ts +5 -0
- package/src/entify/lib/queryFeaturedProducts.ts +5 -0
- package/src/entify/lib/queryLangs.ts +8 -21
- package/src/entify/lib/queryLatestPosts.ts +6 -1
- package/src/entify/lib/queryOnePostBySlug.ts +27 -10
- package/src/entify/lib/queryOnePostCategoryBySlug.ts +5 -0
- package/src/entify/lib/queryOneProductBySlug.ts +13 -3
- package/src/entify/lib/queryOneProductCategoryBySlug.ts +24 -5
- package/src/entify/lib/queryOneTheme.ts +2 -1
- package/src/entify/lib/queryPageBySlug.ts +43 -0
- package/src/entify/lib/queryPageByType.ts +44 -0
- package/src/entify/lib/queryPostCategories.ts +7 -0
- package/src/entify/lib/queryPostSlugs.ts +5 -0
- package/src/entify/lib/queryPosts.ts +56 -39
- package/src/entify/lib/queryProductCategories.ts +7 -0
- package/src/entify/lib/queryProducts.ts +5 -0
- package/src/entify/lib/queryProductsInMenu.ts +14 -12
- package/src/entify/lib/queryUserPosts.ts +5 -0
- package/src/entify/lib/queryWebSiteSettings.ts +2 -3
- package/src/entify/lib/queryWebsite.ts +43 -0
- package/src/entify/lib/searchProducts.ts +7 -0
- package/src/entify/view-model/models.ts +0 -8
- package/src/react/components/ContactForm/index.tsx +6 -29
- package/src/react/components/Medias/index.tsx +270 -273
- package/src/react/components/ProductCard/ProductCta/index.tsx +1 -1
- package/src/react/components/RichTextOutline/index.tsx +4 -5
- package/src/react/components/RichTextOutline/useAcitviedHeading.ts +81 -54
|
@@ -1,54 +1,81 @@
|
|
|
1
|
-
import { useCallback, useEffect, useState } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
1
|
+
import { useCallback, useEffect, useState, useRef, useMemo } from "react";
|
|
2
|
+
import { throttle } from "lodash-es";
|
|
3
|
+
|
|
4
|
+
export function useAcitviedHeading(yOffset = 200) {
|
|
5
|
+
const [activeId, setActiveId] = useState<string | null>(null);
|
|
6
|
+
const anchorElementsRef = useRef<HTMLAnchorElement[]>([]);
|
|
7
|
+
const lastScrollTopRef = useRef(0);
|
|
8
|
+
const activeIdRef = useRef<string | null>(null);
|
|
9
|
+
|
|
10
|
+
// 同步 activeId 到 ref
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
activeIdRef.current = activeId;
|
|
13
|
+
}, [activeId]);
|
|
14
|
+
|
|
15
|
+
// 初始化时获取所有锚点元素
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
anchorElementsRef.current = Array.from(
|
|
18
|
+
document.querySelectorAll('a[href^="#"]')
|
|
19
|
+
);
|
|
20
|
+
}, []);
|
|
21
|
+
|
|
22
|
+
const handleScroll = useCallback(() => {
|
|
23
|
+
const scrollTop = window.scrollY;
|
|
24
|
+
lastScrollTopRef.current = scrollTop;
|
|
25
|
+
|
|
26
|
+
let closestId = null;
|
|
27
|
+
let closestDistance = Infinity;
|
|
28
|
+
|
|
29
|
+
anchorElementsRef.current.forEach((element) => {
|
|
30
|
+
const id = element.getAttribute("href")?.slice(1) || "";
|
|
31
|
+
const targetElement = document.getElementById(id);
|
|
32
|
+
|
|
33
|
+
if (targetElement) {
|
|
34
|
+
const { top } = targetElement.getBoundingClientRect();
|
|
35
|
+
const distance = Math.abs(top);
|
|
36
|
+
if (top <= yOffset && distance < closestDistance) {
|
|
37
|
+
closestId = id;
|
|
38
|
+
closestDistance = distance;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
if (activeIdRef.current !== closestId) {
|
|
44
|
+
setActiveId(closestId);
|
|
45
|
+
}
|
|
46
|
+
}, [yOffset]); // 移除 activeId 依赖,因为我们只需要比较当前值
|
|
47
|
+
|
|
48
|
+
// 使用节流函数包装handleScroll
|
|
49
|
+
const throttledHandleScroll = useMemo(
|
|
50
|
+
() => throttle(handleScroll, 100, { leading: true, trailing: true }),
|
|
51
|
+
[handleScroll]
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const handleHashChange = () => {
|
|
56
|
+
const hash = window.location.hash.substring(1);
|
|
57
|
+
setActiveId(hash);
|
|
58
|
+
|
|
59
|
+
const element = document.getElementById(hash);
|
|
60
|
+
if (element) {
|
|
61
|
+
const y =
|
|
62
|
+
element.getBoundingClientRect().top + window.scrollY - yOffset;
|
|
63
|
+
window.scrollTo({ top: y, behavior: "smooth" });
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
window.addEventListener("hashchange", handleHashChange);
|
|
68
|
+
window.addEventListener("scroll", throttledHandleScroll);
|
|
69
|
+
|
|
70
|
+
// 初始化时检查当前哈希值
|
|
71
|
+
handleHashChange();
|
|
72
|
+
|
|
73
|
+
return () => {
|
|
74
|
+
window.removeEventListener("hashchange", handleHashChange);
|
|
75
|
+
window.removeEventListener("scroll", throttledHandleScroll);
|
|
76
|
+
throttledHandleScroll.cancel(); // 清理节流函数
|
|
77
|
+
};
|
|
78
|
+
}, [handleScroll, throttledHandleScroll, yOffset]);
|
|
79
|
+
|
|
80
|
+
return activeId;
|
|
81
|
+
}
|