블록 UX: 삭제/복제 버튼 및 단축키 추가
CI / test (push) Failing after 6m0s
CI / pr_and_merge (push) Has been skipped

This commit is contained in:
2025-11-18 16:28:46 +09:00
parent cf04c6e56c
commit 8494bd64bc
4 changed files with 207 additions and 1 deletions
+41 -1
View File
@@ -44,6 +44,8 @@ export default function EditorPage() {
const moveBlock = useEditorStore((state) => state.moveBlock);
const undo = useEditorStore((state) => state.undo);
const redo = useEditorStore((state) => state.redo);
const removeBlock = useEditorStore((state) => state.removeBlock);
const duplicateBlock = useEditorStore((state) => state.duplicateBlock);
const [editingBlockId, setEditingBlockId] = useState<string | null>(null);
const [editingText, setEditingText] = useState("");
@@ -200,7 +202,8 @@ export default function EditorPage() {
const rootBlocks = blocks.filter((block) => !block.sectionId);
// 에디터 전역에서 Undo/Redo 단축키(Cmd/Ctrl+Z, Cmd/Ctrl+Shift+Z)
// 에디터 전역에서 Undo/Redo 단축키(Cmd/Ctrl+Z, Cmd/Ctrl+Shift+Z),
// Delete/Backspace 기반 삭제, Cmd/Ctrl+D 기반 복제,
// ArrowUp/ArrowDown을 이용한 선택 블록 이동을 처리한다.
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
@@ -237,8 +240,29 @@ export default function EditorPage() {
const isMac = navigator.platform.toLowerCase().includes("mac");
const metaKey = isMac ? event.metaKey : event.ctrlKey;
// Delete / Backspace 로 선택된 블록 삭제
if (event.key === "Delete" || event.key === "Backspace") {
const { selectedBlockId: currentSelectedId } = (useEditorStore as any).getState();
if (currentSelectedId) {
event.preventDefault();
removeBlock(currentSelectedId);
}
return;
}
// Cmd/Ctrl 없는 경우에는 여기서 종료한다.
if (!metaKey) return;
// Cmd/Ctrl + D → 현재 선택된 블록 복제
if (event.key.toLowerCase() === "d") {
const { selectedBlockId: currentSelectedId } = (useEditorStore as any).getState();
if (currentSelectedId) {
event.preventDefault();
duplicateBlock(currentSelectedId);
}
return;
}
// Cmd/Ctrl + Shift + Z → Redo
if (event.key.toLowerCase() === "z" && event.shiftKey) {
event.preventDefault();
@@ -381,6 +405,22 @@ export default function EditorPage() {
<h2 className="font-medium mb-2"> </h2>
{selectedBlockId ? (
<div className="space-y-3">
<div className="flex gap-2 text-[11px]">
<button
type="button"
className="flex-1 rounded border border-slate-700 bg-slate-900 px-2 py-1 text-slate-100 hover:bg-red-900/60 hover:border-red-700"
onClick={() => removeBlock(selectedBlockId)}
>
</button>
<button
type="button"
className="flex-1 rounded border border-slate-700 bg-slate-900 px-2 py-1 text-slate-100 hover:bg-slate-800"
onClick={() => duplicateBlock(selectedBlockId)}
>
</button>
</div>
{(() => {
const selectedBlock = blocks.find((b) => b.id === selectedBlockId);
if (!selectedBlock) return null;
+59
View File
@@ -67,6 +67,8 @@ export interface EditorState {
moveBlock: (id: string, sectionId: string | null, columnId: string | null) => void;
undo: () => void;
redo: () => void;
removeBlock: (id: string) => void;
duplicateBlock: (id: string) => void;
}
// 간단한 ID 생성기 (추후 uuid 라이브러리로 교체 가능)
@@ -486,6 +488,63 @@ const createEditorState = (set: any, get: any): EditorState => ({
),
}));
},
removeBlock: (id) => {
set((state: EditorState) => {
const current = state.blocks;
const index = current.findIndex((b) => b.id === id);
if (index === -1) {
return state;
}
const nextBlocks = current.filter((b) => b.id !== id);
// 선택 상태는 삭제된 블록의 이전 블록 또는 다음 블록으로 이동한다.
let nextSelected: string | null = null;
if (nextBlocks.length > 0) {
const prevIndex = index - 1;
if (prevIndex >= 0) {
nextSelected = nextBlocks[prevIndex].id;
} else {
// 첫 블록이 삭제된 경우, 새 첫 블록을 선택한다.
nextSelected = nextBlocks[0].id;
}
}
return {
...state,
blocks: nextBlocks,
selectedBlockId: nextSelected,
};
});
},
duplicateBlock: (id) => {
set((state: EditorState) => {
const current = state.blocks;
const index = current.findIndex((b) => b.id === id);
if (index === -1) {
return state;
}
const original = current[index];
const newId = createId();
const cloned: Block = {
...original,
id: newId,
// props 는 얕은 복사로 충분 (현재 props 는 모두 평면 구조)
props: { ...(original.props as any) },
};
const nextBlocks = [...current];
nextBlocks.splice(index + 1, 0, cloned);
return {
...state,
blocks: nextBlocks,
selectedBlockId: newId,
};
});
},
undo: () => {
const { history, blocks } = get();
if (history.length === 0) return;