tabs tools

This commit is contained in:
Jan Prochazka
2021-03-07 07:57:17 +01:00
parent 3dd99a44cb
commit 2eebe44f87
6 changed files with 89 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
<script context="module" lang="ts">
function createTabComponent(selectedTab) {
const tabComponent = tabs[selectedTab.tabComponent];
const tabComponent = tabs[selectedTab.tabComponent]?.default;
if (tabComponent) {
return {
tabComponent,

View File

@@ -9,6 +9,7 @@ interface TabDefinition {
selected: boolean;
busy: boolean;
tabid: string;
tabComponent: string;
}
export function writableWithStorage<T>(defaultValue: T, storageName) {

View File

View File

@@ -1,3 +1,8 @@
<script lang="ts" context="module">
export const matchingProps = ['conid', 'database', 'schemaName', 'pureName'];
export const allowAddToFavorites = props => true;
</script>
<script lang="ts">
import App from '../App.svelte';
import TableDataGrid from '../datagrid/TableDataGrid.svelte';

View File

@@ -1,4 +1,4 @@
import TableDataTab from './TableDataTab.svelte';
import * as TableDataTab from './TableDataTab.svelte';
// import ViewDataTab from './ViewDataTab';
// import TableStructureTab from './TableStructureTab';
// import QueryTab from './QueryTab';

View File

@@ -1,15 +1,93 @@
import _ from 'lodash';
import uuidv1 from 'uuid/v1';
import { get } from 'svelte/store';
import { openedTabs } from '../stores';
import tabs from '../tabs';
import { setSelectedTabFunc } from './common';
import localforage from 'localforage';
import stableStringify from 'json-stable-stringify';
function findFreeNumber(numbers: number[]) {
if (numbers.length == 0) return 1;
return _.max(numbers) + 1;
// let res = 1;
// while (numbers.includes(res)) res += 1;
// return res;
}
export default async function openNewTab(newTab, initialData = undefined, options = undefined) {
// console.log('OPENING NEW TAB', newTab);
const oldTabs = get(openedTabs);
let existing = null;
const { savedFile, savedFolder, savedFilePath } = newTab.props || {};
if (savedFile || savedFilePath) {
existing = oldTabs.find(
x =>
x.props &&
x.tabComponent == newTab.tabComponent &&
x.closedTime == null &&
x.props.savedFile == savedFile &&
x.props.savedFolder == savedFolder &&
x.props.savedFilePath == savedFilePath
);
}
const { forceNewTab } = options || {};
const component = tabs[newTab.tabComponent];
if (!existing && !forceNewTab && component && component.matchingProps) {
const testString = stableStringify(_.pick(newTab.props || {}, component.matchingProps));
existing = oldTabs.find(
x =>
x.props &&
x.tabComponent == newTab.tabComponent &&
x.closedTime == null &&
stableStringify(_.pick(x.props || {}, component.matchingProps)) == testString
);
}
if (existing) {
openedTabs.update(tabs => setSelectedTabFunc(tabs, existing.tabid));
return;
}
// new tab will be created
if (newTab.title.endsWith('#')) {
const numbers = oldTabs
.filter(x => x.closedTime == null && x.title && x.title.startsWith(newTab.title))
.map(x => parseInt(x.title.substring(newTab.title.length)));
newTab.title = `${newTab.title}${findFreeNumber(numbers)}`;
}
const tabid = uuidv1();
openedTabs.update(tabs => [
...(tabs || []).map(x => ({ ...x, selected: false })),
if (initialData) {
for (const key of _.keys(initialData)) {
if (key == 'editor') {
await localforage.setItem(`tabdata_${key}_${tabid}`, initialData[key]);
} else {
localStorage.setItem(`tabdata_${key}_${tabid}`, JSON.stringify(initialData[key]));
}
}
}
openedTabs.update(files => [
...(files || []).map(x => ({ ...x, selected: false })),
{
tabid,
selected: true,
...newTab,
},
]);
// console.log('OPENING NEW TAB', newTab);
// const tabid = uuidv1();
// openedTabs.update(tabs => [
// ...(tabs || []).map(x => ({ ...x, selected: false })),
// {
// tabid,
// selected: true,
// ...newTab,
// },
// ]);
}