@lambo-design/wang-editor 2.0.0-beta.2 → 2.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.vue +68 -28
package/package.json
CHANGED
package/src/index.vue
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<div style="border: 1px solid #ccc;">
|
|
2
|
+
<div style="border: 1px solid #ccc; z-index: 999">
|
|
3
3
|
<Toolbar
|
|
4
4
|
style="border-bottom: 1px solid #ccc"
|
|
5
5
|
:editor="editor"
|
|
@@ -8,11 +8,12 @@
|
|
|
8
8
|
/>
|
|
9
9
|
<Editor
|
|
10
10
|
style="height: 500px; overflow-y: hidden;"
|
|
11
|
-
v-model="
|
|
11
|
+
v-model="localValue"
|
|
12
12
|
:defaultConfig="editorConfig"
|
|
13
13
|
:mode="mode"
|
|
14
14
|
@onCreated="onCreated"
|
|
15
15
|
@customPaste="handlePaste"
|
|
16
|
+
@onChange="handleChange"
|
|
16
17
|
/>
|
|
17
18
|
</div>
|
|
18
19
|
</template>
|
|
@@ -21,6 +22,7 @@
|
|
|
21
22
|
import axios from 'axios';
|
|
22
23
|
import {oneOf} from '@lambo-design/shared/utils/platform';
|
|
23
24
|
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
|
|
25
|
+
import '@wangeditor/editor/dist/css/style.css'
|
|
24
26
|
|
|
25
27
|
export default {
|
|
26
28
|
name: 'LamboWangEditor',
|
|
@@ -31,8 +33,11 @@ export default {
|
|
|
31
33
|
toolbarConfig: {},
|
|
32
34
|
editorConfig: {
|
|
33
35
|
placeholder: '请输入内容...',
|
|
34
|
-
zIndex: 999
|
|
36
|
+
zIndex: 999,
|
|
37
|
+
MENU_CONF: {}
|
|
35
38
|
},
|
|
39
|
+
localValue: this.value,
|
|
40
|
+
pendingContent: null,
|
|
36
41
|
}
|
|
37
42
|
},
|
|
38
43
|
props: {
|
|
@@ -114,7 +119,7 @@ export default {
|
|
|
114
119
|
*/
|
|
115
120
|
excludeKeys: {
|
|
116
121
|
type: Array,
|
|
117
|
-
default: []
|
|
122
|
+
default: () => []
|
|
118
123
|
}
|
|
119
124
|
|
|
120
125
|
},
|
|
@@ -133,16 +138,56 @@ export default {
|
|
|
133
138
|
},
|
|
134
139
|
},
|
|
135
140
|
methods: {
|
|
141
|
+
setHtml(val) {
|
|
142
|
+
if (this.editor) {
|
|
143
|
+
this.editor.setHtml(val)
|
|
144
|
+
} else {
|
|
145
|
+
// 编辑器尚未初始化时的处理
|
|
146
|
+
console.warn('Editor not initialized yet, queuing the content update...');
|
|
147
|
+
// 可以考虑缓存内容,等待编辑器初始化后设置
|
|
148
|
+
this.pendingContent = val;
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
handleChange(editor) {
|
|
152
|
+
const html = editor.getHtml();
|
|
153
|
+
const text = editor.getText();
|
|
154
|
+
if (this.cache) {
|
|
155
|
+
localStorage.editorCache = html;
|
|
156
|
+
}
|
|
157
|
+
this.$emit('input', this.valueType === 'html' ? html : text);
|
|
158
|
+
this.$emit('on-change', html, text);
|
|
159
|
+
},
|
|
136
160
|
onCreated(editor) {
|
|
137
161
|
this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
|
|
162
|
+
// 加载本地存储内容或传入的 value
|
|
163
|
+
const htmlCache = this.value || localStorage.getItem('editorCache');
|
|
164
|
+
if (this.cache && htmlCache && !this.value) {
|
|
165
|
+
this.editor.setHtml(htmlCache);
|
|
166
|
+
}
|
|
167
|
+
// 处理初始化前设置的内容
|
|
168
|
+
if (this.pendingContent != null) {
|
|
169
|
+
this.editor.setHtml(this.pendingContent);
|
|
170
|
+
this.pendingContent = null;
|
|
171
|
+
}
|
|
138
172
|
},
|
|
139
173
|
handlePaste(editor, event, callback) {
|
|
140
174
|
if (this.preservePasteStyle) {
|
|
141
175
|
// 保留源格式的处理
|
|
142
176
|
const html = event.clipboardData.getData('text/html')
|
|
177
|
+
|
|
178
|
+
const items = event.clipboardData.items;
|
|
179
|
+
// 检查是否有图片数据
|
|
180
|
+
for (let i = 0; i < items.length; i++) {
|
|
181
|
+
const item = items[i];
|
|
182
|
+
if (item.type.indexOf('image') !== -1) {
|
|
183
|
+
// 有图片数据,让编辑器处理
|
|
184
|
+
callback(true); // 允许默认粘贴行为
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
143
188
|
if (html) {
|
|
144
189
|
// 插入带格式的HTML
|
|
145
|
-
editor.
|
|
190
|
+
editor.dangerouslyInsertHtml(html)
|
|
146
191
|
} else {
|
|
147
192
|
// 没有HTML内容时回退到纯文本
|
|
148
193
|
editor.insertText(event.clipboardData.getData('text/plain'))
|
|
@@ -157,29 +202,41 @@ export default {
|
|
|
157
202
|
callback(false)
|
|
158
203
|
},
|
|
159
204
|
},
|
|
160
|
-
|
|
205
|
+
created() {
|
|
206
|
+
let self = this
|
|
161
207
|
// 如果仅仅想排除掉某些菜单,其他都保留,可以使用 excludeKeys 来配置。可通过 toolbar.getConfig().toolbarKeys 查看工具栏的默认配置
|
|
162
|
-
this.toolbarConfig.excludeKeys =
|
|
208
|
+
this.toolbarConfig.excludeKeys = [
|
|
209
|
+
...this.excludeKeys,
|
|
210
|
+
'insertImage', // 网络图片
|
|
211
|
+
'group-video', // 视频菜单组
|
|
212
|
+
]
|
|
163
213
|
|
|
164
214
|
this.editorConfig.MENU_CONF['uploadImage'] = {
|
|
165
215
|
// base64 限制大小,小于该值就插入 base64 格式(而不上传),默认为 0
|
|
166
216
|
base64LimitSize: this.uploadImgShowBase64 ? 5 * 1024 * 1024 : 0,
|
|
167
217
|
|
|
168
218
|
async customUpload(files, insertFn) {
|
|
219
|
+
console.log("customUpload",files)
|
|
169
220
|
const formData = new FormData();
|
|
170
|
-
|
|
171
|
-
|
|
221
|
+
// 检查 files 是否为数组
|
|
222
|
+
if (Array.isArray(files)) {
|
|
223
|
+
for (const file of files) {
|
|
224
|
+
formData.append('file', file);
|
|
225
|
+
}
|
|
226
|
+
} else {
|
|
227
|
+
// 如果 files 不是数组,直接 append
|
|
228
|
+
formData.append('file', files);
|
|
172
229
|
}
|
|
173
230
|
try {
|
|
174
231
|
const response = await axios.post(
|
|
175
|
-
|
|
232
|
+
self.ossServerContext + self.ossFilePutUrl,
|
|
176
233
|
formData
|
|
177
234
|
);
|
|
178
235
|
|
|
179
236
|
if (response.data.code == "1") {
|
|
180
237
|
const result = response.data.data;
|
|
181
238
|
const imageList = result.map(item => ({
|
|
182
|
-
url:
|
|
239
|
+
url: self.ossServerContext + self.ossFileGetImgUrl + item.fileId,
|
|
183
240
|
alt: item.fileName
|
|
184
241
|
}));
|
|
185
242
|
// 循环插入每张图片
|
|
@@ -195,16 +252,6 @@ export default {
|
|
|
195
252
|
// 注意:不需要修改的配置可以省略,wangEditor 会自动合并默认配置
|
|
196
253
|
}
|
|
197
254
|
|
|
198
|
-
this.editorConfig.onChange = (editor) => {
|
|
199
|
-
const html = editor.getHtml();
|
|
200
|
-
const text = editor.getText();
|
|
201
|
-
if (this.cache) {
|
|
202
|
-
localStorage.editorCache = html;
|
|
203
|
-
}
|
|
204
|
-
this.$emit('input', this.valueType === 'html' ? html : text);
|
|
205
|
-
this.$emit('on-change', html, text);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
255
|
this.editorConfig.MENU_CONF['insertLink'] = {
|
|
209
256
|
checkLink: (text, link) => {
|
|
210
257
|
if (!/^https?:\/\/[^\s/$.?#].[^\s]*$/.test(link)) {
|
|
@@ -216,13 +263,6 @@ export default {
|
|
|
216
263
|
return true;
|
|
217
264
|
}
|
|
218
265
|
}
|
|
219
|
-
|
|
220
|
-
// 加载本地存储内容或传入的 value
|
|
221
|
-
const htmlCache = this.value || localStorage.getItem('editorCache');
|
|
222
|
-
|
|
223
|
-
if (html) {
|
|
224
|
-
editor.setHtml(htmlCache);
|
|
225
|
-
}
|
|
226
266
|
},
|
|
227
267
|
beforeDestroy() {
|
|
228
268
|
const editor = this.editor
|