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
96 lines (84 loc) · 2.48 KB
/
index.js
File metadata and controls
96 lines (84 loc) · 2.48 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
import BaseScrollFloat from '../baseScrollFloat'
import { patch, h } from '../../parser/render/snabbdom'
import { search } from '../../prism/index'
import fileIcons from '../fileIcons'
import './index.css'
const defaultOptions = {
placement: 'bottom-start',
modifiers: {
offset: {
offset: '0, 0'
}
},
showArrow: false
}
class CodePicker extends BaseScrollFloat {
static pluginName = 'codePicker'
constructor (muya, options = {}) {
const name = 'ag-list-picker'
const opts = Object.assign({}, defaultOptions, options)
super(muya, name, opts)
this.renderArray = []
this.oldVnode = null
this.activeItem = null
this.listen()
}
listen () {
super.listen()
const { eventCenter } = this.muya
eventCenter.subscribe('muya-code-picker', ({ reference, lang, cb }) => {
const modes = search(lang)
if (modes.length && reference) {
this.show(reference, cb)
this.renderArray = modes
this.activeItem = modes[0]
this.render()
} else {
this.hide()
}
})
}
render () {
const { renderArray, oldVnode, scrollElement, activeItem } = this
let children = renderArray.map(item => {
let iconClassNames
if (item.name) {
iconClassNames = fileIcons.getClassByLanguage(item.name)
}
// Because `markdown mode in Codemirror` don't have extensions.
// if still can not get the className, add a common className 'atom-icon light-cyan'
if (!iconClassNames) {
iconClassNames = item.name === 'markdown' ? fileIcons.getClassByName('fackname.md') : 'atom-icon light-cyan'
}
const iconSelector = 'span' + iconClassNames.split(/\s/).map(s => `.${s}`).join('')
const icon = h('div.icon-wrapper', h(iconSelector))
const text = h('div.language', item.name)
const selector = activeItem === item ? 'li.item.active' : 'li.item'
return h(selector, {
dataset: {
label: item.name
},
on: {
click: () => {
this.selectItem(item)
}
}
}, [icon, text])
})
if (children.length === 0) {
children = h('div.no-result', 'No result')
}
const vnode = h('ul', children)
if (oldVnode) {
patch(oldVnode, vnode)
} else {
patch(scrollElement, vnode)
}
this.oldVnode = vnode
}
getItemElement (item) {
const { name } = item
return this.floatBox.querySelector(`[data-label="${name}"]`)
}
}
export default CodePicker