[Refactor] useAbsolutePosition composable (#2589)

This commit is contained in:
Chenlei Hu
2025-02-16 15:39:58 -05:00
committed by GitHub
parent 3f4d11c63a
commit d6a5deccd8
2 changed files with 65 additions and 36 deletions

View File

@@ -0,0 +1,48 @@
import type { Size, Vector2 } from '@comfyorg/litegraph'
import { CSSProperties, ref } from 'vue'
import { app } from '@/scripts/app'
import { useCanvasStore } from '@/stores/graphStore'
export interface PositionConfig {
/* The position of the element on litegraph canvas */
pos: Vector2
/* The size of the element on litegraph canvas */
size: Size
/* The scale factor of the canvas */
scale?: number
}
export function useAbsolutePosition() {
const canvasStore = useCanvasStore()
const style = ref<CSSProperties>({
position: 'fixed',
left: '0px',
top: '0px',
width: '0px',
height: '0px'
})
const updatePosition = (
config: PositionConfig,
extraStyle?: CSSProperties
) => {
const { pos, size, scale = canvasStore.canvas?.ds?.scale ?? 1 } = config
const [left, top] = app.canvasPosToClientPos(pos)
const [width, height] = size
style.value = {
...style.value,
left: `${left}px`,
top: `${top}px`,
width: `${width * scale}px`,
height: `${height * scale}px`,
...extraStyle
}
}
return {
style,
updatePosition
}
}