@lambo-design/wang-editor 2.0.0-beta.2 → 2.0.0-beta.4

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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/index.vue +91 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lambo-design/wang-editor",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-beta.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "lambo",
@@ -10,8 +10,8 @@
10
10
  "registry": "https://registry.npmjs.org/"
11
11
  },
12
12
  "devDependencies": {
13
- "@lambo-design/shared": "^1.0.0-beta.334",
14
- "@lambo-design/core": "^4.7.1-beta.174"
13
+ "@lambo-design/core": "^4.7.1-beta.175",
14
+ "@lambo-design/shared": "^1.0.0-beta.334"
15
15
  },
16
16
  "dependencies": {
17
17
  "@wangeditor/editor": "5.1.23",
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="value"
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,21 @@ export default {
114
119
  */
115
120
  excludeKeys: {
116
121
  type: Array,
117
- default: []
122
+ default: () => []
123
+ },
124
+ /**
125
+ * @description 是否使用自定义图片样式插入
126
+ */
127
+ useCustomImageStyle: {
128
+ type: Boolean,
129
+ default: false
130
+ },
131
+ /**
132
+ * @description 自定义图片宽度比例或像素值
133
+ */
134
+ customImageWidth: {
135
+ type: String,
136
+ default: '30%'
118
137
  }
119
138
 
120
139
  },
@@ -133,16 +152,56 @@ export default {
133
152
  },
134
153
  },
135
154
  methods: {
155
+ setHtml(val) {
156
+ if (this.editor) {
157
+ this.editor.setHtml(val)
158
+ } else {
159
+ // 编辑器尚未初始化时的处理
160
+ console.warn('Editor not initialized yet, queuing the content update...');
161
+ // 可以考虑缓存内容,等待编辑器初始化后设置
162
+ this.pendingContent = val;
163
+ }
164
+ },
165
+ handleChange(editor) {
166
+ const html = editor.getHtml();
167
+ const text = editor.getText();
168
+ if (this.cache) {
169
+ localStorage.editorCache = html;
170
+ }
171
+ this.$emit('input', this.valueType === 'html' ? html : text);
172
+ this.$emit('on-change', html, text);
173
+ },
136
174
  onCreated(editor) {
137
175
  this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
176
+ // 加载本地存储内容或传入的 value
177
+ const htmlCache = this.value || localStorage.getItem('editorCache');
178
+ if (this.cache && htmlCache && !this.value) {
179
+ this.editor.setHtml(htmlCache);
180
+ }
181
+ // 处理初始化前设置的内容
182
+ if (this.pendingContent != null) {
183
+ this.editor.setHtml(this.pendingContent);
184
+ this.pendingContent = null;
185
+ }
138
186
  },
139
187
  handlePaste(editor, event, callback) {
140
188
  if (this.preservePasteStyle) {
141
189
  // 保留源格式的处理
142
190
  const html = event.clipboardData.getData('text/html')
191
+
192
+ const items = event.clipboardData.items;
193
+ // 检查是否有图片数据
194
+ for (let i = 0; i < items.length; i++) {
195
+ const item = items[i];
196
+ if (item.type.indexOf('image') !== -1) {
197
+ // 有图片数据,让编辑器处理
198
+ callback(true); // 允许默认粘贴行为
199
+ return;
200
+ }
201
+ }
143
202
  if (html) {
144
203
  // 插入带格式的HTML
145
- editor.setHtml(html)
204
+ editor.dangerouslyInsertHtml(html)
146
205
  } else {
147
206
  // 没有HTML内容时回退到纯文本
148
207
  editor.insertText(event.clipboardData.getData('text/plain'))
@@ -157,34 +216,54 @@ export default {
157
216
  callback(false)
158
217
  },
159
218
  },
160
- mounted() {
219
+ created() {
220
+ let self = this
161
221
  // 如果仅仅想排除掉某些菜单,其他都保留,可以使用 excludeKeys 来配置。可通过 toolbar.getConfig().toolbarKeys 查看工具栏的默认配置
162
- this.toolbarConfig.excludeKeys = this.excludeKeys
222
+ this.toolbarConfig.excludeKeys = [
223
+ ...this.excludeKeys,
224
+ 'insertImage', // 网络图片
225
+ 'group-video', // 视频菜单组
226
+ ]
163
227
 
164
228
  this.editorConfig.MENU_CONF['uploadImage'] = {
165
229
  // base64 限制大小,小于该值就插入 base64 格式(而不上传),默认为 0
166
230
  base64LimitSize: this.uploadImgShowBase64 ? 5 * 1024 * 1024 : 0,
167
231
 
168
232
  async customUpload(files, insertFn) {
233
+ console.log("customUpload",files)
169
234
  const formData = new FormData();
170
- for (const file of files) {
171
- formData.append('file', file);
235
+ // 检查 files 是否为数组
236
+ if (Array.isArray(files)) {
237
+ for (const file of files) {
238
+ formData.append('file', file);
239
+ }
240
+ } else {
241
+ // 如果 files 不是数组,直接 append
242
+ formData.append('file', files);
172
243
  }
173
244
  try {
174
245
  const response = await axios.post(
175
- this.ossServerContext + this.ossFilePutUrl,
246
+ self.ossServerContext + self.ossFilePutUrl,
176
247
  formData
177
248
  );
178
249
 
179
250
  if (response.data.code == "1") {
180
251
  const result = response.data.data;
181
252
  const imageList = result.map(item => ({
182
- url: this.ossServerContext + this.ossFileGetImgUrl + item.fileId,
253
+ url: self.ossServerContext + self.ossFileGetImgUrl + item.fileId,
183
254
  alt: item.fileName
184
255
  }));
185
256
  // 循环插入每张图片
186
257
  imageList.forEach(imageData => {
187
- insertFn(imageData.url, imageData.alt, imageData.url);
258
+ if (self.useCustomImageStyle) {
259
+ // 使用自定义样式插入
260
+ const imgUrl = imageData.url;
261
+ const imgHtml = `<img src="${imgUrl}" alt="${imageData.alt}" style="width: ${self.customImageWidth};"/>`;
262
+ self.editor.dangerouslyInsertHtml(imgHtml);
263
+ } else {
264
+ // 使用默认方式插入
265
+ insertFn(imageData.url, imageData.alt, imageData.url);
266
+ }
188
267
  });
189
268
  }
190
269
  } catch (error) {
@@ -195,16 +274,6 @@ export default {
195
274
  // 注意:不需要修改的配置可以省略,wangEditor 会自动合并默认配置
196
275
  }
197
276
 
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
277
  this.editorConfig.MENU_CONF['insertLink'] = {
209
278
  checkLink: (text, link) => {
210
279
  if (!/^https?:\/\/[^\s/$.?#].[^\s]*$/.test(link)) {
@@ -216,13 +285,6 @@ export default {
216
285
  return true;
217
286
  }
218
287
  }
219
-
220
- // 加载本地存储内容或传入的 value
221
- const htmlCache = this.value || localStorage.getItem('editorCache');
222
-
223
- if (html) {
224
- editor.setHtml(htmlCache);
225
- }
226
288
  },
227
289
  beforeDestroy() {
228
290
  const editor = this.editor