imrpoved closed tabs order algorithm

This commit is contained in:
Jan Prochazka 2021-01-24 10:10:07 +01:00
parent 69fd9bbc67
commit 0e6b8b4f73
5 changed files with 24 additions and 39 deletions

View File

@ -9,6 +9,7 @@ import { FontIcon } from './icons';
import useTheme from './theme/useTheme'; import useTheme from './theme/useTheme';
import usePropsCompare from './utility/usePropsCompare'; import usePropsCompare from './utility/usePropsCompare';
import { useShowMenu } from './modals/showMenu'; import { useShowMenu } from './modals/showMenu';
import { setSelectedTabFunc } from './utility/common';
// const files = [ // const files = [
// { name: 'app.js' }, // { name: 'app.js' },
@ -140,19 +141,12 @@ export default function TabsPanel() {
if (e.target.closest('.tabCloseButton')) { if (e.target.closest('.tabCloseButton')) {
return; return;
} }
setOpenedTabs(files => setOpenedTabs(files => setSelectedTabFunc(files, tabid));
files.map(x => ({
...x,
selected: x.tabid == tabid,
}))
);
}; };
const closeTabFunc = closeCondition => tabid => { const closeTabFunc = closeCondition => tabid => {
setOpenedTabs(files => { setOpenedTabs(files => {
const active = files.find(x => x.tabid == tabid); const active = files.find(x => x.tabid == tabid);
if (!active) return files; if (!active) return files;
const lastSelectedIndex = _.findIndex(files, x => x.tabid == tabid);
let selectedIndex = lastSelectedIndex;
const newFiles = files.map(x => ({ const newFiles = files.map(x => ({
...x, ...x,
@ -163,25 +157,12 @@ export default function TabsPanel() {
return newFiles; return newFiles;
} }
while (selectedIndex >= 0 && newFiles[selectedIndex].closedTime) selectedIndex -= 1; const selectedIndex = _.findLastIndex(newFiles, x => x.closedTime == null);
if (selectedIndex < 0) {
selectedIndex = lastSelectedIndex;
while (selectedIndex < newFiles.length && newFiles[selectedIndex].closedTime) selectedIndex += 1;
}
if (selectedIndex < 0 || selectedIndex >= newFiles.length)
selectedIndex = _.findIndex(newFiles, x => x.closedTime == null);
return newFiles.map((x, index) => ({ return newFiles.map((x, index) => ({
...x, ...x,
selected: index == selectedIndex, selected: index == selectedIndex,
})); }));
// if (selectedIndex != lastSelectedIndex) {
// }
// return newFiles;
}); });
}; };

View File

@ -4,6 +4,7 @@ import moment from 'moment';
import { DropDownMenuItem } from '../modals/DropDownMenu'; import { DropDownMenuItem } from '../modals/DropDownMenu';
import { useSetOpenedTabs } from '../utility/globalState'; import { useSetOpenedTabs } from '../utility/globalState';
import { AppObjectCore } from './AppObjectCore'; import { AppObjectCore } from './AppObjectCore';
import { setSelectedTabFunc } from '../utility/common';
function Menu({ data }) { function Menu({ data }) {
const setOpenedTabs = useSetOpenedTabs(); const setOpenedTabs = useSetOpenedTabs();
@ -27,11 +28,15 @@ function ClosedTabAppObject({ data, commonProps }) {
const onClick = () => { const onClick = () => {
setOpenedTabs(files => setOpenedTabs(files =>
files.map(x => ({ setSelectedTabFunc(
files.map(
x => ({
...x, ...x,
selected: x.tabid == tabid,
closedTime: x.tabid == tabid ? undefined : x.closedTime, closedTime: x.tabid == tabid ? undefined : x.closedTime,
})) }),
tabid
)
)
); );
}; };

View File

@ -10,6 +10,7 @@ import useModalState from '../modals/useModalState';
import LoadingInfo from '../widgets/LoadingInfo'; import LoadingInfo from '../widgets/LoadingInfo';
import { useOpenedTabs, useSetOpenedTabs } from '../utility/globalState'; import { useOpenedTabs, useSetOpenedTabs } from '../utility/globalState';
import useOpenNewTab from '../utility/useOpenNewTab'; import useOpenNewTab from '../utility/useOpenNewTab';
import { setSelectedTabFunc } from '../utility/common';
export default function MarkdownEditorTab({ tabid, tabVisible, toolbarPortalRef, ...other }) { export default function MarkdownEditorTab({ tabid, tabVisible, toolbarPortalRef, ...other }) {
const { editorData, setEditorData, isLoading, saveToStorage } = useEditorData({ tabid }); const { editorData, setEditorData, isLoading, saveToStorage } = useEditorData({ tabid });
@ -29,12 +30,7 @@ export default function MarkdownEditorTab({ tabid, tabVisible, toolbarPortalRef,
await saveToStorage(); await saveToStorage();
const existing = (openedTabs || []).find(x => x.props && x.props.sourceTabId == tabid && x.closedTime == null); const existing = (openedTabs || []).find(x => x.props && x.props.sourceTabId == tabid && x.closedTime == null);
if (existing) { if (existing) {
setOpenedTabs(tabs => setOpenedTabs(tabs => setSelectedTabFunc(tabs, existing.tabid));
tabs.map(x => ({
...x,
selected: x.tabid == existing.tabid,
}))
);
} else { } else {
const thisTab = (openedTabs || []).find(x => x.tabid == tabid); const thisTab = (openedTabs || []).find(x => x.tabid == tabid);
openNewTab({ openNewTab({

View File

@ -15,3 +15,10 @@ export function sleep(milliseconds) {
export function changeTab(tabid, setOpenedTabs, changeFunc) { export function changeTab(tabid, setOpenedTabs, changeFunc) {
setOpenedTabs(files => files.map(tab => (tab.tabid == tabid ? changeFunc(tab) : tab))); setOpenedTabs(files => files.map(tab => (tab.tabid == tabid ? changeFunc(tab) : tab)));
} }
export function setSelectedTabFunc(files, tabid) {
return [
...(files || []).filter(x => x.tabid != tabid).map(x => ({ ...x, selected: false })),
...(files || []).filter(x => x.tabid == tabid).map(x => ({ ...x, selected: true })),
];
}

View File

@ -5,6 +5,7 @@ import stableStringify from 'json-stable-stringify';
import _ from 'lodash'; import _ from 'lodash';
import { useOpenedTabs, useSetOpenedTabs } from './globalState'; import { useOpenedTabs, useSetOpenedTabs } from './globalState';
import tabs from '../tabs'; import tabs from '../tabs';
import { setSelectedTabFunc } from './common';
export default function useOpenNewTab() { export default function useOpenNewTab() {
const setOpenedTabs = useSetOpenedTabs(); const setOpenedTabs = useSetOpenedTabs();
@ -37,12 +38,7 @@ export default function useOpenNewTab() {
} }
if (existing) { if (existing) {
setOpenedTabs(tabs => setOpenedTabs(tabs => setSelectedTabFunc(tabs, existing.tabid));
tabs.map(x => ({
...x,
selected: x.tabid == existing.tabid,
}))
);
return; return;
} }