index.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import React, { useCallback } from 'react';
  2. import { useNode } from './Node.hooks';
  3. import { withErrorBoundary } from 'react-error-boundary';
  4. import { ErrorBoundaryFallbackComponent } from '../_shared/ErrorBoundaryFallbackComponent';
  5. import { Node } from '@/appflowy_app/stores/reducers/document/slice';
  6. import TextBlock from '../TextBlock';
  7. import { TextDelta } from '@/appflowy_app/interfaces/document';
  8. function NodeComponent({ id, ...props }: { id: string } & React.HTMLAttributes<HTMLDivElement>) {
  9. const { node, childIds, delta, isSelected, ref } = useNode(id);
  10. console.log('=====', id);
  11. const renderBlock = useCallback((_props: { node: Node; childIds?: string[]; delta?: TextDelta[] }) => {
  12. switch (_props.node.type) {
  13. case 'text':
  14. if (!_props.delta) return null;
  15. return <TextBlock {..._props} delta={_props.delta} />;
  16. default:
  17. break;
  18. }
  19. }, []);
  20. if (!node) return null;
  21. return (
  22. <div {...props} ref={ref} data-block-id={node.id} className={`relative my-[2px] px-[2px] ${props.className}`}>
  23. {renderBlock({
  24. node,
  25. childIds,
  26. delta,
  27. })}
  28. <div className='block-overlay' />
  29. {isSelected ? <div className='pointer-events-none absolute inset-0 z-[-1] rounded-[4px] bg-[#E0F8FF]' /> : null}
  30. </div>
  31. );
  32. }
  33. const NodeWithErrorBoundary = withErrorBoundary(NodeComponent, {
  34. FallbackComponent: ErrorBoundaryFallbackComponent,
  35. });
  36. export default React.memo(NodeWithErrorBoundary);