@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.
Files changed (36) hide show
  1. package/package.json +4 -4
  2. package/src/component-logic/modal.ts +11 -2
  3. package/src/controller/OpenableController.ts +5 -0
  4. package/src/controller/PageLoader.ts +1 -1
  5. package/src/entify/Entify.ts +56 -0
  6. package/src/entify/IEntify.ts +43 -3
  7. package/src/entify/lib/langFields.ts +2 -0
  8. package/src/entify/lib/newPageMetaOptions.ts +4 -6
  9. package/src/entify/lib/newQueryPageOptions.ts +14 -0
  10. package/src/entify/lib/queryAllProducts.ts +5 -0
  11. package/src/entify/lib/queryFeaturedProducts.ts +5 -0
  12. package/src/entify/lib/queryLangs.ts +8 -21
  13. package/src/entify/lib/queryLatestPosts.ts +6 -1
  14. package/src/entify/lib/queryOnePostBySlug.ts +27 -10
  15. package/src/entify/lib/queryOnePostCategoryBySlug.ts +5 -0
  16. package/src/entify/lib/queryOneProductBySlug.ts +13 -3
  17. package/src/entify/lib/queryOneProductCategoryBySlug.ts +24 -5
  18. package/src/entify/lib/queryOneTheme.ts +2 -1
  19. package/src/entify/lib/queryPageBySlug.ts +43 -0
  20. package/src/entify/lib/queryPageByType.ts +44 -0
  21. package/src/entify/lib/queryPostCategories.ts +7 -0
  22. package/src/entify/lib/queryPostSlugs.ts +5 -0
  23. package/src/entify/lib/queryPosts.ts +56 -39
  24. package/src/entify/lib/queryProductCategories.ts +7 -0
  25. package/src/entify/lib/queryProducts.ts +5 -0
  26. package/src/entify/lib/queryProductsInMenu.ts +14 -12
  27. package/src/entify/lib/queryUserPosts.ts +5 -0
  28. package/src/entify/lib/queryWebSiteSettings.ts +2 -3
  29. package/src/entify/lib/queryWebsite.ts +43 -0
  30. package/src/entify/lib/searchProducts.ts +7 -0
  31. package/src/entify/view-model/models.ts +0 -8
  32. package/src/react/components/ContactForm/index.tsx +6 -29
  33. package/src/react/components/Medias/index.tsx +270 -273
  34. package/src/react/components/ProductCard/ProductCta/index.tsx +1 -1
  35. package/src/react/components/RichTextOutline/index.tsx +4 -5
  36. package/src/react/components/RichTextOutline/useAcitviedHeading.ts +81 -54
@@ -1,54 +1,81 @@
1
- import { useCallback, useEffect, useState } from 'react';
2
-
3
- export function useAcitviedHeading() {
4
- const [activeId, setActiveId] = useState<string | null>(null);
5
-
6
- const handleScroll = useCallback(() => {
7
- const anchorElements = document.querySelectorAll('a[href^="#"]');
8
- let closestId = null;
9
- let closestDistance = Infinity;
10
-
11
- anchorElements.forEach((element) => {
12
- const id = element.getAttribute('href')?.slice(1) || '';
13
- const targetElement = document.getElementById(id);
14
- if (targetElement) {
15
- const { top } = targetElement.getBoundingClientRect();
16
- const distance = Math.abs(top);
17
- // 寻找最接近视口顶部但不超过视口顶部的锚点
18
- if (top <= 160 && distance < closestDistance) {
19
- closestId = id;
20
- closestDistance = distance;
21
- }
22
- }
23
- });
24
-
25
- setActiveId(closestId);
26
- }, []);
27
-
28
- useEffect(() => {
29
- const handleHashChange = () => {
30
- const hash = window.location.hash.substring(1); // 获取哈希值(去掉#)
31
- setActiveId(hash);
32
-
33
- const element = document.getElementById(hash);
34
- if (element) {
35
- const yOffset = -100; // 偏移量
36
- const y = element.getBoundingClientRect().top + window.scrollY - yOffset;
37
- window.scrollTo({ top: y, behavior: 'smooth' });
38
- }
39
- };
40
-
41
- window.addEventListener('hashchange', handleHashChange);
42
- window.addEventListener('scroll', handleScroll);
43
-
44
- // 初始化时检查当前哈希值
45
- handleHashChange();
46
-
47
- return () => {
48
- window.removeEventListener('hashchange', handleHashChange);
49
- window.removeEventListener('scroll', handleScroll);
50
- };
51
- }, [handleScroll]);
52
-
53
- return activeId;
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
+ }