Skip to content

Commit b43c446

Browse files
committed
refactor: abstract createSvgText
1 parent 6861119 commit b43c446

File tree

4 files changed

+38
-31
lines changed

4 files changed

+38
-31
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mind-elixir",
3-
"version": "5.0.4",
3+
"version": "5.0.5",
44
"type": "module",
55
"description": "Mind elixir is a free open source mind map core.",
66
"keywords": [

src/arrow.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { generateUUID, getArrowPoints, getObjById, getOffsetLT, setAttributes } from './utils/index'
22
import LinkDragMoveHelper from './utils/LinkDragMoveHelper'
3-
import { createSvgGroup, editSvgText, svgNS } from './utils/svg'
3+
import { createSvgGroup, createSvgText, editSvgText, svgNS } from './utils/svg'
44
import type { CustomSvg, Topic } from './types/dom'
55
import type { MindElixirInstance, Uid } from './index'
66

@@ -243,19 +243,6 @@ function calcP(data: DivData) {
243243
}
244244
}
245245

246-
const createText = function (string: string, x: number, y: number, color?: string) {
247-
const text = document.createElementNS(svgNS, 'text')
248-
setAttributes(text, {
249-
'text-anchor': 'middle',
250-
x: x + '',
251-
y: y + '',
252-
fill: color || 'rgb(235, 95, 82)',
253-
})
254-
text.dataset.type = 'custom-link'
255-
text.innerHTML = string
256-
return text
257-
}
258-
259246
/**
260247
* FYI
261248
* p1: start point
@@ -291,7 +278,11 @@ const drawArrow = function (mei: MindElixirInstance, from: Topic, to: Topic, obj
291278
// Use extracted common function to calculate midpoint
292279
const { x: halfx, y: halfy } = calcBezierMidPoint(p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y)
293280
const labelColor = obj.style?.labelColor
294-
const label = createText(obj.label, halfx, halfy, labelColor)
281+
const label = createSvgText(obj.label, halfx, halfy, {
282+
anchor: 'middle',
283+
color: labelColor,
284+
dataType: 'custom-link',
285+
})
295286
newSvgGroup.appendChild(label)
296287
newSvgGroup.label = label
297288

src/summary.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { MindElixirInstance, Topic } from '.'
22
import { DirectionClass } from './types/index'
33
import { generateUUID, getOffsetLT, setAttributes } from './utils'
4-
import { editSvgText, svgNS } from './utils/svg'
4+
import { createSvgText, editSvgText, svgNS } from './utils/svg'
55

66
/**
77
* @public
@@ -98,18 +98,6 @@ const createPath = function (d: string, color?: string) {
9898
return path
9999
}
100100

101-
const createText = function (string: string, x: number, y: number, anchor: 'start' | 'end', color?: string) {
102-
const text = document.createElementNS(svgNS, 'text')
103-
setAttributes(text, {
104-
'text-anchor': anchor,
105-
x: x + '',
106-
y: y + '',
107-
fill: color || '#666',
108-
})
109-
text.innerHTML = string
110-
return text
111-
}
112-
113101
const getWrapper = (tpc: Topic) => tpc.parentElement.parentElement
114102

115103
const getDirection = function (mei: MindElixirInstance, { parent, start }: Summary) {
@@ -157,10 +145,10 @@ const drawSummary = function (mei: MindElixirInstance, summary: Summary) {
157145
const color = theme.cssVar['--color']
158146
if (side === DirectionClass.LHS) {
159147
path = createPath(`M ${left + 10} ${top} c -5 0 -10 5 -10 10 L ${left} ${bottom - 10} c 0 5 5 10 10 10 M ${left} ${md} h -10`, color)
160-
text = createText(summaryText, left - 20, md + 6, 'end', color)
148+
text = createSvgText(summaryText, left - 20, md + 6, { anchor: 'end', color })
161149
} else {
162150
path = createPath(`M ${right - 10} ${top} c 5 0 10 5 10 10 L ${right} ${bottom - 10} c 0 5 -5 10 -10 10 M ${right} ${md} h 10`, color)
163-
text = createText(summaryText, right + 20, md + 6, 'start', color)
151+
text = createSvgText(summaryText, right + 20, md + 6, { anchor: 'start', color })
164152
}
165153
const group = creatGroup('s-' + id)
166154
group.appendChild(path)

src/utils/svg.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@ import { selectText } from './dom'
88
const $d = document
99
export const svgNS = 'http://www.w3.org/2000/svg'
1010

11+
export interface SvgTextOptions {
12+
anchor?: 'start' | 'middle' | 'end'
13+
color?: string
14+
dataType?: string
15+
}
16+
17+
/**
18+
* Create an SVG text element with common attributes
19+
*/
20+
export const createSvgText = function (text: string, x: number, y: number, options: SvgTextOptions = {}): SVGTextElement {
21+
const { anchor = 'middle', color, dataType } = options
22+
23+
const textElement = document.createElementNS(svgNS, 'text')
24+
setAttributes(textElement, {
25+
'text-anchor': anchor,
26+
x: x + '',
27+
y: y + '',
28+
fill: color || (anchor === 'middle' ? 'rgb(235, 95, 82)' : '#666'),
29+
})
30+
31+
if (dataType) {
32+
textElement.dataset.type = dataType
33+
}
34+
35+
textElement.innerHTML = text
36+
return textElement
37+
}
38+
1139
export const createPath = function (d: string, color: string, width: string) {
1240
const path = $d.createElementNS(svgNS, 'path')
1341
setAttributes(path, {

0 commit comments

Comments
 (0)