-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextend-menu-drop-panel.html
153 lines (133 loc) · 4.19 KB
/
extend-menu-drop-panel.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>wangEditor extend dropPanel menu</title>
<link href="https://cdn.bootcdn.net/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet">
<!-- <link href="https://cdn.jsdelivr.net/npm/@wangeditor-next/editor@latest/dist/css/style.css" rel="stylesheet"> -->
<link href="https://unpkg.com/@wangeditor-next/editor@latest/dist/css/style.css" rel="stylesheet">
<link href="./css/layout.css" rel="stylesheet">
<style>
.w-e-panel-my-list {
text-align: left;
}
.w-e-panel-my-list li {
display: inline;
cursor: pointer;
padding: 3px 5px;
}
.w-e-panel-my-list li:hover {
background-color: #f1f1f1;
}
</style>
<script src="./js/custom-elem.js"></script>
</head>
<body>
<demo-nav title="wangEditor extend dropPanel menu"></demo-nav>
<div class="page-container">
<div class="page-left">
<demo-menu></demo-menu>
</div>
<div class="page-right">
<!-- 编辑器 DOM -->
<div style="border: 1px solid #ccc;">
<div id="editor-toolbar" style="border-bottom: 1px solid #ccc;"></div>
<div id="editor-text-area" style="height: 500px"></div>
</div>
<!-- 内容状态 -->
<p style="background-color: #f1f1f1;">
Text length: <span id="total-length"></span>;
Selected text length: <span id="selected-length"></span>;
</p>
</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/@wangeditor-next/editor@latest/dist/index.min.js"></script> -->
<script src="https://unpkg.com/@wangeditor-next/editor@latest/dist/index.js"></script>
<script>
const E = window.wangEditor
const LANG = location.href.indexOf('lang=en') > 0 ? 'en' : 'zh-CN'
E.i18nChangeLanguage(LANG) // 切换语言
// Extend menu
class MyMenu {
constructor() {
this.title = 'My menu'
// this.iconSvg = '<svg >...</svg>'
this.tag = 'button'
this.showDropPanel = true
}
getValue(editor) {
return ''
}
isActive(editor) {
return false // or true
}
isDisabled(editor) {
return false // or true
}
exec(editor, value) {
// do nothing 什么都不用做
}
getPanelContentElem(editor) {
const $list = $(`<ul class="w-e-panel-my-list">
<li>北京</li>
<li>上海</li>
<li>深圳</li>
<li>广州</li>
<li>天津</li>
<li>成都</li>
<li>南京</li>
<li>郑州</li>
</ul>`)
$list.on('click', 'li', function () {
editor.insertText(this.innerHTML)
editor.insertText(' ')
})
return $list[0]
// PS:也可以把 $list 缓存下来,这样不用每次重复创建、重复绑定事件,优化性能
}
}
const myMenuConf = {
key: 'myMenu',
factory() {
return new MyMenu()
}
}
E.Boot.registerMenu(myMenuConf)
window.editor = E.createEditor({
selector: '#editor-text-area',
html: '<p><br></p>',
config: {
placeholder: 'Type here...',
MENU_CONF: {
uploadImage: {
fieldName: 'your-fileName',
base64LimitSize: 10 * 1024 * 1024 // 10M 以下插入 base64
}
},
onChange(editor) {
console.log(editor.getHtml())
// 选中文字
const selectionText = editor.getSelectionText()
document.getElementById('selected-length').innerHTML = selectionText.length
// 全部文字
const text = editor.getText().replace(/\n|\r/mg, '')
document.getElementById('total-length').innerHTML = text.length
}
}
})
window.toolbar = E.createToolbar({
editor,
selector: '#editor-toolbar',
config: {
insertKeys: {
index: 0,
keys: ['myMenu'], // show menu in toolbar
}
}
})
</script>
</body>
</html>