Skip to content

Commit 8b3c1a6

Browse files
author
Victor
committed
feat: enhance markdown support for labels and summaries
1 parent 8875e03 commit 8b3c1a6

File tree

7 files changed

+38
-17
lines changed

7 files changed

+38
-17
lines changed

src/arrow.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ const drawArrow = function (mei: MindElixirInstance, from: Topic, to: Topic, obj
300300
const labelColor = obj.style?.labelColor || 'rgb(235, 95, 82)'
301301
const groupId = 'arrow-' + obj.id
302302
newSvgGroup.id = groupId
303-
const label = createLabel(obj.label, halfx, halfy, {
303+
const renderedLabel = mei.markdown ? mei.markdown(obj.label, obj) : obj.label
304+
const label = createLabel(renderedLabel, halfx, halfy, {
304305
anchor: 'middle',
305306
color: labelColor,
306307
dataType: 'arrow',

src/dev.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { snapdom } from '@zumer/snapdom'
1212
import type { Tokens } from 'marked'
1313
import { marked } from 'marked'
1414
import { md2html } from 'simple-markdown-to-html'
15+
import type { Arrow } from './arrow'
16+
import type { Summary } from './summary'
1517

1618
interface Window {
1719
m?: MindElixirInstance
@@ -35,9 +37,9 @@ const options: Options = {
3537
// mouseSelectionButton: 2,
3638
draggable: true,
3739
editable: true,
38-
markdown: (text: string, obj: NodeObj & { useMd?: boolean }) => {
40+
markdown: (text: string, obj: (NodeObj & { useMd?: boolean }) | (Arrow & { useMd?: boolean }) | (Summary & { useMd?: boolean })) => {
3941
if (!text) return ''
40-
if (!obj.useMd) return text
42+
// if (!obj.useMd) return text
4143
try {
4244
// Configure marked renderer to add target="_blank" to links
4345
const renderer = {

src/index.less

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
.map-container {
2+
p {
3+
margin: 0;
4+
}
25
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
3-
font-family: -apple-system, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC,
4-
WenQuanYi Micro Hei, sans-serif;
6+
font-family:
7+
-apple-system,
8+
BlinkMacSystemFont,
9+
Helvetica Neue,
10+
PingFang SC,
11+
Microsoft YaHei,
12+
Source Han Sans SC,
13+
Noto Sans CJK SC,
14+
WenQuanYi Micro Hei,
15+
sans-serif;
516
user-select: none;
617
height: 100%;
718
width: 100%;

src/mouse.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ export default function (mind: MindElixirInstance) {
4040
} else if (!mind.editable) {
4141
return
4242
}
43-
// Check if clicked on a label div
44-
if (target.classList.contains('svg-label')) {
45-
const id = target.dataset.svgId!
46-
const type = target.dataset.type
43+
const label = target.closest('.svg-label') as HTMLElement
44+
if (label) {
45+
const id = label.dataset.svgId!
46+
const type = label.dataset.type
4747
const svgElement = document.getElementById(id)
4848
if (svgElement) {
4949
if (type === 'arrow') {
@@ -84,9 +84,10 @@ export default function (mind: MindElixirInstance) {
8484
mind.beginEdit(target)
8585
}
8686

87-
if (target.classList.contains('svg-label')) {
88-
const id = target.dataset.svgId!
89-
const type = target.dataset.type
87+
const label = target.closest('.svg-label') as HTMLElement
88+
if (label) {
89+
const id = label.dataset.svgId!
90+
const type = label.dataset.type
9091
const svgElement = document.getElementById(id)
9192
if (svgElement) {
9293
if (type === 'arrow') {

src/summary.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,14 @@ const drawSummary = function (mei: MindElixirInstance, summary: Summary) {
167167
const strokeColor = style?.stroke || theme.cssVar['--color']
168168
const labelColor = style?.labelColor || theme.cssVar['--color']
169169
const groupId = 's-' + id
170+
// Render label with markdown if available
171+
const renderedLabel = mei.markdown ? mei.markdown(summaryText, summary) : summaryText
170172
if (side === DirectionClass.LHS) {
171173
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`, strokeColor)
172-
text = createLabel(summaryText, left - 20, md, { anchor: 'end', color: labelColor, dataType: 'summary', svgId: groupId })
174+
text = createLabel(renderedLabel, left - 20, md, { anchor: 'end', color: labelColor, dataType: 'summary', svgId: groupId })
173175
} else {
174176
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`, strokeColor)
175-
text = createLabel(summaryText, right + 20, md, { anchor: 'start', color: labelColor, dataType: 'summary', svgId: groupId })
177+
text = createLabel(renderedLabel, right + 20, md, { anchor: 'start', color: labelColor, dataType: 'summary', svgId: groupId })
176178
}
177179
const group = creatGroup(groupId)
178180
group.appendChild(path)

src/types/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface KeypressOptions {
7373
* @public
7474
*/
7575
export interface MindElixirInstance extends Omit<Required<Options>, 'markdown' | 'imageProxy'>, MindElixirMethods {
76-
markdown?: (markdown: string, obj: NodeObj) => string // Keep markdown as optional
76+
markdown?: (markdown: string, obj: NodeObj | Arrow | Summary) => string // Keep markdown as optional
7777
imageProxy?: (url: string) => string // Keep imageProxy as optional
7878
dragged: Topic[] | null // currently dragged nodes
7979
spacePressed: boolean // space key pressed state
@@ -159,7 +159,7 @@ export interface Options {
159159
* If not provided, markdown will be disabled
160160
* @default undefined
161161
*/
162-
markdown?: (markdown: string, obj: NodeObj) => string
162+
markdown?: (markdown: string, obj: NodeObj | Arrow | Summary) => string
163163
/**
164164
* Image proxy function to handle image URLs, mainly used to solve CORS issues
165165
* If provided, all image URLs will be processed through this function before setting to img src

src/utils/svg.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ export const editSvgText = function (mei: MindElixirInstance, textEl: HTMLDivEle
219219
div.remove()
220220
if (text === origin) return
221221

222-
textEl.textContent = node.label
222+
if (mei.markdown) {
223+
;(textEl as HTMLDivElement).innerHTML = mei.markdown(node.label, node as any)
224+
} else {
225+
textEl.textContent = node.label
226+
}
223227
// Recalculate position with new content while preserving existing color
224228
calculatePrecisePosition(textEl)
225229

0 commit comments

Comments
 (0)