forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (168 loc) · 5.15 KB
/
index.js
File metadata and controls
181 lines (168 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import BaseFloat from '../baseFloat'
import { patch, h } from '../../parser/render/snabbdom'
import { menu, getSubMenu, getLabel } from './config'
import './index.css'
const MAX_SUBMENU_HEIGHT = 400
const ITEM_HEIGHT = 28
const PADDING = 10
const defaultOptions = {
placement: 'bottom',
modifiers: {
offset: {
offset: '0, 10'
}
},
showArrow: false
}
class FrontMenu extends BaseFloat {
static pluginName = 'frontMenu'
constructor (muya, options = {}) {
const name = 'ag-front-menu'
const opts = Object.assign({}, defaultOptions, options)
super(muya, name, opts)
this.oldVnode = null
this.outmostBlock = null
this.startBlock = null
this.endBlock = null
this.options = opts
this.reference = null
const frontMenuContainer = this.frontMenuContainer = document.createElement('div')
Object.assign(this.container.parentNode.style, {
overflow: 'visible'
})
this.container.appendChild(frontMenuContainer)
this.listen()
}
listen () {
const { eventCenter } = this.muya
super.listen()
eventCenter.subscribe('muya-front-menu', ({ reference, outmostBlock, startBlock, endBlock }) => {
if (reference) {
this.outmostBlock = outmostBlock
this.startBlock = startBlock
this.endBlock = endBlock
this.reference = reference
setTimeout(() => {
this.show(reference)
this.render()
}, 0)
} else {
this.hide()
this.reference = null
}
})
}
renderSubMenu (subMenu) {
const { reference } = this
const rect = reference.getBoundingClientRect()
const windowHeight = document.documentElement.clientHeight
const children = subMenu.map(menuItem => {
const { icon, title, label, shortCut } = menuItem
const iconWrapperSelector = 'div.icon-wrapper'
const iconWrapper = h(iconWrapperSelector, h('i.icon', h(`i.icon-${label.replace(/\s/g, '-')}`, {
style: {
background: `url(${icon}) no-repeat`,
'background-size': '100%'
}
}, '')))
const textWrapper = h('span', title)
const shortCutWrapper = h('div.short-cut', [
h('span', shortCut)
])
let itemSelector = `li.item.${label}`
if (label === getLabel(this.outmostBlock)) {
itemSelector += '.active'
}
return h(itemSelector, {
on: {
click: event => {
this.selectItem(event, { label })
}
}
}, [iconWrapper, textWrapper, shortCutWrapper])
})
let subMenuSelector = 'div.submenu'
if (windowHeight - rect.bottom < MAX_SUBMENU_HEIGHT - (ITEM_HEIGHT + PADDING)) {
subMenuSelector += '.align-bottom'
}
return h(subMenuSelector, h('ul', children))
}
render () {
const { oldVnode, frontMenuContainer, outmostBlock, startBlock, endBlock } = this
const { type, functionType } = outmostBlock
const children = menu.map(({ icon, label, text, shortCut }) => {
const subMenu = getSubMenu(outmostBlock, startBlock, endBlock)
const iconWrapperSelector = 'div.icon-wrapper'
const iconWrapper = h(iconWrapperSelector, h('i.icon', h(`i.icon-${label.replace(/\s/g, '-')}`, {
style: {
background: `url(${icon}) no-repeat`,
'background-size': '100%'
}
}, '')))
const textWrapper = h('span', text)
const shortCutWrapper = h('div.short-cut', [
h('span', shortCut)
])
let itemSelector = `li.item.${label}`
const itemChildren = [iconWrapper, textWrapper, shortCutWrapper]
if (label === 'turnInto' && subMenu.length !== 0) {
itemChildren.push(this.renderSubMenu(subMenu))
}
if (label === 'turnInto' && subMenu.length === 0) {
itemSelector += '.disabled'
}
// front matter can not be duplicated.
if (label === 'duplicate' && type === 'pre' && functionType === 'frontmatter') {
itemSelector += '.disabled'
}
return h(itemSelector, {
on: {
click: event => {
this.selectItem(event, { label })
}
}
}, itemChildren)
})
const vnode = h('ul', children)
if (oldVnode) {
patch(oldVnode, vnode)
} else {
patch(frontMenuContainer, vnode)
}
this.oldVnode = vnode
}
selectItem (event, { label }) {
event.preventDefault()
event.stopPropagation()
const { type, functionType } = this.outmostBlock
// front matter can not be duplicated.
if (label === 'duplicate' && type === 'pre' && functionType === 'frontmatter') {
return
}
const { contentState } = this.muya
contentState.selectedBlock = null
switch (label) {
case 'duplicate': {
contentState.duplicate()
break
}
case 'delete': {
contentState.deleteParagraph()
break
}
case 'new': {
contentState.insertParagraph('after', '', true)
break
}
case 'turnInto':
// do nothing, do not hide float box.
return
default:
contentState.updateParagraph(label)
break
}
// delay hide to avoid dispatch enter hander
setTimeout(this.hide.bind(this))
}
}
export default FrontMenu