Skip to content

Free, simple, and intuitive online database diagram editor and SQL generator.

Enhance pan and zoom (#1038)

HelblingPhuafcf94473519
diff --git a/src/components/EditorCanvas/Canvas.jsx b/src/components/EditorCanvas/Canvas.jsx
index f556daed..fb49f217 100644
--- a/src/components/EditorCanvas/Canvas.jsx
+++ b/src/components/EditorCanvas/Canvas.jsx
@@ -79,6 +79,7 @@ export default function Canvas() {
});
const { emitAwareness } = useCollab();
const lastLinkingRef = useRef(false);
+ const rightClickPanned = useRef(false);
useEffect(() => {
if (linking) {
@@ -455,6 +456,7 @@ export default function Canvas() {
const isMouseLeftButton = e.button === 0;
const isMouseMiddleButton = e.button === 1;
+ const isMouseRightButton = e.button === 2;
if (isMouseLeftButton) {
setBulkSelectRect({
@@ -470,7 +472,8 @@ export default function Canvas() {
handlePointerDownOnElement(e, elementPointerDown);
}
pointer.setStyle("crosshair");
- } else if (isMouseMiddleButton) {
+ } else if (isMouseMiddleButton || isMouseRightButton) {
+ if (isMouseRightButton) rightClickPanned.current = false;
setPanning({
isPanning: true,
panStart: transform.pan,
@@ -557,6 +560,7 @@ export default function Canvas() {
if (panning.isPanning && didPan()) {
setSaveState(State.SAVING);
+ if (e.button === 2) rightClickPanned.current = true;
}
setPanning((old) => ({ ...old, isPanning: false }));
pointer.setStyle("default");
@@ -670,9 +674,16 @@ export default function Canvas() {
(e) => {
e.preventDefault();
- if (e.ctrlKey || e.metaKey) {
- // How "eager" the viewport is to
- // center the cursor's coordinates
+ if (e.shiftKey) {
+ setTransform((prev) => ({
+ ...prev,
+ pan: {
+ ...prev.pan,
+ x: prev.pan.x + e.deltaY / prev.zoom,
+ },
+ }));
+ } else {
+ // Default and Ctrl/Meta: zoom centered on cursor
const eagernessFactor = 0.05;
setTransform((prev) => ({
pan: {
@@ -689,22 +700,6 @@ export default function Canvas() {
},
zoom: e.deltaY <= 0 ? prev.zoom * 1.05 : prev.zoom / 1.05,
}));
- } else if (e.shiftKey) {
- setTransform((prev) => ({
- ...prev,
- pan: {
- ...prev.pan,
- x: prev.pan.x + e.deltaY / prev.zoom,
- },
- }));
- } else {
- setTransform((prev) => ({
- ...prev,
- pan: {
- x: prev.pan.x + e.deltaX / prev.zoom,
- y: prev.pan.y + e.deltaY / prev.zoom,
- },
- }));
}
},
canvasRef,
@@ -726,6 +721,12 @@ export default function Canvas() {
onPointerMove={handlePointerMove}
onPointerDown={handlePointerDown}
onPointerUp={handlePointerUp}
+ onContextMenu={(e) => {
+ if (rightClickPanned.current) {
+ e.preventDefault();
+ rightClickPanned.current = false;
+ }
+ }}
className="absolute w-full h-full touch-none"
viewBox={`${viewBox.left} ${viewBox.top} ${viewBox.width} ${viewBox.height}`}
>