@@ -729,6 +729,7 @@ export const Layout: React.FC<LayoutProps> = ({
729729 const panelViewStateHandlersRef = useRef ( new Map < string , PanelViewStateHandlers > ( ) ) ;
730730 const panelViewStatesRef = useRef < Record < string , unknown > > ( { } ) ;
731731 const restoreViewStatesFrameRef = useRef < number | null > ( null ) ;
732+ const lastLayoutSnapshotRef = useRef < string | null > ( null ) ;
732733 const splitRightRef = useRef < ( api : DockviewApi , referencePanelId : string ) => void > ( ( ) => { } ) ;
733734 const splitDownRef = useRef < ( api : DockviewApi , referencePanelId : string ) => void > ( ( ) => { } ) ;
734735 const addTabRef = useRef < ( api : DockviewApi , referencePanelId : string ) => void > ( ( ) => { } ) ;
@@ -750,6 +751,19 @@ export const Layout: React.FC<LayoutProps> = ({
750751 } ) )
751752 ) , [ visibility ] ) ;
752753
754+ const getLayoutSnapshot = useCallback ( ( api : DockviewApi ) : string => {
755+ const raw = api . toJSON ( ) ;
756+ const sanitized = {
757+ ...raw ,
758+ panels : Object . fromEntries (
759+ Object . entries ( raw . panels ) . map ( ( [ id , state ] ) => [ id , { ...state , params : { } } ] ) ,
760+ ) ,
761+ } ;
762+ delete sanitized . activeGroup ;
763+
764+ return JSON . stringify ( sanitized ) ;
765+ } , [ ] ) ;
766+
753767 useEffect ( ( ) => {
754768 saveItem ( 'filesPanelVisible' , visibility . files ) ;
755769 saveItem ( 'searchPanelVisible' , visibility . search ) ;
@@ -912,19 +926,17 @@ export const Layout: React.FC<LayoutProps> = ({
912926 if ( layoutSaveTimerRef . current !== null ) {
913927 clearTimeout ( layoutSaveTimerRef . current ) ;
914928 }
915- restorePanelViewStates ( ) ;
916929 layoutSaveTimerRef . current = window . setTimeout ( ( ) => {
917930 layoutSaveTimerRef . current = null ;
918- const raw = api . toJSON ( ) ;
919- const sanitized = {
920- ...raw ,
921- panels : Object . fromEntries (
922- Object . entries ( raw . panels ) . map ( ( [ id , state ] ) => [ id , { ...state , params : { } } ] ) ,
923- ) ,
924- } ;
925- storeLayoutState ( createLayoutState ( sanitized , getLayoutPanelId ) ) ;
931+ const snapshot = getLayoutSnapshot ( api ) ;
932+ if ( snapshot === lastLayoutSnapshotRef . current ) {
933+ return ;
934+ }
935+ lastLayoutSnapshotRef . current = snapshot ;
936+ const parsedSnapshot = JSON . parse ( snapshot ) as DockviewLayout ;
937+ storeLayoutState ( createLayoutState ( parsedSnapshot , getLayoutPanelId ) ) ;
926938 } , 120 ) ;
927- } , [ restorePanelViewStates ] ) ;
939+ } , [ getLayoutSnapshot ] ) ;
928940
929941 const syncPanels = useCallback ( ( api : DockviewApi ) => {
930942 for ( const panel of panelEntries ) {
@@ -970,13 +982,8 @@ export const Layout: React.FC<LayoutProps> = ({
970982
971983 const syncToolbarSize = useCallback ( ( api : DockviewApi ) => {
972984 const toolbarPanel = api . getPanel ( 'toolbar' ) ;
973- if ( ! toolbarPanel ) {
974- return ;
975- }
976-
977- toolbarPanel . api . setSize ( {
978- height : 44 ,
979- } ) ;
985+ if ( ! toolbarPanel ) return ;
986+ toolbarPanel . api . setSize ( { height : 44 } ) ;
980987 } , [ ] ) ;
981988
982989 const refreshPanelContents = useCallback ( ( api : DockviewApi ) => {
@@ -1189,6 +1196,10 @@ export const Layout: React.FC<LayoutProps> = ({
11891196 const hasRemainingPanels = getPanelsByBaseId ( api , baseId ) . length > 0 ;
11901197 setVisibility ( ( prev ) => ( { ...prev , [ baseId ] : hasRemainingPanels } ) ) ;
11911198 }
1199+
1200+ // dockview resets scroll positions for panels,
1201+ // here is a workaround to restore them
1202+ restorePanelViewStates ( ) ;
11921203
11931204 if ( api . totalPanels === 0 && emptyPaneRestoreTimerRef . current === null ) {
11941205 emptyPaneRestoreTimerRef . current = window . setTimeout ( ( ) => {
@@ -1209,6 +1220,9 @@ export const Layout: React.FC<LayoutProps> = ({
12091220 if ( isRestoringLayoutRef . current ) {
12101221 return ;
12111222 }
1223+ // const layoutSnapshot = getLayoutSnapshot(api);
1224+ // const layoutChanged = layoutSnapshot !== lastLayoutSnapshotRef.current;
1225+ // if (!layoutChanged) return;
12121226 queueSaveLayout ( api ) ;
12131227 } ) ,
12141228 api . onWillDragPanel ( ( ) => {
@@ -1256,6 +1270,9 @@ export const Layout: React.FC<LayoutProps> = ({
12561270 api . getPanel ( 'files' ) ?. api . setActive ( ) ;
12571271 api . getPanel ( 'editor' ) ?. api . setActive ( ) ;
12581272 }
1273+
1274+ lastLayoutSnapshotRef . current = getLayoutSnapshot ( api ) ;
1275+
12591276 syncToolbarSize ( api ) ;
12601277 queueSaveLayout ( api ) ;
12611278 } , [
@@ -1271,6 +1288,7 @@ export const Layout: React.FC<LayoutProps> = ({
12711288 rebindPickerPanels ,
12721289 syncPanels ,
12731290 syncToolbarSize ,
1291+ getLayoutSnapshot ,
12741292 ] ) ;
12751293
12761294 return (
0 commit comments