Skip to content
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

Spike: grid rulers #6508

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion editor/src/components/canvas/canvas-component-entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const CanvasComponentEntryInner = React.memo((props: CanvasComponentEntryProps)
clearRuntimeErrors()
}, [clearRuntimeErrors])

const containerRef = useApplyCanvasOffsetToStyle(true)
const containerRef = useApplyCanvasOffsetToStyle(true, 'xy')

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import { MetadataUtils } from '../../../../core/model/element-metadata-utils'
import * as EP from '../../../../core/shared/element-path'
import * as PP from '../../../../core/shared/property-path'
import { setProperty } from '../../commands/set-property-command'
import {
controlsForGridPlaceholders,
GridRowColumnResizingControls,
} from '../../controls/grid-controls'
import { controlsForGridPlaceholders } from '../../controls/grid-controls'
import type { CanvasStrategyFactory } from '../canvas-strategies'
import { onlyFitWhenDraggingThisControl } from '../canvas-strategies'
import type { InteractionCanvasState } from '../canvas-strategy-types'
Expand All @@ -37,6 +34,7 @@ import type { GridAutoOrTemplateBase } from '../../../../core/shared/element-tem
import { expandGridDimensions, replaceGridTemplateDimensionAtIndex } from './grid-helpers'
import { setCursorCommand } from '../../commands/set-cursor-command'
import { CSSCursor } from '../../canvas-types'
import { GridRowColumnResizingControls } from '../../controls/grid-controls-ruler'

export const resizeGridStrategy: CanvasStrategyFactory = (
canvasState: InteractionCanvasState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const setGridGapStrategy: CanvasStrategyFactory = (
},
key: 'grid-gap-resize-control',
show: 'visible-except-when-other-strategy-is-active',
priority: 'top',
})

const maybeIndicatorProps = gridGapValueIndicatorProps(interactionSession, gridGap)
Expand Down
23 changes: 18 additions & 5 deletions editor/src/components/canvas/controls/canvas-offset-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import { isFollowMode } from '../../editor/editor-modes'
import { liveblocksThrottle } from '../../../../liveblocks.config'

export const CanvasOffsetWrapper = React.memo(
(props: { children?: React.ReactNode; setScaleToo?: boolean }) => {
const elementRef = useApplyCanvasOffsetToStyle(props.setScaleToo ?? false)
(props: { children?: React.ReactNode; setScaleToo?: boolean; limitAxis?: 'x' | 'y' | 'xy' }) => {
const elementRef = useApplyCanvasOffsetToStyle(
props.setScaleToo ?? false,
props.limitAxis ?? 'xy',
)

return (
<div ref={elementRef} style={{ position: 'absolute' }}>
Expand All @@ -21,7 +24,10 @@ export const CanvasOffsetWrapper = React.memo(
},
)

export function useApplyCanvasOffsetToStyle(setScaleToo: boolean): React.RefObject<HTMLDivElement> {
export function useApplyCanvasOffsetToStyle(
setScaleToo: boolean,
limitAxis: 'x' | 'y' | 'xy',
): React.RefObject<HTMLDivElement> {
const elementRef = React.useRef<HTMLDivElement>(null)
const canvasOffsetRef = useRefEditorState((store) => store.editor.canvas.roundedCanvasOffset)
const scaleRef = useRefEditorState((store) => store.editor.canvas.scale)
Expand All @@ -37,11 +43,18 @@ export function useApplyCanvasOffsetToStyle(setScaleToo: boolean): React.RefObje

const applyCanvasOffset = React.useCallback(
(roundedCanvasOffset: CanvasVector) => {
const limitedCanvasOffset =
limitAxis === 'x'
? { x: roundedCanvasOffset.x, y: 0 }
: limitAxis === 'y'
? { x: 0, y: roundedCanvasOffset.y }
: roundedCanvasOffset

if (elementRef.current != null) {
elementRef.current.style.setProperty(
'transform',
(setScaleToo && scaleRef.current < 1 ? `scale(${scaleRef.current})` : '') +
` translate3d(${roundedCanvasOffset.x}px, ${roundedCanvasOffset.y}px, 0)`,
` translate3d(${limitedCanvasOffset.x}px, ${limitedCanvasOffset.y}px, 0)`,
)
elementRef.current.style.setProperty(
'zoom',
Expand All @@ -56,7 +69,7 @@ export function useApplyCanvasOffsetToStyle(setScaleToo: boolean): React.RefObje
}
}
},
[setScaleToo, scaleRef, isScrollAnimationActiveRef, mode],
[setScaleToo, scaleRef, isScrollAnimationActiveRef, mode, limitAxis],
)

useSelectorWithCallback(
Expand Down
Loading