Skip to content

Commit

Permalink
Fix(region): add proper zoom for right region (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexamy authored Jul 11, 2024
1 parent f4e9f3e commit e78de25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
26 changes: 11 additions & 15 deletions src/Region.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,26 @@ export function Region(props: {
}) {
const move = createRegionMovement();
const [parent, setParent] = createSignal<HTMLElement>();
const [size, setSize] = createSignal({ width: 0, height: 0 });

const cursor = createMemo(() => {
if (move.active()) return "grabbing";
return "default";
});

onMount(updateSize);
createEffect(on(() => props.resetTrigger, move.resetView));

createEffect(() => {
onMount(() => {
function updateSize() {
const rect = parent()!.getBoundingClientRect();
move.setRect(rect);
}

const observer = new ResizeObserver(updateSize);
observer.observe(parent()!);
onCleanup(() => observer.disconnect());
});

function updateSize() {
const size = parent()!.getBoundingClientRect();
setSize(size);
}
updateSize();
});

function onMouseEnter() {
parent()?.focus({ preventScroll: true });
Expand All @@ -129,18 +129,14 @@ export function Region(props: {
>
<GridBackground
scale={move.scale()}
width={size().width}
height={size().height}
width={move.rect().width}
height={move.rect().height}
/>
<RegionContext.Provider value={move}>
<Toolbar data-testid={props.testId + "-toolbar"}>
{props.toolbar}
</Toolbar>
<Content
data-testid={props.testId + "-content"}
ref={move.setRef}
style={move.style()}
>
<Content data-testid={props.testId + "-content"} style={move.style()}>
{props.children}
</Content>
<Footer data-testid={props.testId + "-footer"}>
Expand Down
18 changes: 15 additions & 3 deletions src/hooks/createRegionMovement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import { v } from "@/lib/vector";
import { createMemo, createSignal, type JSX } from "solid-js";

export function createRegionMovement() {
const [ref, setRef] = createSignal<HTMLElement>();
const [active, setActive] = createSignal(false);
const [current, setCurrent] = createSignal({ x: 0, y: 0 });
const [rect, setRect] = createSignal({
left: 0,
top: 0,
width: 0,
height: 0,
});

const [translate, setTranslate] = createSignal({ x: 0, y: 0 });
const [origin, setOrigin] = createSignal({ x: 0, y: 0 });
const [scale, setScale] = createSignal(3);

const elementPosition = createMemo(() => {
return { x: rect().left, y: rect().top };
});

const style = createMemo(() => {
const move = `translate(${translate().x}px, ${translate().y}px)`;
const zoom = `scale(${scale()})`;
Expand Down Expand Up @@ -50,7 +59,10 @@ export function createRegionMovement() {
event.preventDefault();
event.stopPropagation();

const mousePosition = { x: event.clientX, y: event.clientY };
const mousePosition = v.subtract(
{ x: event.clientX, y: event.clientY },
elementPosition()
);

const prevScale = scale();
const newScale = getScale(event);
Expand Down Expand Up @@ -96,7 +108,7 @@ export function createRegionMovement() {

// prettier-ignore
return {
setRef,
rect, setRect,
active,
current, origin,
translate, scale, style,
Expand Down

0 comments on commit e78de25

Please sign in to comment.