Repository navigation
Feat/align object to label #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ef6c828
feat(properties): align selection to label centre
u8array 3ff466b
fix(properties): mark align-button icons aria-hidden
u8array 6084cc0
refactor(canvas): expose align action via forwardRef instead of store…
u8array 732095e
refactor(properties): require canvasRef on PropertiesPanel
u8array 0e41272
refactor(properties): extract align icons to their own file
u8array ecb4334
fix(canvas): round align-to-label deltas to integer dots
u8array File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { useT } from "../../lib/useT"; | ||
| import type { AlignAxis } from "../../lib/alignment"; | ||
| import { CenterHIcon, CenterVIcon, CenterBothIcon } from "./AlignIcons"; | ||
|
|
||
| const BUTTON_CLS = | ||
| "p-1.5 rounded border border-border text-muted hover:text-text hover:bg-surface-2 transition-colors"; | ||
|
|
||
| /** | ||
| * Three-button row that centres the current selection on the label. Pure | ||
| * presentation: the caller supplies the imperative `onAlign` handler so the | ||
| * component stays decoupled from the canvas (which owns the live render | ||
| * bboxes). | ||
| */ | ||
| export function AlignButtons({ onAlign }: { onAlign: (axis: AlignAxis) => void }) { | ||
| const t = useT(); | ||
|
|
||
| const buttons: { axis: AlignAxis; title: string; Icon: typeof CenterHIcon }[] = [ | ||
| { axis: "h", title: t.properties.alignCenterH, Icon: CenterHIcon }, | ||
| { axis: "v", title: t.properties.alignCenterV, Icon: CenterVIcon }, | ||
| { axis: "both", title: t.properties.alignCenterBoth, Icon: CenterBothIcon }, | ||
| ]; | ||
|
|
||
| return ( | ||
| <div className="flex gap-1"> | ||
| {buttons.map(({ axis, title, Icon }) => ( | ||
| <button | ||
| key={axis} | ||
| type="button" | ||
| className={BUTTON_CLS} | ||
| title={title} | ||
| aria-label={title} | ||
| onClick={() => onAlign(axis)} | ||
| > | ||
| <Icon className="w-3.5 h-3.5" /> | ||
| </button> | ||
| ))} | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| interface IconProps { | ||
| className?: string; | ||
| } | ||
|
|
||
| /** Object centred on a vertical dashed line — represents horizontal centring. */ | ||
| export function CenterHIcon({ className }: IconProps) { | ||
| return ( | ||
| <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" aria-hidden="true" className={className}> | ||
| <line x1="8" y1="1.5" x2="8" y2="14.5" strokeDasharray="2 2" strokeWidth="1" /> | ||
| <rect x="3" y="5" width="10" height="6" strokeWidth="1" /> | ||
| </svg> | ||
| ); | ||
| } | ||
|
|
||
| /** Object centred on a horizontal dashed line — represents vertical centring. */ | ||
| export function CenterVIcon({ className }: IconProps) { | ||
| return ( | ||
| <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" aria-hidden="true" className={className}> | ||
| <line x1="1.5" y1="8" x2="14.5" y2="8" strokeDasharray="2 2" strokeWidth="1" /> | ||
| <rect x="5" y="3" width="6" height="10" strokeWidth="1" /> | ||
| </svg> | ||
| ); | ||
| } | ||
|
|
||
| /** Object on the cross-hair of two dashed lines — represents bi-axial centring. */ | ||
| export function CenterBothIcon({ className }: IconProps) { | ||
| return ( | ||
| <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" aria-hidden="true" className={className}> | ||
| <line x1="8" y1="1.5" x2="8" y2="14.5" strokeDasharray="2 2" strokeWidth="1" /> | ||
| <line x1="1.5" y1="8" x2="14.5" y2="8" strokeDasharray="2 2" strokeWidth="1" /> | ||
| <rect x="5" y="5" width="6" height="6" strokeWidth="1" /> | ||
| </svg> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { computeGroupCenterDelta } from "./alignment"; | ||
|
|
||
| const target = { x: 0, y: 0, width: 100, height: 60 }; | ||
|
|
||
| describe("computeGroupCenterDelta", () => { | ||
| it("returns zero delta for empty input", () => { | ||
| expect(computeGroupCenterDelta([], target, "both")).toEqual({ dx: 0, dy: 0 }); | ||
| }); | ||
|
|
||
| it("centres a single box on both axes", () => { | ||
| const box = { id: "a", x: 0, y: 0, width: 20, height: 10 }; | ||
| expect(computeGroupCenterDelta([box], target, "both")).toEqual({ | ||
| dx: 40, | ||
| dy: 25, | ||
| }); | ||
| }); | ||
|
|
||
| it("only shifts the requested axis", () => { | ||
| const box = { id: "a", x: 0, y: 0, width: 20, height: 10 }; | ||
| expect(computeGroupCenterDelta([box], target, "h")).toEqual({ dx: 40, dy: 0 }); | ||
| expect(computeGroupCenterDelta([box], target, "v")).toEqual({ dx: 0, dy: 25 }); | ||
| }); | ||
|
|
||
| it("centres the group bbox of a multi-select, preserving relative positions", () => { | ||
| // Two boxes at x=10..30 and x=50..60 → group bbox 10..60 (width 50) | ||
| // Centre target (width 100) → group should land at 25..75 → dx = +15 | ||
| const boxes = [ | ||
| { id: "a", x: 10, y: 5, width: 20, height: 10 }, | ||
| { id: "b", x: 50, y: 20, width: 10, height: 10 }, | ||
| ]; | ||
| const { dx } = computeGroupCenterDelta(boxes, target, "h"); | ||
| expect(dx).toBe(15); | ||
| }); | ||
|
|
||
| it("handles bboxes that already exceed the target (no clamp)", () => { | ||
| const box = { id: "a", x: -10, y: 0, width: 200, height: 10 }; | ||
| // Centre 200-wide box on 100-wide target → leftEdge target = -50, dx = -40 | ||
| expect(computeGroupCenterDelta([box], target, "h").dx).toBe(-40); | ||
| }); | ||
|
|
||
| it("respects target offset (target not anchored at origin)", () => { | ||
| const offsetTarget = { x: 100, y: 100, width: 100, height: 60 }; | ||
| const box = { id: "a", x: 100, y: 100, width: 20, height: 10 }; | ||
| expect(computeGroupCenterDelta([box], offsetTarget, "both")).toEqual({ | ||
| dx: 40, | ||
| dy: 25, | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.