perspective designer auto arrange

This commit is contained in:
Jan Prochazka
2022-08-27 09:19:17 +02:00
parent 728ad21d2f
commit 9e3a457ef5
5 changed files with 76 additions and 27 deletions

View File

@@ -89,6 +89,7 @@ export interface PerspectiveReferenceConfig {
export interface PerspectiveConfig { export interface PerspectiveConfig {
rootDesignerId: string; rootDesignerId: string;
isArranged: boolean;
nodes: PerspectiveNodeConfig[]; nodes: PerspectiveNodeConfig[];
references: PerspectiveReferenceConfig[]; references: PerspectiveReferenceConfig[];
} }
@@ -115,6 +116,7 @@ export function createPerspectiveConfig(rootObject: { schemaName?: string; pureN
nodes: [rootNode], nodes: [rootNode],
references: [], references: [],
rootDesignerId: rootNode.designerId, rootDesignerId: rootNode.designerId,
isArranged: true,
}; };
} }

View File

@@ -175,8 +175,8 @@
canvasHeight = Math.max(3000, maxY + 50); canvasHeight = Math.max(3000, maxY + 50);
} }
function callChange(changeFunc, skipUndoChain = undefined) { function callChange(changeFunc, skipUndoChain = undefined, settings = undefined) {
onChange(changeFunc, skipUndoChain); onChange(changeFunc, skipUndoChain, settings);
tick().then(recomputeReferencePositions); tick().then(recomputeReferencePositions);
} }
@@ -665,7 +665,8 @@
// layout.print(); // layout.print();
callChange(current => { callChange(
current => {
return { return {
...current, ...current,
tables: (current?.tables || []).map(table => { tables: (current?.tables || []).map(table => {
@@ -684,7 +685,10 @@
}; };
}), }),
}; };
}, skipUndoChain); },
skipUndoChain,
{ isCalledFromArrange: true }
);
} }
export async function exportDiagram() { export async function exportDiagram() {

View File

@@ -1,7 +1,8 @@
<script lang="ts"> <script lang="ts">
import { MultipleDatabaseInfo, PerspectiveConfig } from 'dbgate-datalib'; import { MultipleDatabaseInfo, PerspectiveConfig } from 'dbgate-datalib';
import _ from 'lodash'; import _ from 'lodash';
import { onMount } from 'svelte'; import { tick } from 'svelte';
import runCommand from '../commands/runCommand';
import Designer from '../designer/Designer.svelte'; import Designer from '../designer/Designer.svelte';
import QueryDesignerReference from '../designer/QueryDesignerReference.svelte'; import QueryDesignerReference from '../designer/QueryDesignerReference.svelte';
@@ -36,14 +37,21 @@
}; };
} }
function handleChange(value) { function handleChange(value, skipUndoChain, settings) {
onChange(oldValue => { onChange(oldValue => {
const newValue = _.isFunction(value) ? value(createDesignerModel(oldValue, dbInfos)) : value; const newValue = _.isFunction(value) ? value(createDesignerModel(oldValue, dbInfos)) : value;
return { let isArranged = oldValue.isArranged;
if (settings?.isCalledFromArrange) {
isArranged = true;
}
const res = {
...oldValue, ...oldValue,
nodes: oldValue.nodes.map(node => { nodes: oldValue.nodes.map(node => {
const table = newValue.tables?.find(x => x.designerId == node.designerId); const table = newValue.tables?.find(x => x.designerId == node.designerId);
if (table) { if (table && (table.left != node.position?.x || table.top != node.position?.y)) {
if (!settings?.isCalledFromArrange) {
isArranged = false;
}
return { return {
...node, ...node,
position: { x: table.left, y: table.top }, position: { x: table.left, y: table.top },
@@ -52,8 +60,19 @@
return node; return node;
}), }),
}; };
res.isArranged = isArranged;
return res;
}); });
} }
async function detectAutoArrange(config: PerspectiveConfig, dbInfos) {
if (config.isArranged && config.nodes.find(x => !x.position)) {
await tick();
runCommand('designer.arrange');
}
}
$: detectAutoArrange(config, dbInfos);
</script> </script>
<Designer <Designer

View File

@@ -11,6 +11,17 @@
testEnabled: () => getCurrentEditor() != null, testEnabled: () => getCurrentEditor() != null,
onClick: () => getCurrentEditor().defineCustomJoin(), onClick: () => getCurrentEditor().defineCustomJoin(),
}); });
registerCommand({
id: 'perspective.arrange',
category: 'Perspective',
icon: 'icon arrange',
name: 'Arrange',
toolbar: true,
isRelatedToTab: true,
testEnabled: () => getCurrentEditor()?.canArrange(),
onClick: () => getCurrentEditor().arrange(),
});
</script> </script>
<script lang="ts"> <script lang="ts">
@@ -57,6 +68,7 @@
import { useMultipleDatabaseInfo } from '../utility/useMultipleDatabaseInfo'; import { useMultipleDatabaseInfo } from '../utility/useMultipleDatabaseInfo';
import VerticalSplitter from '../elements/VerticalSplitter.svelte'; import VerticalSplitter from '../elements/VerticalSplitter.svelte';
import PerspectiveDesigner from './PerspectiveDesigner.svelte'; import PerspectiveDesigner from './PerspectiveDesigner.svelte';
import runCommand from '../commands/runCommand';
const dbg = debug('dbgate:PerspectiveView'); const dbg = debug('dbgate:PerspectiveView');
@@ -96,6 +108,18 @@
}); });
} }
export function canArrange() {
return !config.isArranged;
}
export function arrange() {
// setConfig(cfg => ({
// ...cfg,
// isArranged: true,
// }));
runCommand('designer.arrange');
}
let perspectiveDatabases = extractPerspectiveDatabases({ conid, database }, config); let perspectiveDatabases = extractPerspectiveDatabases({ conid, database }, config);
$: { $: {
const newDatabases = extractPerspectiveDatabases({ conid, database }, config); const newDatabases = extractPerspectiveDatabases({ conid, database }, config);

View File

@@ -134,7 +134,7 @@
/> />
<svelte:fragment slot="toolstrip"> <svelte:fragment slot="toolstrip">
<ToolStripCommandButton command="designer.arrange" /> <ToolStripCommandButton command="perspective.arrange" />
<ToolStripCommandButton command="perspective.refresh" /> <ToolStripCommandButton command="perspective.refresh" />
<ToolStripCommandButton command="perspective.customJoin" /> <ToolStripCommandButton command="perspective.customJoin" />
<ToolStripSaveButton idPrefix="perspective" /> <ToolStripSaveButton idPrefix="perspective" />