1- import { generateUUID , getArrowPoints , calcP1 , calcP4 , setAttributes } from './utils/index'
1+ import { generateUUID , getArrowPoints , setAttributes } from './utils/index'
22import LinkDragMoveHelper from './utils/LinkDragMoveHelper'
33import { findEle } from './utils/dom'
44import { createSvgGroup } from './utils/svg'
@@ -19,11 +19,38 @@ export type LinkItem = {
1919 y : number
2020 }
2121}
22- export type LinkControllerData = {
23- cx : number
24- cy : number
25- w : number
26- h : number
22+ export type DivData = {
23+ cx : number // center x
24+ cy : number // center y
25+ w : number // div width
26+ h : number // div height
27+ }
28+
29+ // calc starting and ending point using control point and div status
30+ function calcP ( data : DivData , ctrlX : number , ctrlY : number ) {
31+ let x , y
32+ const k = ( data . cy - ctrlY ) / ( ctrlX - data . cx )
33+ if ( k > data . h / data . w || k < - data . h / data . w ) {
34+ if ( data . cy - ctrlY < 0 ) {
35+ x = data . cx - data . h / 2 / k
36+ y = data . cy + data . h / 2
37+ } else {
38+ x = data . cx + data . h / 2 / k
39+ y = data . cy - data . h / 2
40+ }
41+ } else {
42+ if ( data . cx - ctrlX < 0 ) {
43+ x = data . cx + data . w / 2
44+ y = data . cy - ( data . w * k ) / 2
45+ } else {
46+ x = data . cx - data . w / 2
47+ y = data . cy + ( data . w * k ) / 2
48+ }
49+ }
50+ return {
51+ x,
52+ y,
53+ }
2754}
2855
2956const createText = function ( string : string , x : number , y : number , color ?: string ) {
@@ -51,6 +78,11 @@ export const createLink = function (this: MindElixirInstance, from: Topic, to: T
5178 const toCenterX = ( pto . x + pto . width / 2 - map . x ) / this . scaleVal
5279 const toCenterY = ( pto . y + pto . height / 2 - map . y ) / this . scaleVal
5380
81+ // p1: starting point
82+ // p2: control point of starting point
83+ // p3: control point of ending point
84+ // p4: ending point
85+
5486 let p2x , p2y , p3x , p3y
5587 if ( isInitPaint && obj ) {
5688 p2x = fromCenterX + obj . delta1 . x
@@ -85,11 +117,11 @@ export const createLink = function (this: MindElixirInstance, from: Topic, to: T
85117 h : pto . height ,
86118 }
87119
88- const p1 = calcP1 ( fromData , p2x , p2y )
120+ const p1 = calcP ( fromData , p2x , p2y )
89121 const p1x = p1 . x
90122 const p1y = p1 . y
91123
92- const p4 = calcP4 ( toData , p3x , p3y )
124+ const p4 = calcP ( toData , p3x , p3y )
93125 const p4x = p4 . x
94126 const p4y = p4 . y
95127
@@ -116,7 +148,7 @@ export const createLink = function (this: MindElixirInstance, from: Topic, to: T
116148
117149 const halfx = p1x / 8 + ( p2x * 3 ) / 8 + ( p3x * 3 ) / 8 + p4x / 8
118150 const halfy = p1y / 8 + ( p2y * 3 ) / 8 + ( p3y * 3 ) / 8 + p4y / 8
119- const label = createText ( newLinkObj . label , halfx , halfy )
151+ const label = createText ( newLinkObj . label , halfx , halfy , this . theme . cssVar [ '--color' ] )
120152 newSvgGroup . appendChild ( label )
121153
122154 if ( isInitPaint && obj ) {
@@ -200,31 +232,35 @@ export const showLinkController = function (
200232 p3x : number ,
201233 p3y : number ,
202234 linkItem : LinkItem ,
203- fromData : LinkControllerData ,
204- toData : LinkControllerData
235+ fromData : DivData ,
236+ toData : DivData
205237) {
206238 this . linkController . style . display = 'initial'
207239 this . P2 . style . display = 'initial'
208240 this . P3 . style . display = 'initial'
209241
210- const p1 = calcP1 ( fromData , p2x , p2y )
242+ const p1 = calcP ( fromData , p2x , p2y )
211243 let p1x = p1 . x
212244 let p1y = p1 . y
213245
214- const p4 = calcP4 ( toData , p3x , p3y )
246+ const p4 = calcP ( toData , p3x , p3y )
215247 let p4x = p4 . x
216248 let p4y = p4 . y
217249
218250 this . P2 . style . cssText = `top:${ p2y } px;left:${ p2x } px;`
219251 this . P3 . style . cssText = `top:${ p3y } px;left:${ p3x } px;`
220- this . line1 . setAttribute ( 'x1' , p1x )
221- this . line1 . setAttribute ( 'y1' , p1y )
222- this . line1 . setAttribute ( 'x2' , p2x )
223- this . line1 . setAttribute ( 'y2' , p2y )
224- this . line2 . setAttribute ( 'x1' , p3x )
225- this . line2 . setAttribute ( 'y1' , p3y )
226- this . line2 . setAttribute ( 'x2' , p4x )
227- this . line2 . setAttribute ( 'y2' , p4y )
252+ setAttributes ( this . line1 , {
253+ x1 : p1x + '' ,
254+ y1 : p1y + '' ,
255+ x2 : p2x + '' ,
256+ y2 : p2y + '' ,
257+ } )
258+ setAttributes ( this . line2 , {
259+ x1 : p3x + '' ,
260+ y1 : p3y + '' ,
261+ x2 : p4x + '' ,
262+ y2 : p4y + '' ,
263+ } )
228264
229265 if ( this . helper1 ) {
230266 this . helper1 . destory ( this . map )
@@ -242,7 +278,7 @@ export const showLinkController = function (
242278 p2x = p2x - deltaX / this . scaleVal
243279 p2y = p2y - deltaY / this . scaleVal
244280
245- const p1 = calcP1 ( fromData , p2x , p2y )
281+ const p1 = calcP ( fromData , p2x , p2y )
246282 p1x = p1 . x
247283 p1y = p1 . y
248284
@@ -256,10 +292,12 @@ export const showLinkController = function (
256292 x : halfx + '' ,
257293 y : halfy + '' ,
258294 } )
259- this . line1 . setAttribute ( 'x1' , p1x )
260- this . line1 . setAttribute ( 'y1' , p1y )
261- this . line1 . setAttribute ( 'x2' , p2x )
262- this . line1 . setAttribute ( 'y2' , p2y )
295+ setAttributes ( this . line1 , {
296+ x1 : p1x + '' ,
297+ y1 : p1y + '' ,
298+ x2 : p2x + '' ,
299+ y2 : p2y + '' ,
300+ } )
263301 linkItem . delta1 . x = p2x - fromData . cx
264302 linkItem . delta1 . y = p2y - fromData . cy
265303 } )
@@ -268,7 +306,7 @@ export const showLinkController = function (
268306 p3x = p3x - deltaX / this . scaleVal
269307 p3y = p3y - deltaY / this . scaleVal
270308
271- const p4 = calcP4 ( toData , p3x , p3y )
309+ const p4 = calcP ( toData , p3x , p3y )
272310 p4x = p4 . x
273311 p4y = p4 . y
274312 const arrowPoint = getArrowPoints ( p3x , p3y , p4x , p4y )
@@ -284,11 +322,21 @@ export const showLinkController = function (
284322 x : halfx + '' ,
285323 y : halfy + '' ,
286324 } )
287- this . line2 . setAttribute ( 'x1' , p3x )
288- this . line2 . setAttribute ( 'y1' , p3y )
289- this . line2 . setAttribute ( 'x2' , p4x )
290- this . line2 . setAttribute ( 'y2' , p4y )
325+ setAttributes ( this . line2 , {
326+ x1 : p3x + '' ,
327+ y1 : p3y + '' ,
328+ x2 : p4x + '' ,
329+ y2 : p4y + '' ,
330+ } )
291331 linkItem . delta2 . x = p3x - toData . cx
292332 linkItem . delta2 . y = p3y - toData . cy
293333 } )
294334}
335+
336+ export function renderCustomLink ( this : MindElixirInstance ) {
337+ this . linkSvgGroup . innerHTML = ''
338+ for ( const prop in this . linkData ) {
339+ const link = this . linkData [ prop ]
340+ this . createLink ( findEle ( link . from ) , findEle ( link . to ) , true , link )
341+ }
342+ }
0 commit comments