Skip to content

Commit

Permalink
refactor: create text container, place image before text
Browse files Browse the repository at this point in the history
use object property to prevent fixed order
  • Loading branch information
SSShooter committed Oct 12, 2023
1 parent 994f791 commit 4dddb32
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 30 deletions.
36 changes: 19 additions & 17 deletions src/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
me-main {
// main node
& > me-wrapper {
position: relative; // make subline svg's offsetParent to be the main node
position: relative; // make subline svg's offsetParent be the main node
margin: 20px 65px;
& > me-parent {
margin: var(--gap);
Expand Down Expand Up @@ -125,12 +125,7 @@
white-space: pre-wrap;
padding: var(--topic-padding);
line-height: 1.2em; // assure the line-height consistency between different languages
& > div,
& > span,
& > img {
// tags,icons,images should not response to click event
pointer-events: none;
}

// drag preview
.insert-preview {
position: absolute;
Expand Down Expand Up @@ -241,13 +236,19 @@
pointer-events: all;
}

me-tpc > img {
pointer-events: none;
display: block;
margin-top: 8px;
object-fit: cover;
me-tpc {
& > div,
& > span,
& > img {
// tags,icons,images should not response to click event
pointer-events: none;
}
& > img {
display: block;
margin-bottom: 8px;
object-fit: cover;
}
}

.circle {
position: absolute;
height: 10px;
Expand Down Expand Up @@ -299,8 +300,9 @@
border-radius: 6px;
border: #666666 2px solid;
}
}
.selection-area {
background: #4f90f22d;
border: 1px solid #4f90f2;

.selection-area {
background: #4f90f22d;
border: 1px solid #4f90f2;
}
}
2 changes: 1 addition & 1 deletion src/nodeOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ export const beginEdit = function (this: MindElixirInstance, el?: Topic) {
}

export const setNodeTopic = function (this: MindElixirInstance, el: Topic, topic: string) {
el.childNodes[0].textContent = topic
el.text.textContent = topic
el.nodeObj.topic = topic
this.linkDiv()
}
2 changes: 1 addition & 1 deletion src/plugin/exportImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const generateSvg = (mei: MindElixirInstance, noForiegnObject = false) => {
summaries && g.appendChild(summaries)

mapDiv.querySelectorAll('me-tpc').forEach(tpc => {
g.appendChild(convertDivToSvg(mei, tpc as Topic, noForiegnObject ? false : true))
g.appendChild(convertDivToSvg(mei, (tpc as Topic).text, noForiegnObject ? false : true))
})
mapDiv.querySelectorAll('.tags > span').forEach(tag => {
g.appendChild(convertDivToSvg(mei, tag as HTMLElement))
Expand Down
4 changes: 3 additions & 1 deletion src/types/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ export interface Children extends HTMLElement {

export interface Topic extends HTMLElement {
nodeObj: NodeObj
linkContainer: HTMLElement | null
parentNode: Parent
parentElement: Parent
offsetParent: Parent

text: HTMLSpanElement
expander?: Expander

linkContainer?: HTMLElement
image?: HTMLImageElement
icons?: HTMLSpanElement
tags?: HTMLDivElement
Expand Down
39 changes: 29 additions & 10 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ export const findEle = (id: string, instance?: MindElixirInstance) => {
}

export const shapeTpc = function (tpc: Topic, nodeObj: NodeObj) {
tpc.textContent = nodeObj.topic

if (nodeObj.style) {
tpc.style.color = nodeObj.style.color || ''
tpc.style.background = nodeObj.style.background || ''
tpc.style.fontSize = nodeObj.style.fontSize + 'px'
tpc.style.fontWeight = nodeObj.style.fontWeight || 'normal'
}

if (nodeObj.branchColor) {
tpc.style.borderColor = nodeObj.branchColor
}

// TODO
// if (nodeObj.dangerouslySetInnerHTML) {
// tpc.innerHTML = nodeObj.dangerouslySetInnerHTML
// return
// }

if (nodeObj.image) {
const img = nodeObj.image
if (img.url && img.width && img.height) {
Expand All @@ -35,6 +43,16 @@ export const shapeTpc = function (tpc: Topic, nodeObj: NodeObj) {
} else {
console.warn('image url/width/height are required')
}
} else if (tpc.image) {
tpc.image = undefined
}

{
const textContainer = $d.createElement('span')
textContainer.className = 'text'
textContainer.textContent = nodeObj.topic
tpc.appendChild(textContainer)
tpc.text = textContainer
}

if (nodeObj.hyperLink) {
Expand All @@ -46,26 +64,27 @@ export const shapeTpc = function (tpc: Topic, nodeObj: NodeObj) {
tpc.appendChild(linkContainer)
tpc.linkContainer = linkContainer
} else if (tpc.linkContainer) {
tpc.linkContainer.remove()
tpc.linkContainer = null
tpc.linkContainer = undefined
}

if (nodeObj.icons && nodeObj.icons.length) {
const iconsContainer = $d.createElement('span')
iconsContainer.className = 'icons'
iconsContainer.innerHTML = nodeObj.icons.map(icon => `<span>${encodeHTML(icon)}</span>`).join('')
tpc.appendChild(iconsContainer)
tpc.icons = iconsContainer
} else if (tpc.icons) {
tpc.icons = undefined
}

if (nodeObj.tags && nodeObj.tags.length) {
const tagsContainer = $d.createElement('div')
tagsContainer.className = 'tags'
tagsContainer.innerHTML = nodeObj.tags.map(tag => `<span>${encodeHTML(tag)}</span>`).join('')
tpc.appendChild(tagsContainer)
tpc.tags = tagsContainer
}

if (nodeObj.branchColor) {
tpc.style.borderColor = nodeObj.branchColor
} else if (tpc.tags) {
tpc.tags = undefined
}
}

Expand Down Expand Up @@ -122,7 +141,7 @@ export const editTopic = function (this: MindElixirInstance, el: Topic) {
console.time('editTopic')
if (!el) return
const div = $d.createElement('div')
const origin = el.childNodes[0].textContent as string
const origin = el.text.textContent as string
el.appendChild(div)
div.id = 'input-box'
div.textContent = origin
Expand Down Expand Up @@ -161,7 +180,7 @@ export const editTopic = function (this: MindElixirInstance, el: Topic) {
else node.topic = topic
div.remove()
if (topic === origin) return
el.childNodes[0].textContent = node.topic
el.text.textContent = node.topic
this.linkDiv()
this.bus.fire('operation', {
name: 'finishEdit',
Expand Down

0 comments on commit 4dddb32

Please sign in to comment.