feat: maximize window to full viewport (maxViewport = bounds*zoom), clamp drag
This commit is contained in:
1 parent
4228357a35
commit
b2ae4c44d6
2 files changed
+43
-10
No files matched your search
+26
-2
@@ -51,9 +51,24 @@ export default function () {
|
||||
}
|
||||
}, 250)
|
||||
|
||||
// Кламп размера окна: не меньше минимума и не больше видимого viewport.
|
||||
// Видимый viewport в ЭКРАННЫХ пикселях = bounds * zoom (bounds — в canvas-space,
|
||||
// zoom переводит в px). useWindowResize при drag даёт большие значения — main
|
||||
// приводит к допустимому, чтобы окно не вылезало за canvas и не ломало Figma.
|
||||
function clampWindowSize (size: { width: number; height: number }) {
|
||||
const b = figma.viewport.bounds
|
||||
const zoom = figma.viewport.zoom || 1
|
||||
const maxW = Math.max(320, Math.round(b.width * zoom))
|
||||
const maxH = Math.max(200, Math.round(b.height * zoom))
|
||||
return {
|
||||
width: Math.max(320, Math.min(maxW, Math.round(size.width))),
|
||||
height: Math.max(200, Math.min(maxH, Math.round(size.height)))
|
||||
}
|
||||
}
|
||||
|
||||
// Ресайз окна плагина (drag за угол в UI -> useWindowResize -> RESIZE_WINDOW)
|
||||
on('RESIZE_WINDOW', function (windowSize: { width: number; height: number }) {
|
||||
const { width, height } = windowSize
|
||||
const { width, height } = clampWindowSize(windowSize)
|
||||
lastSize = { width, height }
|
||||
figma.ui.resize(width, height)
|
||||
// Если окно задокено — держим прижатым к краю при смене размера
|
||||
@@ -64,7 +79,8 @@ export default function () {
|
||||
|
||||
// Эмуляция докинга: прижать окно к краям canvas (left/right/center).
|
||||
on('DOCK_WINDOW', function (payload: { position: 'left' | 'right' | 'center'; width: number; height: number }) {
|
||||
const { position, width, height } = payload
|
||||
const { position } = payload
|
||||
const { width, height } = clampWindowSize(payload)
|
||||
lastSize = { width, height }
|
||||
figma.ui.resize(width, height)
|
||||
dockedPosition = position
|
||||
@@ -109,6 +125,13 @@ export default function () {
|
||||
const cx = Math.round(x + wCanvas / 2)
|
||||
const cy = Math.round(y + hCanvas / 2)
|
||||
|
||||
// Максимальный размер окна (экранные px) = весь видимый viewport (bounds * zoom).
|
||||
// Используется кнопкой "развернуть" в UI и как верхняя граница drag-ресайза.
|
||||
const maxViewport = {
|
||||
width: Math.max(320, Math.round(b.width * zoom)),
|
||||
height: Math.max(200, Math.round(b.height * zoom))
|
||||
}
|
||||
|
||||
emit('WINDOW_INFO', {
|
||||
x,
|
||||
y,
|
||||
@@ -121,6 +144,7 @@ export default function () {
|
||||
width: Math.round(b.width),
|
||||
height: Math.round(b.height)
|
||||
},
|
||||
maxViewport,
|
||||
gapL,
|
||||
gapR,
|
||||
gapT,
|
||||
|
||||
Reference in new issue
Block a user