-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added feature to auto detect the OS end-of-line (EOL) character.
- Loading branch information
Rohit Bhati
committed
Sep 2, 2024
1 parent
f451f89
commit 632f11a
Showing
6 changed files
with
201 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
290 changes: 175 additions & 115 deletions
290
web/pgadmin/tools/sqleditor/static/js/components/sections/StatusBar.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,175 @@ | ||
|
||
///////////////////////////////////////////////////////////// | ||
// | ||
// pgAdmin 4 - PostgreSQL Tools | ||
// | ||
// Copyright (C) 2013 - 2024, The pgAdmin Development Team | ||
// This software is released under the PostgreSQL Licence | ||
// | ||
////////////////////////////////////////////////////////////// | ||
import React, { useEffect, useState, useContext } from 'react'; | ||
import { styled } from '@mui/material/styles'; | ||
import { Box } from '@mui/material'; | ||
import _ from 'lodash'; | ||
import { QUERY_TOOL_EVENTS } from '../QueryToolConstants'; | ||
import { useStopwatch } from '../../../../../../static/js/custom_hooks'; | ||
import { QueryToolEventsContext } from '../QueryToolComponent'; | ||
import gettext from 'sources/gettext'; | ||
|
||
|
||
const StyledBox = styled(Box)(({theme}) => ({ | ||
display: 'flex', | ||
alignItems: 'center', | ||
...theme.mixins.panelBorder.top, | ||
flexWrap: 'wrap', | ||
backgroundColor: theme.otherVars.editorToolbarBg, | ||
userSelect: 'text', | ||
'& .StatusBar-padding': { | ||
padding: '2px 12px', | ||
'& .StatusBar-mlAuto': { | ||
marginLeft: 'auto', | ||
}, | ||
'& .StatusBar-divider': { | ||
...theme.mixins.panelBorder.right, | ||
}, | ||
}, | ||
})); | ||
|
||
export function StatusBar() { | ||
|
||
const eventBus = useContext(QueryToolEventsContext); | ||
const [position, setPosition] = useState([1, 1]); | ||
const [lastTaskText, setLastTaskText] = useState(null); | ||
const [rowsCount, setRowsCount] = useState([0, 0]); | ||
const [selectedRowsCount, setSelectedRowsCount] = useState(0); | ||
const [dataRowChangeCounts, setDataRowChangeCounts] = useState({ | ||
isDirty: false, | ||
added: 0, | ||
updated: 0, | ||
deleted: 0, | ||
}); | ||
const {seconds, minutes, hours, msec, start:startTimer, pause:pauseTimer, reset:resetTimer} = useStopwatch({}); | ||
|
||
useEffect(()=>{ | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.CURSOR_ACTIVITY, (newPos)=>{ | ||
setPosition(newPos||[1, 1]); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.EXECUTION_END, ()=>{ | ||
pauseTimer(); | ||
setLastTaskText(gettext('Query complete')); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.TASK_START, (taskText, startTime)=>{ | ||
resetTimer(); | ||
startTimer(startTime); | ||
setLastTaskText(taskText); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.TASK_END, (taskText, endTime)=>{ | ||
pauseTimer(endTime); | ||
setLastTaskText(taskText); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.ROWS_FETCHED, (fetched, total)=>{ | ||
setRowsCount([fetched||0, total||0]); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.SELECTED_ROWS_COLS_CELL_CHANGED, (rows)=>{ | ||
setSelectedRowsCount(rows); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.DATAGRID_CHANGED, (_isDirty, dataChangeStore)=>{ | ||
setDataRowChangeCounts({ | ||
added: Object.keys(dataChangeStore.added||{}).length, | ||
updated: Object.keys(dataChangeStore.updated||{}).length, | ||
deleted: Object.keys(dataChangeStore.deleted||{}).length, | ||
}); | ||
}); | ||
}, []); | ||
|
||
let stagedText = ''; | ||
if(dataRowChangeCounts.added > 0) { | ||
stagedText += ' ' + gettext('Added: %s', dataRowChangeCounts.added); | ||
} | ||
if(dataRowChangeCounts.updated > 0) { | ||
stagedText += ' ' + gettext('Updated: %s', dataRowChangeCounts.updated); | ||
} | ||
if(dataRowChangeCounts.deleted > 0) { | ||
stagedText += ' ' + gettext('Deleted: %s', dataRowChangeCounts.deleted); | ||
} | ||
|
||
return ( | ||
<StyledBox> | ||
<Box className='StatusBar-padding StatusBar-divider'>{gettext('Total rows: %s of %s', rowsCount[0], rowsCount[1])}</Box> | ||
{lastTaskText && | ||
<Box className='StatusBar-padding StatusBar-divider'>{lastTaskText} {hours.toString().padStart(2, '0')}:{minutes.toString().padStart(2, '0')}:{seconds.toString().padStart(2, '0')}.{msec.toString().padStart(3, '0')}</Box> | ||
} | ||
{!lastTaskText && !_.isNull(lastTaskText) && | ||
<Box className='StatusBar-padding StatusBar-divider'>{lastTaskText} {hours.toString().padStart(2, '0')}:{minutes.toString().padStart(2, '0')}:{seconds.toString().padStart(2, '0')}.{msec.toString().padStart(3, '0')}</Box> | ||
} | ||
{Boolean(selectedRowsCount) && | ||
<Box className='StatusBar-padding StatusBar-divider'>{gettext('Rows selected: %s',selectedRowsCount)}</Box>} | ||
{stagedText && | ||
<Box className='StatusBar-padding StatusBar-divider'> | ||
<span>{gettext('Changes staged: %s', stagedText)}</span> | ||
</Box> | ||
} | ||
<Box className='StatusBar-padding StatusBar-mlAuto'>{gettext('Ln %s, Col %s', position[0], position[1])}</Box> | ||
</StyledBox> | ||
); | ||
} | ||
|
||
///////////////////////////////////////////////////////////// | ||
// | ||
// pgAdmin 4 - PostgreSQL Tools | ||
// | ||
// Copyright (C) 2013 - 2024, The pgAdmin Development Team | ||
// This software is released under the PostgreSQL Licence | ||
// | ||
////////////////////////////////////////////////////////////// | ||
import React, { useEffect, useState, useContext, useCallback } from 'react'; | ||
import { styled } from '@mui/material/styles'; | ||
import { Box, Tooltip } from '@mui/material'; | ||
import _ from 'lodash'; | ||
import { OS_EOL, QUERY_TOOL_EVENTS } from '../QueryToolConstants'; | ||
import { useStopwatch } from '../../../../../../static/js/custom_hooks'; | ||
import { QueryToolEventsContext } from '../QueryToolComponent'; | ||
import gettext from 'sources/gettext'; | ||
import { PgMenu, PgMenuItem } from '../../../../../../static/js/components/Menu'; | ||
|
||
|
||
const StyledBox = styled(Box)(({theme}) => ({ | ||
display: 'flex', | ||
alignItems: 'center', | ||
...theme.mixins.panelBorder.top, | ||
flexWrap: 'wrap', | ||
backgroundColor: theme.otherVars.editorToolbarBg, | ||
userSelect: 'text', | ||
'& .StatusBar-padding': { | ||
padding: '2px 12px', | ||
'& .StatusBar-mlAuto': { | ||
marginLeft: 'auto', | ||
}, | ||
'& .StatusBar-divider': { | ||
...theme.mixins.panelBorder.right, | ||
}, | ||
}, | ||
})); | ||
|
||
|
||
export function StatusBar() { | ||
const eventBus = useContext(QueryToolEventsContext); | ||
const [position, setPosition] = useState([1, 1]); | ||
const [lastTaskText, setLastTaskText] = useState(null); | ||
const [rowsCount, setRowsCount] = useState([0, 0]); | ||
const [selectedRowsCount, setSelectedRowsCount] = useState(0); | ||
const [dataRowChangeCounts, setDataRowChangeCounts] = useState({ | ||
isDirty: false, | ||
added: 0, | ||
updated: 0, | ||
deleted: 0, | ||
}); | ||
const {seconds, minutes, hours, msec, start:startTimer, pause:pauseTimer, reset:resetTimer} = useStopwatch({}); | ||
|
||
const [checkedMenuItems, setCheckedMenuItems] = useState(OS_EOL); | ||
const [openMenuName, setOpenMenuName] = useState(null); | ||
|
||
const prevMenuOpenIdRef = React.useRef(null); | ||
const eolMenuRef = React.useRef(null); | ||
|
||
const toggleMenu = React.useCallback((e) => { | ||
const name = e.currentTarget?.getAttribute('name'); | ||
setOpenMenuName(() => { | ||
return prevMenuOpenIdRef.current === name ? null : name; | ||
}); | ||
prevMenuOpenIdRef.current = null; | ||
}, []); | ||
|
||
const onMenuClose = React.useCallback(() => { | ||
prevMenuOpenIdRef.current = openMenuName; | ||
setTimeout(() => { | ||
prevMenuOpenIdRef.current = null; | ||
}, 300); | ||
setOpenMenuName(null); | ||
}, [openMenuName]); | ||
|
||
const checkMenuClick = useCallback((e)=>{ | ||
setCheckedMenuItems(e.value); | ||
handleEndOfLineChange(e.value); | ||
onMenuClose(); | ||
}, []); | ||
|
||
const handleEndOfLineChange = (val) => { | ||
const eol = val === 'crlf' ? '\r\n' : '\n'; | ||
eventBus.fireEvent(QUERY_TOOL_EVENTS.CHANGE_EOL, eol); | ||
}; | ||
|
||
useEffect(()=>{ | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.CURSOR_ACTIVITY, (newPos)=>{ | ||
setPosition(newPos||[1, 1]); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.EXECUTION_END, ()=>{ | ||
pauseTimer(); | ||
setLastTaskText(gettext('Query complete')); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.TASK_START, (taskText, startTime)=>{ | ||
resetTimer(); | ||
startTimer(startTime); | ||
setLastTaskText(taskText); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.TASK_END, (taskText, endTime)=>{ | ||
pauseTimer(endTime); | ||
setLastTaskText(taskText); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.ROWS_FETCHED, (fetched, total)=>{ | ||
setRowsCount([fetched||0, total||0]); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.SELECTED_ROWS_COLS_CELL_CHANGED, (rows)=>{ | ||
setSelectedRowsCount(rows); | ||
}); | ||
eventBus.registerListener(QUERY_TOOL_EVENTS.DATAGRID_CHANGED, (_isDirty, dataChangeStore)=>{ | ||
setDataRowChangeCounts({ | ||
added: Object.keys(dataChangeStore.added||{}).length, | ||
updated: Object.keys(dataChangeStore.updated||{}).length, | ||
deleted: Object.keys(dataChangeStore.deleted||{}).length, | ||
}); | ||
}); | ||
}, []); | ||
|
||
let stagedText = ''; | ||
if(dataRowChangeCounts.added > 0) { | ||
stagedText += ' ' + gettext('Added: %s', dataRowChangeCounts.added); | ||
} | ||
if(dataRowChangeCounts.updated > 0) { | ||
stagedText += ' ' + gettext('Updated: %s', dataRowChangeCounts.updated); | ||
} | ||
if(dataRowChangeCounts.deleted > 0) { | ||
stagedText += ' ' + gettext('Deleted: %s', dataRowChangeCounts.deleted); | ||
} | ||
|
||
return ( | ||
<StyledBox> | ||
<Box className='StatusBar-padding StatusBar-divider'>{gettext('Total rows: %s of %s', rowsCount[0], rowsCount[1])}</Box> | ||
{lastTaskText && | ||
<Box className='StatusBar-padding StatusBar-divider'>{lastTaskText} {hours.toString().padStart(2, '0')}:{minutes.toString().padStart(2, '0')}:{seconds.toString().padStart(2, '0')}.{msec.toString().padStart(3, '0')}</Box> | ||
} | ||
{!lastTaskText && !_.isNull(lastTaskText) && | ||
<Box className='StatusBar-padding StatusBar-divider'>{lastTaskText} {hours.toString().padStart(2, '0')}:{minutes.toString().padStart(2, '0')}:{seconds.toString().padStart(2, '0')}.{msec.toString().padStart(3, '0')}</Box> | ||
} | ||
{Boolean(selectedRowsCount) && | ||
<Box className='StatusBar-padding StatusBar-divider'>{gettext('Rows selected: %s',selectedRowsCount)}</Box>} | ||
{stagedText && | ||
<Box className='StatusBar-padding StatusBar-divider'> | ||
<span>{gettext('Changes staged: %s', stagedText)}</span> | ||
</Box> | ||
} | ||
<Box className="StatusBar-padding StatusBar-mlAuto"> | ||
<Tooltip title="Select EOL Sequence"> | ||
<span | ||
onClick={toggleMenu} | ||
ref={eolMenuRef} | ||
name="menu-eoloptions" | ||
style={{ | ||
cursor: 'pointer', | ||
display: 'inline-flex', | ||
alignItems: 'center', | ||
fontSize: 'inherit', | ||
}} | ||
> | ||
{checkedMenuItems.toUpperCase()} | ||
</span> | ||
</Tooltip> | ||
<PgMenu | ||
anchorRef={eolMenuRef} | ||
open={openMenuName=='menu-eoloptions'} | ||
onClose={onMenuClose} | ||
label={gettext('EOL Options Menu')} | ||
> | ||
<PgMenuItem hasCheck value="lf" checked={checkedMenuItems === 'lf'} onClick={checkMenuClick}>{gettext('LF')}</PgMenuItem> | ||
<PgMenuItem hasCheck value="crlf" checked={checkedMenuItems === 'crlf'} onClick={checkMenuClick}>{gettext('CRLF')}</PgMenuItem> | ||
</PgMenu> | ||
</Box> | ||
<Box className='StatusBar-padding StatusBar-mlAuto'>{gettext('Ln %s, Col %s', position[0], position[1])}</Box> | ||
</StyledBox> | ||
); | ||
} |