From 19f96799df3f012cd59ca5e8254429777de87e1e Mon Sep 17 00:00:00 2001 From: Nikhil Mohite Date: Tue, 11 Oct 2022 10:53:46 +0530 Subject: [PATCH] Ensure that the scroll position should be preserved for the result set in the query tool on tab change. #5296 --- .../js/components/QueryToolComponent.jsx | 2 ++ .../js/components/QueryToolConstants.js | 2 ++ .../js/components/sections/ResultSet.jsx | 30 ++++++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/web/pgadmin/tools/sqleditor/static/js/components/QueryToolComponent.jsx b/web/pgadmin/tools/sqleditor/static/js/components/QueryToolComponent.jsx index 921e4bb94..92674eae0 100644 --- a/web/pgadmin/tools/sqleditor/static/js/components/QueryToolComponent.jsx +++ b/web/pgadmin/tools/sqleditor/static/js/components/QueryToolComponent.jsx @@ -333,6 +333,8 @@ export default function QueryToolComponent({params, pgWindow, pgAdmin, selectedN } else if(LayoutHelper.isTabVisible(docker.current, PANELS.HISTORY)) { LayoutHelper.focus(docker.current, PANELS.HISTORY); } + + eventBus.current.fireEvent(QUERY_TOOL_EVENTS.GOTO_LAST_SCROLL); } }); window.addEventListener('beforeunload', onBeforeUnload); diff --git a/web/pgadmin/tools/sqleditor/static/js/components/QueryToolConstants.js b/web/pgadmin/tools/sqleditor/static/js/components/QueryToolConstants.js index 4c9f6db7d..bca2a15b0 100644 --- a/web/pgadmin/tools/sqleditor/static/js/components/QueryToolConstants.js +++ b/web/pgadmin/tools/sqleditor/static/js/components/QueryToolConstants.js @@ -70,6 +70,8 @@ export const QUERY_TOOL_EVENTS = { RESET_LAYOUT: 'RESET_LAYOUT', FORCE_CLOSE_PANEL: 'FORCE_CLOSE_PANEL', RESET_GRAPH_VISUALISER: 'RESET_GRAPH_VISUALISER', + + GOTO_LAST_SCROLL: 'GOTO_LAST_SCROLL', }; export const CONNECTION_STATUS = { diff --git a/web/pgadmin/tools/sqleditor/static/js/components/sections/ResultSet.jsx b/web/pgadmin/tools/sqleditor/static/js/components/sections/ResultSet.jsx index 7374e2eeb..c014e88c1 100644 --- a/web/pgadmin/tools/sqleditor/static/js/components/sections/ResultSet.jsx +++ b/web/pgadmin/tools/sqleditor/static/js/components/sections/ResultSet.jsx @@ -760,6 +760,8 @@ export function ResultSet() { fireRowsColsCellChanged(); }; const [rowsResetKey, setRowsResetKey] = useState(0); + const lastScrollRef = useRef(null); + const isResettingScroll = useRef(true); rsu.current.setEventBus(eventBus); rsu.current.setQtPref(queryToolCtx.preferences?.sqleditor); @@ -908,6 +910,8 @@ export function ResultSet() { } }); eventBus.registerListener(QUERY_TOOL_EVENTS.TRIGGER_INCLUDE_EXCLUDE_FILTER, triggerFilter); + + eventBus.registerListener(QUERY_TOOL_EVENTS.GOTO_LAST_SCROLL, triggerResetScroll); }, []); useEffect(()=>{ @@ -1239,11 +1243,35 @@ export function ResultSet() { return ()=>eventBus.deregisterListener(QUERY_TOOL_EVENTS.TRIGGER_RENDER_GEOMETRIES, renderGeometries); }, [rows, columns, selectedRows.size, selectedColumns.size]); - const handleScroll = (e)=>{ + const handleScroll = (e) => { + // Set scroll current position of RestSet. + if (!_.isNull(e.currentTarget) && isResettingScroll.current) { + lastScrollRef.current = { + ref: { ...e }, + top: e.currentTarget.scrollTop, + left: e.currentTarget.scrollLeft + }; + } + if (isLoadingMore || !rsu.current.isAtBottom(e)) return; eventBus.fireEvent(QUERY_TOOL_EVENTS.FETCH_MORE_ROWS); }; + const triggerResetScroll = () => { + // Reset the scroll position to previously saved location. + if (lastScrollRef.current) { + isResettingScroll.current = false; + setTimeout(() => { + lastScrollRef.current.ref.currentTarget.scroll({ + top: lastScrollRef.current.top, + left: lastScrollRef.current.left, + }); + isResettingScroll.current = true; + }, 1); + } + }; + + const onRowsChange = (newRows, otherInfo)=>{ let row = newRows[otherInfo.indexes[0]]; let clientPK = rowKeyGetter(row);