mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-27 19:36:00 +00:00
duplicate tab popup menu
This commit is contained in:
@@ -19,6 +19,8 @@ function getParsedLocalStorage(key) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const saveHandlersList = [];
|
||||||
|
|
||||||
export default function useEditorData({ tabid, reloadToken = 0, loadFromArgs = null, onInitialData = null }) {
|
export default function useEditorData({ tabid, reloadToken = 0, loadFromArgs = null, onInitialData = null }) {
|
||||||
const localStorageKey = `tabdata_editor_${tabid}`;
|
const localStorageKey = `tabdata_editor_${tabid}`;
|
||||||
let changeCounter = 0;
|
let changeCounter = 0;
|
||||||
@@ -90,6 +92,11 @@ export default function useEditorData({ tabid, reloadToken = 0, loadFromArgs = n
|
|||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const saveToStorageIfNeeded = async () => {
|
||||||
|
if (savedCounter == changeCounter) return; // all saved
|
||||||
|
await saveToStorage();
|
||||||
|
};
|
||||||
|
|
||||||
const saveToStorage = async () => {
|
const saveToStorage = async () => {
|
||||||
if (value == null) return;
|
if (value == null) return;
|
||||||
try {
|
try {
|
||||||
@@ -128,11 +135,13 @@ export default function useEditorData({ tabid, reloadToken = 0, loadFromArgs = n
|
|||||||
onMount(() => {
|
onMount(() => {
|
||||||
window.addEventListener('beforeunload', saveToStorageSync);
|
window.addEventListener('beforeunload', saveToStorageSync);
|
||||||
initialLoad();
|
initialLoad();
|
||||||
|
saveHandlersList.push(saveToStorageIfNeeded);
|
||||||
});
|
});
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
saveToStorage();
|
saveToStorage();
|
||||||
window.removeEventListener('beforeunload', saveToStorageSync);
|
window.removeEventListener('beforeunload', saveToStorageSync);
|
||||||
|
_.remove(saveHandlersList, x => x == saveToStorageIfNeeded);
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -144,3 +153,9 @@ export default function useEditorData({ tabid, reloadToken = 0, loadFromArgs = n
|
|||||||
initialLoad,
|
initialLoad,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function saveAllPendingEditorData() {
|
||||||
|
for (const item of saveHandlersList) {
|
||||||
|
await item();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import tabs from '../tabs';
|
|||||||
import { setSelectedTabFunc } from './common';
|
import { setSelectedTabFunc } from './common';
|
||||||
import localforage from 'localforage';
|
import localforage from 'localforage';
|
||||||
import stableStringify from 'json-stable-stringify';
|
import stableStringify from 'json-stable-stringify';
|
||||||
|
import { saveAllPendingEditorData } from '../query/useEditorData';
|
||||||
|
|
||||||
function findFreeNumber(numbers: number[]) {
|
function findFreeNumber(numbers: number[]) {
|
||||||
if (numbers.length == 0) return 1;
|
if (numbers.length == 0) return 1;
|
||||||
@@ -74,9 +75,9 @@ export default async function openNewTab(newTab, initialData = undefined, option
|
|||||||
openedTabs.update(files => [
|
openedTabs.update(files => [
|
||||||
...(files || []).map(x => ({ ...x, selected: false })),
|
...(files || []).map(x => ({ ...x, selected: false })),
|
||||||
{
|
{
|
||||||
|
...newTab,
|
||||||
tabid,
|
tabid,
|
||||||
selected: true,
|
selected: true,
|
||||||
...newTab,
|
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -91,3 +92,35 @@ export default async function openNewTab(newTab, initialData = undefined, option
|
|||||||
// },
|
// },
|
||||||
// ]);
|
// ]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function duplicateTab(tab) {
|
||||||
|
await saveAllPendingEditorData();
|
||||||
|
|
||||||
|
let title = tab.title;
|
||||||
|
const mtitle = title.match(/^(.*#)[\d]+$/);
|
||||||
|
if (mtitle) title = mtitle[1];
|
||||||
|
|
||||||
|
const keyRegex = /^tabdata_([^_]+)_([^_]+)$/;
|
||||||
|
const initialData = {};
|
||||||
|
for (let i = 0; i < localStorage.length; i++) {
|
||||||
|
const key = localStorage.key(i);
|
||||||
|
const m = key.match(keyRegex);
|
||||||
|
if (m && m[2] == tab.tabid) {
|
||||||
|
initialData[m[1]] = JSON.parse(localStorage.getItem(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const key of await localforage.keys()) {
|
||||||
|
const m = key.match(keyRegex);
|
||||||
|
if (m && m[2] == tab.tabid) {
|
||||||
|
initialData[m[1]] = await localforage.getItem(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
openNewTab(
|
||||||
|
{
|
||||||
|
..._.omit(tab, ['tabid']),
|
||||||
|
title,
|
||||||
|
},
|
||||||
|
initialData,
|
||||||
|
{ forceNewTab: true }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -113,6 +113,7 @@
|
|||||||
import { setSelectedTab } from '../utility/common';
|
import { setSelectedTab } from '../utility/common';
|
||||||
import contextMenu from '../utility/contextMenu';
|
import contextMenu from '../utility/contextMenu';
|
||||||
import { getConnectionInfo } from '../utility/metadataLoaders';
|
import { getConnectionInfo } from '../utility/metadataLoaders';
|
||||||
|
import { duplicateTab } from '../utility/openNewTab';
|
||||||
|
|
||||||
$: currentDbKey =
|
$: currentDbKey =
|
||||||
$currentDatabase && $currentDatabase.name && $currentDatabase.connection
|
$currentDatabase && $currentDatabase.name && $currentDatabase.connection
|
||||||
@@ -162,6 +163,10 @@
|
|||||||
text: 'Close others',
|
text: 'Close others',
|
||||||
onClick: () => closeOthers(tabid),
|
onClick: () => closeOthers(tabid),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
text: 'Duplicate',
|
||||||
|
onClick: () => duplicateTab(tab),
|
||||||
|
},
|
||||||
tabComponent &&
|
tabComponent &&
|
||||||
tabs[tabComponent] &&
|
tabs[tabComponent] &&
|
||||||
tabs[tabComponent].allowAddToFavorites &&
|
tabs[tabComponent].allowAddToFavorites &&
|
||||||
|
|||||||
Reference in New Issue
Block a user