Skip to content

Commit 4dddb32

Browse files
committed
refactor: create text container, place image before text
use object property to prevent fixed order
1 parent 994f791 commit 4dddb32

File tree

5 files changed

+53
-30
lines changed

5 files changed

+53
-30
lines changed

src/index.less

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
me-main {
8282
// main node
8383
& > me-wrapper {
84-
position: relative; // make subline svg's offsetParent to be the main node
84+
position: relative; // make subline svg's offsetParent be the main node
8585
margin: 20px 65px;
8686
& > me-parent {
8787
margin: var(--gap);
@@ -125,12 +125,7 @@
125125
white-space: pre-wrap;
126126
padding: var(--topic-padding);
127127
line-height: 1.2em; // assure the line-height consistency between different languages
128-
& > div,
129-
& > span,
130-
& > img {
131-
// tags,icons,images should not response to click event
132-
pointer-events: none;
133-
}
128+
134129
// drag preview
135130
.insert-preview {
136131
position: absolute;
@@ -241,13 +236,19 @@
241236
pointer-events: all;
242237
}
243238

244-
me-tpc > img {
245-
pointer-events: none;
246-
display: block;
247-
margin-top: 8px;
248-
object-fit: cover;
239+
me-tpc {
240+
& > div,
241+
& > span,
242+
& > img {
243+
// tags,icons,images should not response to click event
244+
pointer-events: none;
245+
}
246+
& > img {
247+
display: block;
248+
margin-bottom: 8px;
249+
object-fit: cover;
250+
}
249251
}
250-
251252
.circle {
252253
position: absolute;
253254
height: 10px;
@@ -299,8 +300,9 @@
299300
border-radius: 6px;
300301
border: #666666 2px solid;
301302
}
302-
}
303-
.selection-area {
304-
background: #4f90f22d;
305-
border: 1px solid #4f90f2;
303+
304+
.selection-area {
305+
background: #4f90f22d;
306+
border: 1px solid #4f90f2;
307+
}
306308
}

src/nodeOperation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ export const beginEdit = function (this: MindElixirInstance, el?: Topic) {
536536
}
537537

538538
export const setNodeTopic = function (this: MindElixirInstance, el: Topic, topic: string) {
539-
el.childNodes[0].textContent = topic
539+
el.text.textContent = topic
540540
el.nodeObj.topic = topic
541541
this.linkDiv()
542542
}

src/plugin/exportImage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ const generateSvg = (mei: MindElixirInstance, noForiegnObject = false) => {
144144
summaries && g.appendChild(summaries)
145145

146146
mapDiv.querySelectorAll('me-tpc').forEach(tpc => {
147-
g.appendChild(convertDivToSvg(mei, tpc as Topic, noForiegnObject ? false : true))
147+
g.appendChild(convertDivToSvg(mei, (tpc as Topic).text, noForiegnObject ? false : true))
148148
})
149149
mapDiv.querySelectorAll('.tags > span').forEach(tag => {
150150
g.appendChild(convertDivToSvg(mei, tag as HTMLElement))

src/types/dom.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ export interface Children extends HTMLElement {
3030

3131
export interface Topic extends HTMLElement {
3232
nodeObj: NodeObj
33-
linkContainer: HTMLElement | null
3433
parentNode: Parent
3534
parentElement: Parent
3635
offsetParent: Parent
3736

37+
text: HTMLSpanElement
3838
expander?: Expander
39+
40+
linkContainer?: HTMLElement
3941
image?: HTMLImageElement
4042
icons?: HTMLSpanElement
4143
tags?: HTMLDivElement

src/utils/dom.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@ export const findEle = (id: string, instance?: MindElixirInstance) => {
1414
}
1515

1616
export const shapeTpc = function (tpc: Topic, nodeObj: NodeObj) {
17-
tpc.textContent = nodeObj.topic
18-
1917
if (nodeObj.style) {
2018
tpc.style.color = nodeObj.style.color || ''
2119
tpc.style.background = nodeObj.style.background || ''
2220
tpc.style.fontSize = nodeObj.style.fontSize + 'px'
2321
tpc.style.fontWeight = nodeObj.style.fontWeight || 'normal'
2422
}
2523

24+
if (nodeObj.branchColor) {
25+
tpc.style.borderColor = nodeObj.branchColor
26+
}
27+
28+
// TODO
29+
// if (nodeObj.dangerouslySetInnerHTML) {
30+
// tpc.innerHTML = nodeObj.dangerouslySetInnerHTML
31+
// return
32+
// }
33+
2634
if (nodeObj.image) {
2735
const img = nodeObj.image
2836
if (img.url && img.width && img.height) {
@@ -35,6 +43,16 @@ export const shapeTpc = function (tpc: Topic, nodeObj: NodeObj) {
3543
} else {
3644
console.warn('image url/width/height are required')
3745
}
46+
} else if (tpc.image) {
47+
tpc.image = undefined
48+
}
49+
50+
{
51+
const textContainer = $d.createElement('span')
52+
textContainer.className = 'text'
53+
textContainer.textContent = nodeObj.topic
54+
tpc.appendChild(textContainer)
55+
tpc.text = textContainer
3856
}
3957

4058
if (nodeObj.hyperLink) {
@@ -46,26 +64,27 @@ export const shapeTpc = function (tpc: Topic, nodeObj: NodeObj) {
4664
tpc.appendChild(linkContainer)
4765
tpc.linkContainer = linkContainer
4866
} else if (tpc.linkContainer) {
49-
tpc.linkContainer.remove()
50-
tpc.linkContainer = null
67+
tpc.linkContainer = undefined
5168
}
69+
5270
if (nodeObj.icons && nodeObj.icons.length) {
5371
const iconsContainer = $d.createElement('span')
5472
iconsContainer.className = 'icons'
5573
iconsContainer.innerHTML = nodeObj.icons.map(icon => `<span>${encodeHTML(icon)}</span>`).join('')
5674
tpc.appendChild(iconsContainer)
5775
tpc.icons = iconsContainer
76+
} else if (tpc.icons) {
77+
tpc.icons = undefined
5878
}
79+
5980
if (nodeObj.tags && nodeObj.tags.length) {
6081
const tagsContainer = $d.createElement('div')
6182
tagsContainer.className = 'tags'
6283
tagsContainer.innerHTML = nodeObj.tags.map(tag => `<span>${encodeHTML(tag)}</span>`).join('')
6384
tpc.appendChild(tagsContainer)
6485
tpc.tags = tagsContainer
65-
}
66-
67-
if (nodeObj.branchColor) {
68-
tpc.style.borderColor = nodeObj.branchColor
86+
} else if (tpc.tags) {
87+
tpc.tags = undefined
6988
}
7089
}
7190

@@ -122,7 +141,7 @@ export const editTopic = function (this: MindElixirInstance, el: Topic) {
122141
console.time('editTopic')
123142
if (!el) return
124143
const div = $d.createElement('div')
125-
const origin = el.childNodes[0].textContent as string
144+
const origin = el.text.textContent as string
126145
el.appendChild(div)
127146
div.id = 'input-box'
128147
div.textContent = origin
@@ -161,7 +180,7 @@ export const editTopic = function (this: MindElixirInstance, el: Topic) {
161180
else node.topic = topic
162181
div.remove()
163182
if (topic === origin) return
164-
el.childNodes[0].textContent = node.topic
183+
el.text.textContent = node.topic
165184
this.linkDiv()
166185
this.bus.fire('operation', {
167186
name: 'finishEdit',

0 commit comments

Comments
 (0)