Skip to content

Commit 153d3a0

Browse files
committed
[#115] refactor: draggable 영역을 컴포넌트에서 블록 핸들러로 변경
1 parent 1e03c89 commit 153d3a0

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

frontend/src/components/molecules/BlockComponent/BlockComponent.tsx

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/** @jsx jsx */
22
/** @jsxRuntime classic */
33
import { jsx, css } from '@emotion/react';
4-
import React from 'react';
5-
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
4+
import React, { useRef } from 'react';
5+
import { useRecoilState, useRecoilValue } from 'recoil';
66

77
import { BlockContent } from '@atoms/index';
88
import { BlockHandler, HoverArea } from '@components/molecules';
@@ -12,7 +12,6 @@ import {
1212
focusState,
1313
blockRefState,
1414
blockMapState,
15-
draggingBlockState,
1615
} from '@stores/page';
1716

1817
const isGridOrColumn = (block: Block): boolean =>
@@ -45,18 +44,10 @@ function BlockComponent({ blockDTO }: Props): JSX.Element {
4544
const [focusId, setFocusId] = useRecoilState<string>(focusState);
4645
const [hoverId, setHoverId] = useRecoilState(hoverState);
4746
const blockRef: any = blockRefState[blockDTO.id];
48-
const setDraggingBlock = useSetRecoilState(draggingBlockState);
49-
50-
const dragStartHandler = (event: React.DragEvent<HTMLDivElement>) => {
51-
event.dataTransfer.effectAllowed = 'move';
52-
event.dataTransfer.dropEffect = 'move';
53-
event.dataTransfer.setData('text/html', event.currentTarget.innerHTML);
54-
55-
setDraggingBlock(blockDTO);
56-
};
47+
const blockComponentRef = useRef(null);
5748

5849
return (
59-
<div css={blockCss} draggable="true" onDragStart={dragStartHandler}>
50+
<div css={blockCss} ref={blockComponentRef}>
6051
<div
6152
css={{ position: 'relative' }}
6253
onMouseEnter={() => setHoverId(blockDTO.id)}
@@ -67,7 +58,12 @@ function BlockComponent({ blockDTO }: Props): JSX.Element {
6758
>
6859
<BlockContent {...blockDTO} />
6960
<HoverArea clickHandler={() => blockRef.current.focus()} />
70-
{hoverId === blockDTO.id && <BlockHandler />}
61+
{hoverId === blockDTO.id && (
62+
<BlockHandler
63+
blockDTO={blockDTO}
64+
blockComponentRef={blockComponentRef}
65+
/>
66+
)}
7167
</div>
7268
{blockDTO.childIdList.length ? (
7369
<div css={descendantsCss(blockDTO)}>

frontend/src/components/molecules/BlockHandler/BlockHandler.tsx

+27-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import React from 'react';
55

66
import { ReactComponent as DraggableIcon } from '@assets/draggable.svg';
77
import { ReactComponent as PlusIcon } from '@assets/plus.svg';
8+
import { useSetRecoilState } from 'recoil';
9+
10+
import { draggingBlockState } from '@/stores';
11+
import { Block } from '@/schemes';
812

913
const buttonWrapperCss = () => css`
1014
display: flex;
@@ -27,11 +31,32 @@ const buttonCss = () => css`
2731
height: 16px;
2832
`;
2933

30-
function BlockHandler(): JSX.Element {
34+
interface Props {
35+
blockDTO: Block;
36+
blockComponentRef: any;
37+
}
38+
39+
function BlockHandler({ blockDTO, blockComponentRef }: Props): JSX.Element {
40+
const setDraggingBlock = useSetRecoilState(draggingBlockState);
41+
42+
const dragStartHandler = (event: React.DragEvent<HTMLDivElement>) => {
43+
event.dataTransfer.effectAllowed = 'move';
44+
event.dataTransfer.dropEffect = 'move';
45+
event.dataTransfer.setDragImage(blockComponentRef.current, 0, 0);
46+
47+
setDraggingBlock(blockDTO);
48+
};
49+
3150
return (
3251
<div css={buttonWrapperCss()}>
3352
<PlusIcon css={buttonCss()} />
34-
<div css={buttonCss()}>
53+
<div
54+
css={buttonCss()}
55+
draggable="true"
56+
onDragStart={dragStartHandler}
57+
onMouseEnter={() => setDraggingBlock(blockDTO)}
58+
onMouseLeave={() => setDraggingBlock(null)}
59+
>
3560
<DraggableIcon css={buttonCss()} />
3661
</div>
3762
</div>

frontend/src/components/molecules/HoverArea/HoverArea.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/** @jsxRuntime classic */
33
import { jsx, css } from '@emotion/react';
44
import { MouseEvent } from 'react';
5+
import { useRecoilValue } from 'recoil';
6+
import { draggingBlockState } from '@/stores';
57

68
const leftHoverAreaCss = css`
79
position: absolute;
@@ -30,9 +32,11 @@ interface Props {
3032
}
3133

3234
function HoverArea({ clickHandler }: Props): JSX.Element {
35+
const draggingBlock = useRecoilValue(draggingBlockState);
36+
3337
return (
3438
<div css={commonHoverAreaCss} onClick={clickHandler} onKeyDown={() => {}}>
35-
<div css={leftHoverAreaCss} />
39+
{!draggingBlock && <div css={leftHoverAreaCss} />}
3640
<div css={rightHoverAreaCss} />
3741
</div>
3842
);

0 commit comments

Comments
 (0)