Skip to content

Commit a71481a

Browse files
committed
refactor: remove top level scope document
1 parent 4ff377e commit a71481a

5 files changed

Lines changed: 32 additions & 39 deletions

File tree

src/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { createDragMoveHelper } from './utils/dragMoveHelper'
1414
import type { Topic } from './docs'
1515

1616
// TODO show up animation
17-
const $d = document
1817

1918
function MindElixir(
2019
this: MindElixirInstance,
@@ -87,7 +86,7 @@ function MindElixir(
8786
this.dragMoveHelper = createDragMoveHelper(this)
8887
this.bus = createBus()
8988

90-
this.container = $d.createElement('div') // map container
89+
this.container = document.createElement('div') // map container
9190
this.selectionContainer = selectionContainer || this.container
9291

9392
this.container.className = 'map-container'
@@ -96,21 +95,21 @@ function MindElixir(
9695
this.theme = theme || (mediaQuery.matches ? DARK_THEME : THEME)
9796

9897
// infrastructure
99-
const canvas = $d.createElement('div') // map-canvas Element
98+
const canvas = document.createElement('div') // map-canvas Element
10099
canvas.className = 'map-canvas'
101100
this.map = canvas
102101
this.container.setAttribute('tabindex', '0')
103102
this.container.appendChild(this.map)
104103
this.el.appendChild(this.container)
105104

106-
this.nodes = $d.createElement('me-nodes')
105+
this.nodes = document.createElement('me-nodes')
107106

108107
this.lines = createLinkSvg('lines') // main link container
109108
this.summarySvg = createLinkSvg('summary') // summary container
110109

111110
this.linkController = createLinkSvg('linkcontroller') // bezier controller container
112-
this.P2 = $d.createElement('div') // bezier P2
113-
this.P3 = $d.createElement('div') // bezier P3
111+
this.P2 = document.createElement('div') // bezier P2
112+
this.P3 = document.createElement('div') // bezier P3
114113
this.P2.className = this.P3.className = 'circle'
115114
this.P2.style.display = this.P3.style.display = 'none'
116115
this.line1 = createLine() // bezier auxiliary line1
@@ -119,7 +118,7 @@ function MindElixir(
119118
this.linkController.appendChild(this.line2)
120119
this.linkSvgGroup = createLinkSvg('topiclinks') // storage user custom link svg
121120

122-
this.labelContainer = $d.createElement('div') // container for SVG labels
121+
this.labelContainer = document.createElement('div') // container for SVG labels
123122
this.labelContainer.className = 'label-container'
124123

125124
this.map.appendChild(this.nodes)

src/plugin/nodeDraggable.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import type { MindElixirInstance } from '../types/index'
33

44
export type InsertType = 'before' | 'after' | 'in' | null
55

6-
const $d = document
7-
86
export const insertPreview = function (tpc: Topic, insertType: InsertType) {
97
if (!insertType) {
108
clearPreview(tpc)
@@ -13,7 +11,7 @@ export const insertPreview = function (tpc: Topic, insertType: InsertType) {
1311
let el = tpc.querySelector('.insert-preview')
1412
const className = `insert-preview ${insertType} show`
1513
if (!el) {
16-
el = $d.createElement('div')
14+
el = document.createElement('div')
1715
tpc.appendChild(el)
1816
}
1917
el.className = className
@@ -207,7 +205,7 @@ export function handleNodeDragMove(mind: MindElixirInstance, state: NodeDragStat
207205

208206
// Check for drop target
209207
// minus threshold infers that position of the cursor is above topic
210-
const topMeet = $d.elementFromPoint(e.clientX, e.clientY - threshold) as Topic
208+
const topMeet = document.elementFromPoint(e.clientX, e.clientY - threshold) as Topic
211209
if (canMove(topMeet, dragged)) {
212210
state.meet = topMeet
213211
const rect = topMeet.getBoundingClientRect()
@@ -218,7 +216,7 @@ export function handleNodeDragMove(mind: MindElixirInstance, state: NodeDragStat
218216
state.insertType = 'in'
219217
}
220218
} else {
221-
const bottomMeet = $d.elementFromPoint(e.clientX, e.clientY + threshold) as Topic
219+
const bottomMeet = document.elementFromPoint(e.clientX, e.clientY + threshold) as Topic
222220
if (canMove(bottomMeet, dragged)) {
223221
state.meet = bottomMeet
224222
const rect = bottomMeet.getBoundingClientRect()

src/utils/dom.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { encodeHTML, getOffsetLT } from '../utils/index'
55
import { layoutChildren } from './layout'
66

77
// DOM manipulation
8-
const $d = document
98
export const findEle = function (this: MindElixirInstance, id: string, el?: HTMLElement) {
109
const scope = this?.el ? this.el : el ? el : document
1110
const ele = scope.querySelector<Topic>(`[data-nodeid="me${id}"]`)
@@ -32,7 +31,7 @@ export const shapeTpc = function (this: MindElixirInstance, tpc: Topic, nodeObj:
3231
if (nodeObj.image) {
3332
const img = nodeObj.image
3433
if (img.url && img.width && img.height) {
35-
const imgEl = $d.createElement('img')
34+
const imgEl = document.createElement('img')
3635
// Use imageProxy function if provided, otherwise use original URL
3736
imgEl.src = this.imageProxy ? this.imageProxy(img.url) : img.url
3837
imgEl.style.width = img.width + 'px'
@@ -48,7 +47,7 @@ export const shapeTpc = function (this: MindElixirInstance, tpc: Topic, nodeObj:
4847
}
4948

5049
{
51-
const textEl = $d.createElement('span')
50+
const textEl = document.createElement('span')
5251
textEl.className = 'text'
5352

5453
// Check if markdown parser is provided and topic contains markdown syntax
@@ -63,7 +62,7 @@ export const shapeTpc = function (this: MindElixirInstance, tpc: Topic, nodeObj:
6362
}
6463

6564
if (nodeObj.hyperLink) {
66-
const linkEl = $d.createElement('a')
65+
const linkEl = document.createElement('a')
6766
linkEl.className = 'hyper-link'
6867
linkEl.target = '_blank'
6968
linkEl.innerText = '🔗'
@@ -75,7 +74,7 @@ export const shapeTpc = function (this: MindElixirInstance, tpc: Topic, nodeObj:
7574
}
7675

7776
if (nodeObj.icons && nodeObj.icons.length) {
78-
const iconsEl = $d.createElement('span')
77+
const iconsEl = document.createElement('span')
7978
iconsEl.className = 'icons'
8079
iconsEl.innerHTML = nodeObj.icons.map(icon => `<span>${encodeHTML(icon)}</span>`).join('')
8180
tpc.appendChild(iconsEl)
@@ -85,11 +84,11 @@ export const shapeTpc = function (this: MindElixirInstance, tpc: Topic, nodeObj:
8584
}
8685

8786
if (nodeObj.tags && nodeObj.tags.length) {
88-
const tagsEl = $d.createElement('div')
87+
const tagsEl = document.createElement('div')
8988
tagsEl.className = 'tags'
9089

9190
nodeObj.tags.forEach(tag => {
92-
const span = $d.createElement('span')
91+
const span = document.createElement('span')
9392

9493
if (typeof tag === 'string') {
9594
span.textContent = tag
@@ -115,7 +114,7 @@ export const shapeTpc = function (this: MindElixirInstance, tpc: Topic, nodeObj:
115114

116115
// everything start from `Wrapper`
117116
export const createWrapper = function (this: MindElixirInstance, nodeObj: NodeObj, omitChildren?: boolean) {
118-
const grp = $d.createElement('me-wrapper') as Wrapper
117+
const grp = document.createElement('me-wrapper') as Wrapper
119118
const { p, tpc } = this.createParent(nodeObj)
120119
grp.appendChild(p)
121120
if (!omitChildren && nodeObj.children && nodeObj.children.length > 0) {
@@ -131,28 +130,28 @@ export const createWrapper = function (this: MindElixirInstance, nodeObj: NodeOb
131130
}
132131

133132
export const createParent = function (this: MindElixirInstance, nodeObj: NodeObj) {
134-
const p = $d.createElement('me-parent') as Parent
133+
const p = document.createElement('me-parent') as Parent
135134
const tpc = this.createTopic(nodeObj)
136135
shapeTpc.call(this, tpc, nodeObj)
137136
p.appendChild(tpc)
138137
return { p, tpc }
139138
}
140139

141140
export const createChildren = function (this: MindElixirInstance, wrappers: Wrapper[]) {
142-
const children = $d.createElement('me-children') as Children
141+
const children = document.createElement('me-children') as Children
143142
children.append(...wrappers)
144143
return children
145144
}
146145

147146
export const createTopic = function (this: MindElixirInstance, nodeObj: NodeObj) {
148-
const topic = $d.createElement('me-tpc') as Topic
147+
const topic = document.createElement('me-tpc') as Topic
149148
topic.nodeObj = nodeObj
150149
topic.dataset.nodeid = 'me' + nodeObj.id
151150
return topic
152151
}
153152

154153
export function selectText(div: HTMLElement) {
155-
const range = $d.createRange()
154+
const range = document.createRange()
156155
range.selectNodeContents(div)
157156
const getSelection = window.getSelection()
158157
if (getSelection) {
@@ -164,7 +163,7 @@ export function selectText(div: HTMLElement) {
164163
export const editTopic = function (this: MindElixirInstance, el: Topic) {
165164
console.time('editTopic')
166165
if (!el) return
167-
const div = $d.createElement('div')
166+
const div = document.createElement('div')
168167
const node = el.nodeObj
169168

170169
// Get the original content from topic
@@ -240,7 +239,7 @@ export const editTopic = function (this: MindElixirInstance, el: Topic) {
240239
}
241240

242241
export const createExpander = function (expanded: boolean | undefined): Expander {
243-
const expander = $d.createElement('me-epd') as Expander
242+
const expander = document.createElement('me-epd') as Expander
244243
// if expanded is undefined, treat as expanded
245244
expander.expanded = expanded !== false
246245
expander.className = expanded !== false ? 'minus' : ''

src/utils/layout.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import type { Children } from '../types/dom'
33
import { DirectionClass, type MindElixirInstance, type NodeObj } from '../types/index'
44
import { shapeTpc } from './dom'
55

6-
const $d = document
7-
86
// Set main nodes' direction and invoke layoutChildren()
97
export const layout = function (this: MindElixirInstance) {
108
console.time('layout')
@@ -13,7 +11,7 @@ export const layout = function (this: MindElixirInstance) {
1311
const tpc = this.createTopic(this.nodeData)
1412
shapeTpc.call(this, tpc, this.nodeData) // shape root tpc
1513
tpc.draggable = false
16-
const root = $d.createElement('me-root')
14+
const root = document.createElement('me-root')
1715
root.appendChild(tpc)
1816

1917
const mainNodes = this.nodeData.children || []
@@ -42,9 +40,9 @@ export const layout = function (this: MindElixirInstance) {
4240
}
4341

4442
const layoutMainNode = function (mei: MindElixirInstance, data: NodeObj[], root: HTMLElement) {
45-
const leftPart = $d.createElement('me-main')
43+
const leftPart = document.createElement('me-main')
4644
leftPart.className = DirectionClass.LHS
47-
const rightPart = $d.createElement('me-main')
45+
const rightPart = document.createElement('me-main')
4846
rightPart.className = DirectionClass.RHS
4947
for (let i = 0; i < data.length; i++) {
5048
const nodeObj = data[i]
@@ -71,7 +69,7 @@ const layoutMainNode = function (mei: MindElixirInstance, data: NodeObj[], root:
7169
}
7270

7371
export const layoutChildren = function (mei: MindElixirInstance, data: NodeObj[]) {
74-
const chldr = $d.createElement('me-children') as Children
72+
const chldr = document.createElement('me-children') as Children
7573
for (let i = 0; i < data.length; i++) {
7674
const nodeObj = data[i]
7775
const { grp } = mei.createWrapper(nodeObj)

src/utils/svg.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { MindElixirInstance } from '../types'
55
import type { CustomSvg } from '../types/dom'
66
import { selectText } from './dom'
77

8-
const $d = document
98
export const svgNS = 'http://www.w3.org/2000/svg'
109

1110
export interface SvgTextOptions {
@@ -85,7 +84,7 @@ export const findLabelBySvgId = function (svgId: string): HTMLDivElement | null
8584
}
8685

8786
export const createPath = function (d: string, color: string, width: string) {
88-
const path = $d.createElementNS(svgNS, 'path')
87+
const path = document.createElementNS(svgNS, 'path')
8988
setAttributes(path, {
9089
d,
9190
stroke: color || '#666',
@@ -96,14 +95,14 @@ export const createPath = function (d: string, color: string, width: string) {
9695
}
9796

9897
export const createLinkSvg = function (klass: string) {
99-
const svg = $d.createElementNS(svgNS, 'svg')
98+
const svg = document.createElementNS(svgNS, 'svg')
10099
svg.setAttribute('class', klass)
101100
svg.setAttribute('overflow', 'visible')
102101
return svg
103102
}
104103

105104
export const createLine = function () {
106-
const line = $d.createElementNS(svgNS, 'line')
105+
const line = document.createElementNS(svgNS, 'line')
107106
line.setAttribute('stroke', '#4dc4ff')
108107
line.setAttribute('fill', 'none')
109108
line.setAttribute('stroke-width', '2')
@@ -123,7 +122,7 @@ export const createArrowGroup = function (
123122
opacity?: string | number
124123
}
125124
): CustomSvg {
126-
const g = $d.createElementNS(svgNS, 'g') as CustomSvg
125+
const g = document.createElementNS(svgNS, 'g') as CustomSvg
127126
const svgs = [
128127
{
129128
name: 'line',
@@ -140,7 +139,7 @@ export const createArrowGroup = function (
140139
] as const
141140
svgs.forEach((item, i) => {
142141
const d = item.d
143-
const path = $d.createElementNS(svgNS, 'path')
142+
const path = document.createElementNS(svgNS, 'path')
144143
const attrs: { [key: string]: string } = {
145144
d,
146145
stroke: style?.stroke || 'rgb(227, 125, 116)',
@@ -160,7 +159,7 @@ export const createArrowGroup = function (
160159
path.setAttribute('stroke-dasharray', style?.strokeDasharray || '8,2')
161160
}
162161

163-
const hotzone = $d.createElementNS(svgNS, 'path')
162+
const hotzone = document.createElementNS(svgNS, 'path')
164163
const hotzoneAttrs = {
165164
d,
166165
stroke: 'transparent',

0 commit comments

Comments
 (0)