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

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lambo-design/wang-editor",
3
- "version": "1.0.0-beta.9",
3
+ "version": "2.0.0-beta.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "lambo",
@@ -9,12 +9,13 @@
9
9
  "access": "public",
10
10
  "registry": "https://registry.npmjs.org/"
11
11
  },
12
- "dependencies": {
13
- "wangeditor": "^3.1.1"
14
- },
15
12
  "devDependencies": {
16
- "@lambo-design/shared": "^1.0.0-beta.250",
17
- "@lambo-design/core": "^4.7.1-beta.158"
13
+ "@lambo-design/shared": "^1.0.0-beta.334",
14
+ "@lambo-design/core": "^4.7.1-beta.174"
15
+ },
16
+ "dependencies": {
17
+ "@wangeditor/editor": "5.1.23",
18
+ "@wangeditor/editor-for-vue": "^1.0.2"
18
19
  },
19
20
  "scripts": {
20
21
  "release": "pnpm release-beta && git push --follow-tags && pnpm re-publish",
package/src/index.vue CHANGED
@@ -1,15 +1,40 @@
1
1
  <template>
2
- <div class="lambo-wang-editor editor-wrapper">
3
- <div :id="editorId"></div>
2
+ <div style="border: 1px solid #ccc;">
3
+ <Toolbar
4
+ style="border-bottom: 1px solid #ccc"
5
+ :editor="editor"
6
+ :defaultConfig="toolbarConfig"
7
+ :mode="mode"
8
+ />
9
+ <Editor
10
+ style="height: 500px; overflow-y: hidden;"
11
+ v-model="value"
12
+ :defaultConfig="editorConfig"
13
+ :mode="mode"
14
+ @onCreated="onCreated"
15
+ @customPaste="handlePaste"
16
+ />
4
17
  </div>
5
18
  </template>
6
19
 
7
20
  <script>
8
21
  import axios from 'axios';
9
22
  import {oneOf} from '@lambo-design/shared/utils/platform';
23
+ import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
10
24
 
11
25
  export default {
12
26
  name: 'LamboWangEditor',
27
+ components: { Editor, Toolbar },
28
+ data() {
29
+ return {
30
+ editor: null,
31
+ toolbarConfig: {},
32
+ editorConfig: {
33
+ placeholder: '请输入内容...',
34
+ zIndex: 999
35
+ },
36
+ }
37
+ },
13
38
  props: {
14
39
  value: {
15
40
  type: String,
@@ -67,130 +92,143 @@ export default {
67
92
  type: String,
68
93
  default: "/oss/file/get/"
69
94
  },
95
+ /**
96
+ * @description 是否保留粘贴内容的样式
97
+ */
98
+ preservePasteStyle: {
99
+ type: Boolean,
100
+ default: false
101
+ },
102
+ /**
103
+ * @description 模式, enum: ['default', 'simple']
104
+ */
105
+ mode: {
106
+ type: String,
107
+ default: 'default',
108
+ validator: (val) => {
109
+ return oneOf(val, ['default', 'simple'])
110
+ }
111
+ },
112
+ /**
113
+ * @description 排除菜单组,写菜单组 key 的值即可
114
+ */
115
+ excludeKeys: {
116
+ type: Array,
117
+ default: []
118
+ }
70
119
 
71
120
  },
72
- computed: {
73
- editorId() {
74
- return `editor${this._uid}`
75
- }
121
+ watch: {
122
+ value: {
123
+ handler: function (newVal, oldVal) {
124
+ if (oldVal === '' && this.editor) {
125
+ if (this.valueType === 'html') {
126
+ this.editor.setHtml(newVal);
127
+ } else {
128
+ // 如果是文本模式,也需要转换为 HTML 格式
129
+ this.editor.setHtml(`<p>${newVal}</p>`);
130
+ }
131
+ }
132
+ }
133
+ },
76
134
  },
77
135
  methods: {
78
- setHtml(val) {
79
- this.editor.txt.html(val)
80
- }
136
+ onCreated(editor) {
137
+ this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
138
+ },
139
+ handlePaste(editor, event, callback) {
140
+ if (this.preservePasteStyle) {
141
+ // 保留源格式的处理
142
+ const html = event.clipboardData.getData('text/html')
143
+ if (html) {
144
+ // 插入带格式的HTML
145
+ editor.setHtml(html)
146
+ } else {
147
+ // 没有HTML内容时回退到纯文本
148
+ editor.insertText(event.clipboardData.getData('text/plain'))
149
+ }
150
+ } else {
151
+ // 不保留格式,强制转为纯文本
152
+ editor.insertText(event.clipboardData.getData('text/plain'))
153
+ }
154
+
155
+ // 阻止默认行为
156
+ event.preventDefault()
157
+ callback(false)
158
+ },
81
159
  },
82
160
  mounted() {
83
- let self = this;
84
- const Editor = require('wangeditor')
85
- self.editor = new Editor(`#${this.editorId}`)
86
- self.editor.customConfig.onchange = (html) => {
87
- let text = self.editor.txt.text()
88
- if (self.cache) localStorage.editorCache = html
89
- this.$emit('input', self.valueType === 'html' ? html : text)
90
- this.$emit('on-change', html, text)
91
- }
92
-
93
- // 删除粘贴内容的样式
94
- self.editor.customConfig.pasteFilterStyle = false
95
- // 设置触发事件间隔
96
- self.editor.customConfig.onchangeTimeout = this.changeInterval
161
+ // 如果仅仅想排除掉某些菜单,其他都保留,可以使用 excludeKeys 来配置。可通过 toolbar.getConfig().toolbarKeys 查看工具栏的默认配置
162
+ this.toolbarConfig.excludeKeys = this.excludeKeys
97
163
 
98
- self.editor.customConfig.zIndex = 999
164
+ this.editorConfig.MENU_CONF['uploadImage'] = {
165
+ // base64 限制大小,小于该值就插入 base64 格式(而不上传),默认为 0
166
+ base64LimitSize: this.uploadImgShowBase64 ? 5 * 1024 * 1024 : 0,
99
167
 
100
- // 配置debug模式
101
- self.editor.customConfig.debug = true
102
- // 隐藏“网络图片”tab
103
- self.editor.customConfig.showLinkImg = false
104
- // 使用 base64 保存图片
105
- self.editor.customConfig.uploadImgShowBase64 = self.uploadImgShowBase64
106
- // 上传图片到服务器
107
- // this.editor.customConfig.uploadImgServer = config.ossServerContext + "/file/wangeditor/put"
108
- //
109
- self.editor.customConfig.uploadImgHeaders = {
110
- // 'Accept': 'multipart/form-data, text/plain, */*',
111
- // 'enctype':'multipart/form-data'
112
- // =""
113
- }
114
- let ossServer = this.ossServerContext;
115
- if(!self.uploadImgShowBase64){
116
- self.editor.customConfig.customUploadImg = function (files, insert) {
117
- let formData = new FormData();
118
- for (let file of files) {
168
+ async customUpload(files, insertFn) {
169
+ const formData = new FormData();
170
+ for (const file of files) {
119
171
  formData.append('file', file);
120
172
  }
121
- // files 是 input 中选中的文件列表
122
- // insert 是获取图片 url 后,插入到编辑器的方法
123
- axios.post(ossServer + self.ossFilePutUrl, formData).then(response => {
124
- if (response.data.code == "1") {
125
- var result = response.data.data;
126
- // 上传代码返回结果之后,将图片插入到编辑器中
127
- let imgUrl = []
128
- result.forEach(item => {
129
- insert(ossServer + self.ossFileGetImgUrl + item.fileId)
130
- // imgUrl.push()
131
- })
173
+ try {
174
+ const response = await axios.post(
175
+ this.ossServerContext + this.ossFilePutUrl,
176
+ formData
177
+ );
132
178
 
179
+ if (response.data.code == "1") {
180
+ const result = response.data.data;
181
+ const imageList = result.map(item => ({
182
+ url: this.ossServerContext + this.ossFileGetImgUrl + item.fileId,
183
+ alt: item.fileName
184
+ }));
185
+ // 循环插入每张图片
186
+ imageList.forEach(imageData => {
187
+ insertFn(imageData.url, imageData.alt, imageData.url);
188
+ });
133
189
  }
134
- })
190
+ } catch (error) {
191
+ console.error('图片上传失败:', error);
192
+ }
135
193
  }
136
-
194
+ // 其他图片上传配置...
195
+ // 注意:不需要修改的配置可以省略,wangEditor 会自动合并默认配置
137
196
  }
138
- // 自定义菜单配置
139
- self.editor.customConfig.menus = [
140
- 'head', // 标题
141
- 'bold', // 粗体
142
- 'fontSize', // 字号
143
- 'fontName', // 字体
144
- 'italic', // 斜体
145
- 'underline', // 下划线
146
- 'strikeThrough', // 删除线
147
- 'foreColor', // 文字颜色
148
- 'backColor', // 背景颜色
149
- 'link', // 插入链接
150
- 'list', // 列表
151
- 'justify', // 对齐方式
152
- 'quote', // 引用
153
- // 'emoticon', // 表情
154
- 'image', // 插入图片
155
- 'table', // 表格
156
- 'video', // 插入视频
157
- 'code', // 插入代码
158
- 'undo', // 撤销
159
- 'redo' // 重复
160
- ]
161
- // 自定义配置颜色(字体颜色、背景色)
162
- self.editor.customConfig.colors = [
163
- '#000000',
164
- '#eeece0',
165
- '#1c487f',
166
- '#4d80bf',
167
- '#c24f4a',
168
- '#8baa4a',
169
- '#7b5ba1',
170
- '#46acc8',
171
- '#f9963b',
172
- '#ffffff'
173
- ]
174
- self.editor.customConfig.pasteTextHandle = function (content) {
175
- // content 即粘贴过来的内容(html 或 纯文本),可进行自定义处理然后返回
176
- if (content == '' && !content) return ''
177
- var str = content
178
- str = str.replace(/<xml>[\s\S]*?<\/xml>/ig, '')
179
- str = str.replace(/<style>[\s\S]*?<\/style>/ig, '')
180
- str = str.replace(/<\/?[^>]*>/g, '')
181
- str = str.replace(/[ | ]*\n/g, '\n')
182
- // str = str.replace(/&nbsp;/ig, '')
183
- return str
197
+
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);
184
206
  }
185
207
 
186
- // create这个方法一定要在所有配置项之后调用
187
- self.editor.create()
188
- // 如果本地有存储加载本地存储内容
189
- let html = self.value || localStorage.editorCache
208
+ this.editorConfig.MENU_CONF['insertLink'] = {
209
+ checkLink: (text, link) => {
210
+ if (!/^https?:\/\/[^\s/$.?#].[^\s]*$/.test(link)) {
211
+ return '链接格式不正确,请输入有效的HTTP/HTTPS地址';
212
+ }
213
+ if (link.includes('javascript:')) {
214
+ return '禁止使用javascript协议';
215
+ }
216
+ return true;
217
+ }
218
+ }
190
219
 
220
+ // 加载本地存储内容或传入的 value
221
+ const htmlCache = this.value || localStorage.getItem('editorCache');
191
222
 
192
- if (html) self.editor.txt.html(html)
193
- }
223
+ if (html) {
224
+ editor.setHtml(htmlCache);
225
+ }
226
+ },
227
+ beforeDestroy() {
228
+ const editor = this.editor
229
+ if (editor == null) return
230
+ editor.destroy() // 组件销毁时,及时销毁编辑器
231
+ },
194
232
  }
195
233
  </script>
196
234