에디터 UX: Undo/Redo 및 키보드 선택 이동 추가

This commit is contained in:
2025-11-18 13:43:25 +09:00
parent adee5c0891
commit e2201f528e
4 changed files with 197 additions and 2 deletions
+62 -1
View File
@@ -1,7 +1,7 @@
"use client";
import type { CSSProperties, ReactNode } from "react";
import { useState } from "react";
import { useEffect, useState } from "react";
import {
DndContext,
PointerSensor,
@@ -41,6 +41,8 @@ export default function EditorPage() {
const replaceBlocks = useEditorStore((state) => state.replaceBlocks);
const reorderBlocks = useEditorStore((state) => state.reorderBlocks);
const moveBlock = useEditorStore((state) => state.moveBlock);
const undo = useEditorStore((state) => state.undo);
const redo = useEditorStore((state) => state.redo);
const [editingBlockId, setEditingBlockId] = useState<string | null>(null);
const [editingText, setEditingText] = useState("");
@@ -197,6 +199,65 @@ export default function EditorPage() {
const rootBlocks = blocks.filter((block) => !block.sectionId);
// 에디터 전역에서 Undo/Redo 단축키(Cmd/Ctrl+Z, Cmd/Ctrl+Shift+Z)와
// ArrowUp/ArrowDown을 이용한 선택 블록 이동을 처리한다.
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
// 방향키로 선택된 블록을 위/아래로 이동 (순서 변경이 아니라 선택만 이동)
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
const { blocks: currentBlocks, selectedBlockId: currentSelectedId, selectBlock: storeSelectBlock } =
(useEditorStore as any).getState();
if (!currentBlocks.length) return;
const currentIndex = currentSelectedId
? currentBlocks.findIndex((b: Block) => b.id === currentSelectedId)
: -1;
let nextIndex = currentIndex;
if (event.key === "ArrowDown") {
nextIndex = currentIndex < currentBlocks.length - 1 ? currentIndex + 1 : currentIndex;
} else if (event.key === "ArrowUp") {
nextIndex = currentIndex > 0 ? currentIndex - 1 : currentIndex;
}
if (nextIndex !== -1 && nextIndex !== currentIndex) {
event.preventDefault();
const nextId = currentBlocks[nextIndex]?.id;
if (nextId) {
storeSelectBlock(nextId);
}
}
return;
}
const isMac = navigator.platform.toLowerCase().includes("mac");
const metaKey = isMac ? event.metaKey : event.ctrlKey;
if (!metaKey) return;
// Cmd/Ctrl + Shift + Z → Redo
if (event.key.toLowerCase() === "z" && event.shiftKey) {
event.preventDefault();
redo();
return;
}
// Cmd/Ctrl + Z → Undo
if (event.key.toLowerCase() === "z") {
event.preventDefault();
undo();
}
};
window.addEventListener("keydown", handleKeyDown);
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, [undo, redo]);
const renderBlocks = (targetBlocks: Block[]) =>
targetBlocks.map((block) => {
const isSelected = block.id === selectedBlockId;
+36
View File
@@ -51,6 +51,8 @@ export interface Block {
export interface EditorState {
blocks: Block[];
selectedBlockId: string | null;
history: Block[][];
future: Block[][];
addTextBlock: () => void;
addButtonBlock: () => void;
addImageBlock: () => void;
@@ -63,6 +65,8 @@ export interface EditorState {
replaceBlocks: (blocks: Block[]) => void;
reorderBlocks: (activeId: string, overId: string) => void;
moveBlock: (id: string, sectionId: string | null, columnId: string | null) => void;
undo: () => void;
redo: () => void;
}
// 간단한 ID 생성기 (추후 uuid 라이브러리로 교체 가능)
@@ -74,6 +78,8 @@ const createId = () => `blk_${Date.now()}_${idCounter++}`;
const createEditorState = (set: any, get: any): EditorState => ({
blocks: [],
selectedBlockId: null,
history: [],
future: [],
// 텍스트 블록 추가: 기본 텍스트와 함께 생성 후 선택 상태로 만든다
addTextBlock: () => {
@@ -112,6 +118,8 @@ const createEditorState = (set: any, get: any): EditorState => ({
set((state: EditorState) => ({
blocks: [...state.blocks, newBlock],
selectedBlockId: id,
history: [...state.history, blocks],
future: [],
}));
},
@@ -478,6 +486,34 @@ const createEditorState = (set: any, get: any): EditorState => ({
),
}));
},
undo: () => {
const { history, blocks } = get();
if (history.length === 0) return;
const previous = history[history.length - 1];
const nextHistory = history.slice(0, -1);
set((state: EditorState) => ({
blocks: previous,
selectedBlockId: previous.length > 0 ? previous[previous.length - 1].id : null,
history: nextHistory,
future: [...state.future, blocks],
}));
},
redo: () => {
const { future, blocks } = get();
if (future.length === 0) return;
const next = future[future.length - 1];
const nextFuture = future.slice(0, -1);
set((state: EditorState) => ({
blocks: next,
selectedBlockId: next.length > 0 ? next[next.length - 1].id : null,
history: [...state.history, blocks],
future: nextFuture,
}));
},
});
// React 컴포넌트에서 사용하는 전역 훅 스토어