Skip to content

Commit d6f225e

Browse files
committed
refactor: refactor custom link module
1 parent 4b18775 commit d6f225e

File tree

3 files changed

+81
-91
lines changed

3 files changed

+81
-91
lines changed

src/customLink.ts

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { generateUUID, getArrowPoints, calcP1, calcP4, setAttributes } from './utils/index'
1+
import { generateUUID, getArrowPoints, setAttributes } from './utils/index'
22
import LinkDragMoveHelper from './utils/LinkDragMoveHelper'
33
import { findEle } from './utils/dom'
44
import { 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

2956
const 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+
}

src/linkDiv.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,7 @@ const linkDiv = function (this: MindElixirInstance, mainNode?: Wrapper) {
113113
}
114114
}
115115

116-
// 4. generate custom link
117-
this.linkSvgGroup.innerHTML = ''
118-
for (const prop in this.linkData) {
119-
const link = this.linkData[prop]
120-
this.createLink(findEle(link.from), findEle(link.to), true, link)
121-
}
122-
116+
this.renderCustomLink()
123117
this.renderSummary()
124118
console.timeEnd('linkDiv')
125119
}

src/utils/index.ts

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { LinkControllerData } from '../customLink'
1+
import type { DivData } from '../customLink'
22
import type { NodeObj, MindElixirInstance, NodeObjExport } from '../types/index'
33

44
export function encodeHTML(s: string) {
@@ -78,58 +78,6 @@ export function getArrowPoints(p3x: number, p3y: number, p4x: number, p4y: numbe
7878
}
7979
}
8080

81-
export function calcP1(fromData: LinkControllerData, p2x: number, p2y: number) {
82-
let x, y
83-
const k = (fromData.cy - p2y) / (p2x - fromData.cx)
84-
if (k > fromData.h / fromData.w || k < -fromData.h / fromData.w) {
85-
if (fromData.cy - p2y < 0) {
86-
x = fromData.cx - fromData.h / 2 / k
87-
y = fromData.cy + fromData.h / 2
88-
} else {
89-
x = fromData.cx + fromData.h / 2 / k
90-
y = fromData.cy - fromData.h / 2
91-
}
92-
} else {
93-
if (fromData.cx - p2x < 0) {
94-
x = fromData.cx + fromData.w / 2
95-
y = fromData.cy - (fromData.w * k) / 2
96-
} else {
97-
x = fromData.cx - fromData.w / 2
98-
y = fromData.cy + (fromData.w * k) / 2
99-
}
100-
}
101-
return {
102-
x,
103-
y,
104-
}
105-
}
106-
107-
export function calcP4(toData: LinkControllerData, p3x: number, p3y: number) {
108-
let x, y
109-
const k = (toData.cy - p3y) / (p3x - toData.cx)
110-
if (k > toData.h / toData.w || k < -toData.h / toData.w) {
111-
if (toData.cy - p3y < 0) {
112-
x = toData.cx - toData.h / 2 / k
113-
y = toData.cy + toData.h / 2
114-
} else {
115-
x = toData.cx + toData.h / 2 / k
116-
y = toData.cy - toData.h / 2
117-
}
118-
} else {
119-
if (toData.cx - p3x < 0) {
120-
x = toData.cx + toData.w / 2
121-
y = toData.cy - (toData.w * k) / 2
122-
} else {
123-
x = toData.cx - toData.w / 2
124-
y = toData.cy + (toData.w * k) / 2
125-
}
126-
}
127-
return {
128-
x,
129-
y,
130-
}
131-
}
132-
13381
export function generateUUID(): string {
13482
return (new Date().getTime().toString(16) + Math.random().toString(16).substr(2)).substr(2, 16)
13583
}

0 commit comments

Comments
 (0)