@@ -4,6 +4,7 @@ import { datasourceApi } from '@/api/datasource'
44import { useI18n } from ' vue-i18n'
55import { Graph , Cell , Shape } from ' @antv/x6'
66import type { AnyColumn } from ' element-plus-secondary/es/components/table-v2/src/common.mjs'
7+ import { debounce } from ' lodash-es'
78
89const LINE_HEIGHT = 36
910const NODE_WIDTH = 180
@@ -23,7 +24,9 @@ const emits = defineEmits(['getTableName'])
2324
2425const { t } = useI18n ()
2526const loading = ref (false )
26-
27+ const tooltipY = ref (' -999px' )
28+ const tooltipX = ref (' -999px' )
29+ const tooltipContent = ref (' ' )
2730const nodeIds = ref <any []>([])
2831
2932const cells = ref <Cell []>([])
@@ -43,6 +46,12 @@ const edgeOPtion = {
4346}
4447let graph: any
4548
49+ const resetTooltip = () => {
50+ tooltipY .value = ' -1000px'
51+ tooltipX .value = ' -1000px'
52+ tooltipContent .value = ' '
53+ }
54+
4655const initGraph = () => {
4756 Graph .registerPortLayout (
4857 ' erPortPosition' ,
@@ -196,6 +205,34 @@ const initGraph = () => {
196205 })
197206 })
198207
208+ graph .on (
209+ ' node:port:mouseenter' ,
210+ debounce (({ e , node , port }: any ) => {
211+ tooltipY .value = e .offsetY + ' px'
212+ tooltipX .value = e .offsetX + ' px'
213+ tooltipContent .value = node .port .ports .find (
214+ (ele : any ) => + port === ele .id
215+ ).attrs .portNameLabel .text
216+ }, 100 )
217+ )
218+
219+ graph .on (
220+ ' cell:mouseover' ,
221+ debounce (({ e , cell }: any ) => {
222+ if (cell .store .data .shape === ' edge' ) return
223+ tooltipY .value = e .offsetY + ' px'
224+ tooltipX .value = e .offsetX + ' px'
225+ tooltipContent .value = cell .store .data .attrs ?.label ?.text
226+ }, 100 )
227+ )
228+
229+ graph .on (
230+ ' node:mouseleave' ,
231+ debounce (() => {
232+ resetTooltip ()
233+ }, 100 )
234+ )
235+
199236 graph .on (' node:mouseenter' , ({ node }: any ) => {
200237 node .addTools ({
201238 name: ' button' ,
@@ -224,8 +261,10 @@ const initGraph = () => {
224261 y: 0 ,
225262 offset: { x: 165 , y: 28 },
226263 onClick({ view }: any ) {
264+ node .removeTools ()
227265 graph .removeNode (view .cell .id )
228266 nodeIds .value = nodeIds .value .filter ((ele ) => ele !== view .cell .id )
267+ resetTooltip ()
229268 if (! nodeIds .value .length ) {
230269 graph .dispose ()
231270 graph = null
@@ -267,7 +306,7 @@ const getTableData = () => {
267306 }
268307 })
269308 graph .resetCells (cells .value )
270- graph .zoomToFit ({ padding: 10 , maxScale: 1 })
309+ graph .zoomToFit ({ padding: 100 })
271310 emits (' getTableName' , [... nodeIds .value ])
272311 })
273312 })
@@ -285,13 +324,18 @@ const dragover = () => {
285324 // do
286325}
287326
288- const addNode = (node : any ) => {
327+ const addNode = (node : any , tableX : any , tableY : any ) => {
289328 if (! graph ) {
290329 initGraph ()
291330 }
331+ const { x, y } = graph .pageToLocal (tableX , tableY )
292332 graph .addNode (
293333 graph .createNode ({
294334 ... node ,
335+ position: {
336+ x ,
337+ y ,
338+ },
295339 attrs: {
296340 label: {
297341 text: node .label ,
@@ -322,10 +366,6 @@ const clickTable = (table: any) => {
322366 label: table .table_name ,
323367 width: 150 ,
324368 height: 24 ,
325- position: {
326- x: table .x ,
327- y: table .y ,
328- },
329369 ports: res .map ((ele : any ) => {
330370 return {
331371 id: ele .id ,
@@ -343,7 +383,7 @@ const clickTable = (table: any) => {
343383 }
344384 nodeIds .value = [... nodeIds .value , table .id ]
345385 nextTick (() => {
346- addNode (node )
386+ addNode (node , table . x , table . y )
347387 })
348388 emits (' getTableName' , [... nodeIds .value ])
349389 })
@@ -355,7 +395,11 @@ const clickTable = (table: any) => {
355395const drop = (e : any ) => {
356396 const obj = JSON .parse (e .dataTransfer .getData (' table' ) || ' {}' )
357397 if (! obj .id ) return
358- clickTable ({ ... obj , x: e .layerX , y: e .layerY })
398+ clickTable ({
399+ ... obj ,
400+ x: e .pageX ,
401+ y: e .pageY ,
402+ })
359403}
360404const save = () => {
361405 datasourceApi .relationSave (props .id , graph .toJSON ().cells ).then (() => {
@@ -403,9 +447,31 @@ const save = () => {
403447 {{ t('common.save') }}
404448 </el-button >
405449 </div >
450+ <div class =" tooltip-content" :style =" { top: tooltipY, left: tooltipX, position: 'absolute' }" >
451+ {{ tooltipContent }}
452+ </div >
406453</template >
407454
408455<style lang="less" scoped>
456+ .tooltip-content {
457+ font-weight : 400 ;
458+ direction : ltr ;
459+ font-synthesis : none ;
460+ text-rendering : optimizeLegibility ;
461+ outline : none ;
462+ border-radius : 4px ;
463+ padding : 5px 11px ;
464+ font-size : 12px ;
465+ line-height : 20px ;
466+ min-width : 10px ;
467+ overflow-wrap : break-word ;
468+ word-break : normal ;
469+ visibility : visible ;
470+ color : #fff ;
471+ background : #303133 ;
472+ border : 1px solid #303133 ;
473+ transform : translate (20px , -15px );
474+ }
409475.save-btn {
410476 position : absolute ;
411477 right : 16px ;
0 commit comments