inplace editor improvements

This commit is contained in:
Jan Prochazka
2020-03-25 21:48:44 +01:00
parent 01e2cb3087
commit cdb3215b5f
5 changed files with 93 additions and 22 deletions

View File

@@ -28,6 +28,7 @@ import useModalState from '../modals/useModalState';
import ConfirmSqlModal from '../modals/ConfirmSqlModal'; import ConfirmSqlModal from '../modals/ConfirmSqlModal';
import { changeSetToSql, createChangeSet } from '@dbgate/datalib'; import { changeSetToSql, createChangeSet } from '@dbgate/datalib';
import { scriptToSql } from '@dbgate/sqltree'; import { scriptToSql } from '@dbgate/sqltree';
import { sleep } from '../utility/common';
const GridContainer = styled.div` const GridContainer = styled.div`
position: absolute; position: absolute;
@@ -106,6 +107,11 @@ export default function DataGridCore(props) {
const [inplaceEditorCell, setInplaceEditorCell] = React.useState(nullCell); const [inplaceEditorCell, setInplaceEditorCell] = React.useState(nullCell);
const [inplaceEditorInitText, setInplaceEditorInitText] = React.useState(''); const [inplaceEditorInitText, setInplaceEditorInitText] = React.useState('');
const [inplaceEditorShouldSave, setInplaceEditorShouldSave] = React.useState(false);
const [inplaceEditorChangedOnCreate, setInplaceEditorChangedOnCreate] = React.useState(false);
const changeSetRef = React.useRef(changeSet);
changeSetRef.current = changeSet;
const loadNextData = async () => { const loadNextData = async () => {
if (isLoading) return; if (isLoading) return;
@@ -217,10 +223,19 @@ export default function DataGridCore(props) {
} }
}, [tabVisible, tableElement]); }, [tabVisible, tableElement]);
const handleCloseInplaceEditor = React.useCallback(() => { const handleCloseInplaceEditor = React.useCallback(
setInplaceEditorCell(null); mode => {
setInplaceEditorInitText(null); const [row, col] = currentCell || [];
}, []); setInplaceEditorCell(null);
setInplaceEditorInitText(null);
setInplaceEditorShouldSave(false);
if (tableElement) tableElement.focus();
// @ts-ignore
if (mode == 'enter' && row) moveCurrentCell(row + 1, col);
if (mode == 'save') setTimeout(handleSave, 1);
},
[tableElement, currentCell]
);
const visibleRealColumns = React.useMemo( const visibleRealColumns = React.useMemo(
() => countVisibleRealColumns(columnSizes, firstVisibleColumnScrollIndex, gridScrollAreaWidth, columns), () => countVisibleRealColumns(columnSizes, firstVisibleColumnScrollIndex, gridScrollAreaWidth, columns),
@@ -261,6 +276,7 @@ export default function DataGridCore(props) {
setDragStartCell(cell); setDragStartCell(cell);
if (isRegularCell(cell) && !_.isEqual(cell, inplaceEditorCell) && _.isEqual(cell, currentCell)) { if (isRegularCell(cell) && !_.isEqual(cell, inplaceEditorCell) && _.isEqual(cell, currentCell)) {
setInplaceEditorShouldSave(false);
setInplaceEditorCell(cell); setInplaceEditorCell(cell);
} else if (!_.isEqual(cell, inplaceEditorCell)) { } else if (!_.isEqual(cell, inplaceEditorCell)) {
handleCloseInplaceEditor(); handleCloseInplaceEditor();
@@ -305,8 +321,18 @@ export default function DataGridCore(props) {
setvScrollValueToSetDate(new Date()); setvScrollValueToSetDate(new Date());
} }
function handleSave() { // async function blurEditorAndSave() {
const script = changeSetToSql(changeSet); // setInplaceEditorCell(null);
// setInplaceEditorInitText(null);
// await sleep(1);
// }
function handleSave() {
if (inplaceEditorCell) {
setInplaceEditorShouldSave(true);
return;
}
const script = changeSetToSql(changeSetRef.current);
const sql = scriptToSql(display.driver, script); const sql = scriptToSql(display.driver, script);
setConfirmSql(sql); setConfirmSql(sql);
confirmSqlModalState.open(); confirmSqlModalState.open();
@@ -329,6 +355,20 @@ export default function DataGridCore(props) {
} }
function handleGridKeyDown(event) { function handleGridKeyDown(event) {
if (event.keyCode == keycodes.s && event.ctrlKey) {
event.preventDefault();
handleSave();
// this.saveAndFocus();
}
if (event.keyCode == keycodes.r && event.ctrlKey) {
event.preventDefault();
// revertRowChanges();
// this.saveAndFocus();
}
if (inplaceEditorCell) return;
if ( if (
!event.ctrlKey && !event.ctrlKey &&
!event.altKey && !event.altKey &&
@@ -337,14 +377,14 @@ export default function DataGridCore(props) {
event.keyCode == keycodes.dash) event.keyCode == keycodes.dash)
) { ) {
setInplaceEditorInitText(event.nativeEvent.key); setInplaceEditorInitText(event.nativeEvent.key);
setInplaceEditorShouldSave(false);
setInplaceEditorCell(currentCell); setInplaceEditorCell(currentCell);
// console.log('event', event.nativeEvent); // console.log('event', event.nativeEvent);
} }
if (event.keyCode == keycodes.s && event.ctrlKey) { if (event.keyCode == keycodes.f2) {
event.preventDefault(); setInplaceEditorShouldSave(false);
handleSave(); setInplaceEditorCell(currentCell);
// this.saveAndFocus();
} }
const moved = handleCursorMove(event); const moved = handleCursorMove(event);
@@ -421,7 +461,7 @@ export default function DataGridCore(props) {
return ['filter', columnRealIndex]; return ['filter', columnRealIndex];
} }
function moveCurrentCell(row, col, event) { function moveCurrentCell(row, col, event = null) {
const rowCount = rowCountNewIncluded; const rowCount = rowCountNewIncluded;
if (row < 0) row = 0; if (row < 0) row = 0;
@@ -495,11 +535,11 @@ export default function DataGridCore(props) {
}; };
// console.log('visibleRealColumnIndexes', visibleRealColumnIndexes); // console.log('visibleRealColumnIndexes', visibleRealColumnIndexes);
console.log( // console.log(
'gridScrollAreaWidth / columnSizes.getVisibleScrollSizeSum()', // 'gridScrollAreaWidth / columnSizes.getVisibleScrollSizeSum()',
gridScrollAreaWidth, // gridScrollAreaWidth,
columnSizes.getVisibleScrollSizeSum() // columnSizes.getVisibleScrollSizeSum()
); // );
return ( return (
<GridContainer ref={containerRef}> <GridContainer ref={containerRef}>
@@ -566,6 +606,7 @@ export default function DataGridCore(props) {
visibleRealColumns={visibleRealColumns} visibleRealColumns={visibleRealColumns}
inplaceEditorCell={inplaceEditorCell} inplaceEditorCell={inplaceEditorCell}
inplaceEditorInitText={inplaceEditorInitText} inplaceEditorInitText={inplaceEditorInitText}
inplaceEditorShouldSave={inplaceEditorShouldSave}
onCloseInplaceEditor={handleCloseInplaceEditor} onCloseInplaceEditor={handleCloseInplaceEditor}
cellIsSelected={cellIsSelected} cellIsSelected={cellIsSelected}
changeSet={changeSet} changeSet={changeSet}
@@ -592,7 +633,12 @@ export default function DataGridCore(props) {
onScroll={handleRowScroll} onScroll={handleRowScroll}
viewportRatio={visibleRowCountUpperBound / rowCountNewIncluded} viewportRatio={visibleRowCountUpperBound / rowCountNewIncluded}
/> />
<ConfirmSqlModal modalState={confirmSqlModalState} sql={confirmSql} engine={display.engine} onConfirm={handleConfirmSql} /> <ConfirmSqlModal
modalState={confirmSqlModalState}
sql={confirmSql}
engine={display.engine}
onConfirm={handleConfirmSql}
/>
</GridContainer> </GridContainer>
); );
} }

View File

@@ -81,6 +81,7 @@ export default function DataGridRow({
visibleRealColumns, visibleRealColumns,
inplaceEditorCell, inplaceEditorCell,
inplaceEditorInitText, inplaceEditorInitText,
inplaceEditorShouldSave,
onCloseInplaceEditor, onCloseInplaceEditor,
cellIsSelected, cellIsSelected,
row, row,
@@ -121,6 +122,7 @@ export default function DataGridRow({
setChangeSet={setChangeSet} setChangeSet={setChangeSet}
definition={display.getChangeSetField(row, col.uniqueName)} definition={display.getChangeSetField(row, col.uniqueName)}
onClose={onCloseInplaceEditor} onClose={onCloseInplaceEditor}
shouldSave={inplaceEditorShouldSave}
/> />
) : ( ) : (
<> <>

View File

@@ -14,9 +14,19 @@ const StyledInput = styled.input`
padding: 0px; padding: 0px;
`; `;
export default function InplaceEditor({ widthPx, value, definition, changeSet, setChangeSet, onClose, selectAll }) { export default function InplaceEditor({
widthPx,
value,
definition,
changeSet,
setChangeSet,
onClose,
selectAll,
shouldSave,
changedOnCreate,
}) {
const editorRef = React.useRef(); const editorRef = React.useRef();
const isChangedRef = React.createRef(); const isChangedRef = React.createRef(changedOnCreate);
React.useEffect(() => { React.useEffect(() => {
const editor = editorRef.current; const editor = editorRef.current;
editor.value = value; editor.value = value;
@@ -30,13 +40,26 @@ export default function InplaceEditor({ widthPx, value, definition, changeSet, s
const editor = editorRef.current; const editor = editorRef.current;
setChangeSet(setChangeSetValue(changeSet, definition, editor.value)); setChangeSet(setChangeSetValue(changeSet, definition, editor.value));
} }
onClose();
}
if (shouldSave) {
const editor = editorRef.current;
setChangeSet(setChangeSetValue(changeSet, definition, editor.value));
editor.blur();
onClose('save');
} }
function handleKeyDown(event) { function handleKeyDown(event) {
const editor = editorRef.current;
switch (event.keyCode) { switch (event.keyCode) {
case keycodes.escape: case keycodes.escape:
isChangedRef.current = false; isChangedRef.current = false;
onClose(); onClose();
break; break;
case keycodes.enter:
setChangeSet(setChangeSetValue(changeSet, definition, editor.value));
editor.blur();
onClose('enter');
break;
} }
} }
return ( return (

View File

@@ -68,8 +68,8 @@ export function countColumnSizes(loadedRows, columns, containerWidth, display) {
export function countVisibleRealColumns(columnSizes, firstVisibleColumnScrollIndex, gridScrollAreaWidth, columns) { export function countVisibleRealColumns(columnSizes, firstVisibleColumnScrollIndex, gridScrollAreaWidth, columns) {
const visibleColumnCount = columnSizes.getVisibleScrollCount(firstVisibleColumnScrollIndex, gridScrollAreaWidth); const visibleColumnCount = columnSizes.getVisibleScrollCount(firstVisibleColumnScrollIndex, gridScrollAreaWidth);
console.log('visibleColumnCount', visibleColumnCount); // console.log('visibleColumnCount', visibleColumnCount);
console.log('gridScrollAreaWidth', gridScrollAreaWidth); // console.log('gridScrollAreaWidth', gridScrollAreaWidth);
const visibleRealColumnIndexes = []; const visibleRealColumnIndexes = [];
const modelIndexes = {}; const modelIndexes = {};

View File

@@ -15,7 +15,7 @@ export default function TableDataTab({ conid, database, schemaName, pureName, ta
const [cache, setCache] = React.useState(createGridCache()); const [cache, setCache] = React.useState(createGridCache());
const [changeSet, setChangeSet] = React.useState(createChangeSet()); const [changeSet, setChangeSet] = React.useState(createChangeSet());
console.log('changeSet', changeSet); // console.log('changeSet', changeSet);
const connection = useConnectionInfo(conid); const connection = useConnectionInfo(conid);
const display = React.useMemo( const display = React.useMemo(