Fixed terminal size detection issue in PSQL when opening multiple tabs rapidly.

pull/8897/head
Aditya Toshniwal 2025-06-25 14:58:46 +05:30
parent 536191cef6
commit f3a87de967
2 changed files with 50 additions and 44 deletions

View File

@ -224,14 +224,17 @@ export default class Psql {
<ModalProvider> <ModalProvider>
<NotifierProvider pgAdmin={pgAdmin} pgWindow={pgWindow} /> <NotifierProvider pgAdmin={pgAdmin} pgWindow={pgWindow} />
{ params.error ? { params.error ?
<ToolErrorView <ToolErrorView
error={params.error} error={params.error}
panelId={`${BROWSER_PANELS.PSQL_TOOL}_${params.trans_id}`} panelId={`${BROWSER_PANELS.PSQL_TOOL}_${params.trans_id}`}
panelDocker={panelDocker} panelDocker={panelDocker}
/> : /> :
<PsqlComponent <PsqlComponent
params={params} params={params}
pgAdmin={pgAdmin} /> pgAdmin={pgAdmin}
panelId={`${BROWSER_PANELS.PSQL_TOOL}_${params.trans_id}`}
panelDocker={panelDocker}
/>
} }
</ModalProvider> </ModalProvider>
</ApplicationStateProvider> </ApplicationStateProvider>

View File

@ -7,7 +7,7 @@
// //
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
import React, { useEffect, useCallback, useRef } from 'react'; import React, { useEffect } from 'react';
import { Box, styled, useTheme } from '@mui/material'; import { Box, styled, useTheme } from '@mui/material';
import url_for from 'sources/url_for'; import url_for from 'sources/url_for';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
@ -21,6 +21,7 @@ import { copyToClipboard } from '../../../../../static/js/clipboard';
import 'pgadmin.browser.keyboard'; import 'pgadmin.browser.keyboard';
import gettext from 'sources/gettext'; import gettext from 'sources/gettext';
import { useApplicationState } from '../../../../../settings/static/ApplicationStateProvider'; import { useApplicationState } from '../../../../../settings/static/ApplicationStateProvider';
import { LAYOUT_EVENTS } from '../../../../../static/js/helpers/Layout';
const Root = styled(Box)(()=>({ const Root = styled(Box)(()=>({
width: '100%', width: '100%',
@ -112,7 +113,7 @@ function psql_terminal_io(term, socket, platform, pgAdmin) {
term.onKey(function (ev) { term.onKey(function (ev) {
let key = ev.key; let key = ev.key;
/* /*
Using the Option/Alt key to type special characters (such as '\', '[', etc.) often does not register Using the Option/Alt key to type special characters (such as '\', '[', etc.) often does not register
the correct character in ev.key when using xterm.js. This is due to limitations in how browsers and the correct character in ev.key when using xterm.js. This is due to limitations in how browsers and
xterm.js handle modifier keys across platforms. xterm.js handle modifier keys across platforms.
@ -142,33 +143,13 @@ function psql_socket() {
}); });
} }
export default function PsqlComponent({ params, pgAdmin }) { export default function PsqlComponent({ params, pgAdmin, panelId, panelDocker }) {
const theme = useTheme(); const theme = useTheme();
const termRef = React.useRef(null); const termRef = React.useRef(null);
const containerRef = React.useRef(null); const containerRef = React.useRef(null);
const fitAddonRef = useRef(null);
const {saveToolData, isSaveToolDataEnabled} = useApplicationState(); const {saveToolData, isSaveToolDataEnabled} = useApplicationState();
const initializePsqlTool = useCallback((params)=>{ const setTheme = ()=>{
const term = new Terminal({
cursorBlink: true,
scrollback: 5000,
});
/* Addon for fitAddon, webLinkAddon, SearchAddon */
fitAddonRef.current = psql_Addon(term);
/* Open terminal */
term.open(containerRef.current);
/* Socket */
const socket = psql_socket();
psql_socket_io(socket, params.is_enable, params.sid, params.db, params.server_type, fitAddonRef.current, term, params.role);
psql_terminal_io(term, socket, params.platform, pgAdmin);
return [term, socket];
}, [params, pgAdmin]);;
const setTheme = useCallback(()=>{
if(termRef.current) { if(termRef.current) {
termRef.current.options.theme = { termRef.current.options.theme = {
background: theme.palette.background.default, background: theme.palette.background.default,
@ -178,36 +159,56 @@ export default function PsqlComponent({ params, pgAdmin }) {
selectionBackground: `${theme.otherVars.editor.selectionBg}`, selectionBackground: `${theme.otherVars.editor.selectionBg}`,
}; };
} }
}, [theme]); };
useEffect(()=>{ useEffect(()=>{
const [term, socket] = initializePsqlTool(params); // Initialize terminal
const term = new Terminal({
cursorBlink: true,
scrollback: 5000,
});
termRef.current = term; termRef.current = term;
setTheme(); setTheme();
const observer = new ResizeObserver((entries) => { /* Addon for fitAddon, webLinkAddon, SearchAddon */
for (let entry of entries) { const fitAddon = psql_Addon(term);
if (entry.contentRect.width > 0 && entry.contentRect.height > 0) {
fitAddonRef.current?.fit();
socket.emit('resize', { cols: term.cols, rows: term.rows});
term.focus();
observer.disconnect(); // Only do this once
}
}
});
if (containerRef.current) { /* Open terminal */
observer.observe(containerRef.current); term.open(containerRef.current);
}
/* Socket */
const socket = psql_socket();
psql_socket_io(socket, params.is_enable, params.sid, params.db, params.server_type, fitAddon, term, params.role);
psql_terminal_io(term, socket, params.platform, pgAdmin);
const setTerminalSize = ()=>{
// Set terminal size
fitAddon.fit();
setTimeout(function(){
socket.emit('resize', {'cols': term.cols, 'rows': term.rows});
}, 1000);
// Focus on terminal
termRef.current.focus();
};
setTerminalSize();
// Save tool data if enabled
if(isSaveToolDataEnabled('psql')){ if(isSaveToolDataEnabled('psql')){
saveToolData('psql', params, params.trans_id, null); saveToolData('psql', params, params.trans_id, null);
} }
const deregFocus = panelDocker.eventBus.registerListener(LAYOUT_EVENTS.ACTIVE, _.debounce((currentTabId)=>{
if(panelId == currentTabId) {
setTerminalSize();
}
}, 100));
return () => { return () => {
deregFocus();
term.dispose(); term.dispose();
socket.disconnect(); socket.disconnect();
observer.disconnect();
}; };
}, []); }, []);
@ -232,4 +233,6 @@ PsqlComponent.propTypes = {
trans_id: PropTypes.number trans_id: PropTypes.number
}), }),
pgAdmin: PropTypes.object.isRequired, pgAdmin: PropTypes.object.isRequired,
panelId: PropTypes.string,
panelDocker: PropTypes.object,
}; };