-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: add canvas snap/alignment guidelines for frames #2762
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
+384
−1
Merged
Changes from all commits
Commits
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
67 changes: 67 additions & 0 deletions
67
apps/web/client/src/app/project/[id]/_components/canvas/overlay/elements/snap-guidelines.tsx
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,67 @@ | ||
'use client'; | ||
|
||
import { useEditorEngine } from '@/components/store/editor'; | ||
import { observer } from 'mobx-react-lite'; | ||
|
||
const SNAP_VISUAL_CONFIG = { | ||
TOP_BAR_HEIGHT: 28, | ||
TOP_BAR_MARGIN: 10, | ||
} as const; | ||
|
||
export const SnapGuidelines = observer(() => { | ||
const editorEngine = useEditorEngine(); | ||
const snapLines = editorEngine.snap.activeSnapLines; | ||
|
||
if (snapLines.length === 0) { | ||
return null; | ||
} | ||
|
||
const scale = editorEngine.canvas.scale; | ||
const canvasPosition = editorEngine.canvas.position; | ||
|
||
return ( | ||
<div | ||
className="absolute inset-0 pointer-events-none" | ||
style={{ | ||
transform: `translate(${canvasPosition.x}px, ${canvasPosition.y}px) scale(${scale})`, | ||
transformOrigin: '0 0', | ||
}} | ||
> | ||
{snapLines.map((line) => { | ||
if (line.orientation === 'horizontal') { | ||
const visualOffset = (SNAP_VISUAL_CONFIG.TOP_BAR_HEIGHT + SNAP_VISUAL_CONFIG.TOP_BAR_MARGIN) / scale; | ||
|
||
return ( | ||
<div | ||
key={line.id} | ||
className="absolute bg-red-500" | ||
style={{ | ||
left: `${line.start}px`, | ||
top: `${line.position + visualOffset}px`, | ||
width: `${line.end - line.start}px`, | ||
height: `${Math.max(1, 2 / scale)}px`, | ||
opacity: 0.9, | ||
boxShadow: '0 0 4px rgba(239, 68, 68, 0.6)', | ||
}} | ||
/> | ||
); | ||
} else { | ||
return ( | ||
<div | ||
key={line.id} | ||
className="absolute bg-red-500" | ||
style={{ | ||
left: `${line.position}px`, | ||
top: `${line.start}px`, | ||
width: `${Math.max(1, 2 / scale)}px`, | ||
height: `${line.end - line.start}px`, | ||
opacity: 0.9, | ||
boxShadow: '0 0 4px rgba(239, 68, 68, 0.6)', | ||
}} | ||
/> | ||
); | ||
} | ||
})} | ||
</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
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
240 changes: 240 additions & 0 deletions
240
apps/web/client/src/components/store/editor/snap/index.ts
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,240 @@ | ||
import type { RectDimension, RectPosition } from '@onlook/models'; | ||
import { makeAutoObservable } from 'mobx'; | ||
import type { EditorEngine } from '../engine'; | ||
import type { SnapBounds, SnapConfig, SnapFrame, SnapLine, SnapTarget } from './types'; | ||
import { SnapLineType } from './types'; | ||
|
||
const SNAP_CONFIG = { | ||
DEFAULT_THRESHOLD: 12, | ||
LINE_EXTENSION: 160, | ||
} as const; | ||
|
||
export class SnapManager { | ||
config: SnapConfig = { | ||
threshold: SNAP_CONFIG.DEFAULT_THRESHOLD, | ||
enabled: true, | ||
showGuidelines: true, | ||
}; | ||
|
||
activeSnapLines: SnapLine[] = []; | ||
|
||
constructor(private editorEngine: EditorEngine) { | ||
makeAutoObservable(this); | ||
} | ||
|
||
private createSnapBounds(position: RectPosition, dimension: RectDimension): SnapBounds { | ||
const left = position.x; | ||
const top = position.y; | ||
const right = position.x + dimension.width; | ||
const bottom = position.y + dimension.height; | ||
const centerX = position.x + dimension.width / 2; | ||
const centerY = position.y + dimension.height / 2; | ||
|
||
return { | ||
left, | ||
top, | ||
right, | ||
bottom, | ||
centerX, | ||
centerY, | ||
width: dimension.width, | ||
height: dimension.height, | ||
}; | ||
} | ||
|
||
private getSnapFrames(excludeFrameId?: string): SnapFrame[] { | ||
return this.editorEngine.frames.getAll() | ||
.filter(frameData => frameData.frame.id !== excludeFrameId) | ||
.map(frameData => { | ||
const frame = frameData.frame; | ||
return { | ||
id: frame.id, | ||
position: frame.position, | ||
dimension: frame.dimension, | ||
bounds: this.createSnapBounds(frame.position, frame.dimension), | ||
}; | ||
}); | ||
} | ||
|
||
calculateSnapTarget( | ||
dragFrameId: string, | ||
currentPosition: RectPosition, | ||
dimension: RectDimension, | ||
): SnapTarget | null { | ||
if (!this.config.enabled) { | ||
return null; | ||
} | ||
|
||
const dragBounds = this.createSnapBounds(currentPosition, dimension); | ||
const otherFrames = this.getSnapFrames(dragFrameId); | ||
|
||
if (otherFrames.length === 0) { | ||
return null; | ||
} | ||
|
||
const snapCandidates: Array<{ position: RectPosition; lines: SnapLine[]; distance: number }> = []; | ||
|
||
for (const otherFrame of otherFrames) { | ||
const candidates = this.calculateSnapCandidates(dragBounds, otherFrame); | ||
snapCandidates.push(...candidates); | ||
} | ||
|
||
if (snapCandidates.length === 0) { | ||
return null; | ||
} | ||
|
||
snapCandidates.sort((a, b) => a.distance - b.distance); | ||
Kitenite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const bestCandidate = snapCandidates[0]; | ||
|
||
if (!bestCandidate || bestCandidate.distance > this.config.threshold) { | ||
return null; | ||
} | ||
|
||
const firstLine = bestCandidate.lines[0]; | ||
if (!firstLine) { | ||
return null; | ||
} | ||
|
||
return { | ||
position: bestCandidate.position, | ||
snapLines: [firstLine], | ||
distance: bestCandidate.distance, | ||
}; | ||
} | ||
|
||
private calculateSnapCandidates( | ||
dragBounds: SnapBounds, | ||
otherFrame: SnapFrame, | ||
): Array<{ position: RectPosition; lines: SnapLine[]; distance: number }> { | ||
const candidates: Array<{ position: RectPosition; lines: SnapLine[]; distance: number }> = []; | ||
|
||
const edgeAlignments = [ | ||
{ | ||
type: SnapLineType.EDGE_LEFT, | ||
dragOffset: dragBounds.left, | ||
targetValue: otherFrame.bounds.left, | ||
orientation: 'vertical' as const, | ||
}, | ||
{ | ||
type: SnapLineType.EDGE_LEFT, | ||
dragOffset: dragBounds.right, | ||
targetValue: otherFrame.bounds.left, | ||
orientation: 'vertical' as const, | ||
}, | ||
{ | ||
type: SnapLineType.EDGE_RIGHT, | ||
dragOffset: dragBounds.left, | ||
targetValue: otherFrame.bounds.right, | ||
orientation: 'vertical' as const, | ||
}, | ||
{ | ||
type: SnapLineType.EDGE_RIGHT, | ||
dragOffset: dragBounds.right, | ||
targetValue: otherFrame.bounds.right, | ||
orientation: 'vertical' as const, | ||
}, | ||
{ | ||
type: SnapLineType.EDGE_TOP, | ||
dragOffset: dragBounds.top, | ||
targetValue: otherFrame.bounds.top, | ||
orientation: 'horizontal' as const, | ||
}, | ||
{ | ||
type: SnapLineType.EDGE_TOP, | ||
dragOffset: dragBounds.bottom, | ||
targetValue: otherFrame.bounds.top, | ||
orientation: 'horizontal' as const, | ||
}, | ||
{ | ||
type: SnapLineType.EDGE_BOTTOM, | ||
dragOffset: dragBounds.top, | ||
targetValue: otherFrame.bounds.bottom, | ||
orientation: 'horizontal' as const, | ||
}, | ||
{ | ||
type: SnapLineType.EDGE_BOTTOM, | ||
dragOffset: dragBounds.bottom, | ||
targetValue: otherFrame.bounds.bottom, | ||
orientation: 'horizontal' as const, | ||
}, | ||
{ | ||
type: SnapLineType.CENTER_HORIZONTAL, | ||
dragOffset: dragBounds.centerY, | ||
targetValue: otherFrame.bounds.centerY, | ||
orientation: 'horizontal' as const, | ||
}, | ||
{ | ||
type: SnapLineType.CENTER_VERTICAL, | ||
dragOffset: dragBounds.centerX, | ||
targetValue: otherFrame.bounds.centerX, | ||
orientation: 'vertical' as const, | ||
}, | ||
]; | ||
|
||
for (const alignment of edgeAlignments) { | ||
const distance = Math.abs(alignment.dragOffset - alignment.targetValue); | ||
|
||
if (distance <= this.config.threshold) { | ||
const offset = alignment.targetValue - alignment.dragOffset; | ||
const newPosition = alignment.orientation === 'horizontal' | ||
? { x: dragBounds.left, y: dragBounds.top + offset } | ||
: { x: dragBounds.left + offset, y: dragBounds.top }; | ||
|
||
const snapLine = this.createSnapLine(alignment.type, alignment.orientation, alignment.targetValue, otherFrame, dragBounds); | ||
|
||
|
||
candidates.push({ | ||
position: newPosition, | ||
lines: [snapLine], | ||
distance, | ||
}); | ||
} | ||
} | ||
|
||
return candidates; | ||
} | ||
|
||
private createSnapLine( | ||
type: SnapLineType, | ||
orientation: 'horizontal' | 'vertical', | ||
position: number, | ||
otherFrame: SnapFrame, | ||
dragBounds: SnapBounds, | ||
): SnapLine { | ||
let start: number; | ||
let end: number; | ||
|
||
if (orientation === 'horizontal') { | ||
start = Math.min(dragBounds.left, otherFrame.bounds.left) - SNAP_CONFIG.LINE_EXTENSION; | ||
end = Math.max(dragBounds.right, otherFrame.bounds.right) + SNAP_CONFIG.LINE_EXTENSION; | ||
} else { | ||
start = Math.min(dragBounds.top, otherFrame.bounds.top) - SNAP_CONFIG.LINE_EXTENSION; | ||
end = Math.max(dragBounds.bottom, otherFrame.bounds.bottom) + SNAP_CONFIG.LINE_EXTENSION; | ||
} | ||
|
||
return { | ||
id: `${type}-${otherFrame.id}-${Date.now()}`, | ||
type, | ||
orientation, | ||
position, | ||
start, | ||
end, | ||
frameIds: [otherFrame.id], | ||
}; | ||
} | ||
|
||
showSnapLines(lines: SnapLine[]): void { | ||
if (!this.config.showGuidelines) { | ||
return; | ||
} | ||
this.activeSnapLines = lines; | ||
} | ||
|
||
hideSnapLines(): void { | ||
this.activeSnapLines = []; | ||
} | ||
|
||
setConfig(config: Partial<SnapConfig>): void { | ||
Object.assign(this.config, config); | ||
} | ||
} |
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.