8000 Feature: 드래그 & 스크롤 기능 추가 by guesung · Pull Request #150 · guesung/Web-Memo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feature: 드래그 & 스크롤 기능 추가 #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default function MemoGrid({ lng, memos, gridKey }: MemoGridProps) {
const [items, setItems] = useState(() => getItems(0, MEMO_UNIT));
const dragBoxRef = useRef<HTMLDivElement>(null);
const [dialogMemoId, setDialogMemoId] = useState<number | null>();
const rafRef = useRef<number>();

const [selectedMemoIds, setSelectedMemoIds] = useState<number[]>([]);

Expand Down Expand Up @@ -77,45 +78,45 @@ export default function MemoGrid({ lng, memos, gridKey }: MemoGridProps) {
const dragBox = dragBoxRef.current;
if (!dragBox) return;

const handleMouseMove = (mouseMoveEvent: globalThis.MouseEvent) => {
mouseMoveEvent.stopPropagation();
let lastMouseEvent: MouseEvent | null = null;

const updateDragBox = () => {
if (!lastMouseEvent || !dragBox) return;

const [dragEndX, dragEndY] = [mouseMoveEvent.clientX, mouseMoveEvent.clientY];
const [dragEndX, dragEndY] = [lastMouseEvent.clientX, lastMouseEvent.clientY];

// 스크롤
// const isNearBottom = window.innerHeight - dragEndY < SCROLL_INTERVAL;
// const isNearTop = dragEndY < SCROLL_INTERVAL;

// const isAtBottom = container.scrollTop + container.clientHeight >= container.scrollHeight;
// const isAtTop = container.scrollTop === 0;

// if (isNearBottom && !bottomTimeoutRef.current && !isAtBottom) {
// bottomTimeoutRef.current = setInterval(() => {
// if (container.scrollTop + container.clientHeight >= container.scrollHeight) {
// clearInterval(bottomTimeoutRef.current!);
// bottomTimeoutRef.current = null;
// return;
// }
// container.scrollBy({ top: SCROLL_UNIT, behavior: 'auto' });
// }, 50);
// } else if (!isNearBottom && bottomTimeoutRef.current) {
// clearInterval(bottomTimeoutRef.current);
// bottomTimeoutRef.current = null;
// }

// if (isNearTop && !topTimeoutRef.current && !isAtTop) {
// topTimeoutRef.current = setInterval(() => {
// if (container.scrollTop === 0) {
// clearInterval(topTimeoutRef.current!);
// topTimeoutRef.current = null;
// return;
// }
// container.scrollBy({ top: -SCROLL_UNIT, behavior: 'auto' });
// }, 50);
// } else if (!isNearTop && topTimeoutRef.current) {
// clearInterval(topTimeoutRef.current);
// topTimeoutRef.current = null;
// }
const isNearBottom = window.innerHeight - dragEndY < SCROLL_INTERVAL;
const isAtBottom = container.scrollTop + container.clientHeight >= container.scrollHeight;
if (isNearBottom && !bottomTimeoutRef.current && !isAtBottom) {
bottomTimeoutRef.current = setInterval(() => {
if (container.scrollTop + container.clientHeight >= container.scrollHeight) {
clearInterval(bottomTimeoutRef.current!);
bottomTimeoutRef.current = null;
return;
}
container.scrollBy({ top: SCROLL_UNIT, behavior: 'auto' });
}, 50);
} else if (!isNearBottom && bottomTimeoutRef.current) {
clearInterval(bottomTimeoutRef.current);
bottomTimeoutRef.current = null;
}

const isNearTop = dragEndY < SCROLL_INTERVAL;
const isAtTop = container.scrollTop === 0;
if (isNearTop && !topTimeoutRef.current && !isAtTop) {
topTimeoutRef.current = setInterval(() => {
if (container.scrollTop === 0) {
clearInterval(topTimeoutRef.current!);
topTimeoutRef.current = null;
return;
}
container.scrollBy({ top: -SCROLL_UNIT, behavior: 'auto' });
}, 50);
} else if (!isNearTop && topTimeoutRef.current) {
clearInterval(topTimeoutRef.current);
topTimeoutRef.current = null;
}

const containerScrolledY = dragStartY - (container.scrollTop - initialScrollTop);

Expand All @@ -141,10 +142,24 @@ export default function MemoGrid({ lng, memos, gridKey }: MemoGridProps) {
.map(element => Number(element.id));

setSelectedMemoIds(selectedIds);
rafRef.current = requestAnimationFrame(updateDragBox);
};

const handleMouseMove = (mouseMoveEvent: globalThis.MouseEvent) => {
mouseMoveEvent.stopPropagation();
lastMouseEvent = mouseMoveEvent;

if (!rafRef.current) {
rafRef.current = requestAnimationFrame(updateDragBox);
}
};

const handleMouseUp = () => {
document.body.removeEventListener('mousemove', handleMouseMove);
if (rafRef.current) {
cancelAnimationFrame(rafRef.current);
rafRef.current = undefined;
}

if (dragBoxRef.current) dragBoxRef.current.style.transform = '';

Expand Down Expand Up @@ -193,6 +208,14 @@ export default function MemoGrid({ lng, memos, gridKey }: MemoGridProps) {

useKeyboardBind({ key: 'Escape', callback: closeMemoOption });

useEffect(() => {
return () => {
if (rafRef.current) {
cancelAnimationFrame(rafRef.current);
}
};
}, []);

if (!memos) return <Loading />;

return (
Expand All @@ -214,6 +237,7 @@ export default function MemoGrid({ lng, memos, gridKey }: MemoGridProps) {
className="container h-screen"
placeholder={<Skeleton className="h-[300px] w-[300px]" />}
gap={16}
useRecycle={false}
align="center"
useResizeObserver
observeChildren
Expand Down Expand Up @@ -255,7 +279,6 @@ export default function MemoGrid({ lng, memos, gridKey }: MemoGridProps) {
),
)}
</MasonryInfiniteGrid>

{dialogMemoId && <MemoDialog lng={lng} memoId={dialogMemoId} setDialogMemoId={setDialogMemoId} />}
</div>
);
Expand Down
0