feat: Add BlockEditor component with UI for managing block styles, size, and a draggable panel.
This commit is contained in:
@@ -112,21 +112,41 @@ const BlockEditor: React.FC<BlockEditorProps> = ({ blockEditorRef, currentBlock,
|
||||
}
|
||||
};
|
||||
|
||||
const onPointerMove = (ev: PointerEvent) => {
|
||||
if (!draggingRef.current) return;
|
||||
const dx = ev.clientX - startXRef.current;
|
||||
const dy = ev.clientY - startYRef.current;
|
||||
const { width, height } = getPanelDimensions();
|
||||
const maxX = window.innerWidth - width - 8;
|
||||
const maxY = window.innerHeight - height - 8;
|
||||
let nx = startLeftRef.current + dx;
|
||||
let ny = startTopRef.current + dy;
|
||||
if (nx < 0) nx = 0;
|
||||
if (ny < 0) ny = 0;
|
||||
if (nx > maxX) nx = maxX;
|
||||
if (ny > maxY) ny = maxY;
|
||||
setPositionFromPixels(nx, ny);
|
||||
};
|
||||
|
||||
const onPointerUp = () => {
|
||||
draggingRef.current = false;
|
||||
window.removeEventListener("pointermove", onPointerMove);
|
||||
window.removeEventListener("pointerup", onPointerUp);
|
||||
};
|
||||
|
||||
window.addEventListener("resize", handleResize);
|
||||
return () => {
|
||||
window.removeEventListener("resize", handleResize);
|
||||
draggingRef.current = false;
|
||||
window.removeEventListener("pointermove", onPointerMove);
|
||||
window.removeEventListener("pointerup", onPointerUp);
|
||||
};
|
||||
}, [editorPosition, setEditorPosition]);
|
||||
|
||||
const startDrag = (ev: React.PointerEvent) => {
|
||||
if (ev.detail > 1) return;
|
||||
|
||||
ev.preventDefault();
|
||||
const panel = panelRef.current || (blockEditorRef && (blockEditorRef as any).current);
|
||||
if (!panel) return;
|
||||
|
||||
panel.setPointerCapture?.(ev.pointerId);
|
||||
|
||||
draggingRef.current = true;
|
||||
startXRef.current = ev.clientX;
|
||||
startYRef.current = ev.clientY;
|
||||
@@ -137,17 +157,15 @@ const BlockEditor: React.FC<BlockEditorProps> = ({ blockEditorRef, currentBlock,
|
||||
if (!draggingRef.current) return;
|
||||
const dx = e.clientX - startXRef.current;
|
||||
const dy = e.clientY - startYRef.current;
|
||||
|
||||
const { width, height } = getPanelDimensions();
|
||||
const maxX = window.innerWidth - width - 8;
|
||||
const maxY = window.innerHeight - height - 8;
|
||||
|
||||
let nx = startLeftRef.current + dx;
|
||||
let ny = startTopRef.current + dy;
|
||||
|
||||
nx = Math.max(0, Math.min(nx, maxX));
|
||||
ny = Math.max(0, Math.min(ny, maxY));
|
||||
|
||||
if (nx < 0) nx = 0;
|
||||
if (ny < 0) ny = 0;
|
||||
if (nx > maxX) nx = maxX;
|
||||
if (ny > maxY) ny = maxY;
|
||||
setPositionFromPixels(nx, ny);
|
||||
};
|
||||
|
||||
@@ -155,7 +173,9 @@ const BlockEditor: React.FC<BlockEditorProps> = ({ blockEditorRef, currentBlock,
|
||||
draggingRef.current = false;
|
||||
window.removeEventListener("pointermove", onPointerMove);
|
||||
window.removeEventListener("pointerup", onPointerUp);
|
||||
panel.releasePointerCapture?.(e.pointerId);
|
||||
try {
|
||||
(panel as Element).releasePointerCapture?.((e as any).pointerId);
|
||||
} catch (err) {}
|
||||
};
|
||||
|
||||
window.addEventListener("pointermove", onPointerMove);
|
||||
|
||||
Reference in New Issue
Block a user