/
HeaderSelectMenu.ts
89 lines (79 loc) · 2.25 KB
/
HeaderSelectMenu.ts
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
/**
* @description header menu
* @author wangfupeng
*/
import { ISelectMenu, IDomEditor, IOption, t } from '@wangeditor/core'
import { HEADER_SVG } from '../../../constants/icon-svg'
import { getHeaderType, isMenuDisabled, setHeaderType } from '../helper'
class HeaderSelectMenu implements ISelectMenu {
readonly title = t('header.title')
readonly iconSvg = HEADER_SVG
readonly tag = 'select'
readonly width = 60
getOptions(editor: IDomEditor): IOption[] {
// 基本的 options 列表
const options = [
// value 和 elemNode.type 对应
{
value: 'header1',
text: 'H1',
styleForRenderMenuList: { 'font-size': '32px', 'font-weight': 'bold' },
},
{
value: 'header2',
text: 'H2',
styleForRenderMenuList: { 'font-size': '24px', 'font-weight': 'bold' },
},
{
value: 'header3',
text: 'H3',
styleForRenderMenuList: { 'font-size': '18px', 'font-weight': 'bold' },
},
{
value: 'header4',
text: 'H4',
styleForRenderMenuList: { 'font-size': '16px', 'font-weight': 'bold' },
},
{
value: 'header5',
text: 'H5',
styleForRenderMenuList: { 'font-size': '13px', 'font-weight': 'bold' },
},
{ value: 'paragraph', text: t('header.text') },
]
// 获取 value ,设置 selected
const curValue = this.getValue(editor).toString()
options.forEach((opt: IOption) => {
if (opt.value === curValue) {
opt.selected = true
} else {
delete opt.selected
}
})
return options
}
isActive(editor: IDomEditor): boolean {
// select menu 会显示 selected value ,用不到 active
return false
}
/**
* 获取选中节点的 node.type
* @param editor editor
*/
getValue(editor: IDomEditor): string | boolean {
return getHeaderType(editor)
}
isDisabled(editor: IDomEditor): boolean {
return isMenuDisabled(editor)
}
/**
* 执行命令
* @param editor editor
* @param value node.type
*/
exec(editor: IDomEditor, value: string | boolean) {
//【注意】value 是 select change 时获取的,并不是 this.getValue 的值
setHeaderType(editor, value.toString())
}
}
export default HeaderSelectMenu