@lambo-design/wang-editor 1.0.0-beta.8 → 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.8",
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/core": "^4.7.1-beta.158",
17
- "@lambo-design/shared": "^1.0.0-beta.250"
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,129 +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
- }
161
+ // 如果仅仅想排除掉某些菜单,其他都保留,可以使用 excludeKeys 来配置。可通过 toolbar.getConfig().toolbarKeys 查看工具栏的默认配置
162
+ this.toolbarConfig.excludeKeys = this.excludeKeys
92
163
 
93
- // 删除粘贴内容的样式
94
- self.editor.customConfig.pasteFilterStyle = false
95
- // 设置触发事件间隔
96
- self.editor.customConfig.onchangeTimeout = this.changeInterval
164
+ this.editorConfig.MENU_CONF['uploadImage'] = {
165
+ // base64 限制大小,小于该值就插入 base64 格式(而不上传),默认为 0
166
+ base64LimitSize: this.uploadImgShowBase64 ? 5 * 1024 * 1024 : 0,
97
167
 
98
- self.editor.customConfig.zIndex = 999
168
+ async customUpload(files, insertFn) {
169
+ const formData = new FormData();
170
+ for (const file of files) {
171
+ formData.append('file', file);
172
+ }
173
+ try {
174
+ const response = await axios.post(
175
+ this.ossServerContext + this.ossFilePutUrl,
176
+ formData
177
+ );
99
178
 
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
- // =""
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
+ });
189
+ }
190
+ } catch (error) {
191
+ console.error('图片上传失败:', error);
192
+ }
193
+ }
194
+ // 其他图片上传配置...
195
+ // 注意:不需要修改的配置可以省略,wangEditor 会自动合并默认配置
113
196
  }
114
- let ossServer = this.ossServerContext;
115
- self.editor.customConfig.customUploadImg = function (files, insert) {
116
- let formData = new FormData();
117
- for (let file of files) {
118
- formData.append('file', file);
197
+
198
+ this.editorConfig.onChange = (editor) => {
199
+ const html = editor.getHtml();
200
+ const text = editor.getText();
201
+ if (this.cache) {
202
+ localStorage.editorCache = html;
119
203
  }
120
- // files 是 input 中选中的文件列表
121
- // insert 是获取图片 url 后,插入到编辑器的方法
122
- axios.post(ossServer + self.ossFilePutUrl, formData).then(response => {
123
- if (response.data.code == "1") {
124
- var result = response.data.data;
125
- // 上传代码返回结果之后,将图片插入到编辑器中
126
- let imgUrl = []
127
- result.forEach(item => {
128
- insert(ossServer + self.ossFileGetImgUrl + item.fileId)
129
- // imgUrl.push()
130
- })
204
+ this.$emit('input', this.valueType === 'html' ? html : text);
205
+ this.$emit('on-change', html, text);
206
+ }
131
207
 
208
+ this.editorConfig.MENU_CONF['insertLink'] = {
209
+ checkLink: (text, link) => {
210
+ if (!/^https?:\/\/[^\s/$.?#].[^\s]*$/.test(link)) {
211
+ return '链接格式不正确,请输入有效的HTTP/HTTPS地址';
132
212
  }
133
- })
134
-
135
-
136
- }
137
- // 自定义菜单配置
138
- self.editor.customConfig.menus = [
139
- 'head', // 标题
140
- 'bold', // 粗体
141
- 'fontSize', // 字号
142
- 'fontName', // 字体
143
- 'italic', // 斜体
144
- 'underline', // 下划线
145
- 'strikeThrough', // 删除线
146
- 'foreColor', // 文字颜色
147
- 'backColor', // 背景颜色
148
- 'link', // 插入链接
149
- 'list', // 列表
150
- 'justify', // 对齐方式
151
- 'quote', // 引用
152
- // 'emoticon', // 表情
153
- 'image', // 插入图片
154
- 'table', // 表格
155
- 'video', // 插入视频
156
- 'code', // 插入代码
157
- 'undo', // 撤销
158
- 'redo' // 重复
159
- ]
160
- // 自定义配置颜色(字体颜色、背景色)
161
- self.editor.customConfig.colors = [
162
- '#000000',
163
- '#eeece0',
164
- '#1c487f',
165
- '#4d80bf',
166
- '#c24f4a',
167
- '#8baa4a',
168
- '#7b5ba1',
169
- '#46acc8',
170
- '#f9963b',
171
- '#ffffff'
172
- ]
173
- self.editor.customConfig.pasteTextHandle = function (content) {
174
- // content 即粘贴过来的内容(html 或 纯文本),可进行自定义处理然后返回
175
- if (content == '' && !content) return ''
176
- var str = content
177
- str = str.replace(/<xml>[\s\S]*?<\/xml>/ig, '')
178
- str = str.replace(/<style>[\s\S]*?<\/style>/ig, '')
179
- str = str.replace(/<\/?[^>]*>/g, '')
180
- str = str.replace(/[ | ]*\n/g, '\n')
181
- // str = str.replace(/&nbsp;/ig, '')
182
- return str
213
+ if (link.includes('javascript:')) {
214
+ return '禁止使用javascript协议';
215
+ }
216
+ return true;
217
+ }
183
218
  }
184
219
 
185
- // create这个方法一定要在所有配置项之后调用
186
- self.editor.create()
187
- // 如果本地有存储加载本地存储内容
188
- let html = self.value || localStorage.editorCache
189
-
220
+ // 加载本地存储内容或传入的 value
221
+ const htmlCache = this.value || localStorage.getItem('editorCache');
190
222
 
191
- if (html) self.editor.txt.html(html)
192
- }
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
+ },
193
232
  }
194
233
  </script>
195
234