introduced designer id, bring to front

This commit is contained in:
Jan Prochazka
2020-12-26 10:28:49 +01:00
parent 54bdaac64f
commit 95e0905f01
3 changed files with 41 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import DesignerTable from './DesignerTable'; import DesignerTable from './DesignerTable';
import uuidv1 from 'uuid/v1';
const Wrapper = styled.div` const Wrapper = styled.div`
flex: 1; flex: 1;
@@ -13,6 +14,7 @@ export default function Designer({ value, onChange }) {
e.preventDefault(); e.preventDefault();
if (!data) return; if (!data) return;
var json = JSON.parse(data); var json = JSON.parse(data);
json.designerId = uuidv1();
onChange({ onChange({
...value, ...value,
tables: [...(tables || []), json], tables: [...(tables || []), json],
@@ -26,20 +28,29 @@ export default function Designer({ value, onChange }) {
}; };
const changeTable = React.useCallback( const changeTable = React.useCallback(
(table, index) => { (table) => {
const newValue = { onChange((current) => ({
...value, ...current,
tables: (tables || []).map((t, i) => (i == index ? table : t)), tables: (current.tables || []).map((x) => (x.designerId == table.designerId ? table : x)),
}; }));
onChange(newValue); },
[onChange]
);
const bringToFront = React.useCallback(
(table) => {
onChange((current) => ({
...current,
tables: [...(current.tables || []).filter((x) => x.designerId != table.designerId), table],
}));
}, },
[onChange] [onChange]
); );
return ( return (
<Wrapper onDragOver={(e) => e.preventDefault()} onDrop={handleDrop}> <Wrapper onDragOver={(e) => e.preventDefault()} onDrop={handleDrop}>
{(tables || []).map((table, index) => ( {(tables || []).map((table) => (
<DesignerTable key={index} {...table} index={index} onChangeTable={changeTable} /> <DesignerTable key={table.designerId} {...table} onChangeTable={changeTable} onBringToFront={bringToFront} />
))} ))}
</Wrapper> </Wrapper>
); );

View File

@@ -23,7 +23,7 @@ const ColumnsWrapper = styled.div`
`; `;
export default function DesignerTable(props) { export default function DesignerTable(props) {
const { pureName, columns, left, top, onChangeTable, index } = props; const { pureName, columns, left, top, onChangeTable, onBringToFront } = props;
const [movingPosition, setMovingPosition] = React.useState(null); const [movingPosition, setMovingPosition] = React.useState(null);
const movingPositionRef = React.useRef(null); const movingPositionRef = React.useRef(null);
@@ -54,14 +54,11 @@ export default function DesignerTable(props) {
const handleMoveEnd = React.useCallback( const handleMoveEnd = React.useCallback(
(e) => { (e) => {
if (movingPositionRef.current) { if (movingPositionRef.current) {
onChangeTable( onChangeTable({
{
...props, ...props,
left: movingPositionRef.current.left, left: movingPositionRef.current.left,
top: movingPositionRef.current.top, top: movingPositionRef.current.top,
}, });
index
);
} }
movingPositionRef.current = null; movingPositionRef.current = null;
@@ -71,7 +68,7 @@ export default function DesignerTable(props) {
// this.props.designer.changedModel(true); // this.props.designer.changedModel(true);
}, },
[onChangeTable, index] [onChangeTable, props]
); );
React.useEffect(() => { React.useEffect(() => {
@@ -103,6 +100,7 @@ export default function DesignerTable(props) {
left: movingPosition ? movingPosition.left : left, left: movingPosition ? movingPosition.left : left,
top: movingPosition ? movingPosition.top : top, top: movingPosition ? movingPosition.top : top,
}} }}
onMouseDown={() => onBringToFront(props)}
> >
<Header onMouseDown={headerMouseDown}>{pureName}</Header> <Header onMouseDown={headerMouseDown}>{pureName}</Header>
<ColumnsWrapper> <ColumnsWrapper>

View File

@@ -4,6 +4,19 @@ import localforage from 'localforage';
import { changeTab } from './common'; import { changeTab } from './common';
import { useSetOpenedTabs } from './globalState'; import { useSetOpenedTabs } from './globalState';
function getParsedLocalStorage(key) {
const value = localStorage.getItem(key);
if (value != null) {
try {
const res = JSON.parse(value);
return res;
} catch (e) {
localStorage.removeItem(key);
}
}
return null;
}
export default function useEditorData({ tabid, reloadToken = 0, loadFromArgs = null }) { export default function useEditorData({ tabid, reloadToken = 0, loadFromArgs = null }) {
const localStorageKey = `tabdata_editor_${tabid}`; const localStorageKey = `tabdata_editor_${tabid}`;
const setOpenedTabs = useSetOpenedTabs(); const setOpenedTabs = useSetOpenedTabs();
@@ -34,7 +47,7 @@ export default function useEditorData({ tabid, reloadToken = 0, loadFromArgs = n
console.error(err.response); console.error(err.response);
} }
} else { } else {
const initFallback = localStorage.getItem(localStorageKey); const initFallback = getParsedLocalStorage(localStorageKey);
if (initFallback != null) { if (initFallback != null) {
const init = JSON.parse(initFallback); const init = JSON.parse(initFallback);
setValue(init); setValue(init);