BlockSideTools.hooks.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { BlockType } from '@/appflowy_app/interfaces/document';
  2. import { useAppSelector } from '@/appflowy_app/stores/store';
  3. import { debounce } from '@/appflowy_app/utils/tool';
  4. import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
  5. import { YDocControllerContext } from '../../../stores/effects/document/document_controller';
  6. import { Node } from '@/appflowy_app/stores/reducers/document/slice';
  7. import { v4 } from 'uuid';
  8. export function useBlockSideTools({ container }: { container: HTMLDivElement }) {
  9. const [nodeId, setHoverNodeId] = useState<string>('');
  10. const ref = useRef<HTMLDivElement | null>(null);
  11. const nodes = useAppSelector((state) => state.document.nodes);
  12. const { insertAfter } = useController();
  13. const handleMouseMove = useCallback((e: MouseEvent) => {
  14. const { clientX, clientY } = e;
  15. const x = clientX;
  16. const y = clientY;
  17. const id = getNodeIdByPoint(x, y);
  18. if (!id) {
  19. setHoverNodeId('');
  20. } else {
  21. if ([BlockType.ColumnBlock].includes(nodes[id].type)) {
  22. setHoverNodeId('');
  23. return;
  24. }
  25. setHoverNodeId(id);
  26. }
  27. }, []);
  28. const debounceMove = useMemo(() => debounce(handleMouseMove, 30), [handleMouseMove]);
  29. useEffect(() => {
  30. const el = ref.current;
  31. if (!el || !nodeId) return;
  32. const node = nodes[nodeId];
  33. if (!node) {
  34. el.style.opacity = '0';
  35. el.style.zIndex = '-1';
  36. } else {
  37. el.style.opacity = '1';
  38. el.style.zIndex = '1';
  39. el.style.top = '1px';
  40. if (node?.type === BlockType.HeadingBlock) {
  41. if (node.data.style?.level === 1) {
  42. el.style.top = '8px';
  43. } else if (node.data.style?.level === 2) {
  44. el.style.top = '6px';
  45. } else {
  46. el.style.top = '5px';
  47. }
  48. }
  49. }
  50. }, [nodeId, nodes]);
  51. const handleAddClick = useCallback(() => {
  52. if (!nodeId) return;
  53. insertAfter(nodes[nodeId]);
  54. }, [nodeId, nodes]);
  55. useEffect(() => {
  56. container.addEventListener('mousemove', debounceMove);
  57. return () => {
  58. container.removeEventListener('mousemove', debounceMove);
  59. };
  60. }, [debounceMove]);
  61. return {
  62. nodeId,
  63. ref,
  64. handleAddClick,
  65. };
  66. }
  67. function useController() {
  68. const controller = useContext(YDocControllerContext);
  69. const insertAfter = useCallback((node: Node) => {
  70. const parentId = node.parent;
  71. if (!parentId || !controller) return;
  72. controller.transact([
  73. () => {
  74. const newNode = {
  75. id: v4(),
  76. delta: [],
  77. type: BlockType.TextBlock,
  78. };
  79. controller.insert(newNode, parentId, node.id);
  80. },
  81. ]);
  82. }, []);
  83. return {
  84. insertAfter,
  85. };
  86. }
  87. function getNodeIdByPoint(x: number, y: number) {
  88. const viewportNodes = document.querySelectorAll('[data-block-id]');
  89. let node: {
  90. el: Element;
  91. rect: DOMRect;
  92. } | null = null;
  93. viewportNodes.forEach((el) => {
  94. const rect = el.getBoundingClientRect();
  95. if (rect.x + rect.width - 1 >= x && rect.y + rect.height - 1 >= y && rect.y <= y) {
  96. if (!node || rect.y > node.rect.y) {
  97. node = {
  98. el,
  99. rect,
  100. };
  101. }
  102. }
  103. });
  104. return node
  105. ? (
  106. node as {
  107. el: Element;
  108. rect: DOMRect;
  109. }
  110. ).el.getAttribute('data-block-id')
  111. : null;
  112. }