Browse Source

feat(nc-gui): show relations on edge hover

pull/4071/head
braks 2 years ago
parent
commit
8ce6904edb
  1. 49
      packages/nc-gui/components/erd/utils.ts

49
packages/nc-gui/components/erd/utils.ts

@ -97,12 +97,36 @@ export function useErdElements(tables: MaybeRef<TableType[]>, props: MaybeRef<Er
}, [] as Relation[]), }, [] as Relation[]),
) )
const edgeMMTableLabel = (modelId?: string) => { function edgeLabel({ type, source, target, modelId, childColId, parentColId }: Relation) {
if (!modelId) return '' const typeLabel = type === 'mm' ? 'many to many' : 'has many'
const parentCol = metasWithIdAsKey.value[source].columns?.find((col) => {
const colOptions = col.colOptions as LinkToAnotherRecordType
if (!colOptions) return false
return (
colOptions.fk_child_column_id === childColId &&
colOptions.fk_parent_column_id === parentColId &&
colOptions.fk_mm_model_id === modelId
)
})
const childCol = metasWithIdAsKey.value[target].columns?.find((col) => {
const colOptions = col.colOptions as LinkToAnotherRecordType
if (!colOptions) return false
return colOptions.fk_parent_column_id === (type === 'mm' ? childColId : parentColId)
})
if (!parentCol || !childCol) return
if (type === 'mm') {
if (config.showJunctionTableNames) {
if (!modelId) return undefined
const mmModel = metasWithIdAsKey.value[modelId] const mmModel = metasWithIdAsKey.value[modelId]
if (!mmModel) return '' if (!mmModel) return undefined
if (mmModel.title !== mmModel.table_name) { if (mmModel.title !== mmModel.table_name) {
return `${mmModel.title} (${mmModel.table_name})` return `${mmModel.title} (${mmModel.table_name})`
@ -110,6 +134,10 @@ export function useErdElements(tables: MaybeRef<TableType[]>, props: MaybeRef<Er
return mmModel.title return mmModel.title
} }
}
return `${parentCol.title} ${typeLabel} ${childCol.title}`
}
function createNodes() { function createNodes() {
return erdTables.value.flatMap((table) => { return erdTables.value.flatMap((table) => {
@ -139,7 +167,6 @@ export function useErdElements(tables: MaybeRef<TableType[]>, props: MaybeRef<Er
function createEdges() { function createEdges() {
return relations.value.reduce<Edge[]>((acc, { source, target, childColId, parentColId, type, modelId }) => { return relations.value.reduce<Edge[]>((acc, { source, target, childColId, parentColId, type, modelId }) => {
let sourceColumnId, targetColumnId let sourceColumnId, targetColumnId
let edgeLabel = ''
if (type === 'hm') { if (type === 'hm') {
sourceColumnId = childColId sourceColumnId = childColId
@ -149,13 +176,19 @@ export function useErdElements(tables: MaybeRef<TableType[]>, props: MaybeRef<Er
if (type === 'mm') { if (type === 'mm') {
sourceColumnId = parentColId sourceColumnId = parentColId
targetColumnId = childColId targetColumnId = childColId
edgeLabel = config.showJunctionTableNames ? edgeMMTableLabel(modelId) : ''
} }
if (source !== target) dagreGraph.setEdge(source, target) if (source !== target) dagreGraph.setEdge(source, target)
acc.push({ acc.push({
id: `e-${sourceColumnId}-${source}-${targetColumnId}-${target}-#${edgeLabel}`, id: `e-${sourceColumnId}-${source}-${targetColumnId}-${target}-#${edgeLabel({
source,
target,
type,
childColId,
parentColId,
modelId,
})}`,
source: `${source}`, source: `${source}`,
target: `${target}`, target: `${target}`,
sourceHandle: `s-${sourceColumnId}-${source}`, sourceHandle: `s-${sourceColumnId}-${source}`,
@ -168,7 +201,7 @@ export function useErdElements(tables: MaybeRef<TableType[]>, props: MaybeRef<Er
data: { data: {
isManyToMany: type === 'mm', isManyToMany: type === 'mm',
isSelfRelation: source === target && sourceColumnId === targetColumnId, isSelfRelation: source === target && sourceColumnId === targetColumnId,
label: edgeLabel, label: edgeLabel({ type, source, target, childColId, parentColId, modelId }),
}, },
} as Edge) } as Edge)
@ -221,6 +254,8 @@ export function useErdElements(tables: MaybeRef<TableType[]>, props: MaybeRef<Er
elements.value.forEach((el) => { elements.value.forEach((el) => {
if (isNode(el)) { if (isNode(el)) {
console.log(el.data.columnLength)
dagreGraph.setNode(el.id, { dagreGraph.setNode(el.id, {
width: skeleton ? nodeWidth * 2.5 : nodeWidth, width: skeleton ? nodeWidth * 2.5 : nodeWidth,
height: nodeHeight + (skeleton ? 250 : nodeHeight * el.data.columnLength), height: nodeHeight + (skeleton ? 250 : nodeHeight * el.data.columnLength),

Loading…
Cancel
Save