mirror of
https://github.com/dbgate/dbgate
synced 2024-11-08 04:35:58 +00:00
open existing tab, when possible
This commit is contained in:
parent
0857757ce2
commit
0081deb844
@ -109,6 +109,8 @@ export function SavedSqlFileAppObject({ data, commonProps }) {
|
|||||||
newQuery({
|
newQuery({
|
||||||
title: file,
|
title: file,
|
||||||
initialData: data,
|
initialData: data,
|
||||||
|
// @ts-ignore
|
||||||
|
savedFile: file,
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
@ -131,6 +133,9 @@ export function SavedShellFileAppObject({ data, commonProps }) {
|
|||||||
title: file,
|
title: file,
|
||||||
icon: 'img shell',
|
icon: 'img shell',
|
||||||
tabComponent: 'ShellTab',
|
tabComponent: 'ShellTab',
|
||||||
|
props: {
|
||||||
|
savedFile: file,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
data
|
data
|
||||||
);
|
);
|
||||||
@ -165,6 +170,7 @@ export function SavedChartFileAppObject({ data, commonProps }) {
|
|||||||
props: {
|
props: {
|
||||||
conid: connection._id,
|
conid: connection._id,
|
||||||
database,
|
database,
|
||||||
|
savedFile: file,
|
||||||
},
|
},
|
||||||
tabComponent: 'ChartTab',
|
tabComponent: 'ChartTab',
|
||||||
},
|
},
|
||||||
@ -201,6 +207,9 @@ export function SavedMarkdownFileAppObject({ data, commonProps }) {
|
|||||||
title: file,
|
title: file,
|
||||||
icon: 'img markdown',
|
icon: 'img markdown',
|
||||||
tabComponent: 'MarkdownEditorTab',
|
tabComponent: 'MarkdownEditorTab',
|
||||||
|
props: {
|
||||||
|
savedFile: file,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
data
|
data
|
||||||
);
|
);
|
||||||
|
@ -8,8 +8,13 @@ export default function SaveTabModal({ data, folder, format, modalState, tabid,
|
|||||||
const setOpenedTabs = useSetOpenedTabs();
|
const setOpenedTabs = useSetOpenedTabs();
|
||||||
const openedTabs = useOpenedTabs();
|
const openedTabs = useOpenedTabs();
|
||||||
|
|
||||||
const name = openedTabs.find((x) => x.tabid == tabid).title;
|
const { savedFile } = openedTabs.find((x) => x.tabid == tabid).props || {};
|
||||||
const onSave = (name) => changeTab(tabid, setOpenedTabs, (tab) => ({ ...tab, title: name }));
|
const onSave = (name) =>
|
||||||
|
changeTab(tabid, setOpenedTabs, (tab) => ({
|
||||||
|
...tab,
|
||||||
|
title: name,
|
||||||
|
props: { ...tab.props, savedFile: name },
|
||||||
|
}));
|
||||||
|
|
||||||
const handleKeyboard = React.useCallback(
|
const handleKeyboard = React.useCallback(
|
||||||
(e) => {
|
(e) => {
|
||||||
@ -31,6 +36,13 @@ export default function SaveTabModal({ data, folder, format, modalState, tabid,
|
|||||||
}, [tabVisible, handleKeyboard]);
|
}, [tabVisible, handleKeyboard]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SaveFileModal data={data} folder={folder} format={format} modalState={modalState} name={name} onSave={onSave} />
|
<SaveFileModal
|
||||||
|
data={data}
|
||||||
|
folder={folder}
|
||||||
|
format={format}
|
||||||
|
modalState={modalState}
|
||||||
|
name={savedFile || 'newFile'}
|
||||||
|
onSave={onSave}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -4,3 +4,5 @@ import JslDataGrid from '../sqleditor/JslDataGrid';
|
|||||||
export default function ArchiveFileTab({ archiveFolder, archiveFile, tabVisible, toolbarPortalRef, tabid }) {
|
export default function ArchiveFileTab({ archiveFolder, archiveFile, tabVisible, toolbarPortalRef, tabid }) {
|
||||||
return <JslDataGrid jslid={`archive://${archiveFolder}/${archiveFile}`} />;
|
return <JslDataGrid jslid={`archive://${archiveFolder}/${archiveFile}`} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ArchiveFileTab.matchingProps = ['archiveFile', 'archiveFolder'];
|
||||||
|
@ -26,3 +26,5 @@ export default function TableDataTab({ conid, database, schemaName, pureName, ta
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TableDataTab.matchingProps = ['conid', 'database', 'schemaName', 'pureName'];
|
||||||
|
@ -54,3 +54,5 @@ export default function ViewDataTab({ conid, database, schemaName, pureName, tab
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ViewDataTab.matchingProps = ['conid', 'database', 'schemaName', 'pureName'];
|
@ -1,13 +1,49 @@
|
|||||||
import uuidv1 from 'uuid/v1';
|
import uuidv1 from 'uuid/v1';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import localforage from 'localforage';
|
import localforage from 'localforage';
|
||||||
import { useSetOpenedTabs } from './globalState';
|
import stableStringify from 'json-stable-stringify';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import { useOpenedTabs, useSetOpenedTabs } from './globalState';
|
||||||
|
import tabs from '../tabs';
|
||||||
|
|
||||||
export default function useOpenNewTab() {
|
export default function useOpenNewTab() {
|
||||||
const setOpenedTabs = useSetOpenedTabs();
|
const setOpenedTabs = useSetOpenedTabs();
|
||||||
|
const openedTabs = useOpenedTabs();
|
||||||
|
|
||||||
const openNewTab = React.useCallback(
|
const openNewTab = React.useCallback(
|
||||||
async (newTab, initialData = undefined) => {
|
async (newTab, initialData = undefined) => {
|
||||||
|
let existing = null;
|
||||||
|
|
||||||
|
const { savedFile } = newTab.props || {};
|
||||||
|
if (savedFile) {
|
||||||
|
existing = openedTabs.find(
|
||||||
|
(x) =>
|
||||||
|
x.props && x.tabComponent == newTab.tabComponent && x.closedTime == null && x.props.savedFile == savedFile
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const component = tabs[newTab.tabComponent];
|
||||||
|
if (!existing && component && component.matchingProps) {
|
||||||
|
const testString = stableStringify(_.pick(newTab.props || {}, component.matchingProps));
|
||||||
|
existing = openedTabs.find(
|
||||||
|
(x) =>
|
||||||
|
x.props &&
|
||||||
|
x.tabComponent == newTab.tabComponent &&
|
||||||
|
x.closedTime == null &&
|
||||||
|
stableStringify(_.pick(x.props || {}, component.matchingProps)) == testString
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existing) {
|
||||||
|
setOpenedTabs((tabs) =>
|
||||||
|
tabs.map((x) => ({
|
||||||
|
...x,
|
||||||
|
selected: x.tabid == existing.tabid,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const tabid = uuidv1();
|
const tabid = uuidv1();
|
||||||
if (initialData) {
|
if (initialData) {
|
||||||
await localforage.setItem(`tabdata_${tabid}`, initialData);
|
await localforage.setItem(`tabdata_${tabid}`, initialData);
|
||||||
|
Loading…
Reference in New Issue
Block a user