@@ -10,6 +10,7 @@ import { useEffect, useState, useRef, useCallback } from "react";
1010import { useSocketStore } from "@src/stores/useSocketStore" ;
1111import { useToastStore } from "@src/stores/useToastStore" ;
1212import { Page } from "@src/types/page" ;
13+ import { PAGE , SIDE_BAR } from "@src/constants/size" ;
1314
1415const PAGE_OFFSET = 60 ;
1516
@@ -86,6 +87,42 @@ export const usePagesManage = (workspace: WorkSpace | null, clientId: number | n
8687 return Math . max ( 0 , ...pages . map ( ( page ) => page . zIndex ) ) + 1 ;
8788 } ;
8889
90+ const adjustPagePosition = ( page : Page ) => {
91+ const PADDING = 20 ;
92+ const maxWidth = window . innerWidth - SIDE_BAR . WIDTH - PADDING * 2 ;
93+ const maxHeight = window . innerHeight - PADDING * 2 ;
94+
95+ // 페이지가 최소 크기보다 작아지지 않도록 보장
96+ const width = PAGE . WIDTH ;
97+ const height = PAGE . HEIGHT ;
98+
99+ // 새로운 위치 계산
100+ let newX = page . x ;
101+ let newY = page . y ;
102+
103+ // 오른쪽 경계를 벗어나는 경우
104+ if ( newX + width > maxWidth ) {
105+ newX = Math . max ( 0 , maxWidth - width ) ;
106+ }
107+
108+ // 왼쪽 경계를 벗어나는 경우
109+ if ( newX < 0 ) {
110+ newX = 0 ;
111+ }
112+
113+ // 아래쪽 경계를 벗어나는 경우
114+ if ( newY + height > maxHeight ) {
115+ newY = Math . max ( 0 , maxHeight - height ) ;
116+ }
117+
118+ // 위쪽 경계를 벗어나는 경우
119+ if ( newY < 0 ) {
120+ newY = 0 ;
121+ }
122+
123+ return { x : newX , y : newY } ;
124+ } ;
125+
89126 const fetchPage = ( ) => {
90127 const operation = {
91128 type : "pageCreate" ,
@@ -170,11 +207,25 @@ export const usePagesManage = (workspace: WorkSpace | null, clientId: number | n
170207
171208 // 페이지를 활성화하고 표시
172209 setPages ( ( prevPages ) =>
173- prevPages . map ( ( p ) =>
174- p . id === pageId
175- ? { ...p , isActive : true , isVisible : true , zIndex : getZIndex ( ) }
176- : { ...p , isActive : false } ,
177- ) ,
210+ prevPages . map ( ( p ) => {
211+ if ( p . id === pageId ) {
212+ // isLoaded가 false일 때만 위치 재조정
213+ if ( ! p . isLoaded ) {
214+ const adjustedPosition = adjustPagePosition ( p ) ;
215+ return {
216+ ...p ,
217+ isActive : true ,
218+ isVisible : true ,
219+ zIndex : getZIndex ( ) ,
220+ x : adjustedPosition . x ,
221+ y : adjustedPosition . y ,
222+ } ;
223+ }
224+ // 이미 로드된 페이지는 위치 유지
225+ return { ...p , isActive : true , isVisible : true , zIndex : getZIndex ( ) } ;
226+ }
227+ return { ...p , isActive : false } ;
228+ } ) ,
178229 ) ;
179230
180231 setTimeout ( ( ) => {
0 commit comments