mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-30 16:13:58 +00:00
save chart to editor data
This commit is contained in:
@@ -7,7 +7,7 @@ import { HorizontalSplitter } from '../widgets/Splitter';
|
|||||||
import WidgetColumnBar, { WidgetColumnBarItem } from '../widgets/WidgetColumnBar';
|
import WidgetColumnBar, { WidgetColumnBarItem } from '../widgets/WidgetColumnBar';
|
||||||
import { FormCheckboxField, FormSelectField } from '../utility/forms';
|
import { FormCheckboxField, FormSelectField } from '../utility/forms';
|
||||||
import DataChart from './DataChart';
|
import DataChart from './DataChart';
|
||||||
import { FormProvider } from '../utility/FormProvider';
|
import { FormProviderCore } from '../utility/FormProvider';
|
||||||
|
|
||||||
const LeftContainer = styled.div`
|
const LeftContainer = styled.div`
|
||||||
background-color: ${(props) => props.theme.manager_background};
|
background-color: ${(props) => props.theme.manager_background};
|
||||||
@@ -15,13 +15,13 @@ const LeftContainer = styled.div`
|
|||||||
flex: 1;
|
flex: 1;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export default function ChartEditor({ data }) {
|
export default function ChartEditor({ data, config, setConfig }) {
|
||||||
const [managerSize, setManagerSize] = React.useState(0);
|
const [managerSize, setManagerSize] = React.useState(0);
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const availableColumnNames = data ? data.structure.columns.map((x) => x.columnName) : [];
|
const availableColumnNames = data ? data.structure.columns.map((x) => x.columnName) : [];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FormProvider initialValues={{ chartType: 'bar' }}>
|
<FormProviderCore values={config} setValues={setConfig}>
|
||||||
<HorizontalSplitter initialValue="300px" size={managerSize} setSize={setManagerSize}>
|
<HorizontalSplitter initialValue="300px" size={managerSize} setSize={setManagerSize}>
|
||||||
<LeftContainer theme={theme}>
|
<LeftContainer theme={theme}>
|
||||||
<WidgetColumnBar>
|
<WidgetColumnBar>
|
||||||
@@ -49,6 +49,6 @@ export default function ChartEditor({ data }) {
|
|||||||
|
|
||||||
<DataChart data={data} />
|
<DataChart data={data} />
|
||||||
</HorizontalSplitter>
|
</HorizontalSplitter>
|
||||||
</FormProvider>
|
</FormProviderCore>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -356,6 +356,7 @@ export default function DataGridCore(props) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
data: getSelectedFreeData(),
|
data: getSelectedFreeData(),
|
||||||
|
config: { chartType: 'bar' },
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import _ from 'lodash';
|
||||||
import { createFreeTableModel } from 'dbgate-datalib';
|
import { createFreeTableModel } from 'dbgate-datalib';
|
||||||
import useUndoReducer from '../utility/useUndoReducer';
|
import useUndoReducer from '../utility/useUndoReducer';
|
||||||
import { useSetOpenedTabs } from '../utility/globalState';
|
import { useSetOpenedTabs } from '../utility/globalState';
|
||||||
@@ -15,7 +16,6 @@ import SaveTabModal from '../modals/SaveTabModal';
|
|||||||
import ChartEditor from '../charts/ChartEditor';
|
import ChartEditor from '../charts/ChartEditor';
|
||||||
|
|
||||||
export default function ChartTab({ tabVisible, toolbarPortalRef, tabid }) {
|
export default function ChartTab({ tabVisible, toolbarPortalRef, tabid }) {
|
||||||
const [config, setConfig] = useGridConfig(tabid);
|
|
||||||
const [modelState, dispatchModel] = useUndoReducer(createFreeTableModel());
|
const [modelState, dispatchModel] = useUndoReducer(createFreeTableModel());
|
||||||
const saveFileModalState = useModalState();
|
const saveFileModalState = useModalState();
|
||||||
const { initialData, setEditorData, errorMessage, isLoading } = useEditorData({
|
const { initialData, setEditorData, errorMessage, isLoading } = useEditorData({
|
||||||
@@ -31,6 +31,16 @@ export default function ChartTab({ tabVisible, toolbarPortalRef, tabid }) {
|
|||||||
setEditorData(modelState.value);
|
setEditorData(modelState.value);
|
||||||
}, [modelState]);
|
}, [modelState]);
|
||||||
|
|
||||||
|
const setConfig = React.useCallback(
|
||||||
|
(config) =>
|
||||||
|
// @ts-ignore
|
||||||
|
dispatchModel({
|
||||||
|
type: 'compute',
|
||||||
|
compute: (v) => ({ ...v, config: _.isFunction(config) ? config(v.config) : config }),
|
||||||
|
}),
|
||||||
|
[dispatchModel]
|
||||||
|
);
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return <LoadingInfo wrapper message="Loading data" />;
|
return <LoadingInfo wrapper message="Loading data" />;
|
||||||
}
|
}
|
||||||
@@ -40,7 +50,11 @@ export default function ChartTab({ tabVisible, toolbarPortalRef, tabid }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<ChartEditor data={modelState.value && modelState.value.data} />
|
<ChartEditor
|
||||||
|
data={modelState.value && modelState.value.data}
|
||||||
|
config={modelState.value ? modelState.value.config || {} : {}}
|
||||||
|
setConfig={setConfig}
|
||||||
|
/>
|
||||||
<SaveTabModal
|
<SaveTabModal
|
||||||
modalState={saveFileModalState}
|
modalState={saveFileModalState}
|
||||||
data={modelState.value}
|
data={modelState.value}
|
||||||
|
|||||||
@@ -5,6 +5,14 @@ const FormContext = React.createContext(null);
|
|||||||
|
|
||||||
export function FormProvider({ children, initialValues = {} }) {
|
export function FormProvider({ children, initialValues = {} }) {
|
||||||
const [values, setValues] = React.useState(initialValues);
|
const [values, setValues] = React.useState(initialValues);
|
||||||
|
return (
|
||||||
|
<FormProviderCore values={values} setValues={setValues}>
|
||||||
|
{children}
|
||||||
|
</FormProviderCore>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function FormProviderCore({ children, values, setValues }) {
|
||||||
const [submitAction, setSubmitAction] = React.useState(null);
|
const [submitAction, setSubmitAction] = React.useState(null);
|
||||||
const handleEnter = React.useCallback(
|
const handleEnter = React.useCallback(
|
||||||
(e) => {
|
(e) => {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
|
import _ from 'lodash';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
function reducer(state, action) {
|
function reducer(state, action) {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case 'set':
|
case 'set':
|
||||||
// console.log('SET', state.history, action.value);
|
|
||||||
return {
|
return {
|
||||||
history: [...state.history.slice(0, state.current + 1), action.value],
|
history: [...state.history.slice(0, state.current + 1), action.value],
|
||||||
current: state.current + 1,
|
current: state.current + 1,
|
||||||
@@ -11,6 +11,16 @@ function reducer(state, action) {
|
|||||||
canUndo: true,
|
canUndo: true,
|
||||||
canRedo: false,
|
canRedo: false,
|
||||||
};
|
};
|
||||||
|
case 'compute': {
|
||||||
|
const newValue = action.compute(state.history[state.current]);
|
||||||
|
return {
|
||||||
|
history: [...state.history.slice(0, state.current + 1), newValue],
|
||||||
|
current: state.current + 1,
|
||||||
|
value: newValue,
|
||||||
|
canUndo: true,
|
||||||
|
canRedo: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
case 'undo':
|
case 'undo':
|
||||||
if (state.current > 0)
|
if (state.current > 0)
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user