drag & drop references

This commit is contained in:
Jan Prochazka 2021-03-18 18:34:36 +01:00
parent 9e9df60d37
commit 47a045fc24
5 changed files with 161 additions and 23 deletions

View File

@ -11,13 +11,55 @@
export let designerId; export let designerId;
export let onChangeColumn; export let onChangeColumn;
export let domLine; export let domLine;
export let sourceDragColumn$;
export let targetDragColumn$;
export let onCreateReference;
$: designerColumn = (designer.columns || []).find( $: designerColumn = (designer.columns || []).find(
x => x.designerId == designerId && x.columnName == column.columnName x => x.designerId == designerId && x.columnName == column.columnName
); );
</script> </script>
<div bind:this={domLine}> <div
class="line"
bind:this={domLine}
draggable={true}
on:dragstart={e => {
const dragData = {
...column,
designerId,
};
sourceDragColumn$.set(dragData);
e.dataTransfer.setData('designer_column_drag_data', JSON.stringify(dragData));
}}
on:dragend={e => {
sourceDragColumn$.set(null);
targetDragColumn$.set(null);
}}
on:dragover={e => {
if ($sourceDragColumn$) {
e.preventDefault();
targetDragColumn$.set({
...column,
designerId,
});
}
}}
on:drop={e => {
var data = e.dataTransfer.getData('designer_column_drag_data');
e.preventDefault();
if (!data) return;
onCreateReference($sourceDragColumn$, $targetDragColumn$);
sourceDragColumn$.set(null);
targetDragColumn$.set(null);
}}
class:isDragSource={$sourceDragColumn$ &&
$sourceDragColumn$.designerId == designerId &&
$sourceDragColumn$.columnName == column.columnName}
class:isDragTarget={$targetDragColumn$ &&
$targetDragColumn$.designerId == designerId &&
$targetDragColumn$.columnName == column.columnName}
>
<CheckboxField <CheckboxField
checked={!!(designer.columns || []).find( checked={!!(designer.columns || []).find(
x => x.designerId == designerId && x.columnName == column.columnName && x.isOutput x => x.designerId == designerId && x.columnName == column.columnName && x.isOutput
@ -56,3 +98,15 @@
<FontIcon icon="img group" /> <FontIcon icon="img group" />
{/if} {/if}
</div> </div>
<style>
.line:hover {
background: var(--theme-bg-1);
}
.line.isDragSource {
background: var(--theme-bg-gold);
}
.line.isDragTarget {
background: var(--theme-bg-gold);
}
</style>

View File

@ -10,6 +10,8 @@
import _ from 'lodash'; import _ from 'lodash';
import createRef from '../utility/createRef'; import createRef from '../utility/createRef';
import DesignerReference from './DesignerReference.svelte'; import DesignerReference from './DesignerReference.svelte';
import { writable } from 'svelte/store';
import { tick } from 'svelte';
export let value; export let value;
export let onChange; export let onChange;
@ -18,6 +20,9 @@
let domCanvas; let domCanvas;
const sourceDragColumn$ = writable(null);
const targetDragColumn$ = writable(null);
$: tables = value?.tables as any[]; $: tables = value?.tables as any[];
$: references = value?.references as any[]; $: references = value?.references as any[];
@ -40,15 +45,20 @@
return tables; return tables;
} }
function callChange(changeFunc, skipUndoChain = undefined) {
onChange(changeFunc, skipUndoChain);
tick().then(recomputeReferencePositions);
}
const changeTable = table => { const changeTable = table => {
onChange(current => ({ callChange(current => ({
...current, ...current,
tables: fixPositions((current.tables || []).map(x => (x.designerId == table.designerId ? table : x))), tables: fixPositions((current.tables || []).map(x => (x.designerId == table.designerId ? table : x))),
})); }));
}; };
const bringToFront = table => { const bringToFront = table => {
onChange( callChange(
current => ({ current => ({
...current, ...current,
tables: [...(current.tables || []).filter(x => x.designerId != table.designerId), table], tables: [...(current.tables || []).filter(x => x.designerId != table.designerId), table],
@ -58,7 +68,7 @@
}; };
const removeTable = table => { const removeTable = table => {
onChange(current => ({ callChange(current => ({
...current, ...current,
tables: (current.tables || []).filter(x => x.designerId != table.designerId), tables: (current.tables || []).filter(x => x.designerId != table.designerId),
references: (current.references || []).filter( references: (current.references || []).filter(
@ -69,21 +79,21 @@
}; };
const changeReference = ref => { const changeReference = ref => {
onChange(current => ({ callChange(current => ({
...current, ...current,
references: (current.references || []).map(x => (x.designerId == ref.designerId ? ref : x)), references: (current.references || []).map(x => (x.designerId == ref.designerId ? ref : x)),
})); }));
}; };
const removeReference = ref => { const removeReference = ref => {
onChange(current => ({ callChange(current => ({
...current, ...current,
references: (current.references || []).filter(x => x.designerId != ref.designerId), references: (current.references || []).filter(x => x.designerId != ref.designerId),
})); }));
}; };
const handleCreateReference = (source, target) => { const handleCreateReference = (source, target) => {
onChange(current => { callChange(current => {
const existingReference = (current.references || []).find( const existingReference = (current.references || []).find(
x => x =>
(x.sourceId == source.designerId && x.targetId == target.designerId) || (x.sourceId == source.designerId && x.targetId == target.designerId) ||
@ -139,7 +149,7 @@
schemaName: foreignKey.refSchemaName, schemaName: foreignKey.refSchemaName,
}); });
const newTableDesignerId = uuidv1(); const newTableDesignerId = uuidv1();
onChange(current => { callChange(current => {
const fromTable = (current.tables || []).find(x => x.designerId == designerId); const fromTable = (current.tables || []).find(x => x.designerId == designerId);
if (!fromTable) return; if (!fromTable) return;
return { return {
@ -171,7 +181,7 @@
}; };
const handleSelectColumn = column => { const handleSelectColumn = column => {
onChange( callChange(
current => ({ current => ({
...current, ...current,
columns: (current.columns || []).find( columns: (current.columns || []).find(
@ -185,7 +195,7 @@
}; };
const handleChangeColumn = (column, changeFunc) => { const handleChangeColumn = (column, changeFunc) => {
onChange(current => { callChange(current => {
const currentColumns = (current || {}).columns || []; const currentColumns = (current || {}).columns || [];
const existing = currentColumns.find(x => x.designerId == column.designerId && x.columnName == column.columnName); const existing = currentColumns.find(x => x.designerId == column.designerId && x.columnName == column.columnName);
if (existing) { if (existing) {
@ -214,7 +224,7 @@
json.left = e.clientX - rect.left; json.left = e.clientX - rect.left;
json.top = e.clientY - rect.top; json.top = e.clientY - rect.top;
onChange(current => { callChange(current => {
const foreignKeys = _.compact([ const foreignKeys = _.compact([
...(json.foreignKeys || []).map(fk => { ...(json.foreignKeys || []).map(fk => {
const tables = ((current || {}).tables || []).filter( const tables = ((current || {}).tables || []).filter(
@ -267,9 +277,9 @@
}); });
}; };
function handleMoveReferences() { function recomputeReferencePositions() {
for(const ref of Object.values(referenceRefs) as any[]) { for (const ref of Object.values(referenceRefs) as any[]) {
ref.recomputePosition(); if (ref) ref.recomputePosition();
} }
} }
</script> </script>
@ -308,13 +318,15 @@
onSelectColumn={handleSelectColumn} onSelectColumn={handleSelectColumn}
onChangeColumn={handleChangeColumn} onChangeColumn={handleChangeColumn}
onAddReferenceByColumn={handleAddReferenceByColumn} onAddReferenceByColumn={handleAddReferenceByColumn}
onMoveReferences={handleMoveReferences} onMoveReferences={recomputeReferencePositions}
{table} {table}
onChangeTable={changeTable} onChangeTable={changeTable}
onBringToFront={bringToFront} onBringToFront={bringToFront}
onRemoveTable={removeTable} onRemoveTable={removeTable}
{domCanvas} {domCanvas}
designer={value} designer={value}
{sourceDragColumn$}
{targetDragColumn$}
/> />
{/each} {/each}
</div> </div>

View File

@ -1,5 +1,7 @@
<script lang="ts"> <script lang="ts">
import _ from 'lodash'; import _ from 'lodash';
import { isConnectedByReference } from './designerTools';
import contextMenu from '../utility/contextMenu';
export let reference; export let reference;
export let onRemoveReference; export let onRemoveReference;
@ -16,8 +18,6 @@
const buswi = 10; const buswi = 10;
const extwi = 25; const extwi = 25;
// const lineStyle = { fill: 'none', stroke: 'black', strokeWidth: 2 };
export function recomputePosition() { export function recomputePosition() {
const { designerId, sourceId, targetId, columns, joinType } = reference; const { designerId, sourceId, targetId, columns, joinType } = reference;
@ -60,6 +60,35 @@
domTables; domTables;
recomputePosition(); recomputePosition();
} }
function createMenu() {
const isConnected = isConnectedByReference(
designer,
{ designerId: reference?.sourceId },
{ designerId: reference?.targetId },
reference
);
const setJoinType = joinType => {
onChangeReference({
...reference,
joinType,
});
};
return [
{ text: 'Remove', onClick: () => onRemoveReference(reference) },
!isConnected && [
{ divider: true },
{ onClick: () => setJoinType('INNER JOIN'), text: 'Set INNER JOIN' },
{ onClick: () => setJoinType('LEFT JOIN'), text: 'Set LEFT JOIN' },
{ onClick: () => setJoinType('RIGHT JOIN'), text: 'Set RIGHT JOIN' },
{ onClick: () => setJoinType('FULL OUTER JOIN'), text: 'Set FULL OUTER JOIN' },
{ onClick: () => setJoinType('CROSS JOIN'), text: 'Set CROSS JOIN' },
{ onClick: () => setJoinType('WHERE EXISTS'), text: 'Set WHERE EXISTS' },
{ onClick: () => setJoinType('WHERE NOT EXISTS'), text: 'Set WHERE NOT EXISTS' },
],
];
}
</script> </script>
{#if src && dst && minpos} {#if src && dst && minpos}
@ -88,7 +117,20 @@
`} `}
/> />
{/each} {/each}
</svg>> </svg>
<div
use:contextMenu={createMenu}
class="wrapper"
style={`left: ${(src.x + extwi * minpos.dirsrc + dst.x + extwi * minpos.dirdst) / 2 - 16}px;
top: ${(src.y + dst.y) / 2 - 16}px`}
>
<div class="text">
{_.snakeCase(reference?.joinType || 'CROSS JOIN')
.replace('_', '\xa0')
.replace('_', '\xa0')}
</div>
</div>
{/if} {/if}
<style> <style>
@ -106,4 +148,25 @@
stroke: var(--theme-bg-4); stroke: var(--theme-bg-4);
stroke-width: 2; stroke-width: 2;
} }
.wrapper {
position: absolute;
border: 1px solid var(--theme-border);
background-color: var(--theme-bg-1);
z-index: 900;
border-radius: 10px;
width: 32px;
height: 32px;
}
.text {
position: relative;
float: left;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 900;
white-space: nowrap;
background-color: var(--theme-bg-1);
}
</style> </style>

View File

@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { tick } from 'svelte'; import { tick } from 'svelte';
import FontIcon from '../icons/FontIcon.svelte'; import FontIcon from '../icons/FontIcon.svelte';
import moveDrag from '../utility/moveDrag'; import moveDrag from '../utility/moveDrag';
@ -14,11 +14,16 @@ import { tick } from 'svelte';
export let onAddReferenceByColumn; export let onAddReferenceByColumn;
export let onSelectColumn; export let onSelectColumn;
export let onChangeColumn; export let onChangeColumn;
export let sourceDragColumn;
export let setSourceDragColumn; // export let sourceDragColumn;
export let targetDragColumn; // export let setSourceDragColumn;
export let setTargetDragColumn; // export let targetDragColumn;
// export let setTargetDragColumn;
// export let onChangeDomTable; // export let onChangeDomTable;
export let sourceDragColumn$;
export let targetDragColumn$;
export let domCanvas; export let domCanvas;
export let domTablesRef; export let domTablesRef;
export let designer; export let designer;
@ -86,6 +91,9 @@ import { tick } from 'svelte';
{designer} {designer}
{designerId} {designerId}
{onChangeColumn} {onChangeColumn}
{sourceDragColumn$}
{targetDragColumn$}
{onCreateReference}
bind:domLine={columnRefs[column.columnName]} bind:domLine={columnRefs[column.columnName]}
/> />
{/each} {/each}

View File

@ -3,6 +3,7 @@ export default function moveDrag(node, [onStart, onMove, onEnd]) {
let startY = null; let startY = null;
const handleMoveDown = e => { const handleMoveDown = e => {
if (e.button != 0) return;
startX = e.clientX; startX = e.clientX;
startY = e.clientY; startY = e.clientY;
document.addEventListener('mousemove', handleMoveMove, true); document.addEventListener('mousemove', handleMoveMove, true);