activator

This commit is contained in:
Jan Prochazka 2021-04-07 20:51:07 +02:00
parent 0dac1ada5f
commit 54cf6ad411
2 changed files with 56 additions and 18 deletions

View File

@ -1,7 +1,5 @@
<script lang="ts" context="module"> <script lang="ts" context="module">
let lastFocusedEditor = null; const getCurrentEditor = () => getActiveComponent('CollectionJsonView');
const getCurrentEditor = () =>
lastFocusedEditor?.getTabId && lastFocusedEditor?.getTabId() == getActiveTabId() ? lastFocusedEditor : null;
registerCommand({ registerCommand({
id: 'dataJson.switchToTable', id: 'dataJson.switchToTable',
@ -16,17 +14,15 @@
<script lang="ts"> <script lang="ts">
import _ from 'lodash'; import _ from 'lodash';
import { getContext, onMount } from 'svelte'; import { onMount } from 'svelte';
import { get_current_component } from 'svelte/internal';
import invalidateCommands from '../commands/invalidateCommands';
import registerCommand from '../commands/registerCommand'; import registerCommand from '../commands/registerCommand';
import ChangeSetGrider from '../datagrid/ChangeSetGrider'; import ChangeSetGrider from '../datagrid/ChangeSetGrider';
import createActivator, { getActiveComponent } from '../utility/createActivator';
import { loadCollectionDataPage } from '../datagrid/CollectionDataGridCore.svelte'; import { loadCollectionDataPage } from '../datagrid/CollectionDataGridCore.svelte';
import LoadingInfo from '../elements/LoadingInfo.svelte'; import LoadingInfo from '../elements/LoadingInfo.svelte';
import Pager from '../elements/Pager.svelte'; import Pager from '../elements/Pager.svelte';
import { getActiveTabId } from '../stores';
import contextMenu, { getContextMenu, registerMenu } from '../utility/contextMenu'; import contextMenu, { getContextMenu, registerMenu } from '../utility/contextMenu';
import CollectionJsonRow from './CollectionJsonRow.svelte'; import CollectionJsonRow from './CollectionJsonRow.svelte';
@ -39,9 +35,7 @@
export let changeSetState; export let changeSetState;
export let dispatchChangeSet; export let dispatchChangeSet;
const instance = get_current_component(); export const activator = createActivator('CollectionJsonView', true);
const tabVisible: any = getContext('tabVisible');
const tabid = getContext('tabid');
let isLoading = false; let isLoading = false;
let loadedTime = null; let loadedTime = null;
@ -62,12 +56,6 @@
loadData(); loadData();
} }
$: if ($tabVisible) lastFocusedEditor = instance;
export function getTabId() {
return tabid;
}
export function switchToTable() { export function switchToTable() {
setConfig(cfg => ({ setConfig(cfg => ({
...cfg, ...cfg,
@ -77,8 +65,6 @@
onMount(() => { onMount(() => {
loadData(); loadData();
if ($tabVisible) lastFocusedEditor = instance;
invalidateCommands();
}); });
registerMenu({ command: 'dataJson.switchToTable' }); registerMenu({ command: 'dataJson.switchToTable' });

View File

@ -0,0 +1,52 @@
import { getContext } from 'svelte';
import { get_current_component, onMount, setContext } from 'svelte/internal';
import invalidateCommands from '../commands/invalidateCommands';
const lastActiveDictionary = {};
export default function createActivator(name, activateOnTabVisible) {
const instance = get_current_component();
const tabVisible: any = getContext('tabVisible');
const tabid = getContext('tabid');
const parentActivatorInstance = getContext('parentActivatorInstance') as any;
setContext('parentActivatorInstance', instance);
let tabVisibleValue;
onMount(() => {
const unsubscribeTabVisible = tabVisible.subscribe(value => {
tabVisibleValue = value;
if (activateOnTabVisible) {
activate();
}
});
invalidateCommands();
return () => {
unsubscribeTabVisible();
};
});
const activate = () => {
lastActiveDictionary[name] = instance;
if (parentActivatorInstance) {
parentActivatorInstance.activator.activate();
}
};
const getTabVisible = () => tabVisibleValue;
return {
activate,
tabid,
getTabVisible,
};
}
export function getActiveComponent(name) {
const current = lastActiveDictionary[name];
if (!current) return null;
if (!current.activator) return null;
if (!current.activator.getTabVisible) return null;
if (!current.activator.getTabVisible()) return null;
return current;
}