InteractiveButton.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { Divider, Space } from "antd";
  2. import SuggestionButton from "./SuggestionButton";
  3. import DiscussionButton from "../../discussion/DiscussionButton";
  4. import { ISentence } from "../SentEdit";
  5. import { MouseEventHandler, useEffect, useState } from "react";
  6. interface IWidget {
  7. data: ISentence;
  8. compact?: boolean;
  9. float?: boolean;
  10. hideCount?: boolean;
  11. hideInZero?: boolean;
  12. onMouseEnter?: MouseEventHandler | undefined;
  13. onMouseLeave?: MouseEventHandler | undefined;
  14. }
  15. const InteractiveButton = ({
  16. data,
  17. compact = false,
  18. float = false,
  19. hideCount = false,
  20. hideInZero = false,
  21. onMouseEnter,
  22. onMouseLeave,
  23. }: IWidget) => {
  24. const [left, setLeft] = useState(0);
  25. const [width, setWidth] = useState(0);
  26. useEffect(() => {
  27. // 获取目标元素
  28. const targetNode = document.getElementsByClassName("article_shell")[0];
  29. const rect = targetNode.getBoundingClientRect();
  30. setLeft(rect.left);
  31. setWidth(rect.width);
  32. // 创建ResizeObserver实例并传入回调函数
  33. const resizeObserver = new ResizeObserver((entries) => {
  34. for (let entry of entries) {
  35. const { width, height } = entry.contentRect;
  36. //console.log(`Element size: ${width}px x ${height}px`);
  37. setWidth((origin) => width);
  38. }
  39. });
  40. // 观察目标节点
  41. resizeObserver.observe(targetNode);
  42. // 定义一个函数来处理窗口大小变化
  43. function handleResize() {
  44. // 获取窗口宽度
  45. const windowWidth = window.innerWidth;
  46. // 在控制台中输出新的窗口宽度
  47. //console.log(`新的窗口宽度是:${windowWidth}px`);
  48. // 假设你有一个div元素,它的id是"myDiv"
  49. const myDiv = document.getElementsByClassName("article_shell")[0];
  50. // 使用getBoundingClientRect()方法获取元素的位置和大小
  51. const rect = myDiv.getBoundingClientRect();
  52. // 从返回的对象中获取left属性
  53. const leftPosition = rect.left;
  54. // 输出left位置
  55. //console.log(`div的left位置是:${leftPosition}px`);
  56. setLeft((origin) => leftPosition);
  57. // 在这里,你可以根据窗口宽度来调整页面布局或样式
  58. }
  59. // 为resize事件添加监听器
  60. window.addEventListener("resize", handleResize);
  61. }, []);
  62. const ButtonInner = (
  63. <Space
  64. size={"small"}
  65. onMouseEnter={onMouseEnter}
  66. onMouseLeave={onMouseLeave}
  67. >
  68. <SuggestionButton
  69. data={data}
  70. hideCount={hideCount}
  71. hideInZero={hideInZero}
  72. />
  73. {compact ? undefined : <Divider type="vertical" />}
  74. <DiscussionButton
  75. hideCount={hideCount}
  76. hideInZero={hideInZero}
  77. initCount={data.suggestionCount?.discussion}
  78. resId={data.id}
  79. />
  80. </Space>
  81. );
  82. return float ? (
  83. <span
  84. className="sent_read_interactive_button"
  85. style={{ position: "absolute", left: left + width }}
  86. >
  87. {ButtonInner}
  88. </span>
  89. ) : (
  90. ButtonInner
  91. );
  92. };
  93. export default InteractiveButton;