@sy-common/wang-editor 1.0.0-beta.2 → 1.0.0-beta.20

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 +6 -4
  2. package/src/index.vue +212 -20
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sy-common/wang-editor",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.20",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "lambo",
@@ -9,13 +9,15 @@
9
9
  "access": "public",
10
10
  "registry": "https://registry.npmjs.org/"
11
11
  },
12
- "devDependencies": {
13
- "@lambo-design/core": "^4.7.1-beta.140",
12
+ "dependencies": {
14
13
  "@wangeditor-next/editor": "^5.6.0",
15
14
  "@wangeditor-next/editor-for-vue2": "^1.0.2"
16
15
  },
16
+ "devDependencies": {
17
+ "@lambo-design/core": "^4.7.1-beta.140"
18
+ },
17
19
  "scripts": {
18
- "release-upload-file": "pnpm release-beta && git push --follow-tags && pnpm re-publish",
20
+ "release": "pnpm release-beta && git push --follow-tags && pnpm re-publish",
19
21
  "release-major": "standard-version --release-as major",
20
22
  "release-minor": "standard-version --release-as minor",
21
23
  "release-patch": "standard-version --release-as patch",
package/src/index.vue CHANGED
@@ -1,5 +1,5 @@
1
1
  <template>
2
- <div >
2
+ <div style="z-index: 999">
3
3
  <!-- 工具栏 -->
4
4
  <Toolbar
5
5
  :editor="editor"
@@ -7,7 +7,7 @@
7
7
  />
8
8
  <!-- 编辑器 -->
9
9
  <Editor
10
- style="height: 400px; overflow-y: hidden;"
10
+ :style="editorStyle"
11
11
  :defaultConfig="editorConfig"
12
12
  v-model="html"
13
13
  @onChange="onChange"
@@ -19,11 +19,11 @@
19
19
  <script>
20
20
  import '@wangeditor-next/editor/dist/css/style.css'
21
21
  import {oneOf} from '@lambo-design/shared/utils/platform';
22
- import { Editor, Toolbar } from '@wangeditor-next/editor-for-vue2'
22
+ import {Editor, Toolbar} from '@wangeditor-next/editor-for-vue2'
23
23
 
24
24
  export default {
25
25
  name: 'WangEditor',
26
- components: { Editor, Toolbar },
26
+ components: {Editor, Toolbar},
27
27
  props: {
28
28
  value: {
29
29
  type: String,
@@ -89,6 +89,22 @@ export default {
89
89
  default: false
90
90
  },
91
91
 
92
+ readOnly: {
93
+ type: Boolean,
94
+ default: false
95
+ },
96
+ /**
97
+ * @description: 隐藏菜单
98
+ */
99
+ hideMenu: {
100
+ type: Array,
101
+ default: () => []
102
+ },
103
+
104
+ height: {
105
+ type: [Number, String],
106
+ default: 400
107
+ }
92
108
  },
93
109
  data() {
94
110
  return {
@@ -101,20 +117,112 @@ export default {
101
117
  editorConfig: {
102
118
  placeholder: '请输入内容...',
103
119
  MENU_CONF: {
104
-
120
+ fontFamily: {
121
+ fontFamilyList: [
122
+ "黑体",
123
+ {
124
+ name: "仿宋",
125
+ value: "仿宋"
126
+ },
127
+ "仿宋_GB2312",
128
+ "楷体",
129
+ "楷体_GB2312",
130
+ "标楷体",
131
+ "华文仿宋",
132
+ "华文楷体",
133
+ {
134
+ name: "宋体",
135
+ value: "宋体"
136
+ }, "隶书", "方正小标宋简体", "思源宋体‌", "微软雅黑", "Arial", "Tahoma", "Verdana", "Times New Roman", "Courier New"]
137
+ },
138
+ color: {
139
+ colors: [
140
+ "rgb(0, 0, 0)",
141
+ "rgb(38, 38, 38)",
142
+ "rgb(89, 89, 89)",
143
+ "rgb(140, 140, 140)",
144
+ "rgb(191, 191, 191)",
145
+ "rgb(217, 217, 217)",
146
+ "rgb(233, 233, 233)",
147
+ "rgb(245, 245, 245)",
148
+ "rgb(250, 250, 250)",
149
+ "rgb(255, 255, 255)",
150
+ "rgb(225, 60, 57)",
151
+ "rgb(231, 95, 51)",
152
+ "rgb(235, 144, 58)",
153
+ "rgb(245, 219, 77)",
154
+ "rgb(114, 192, 64)",
155
+ "rgb(89, 191, 192)",
156
+ "rgb(66, 144, 247)",
157
+ "rgb(54, 88, 226)",
158
+ "rgb(106, 57, 201)",
159
+ "rgb(216, 68, 147)",
160
+ "rgb(251, 233, 230)",
161
+ "rgb(252, 237, 225)",
162
+ "rgb(252, 239, 212)",
163
+ "rgb(252, 251, 207)",
164
+ "rgb(231, 246, 213)",
165
+ "rgb(218, 244, 240)",
166
+ "rgb(217, 237, 250)",
167
+ "rgb(224, 232, 250)",
168
+ "rgb(237, 225, 248)",
169
+ "rgb(246, 226, 234)",
170
+ "rgb(255, 163, 158)",
171
+ "rgb(255, 187, 150)",
172
+ "rgb(255, 213, 145)",
173
+ "rgb(255, 251, 143)",
174
+ "rgb(183, 235, 143)",
175
+ "rgb(135, 232, 222)",
176
+ "rgb(145, 213, 255)",
177
+ "rgb(173, 198, 255)",
178
+ "rgb(211, 173, 247)",
179
+ "rgb(255, 173, 210)",
180
+ "rgb(255, 77, 79)",
181
+ "rgb(255, 122, 69)",
182
+ "rgb(255, 169, 64)",
183
+ "rgb(255, 236, 61)",
184
+ "rgb(115, 209, 61)",
185
+ "rgb(54, 207, 201)",
186
+ "rgb(64, 169, 255)",
187
+ "rgb(89, 126, 247)",
188
+ "rgb(146, 84, 222)",
189
+ "rgb(247, 89, 171)",
190
+ "rgb(207, 19, 34)",
191
+ "rgb(212, 56, 13)",
192
+ "rgb(212, 107, 8)",
193
+ "rgb(212, 177, 6)",
194
+ "rgb(56, 158, 13)",
195
+ "rgb(8, 151, 156)",
196
+ "rgb(9, 109, 217)",
197
+ "rgb(29, 57, 196)",
198
+ "rgb(83, 29, 171)",
199
+ "rgb(196, 29, 127)",
200
+ "rgb(130, 0, 20)",
201
+ "rgb(135, 20, 0)",
202
+ "rgb(135, 56, 0)",
203
+ "rgb(97, 71, 0)",
204
+ "rgb(19, 82, 0)",
205
+ "rgb(0, 71, 79)",
206
+ "rgb(0, 58, 140)",
207
+ "rgb(6, 17, 120)",
208
+ "rgb(34, 7, 94)",
209
+ "rgb(120, 6, 80)",
210
+ "rgb(250,64,6)"
211
+ ]
212
+ },
105
213
  uploadImage: {
106
214
  //上传图片配置
107
- server: this.ossServerContext+this.ossFilePutUrl, //上传接口地址
215
+ server: this.ossServerContext + this.ossFilePutUrl, //上传接口地址
108
216
  fieldName: 'file', //上传文件名
109
217
  methods: 'post',
110
218
  metaWithUrl: true, // 参数拼接到 url 上
111
219
  //自定义插入图片
112
- customInsert:(res, insertFn)=>{
220
+ customInsert: (res, insertFn) => {
113
221
  let self = this;
114
222
  const imgInfo = res.data[0] || {}
115
- const { fileId, fileName, contentType } = imgInfo
223
+ const {fileId, fileName, contentType} = imgInfo
116
224
  insertFn(self.ossServerContext + self.ossFileGetImgUrl + fileId, fileName, self.ossServerContext + self.ossFileGetImgUrl + fileId)
117
- } ,
225
+ },
118
226
 
119
227
  },
120
228
  }
@@ -124,37 +232,121 @@ export default {
124
232
  watch: {
125
233
  value: {
126
234
  handler: function (newVal, oldVal) {
127
- // console.log('watch',newVal+" "+oldVal)
128
- if (oldVal === '') {
235
+ // 只有初次赋值需要处理:解决wangeditor表格、单元格边框属性赋值会丢失问题
236
+ if (oldVal === '<p><br></p>') {
129
237
  if (this.valueType === 'html') {
130
- this.editor.setHtml(newVal)
238
+ let html = newVal;
239
+ // 正则匹配 <table>...</table> 区域
240
+ const tableRegex = /(<table)([\s\S]*?)(<\/table>)/gi;
241
+ // 替换表格区域中的 borderWidth: **px;
242
+ html = html.replace(tableRegex, (match, startTag, content, endTag) => {
243
+ // 只对表格内容进行替换
244
+ const updatedContent = content.replace(/border-width:\s*(\d+)px;/g, 'border-width: $1;');
245
+ return `${startTag}${updatedContent}${endTag}`;
246
+ });
247
+
248
+ // 标记已存表格
249
+ const tempDiv = document.createElement('div'); // 创建临时容器解析 HTML
250
+ tempDiv.innerHTML = html;
251
+ const tables = tempDiv.querySelectorAll('table');
252
+ tables.forEach(table => {
253
+ table.setAttribute('data-processed', 'true');
254
+ });
255
+ // 更新处理后的 HTML
256
+ const updatedHtml = tempDiv.innerHTML;
257
+ this.editor.setHtml(updatedHtml);
258
+ // this.editor.setHtml(html);
131
259
  } else {
132
260
  this.editor.insertText(newVal)
133
261
  }
134
262
  }
135
263
  }
136
264
  },
265
+
266
+ readOnly: {
267
+ handler(newVal){
268
+ this.editorConfig.readOnly = newVal
269
+ if(newVal){
270
+ this.toolbarConfig.toolbarKeys = ['fullScreen']
271
+ }
272
+ },
273
+ immediate: true,
274
+ },
275
+
276
+ hideMenu: {
277
+ handler(newVal){
278
+ this.toolbarConfig.excludeKeys = newVal
279
+ },
280
+ immediate: true,
281
+ deep: true
282
+ }
137
283
  },
138
284
  computed: {
139
- // editorId() {
140
- // return `editor${this._uid}`
141
- // }
285
+ editorStyle() {
286
+ let finalHeight = this.height;
287
+ // 如果是数字,自动拼接 px 单位
288
+ if (typeof finalHeight === 'number') {
289
+ finalHeight = `${finalHeight}px`;
290
+ }
291
+
292
+ return {
293
+ height: finalHeight,
294
+ 'overflow-y': 'hidden'
295
+ }
296
+ }
142
297
  },
143
298
  methods: {
144
299
 
145
300
  onCreated(editor) {
146
301
  this.editor = Object.seal(editor) // 【注意】一定要用 Object.seal() 否则会报错
147
- // 如果本地有存储加载本地存储内容
148
302
  let html = this.value
149
303
  if (html) this.editor.setHtml(html)
304
+
150
305
  },
151
306
  onChange(editor) {
152
307
  let self = this;
153
308
  let html = this.editor.getHtml()
154
- if (html.includes('table')){
155
- html = html.replace(/<table /g,'<table class="wangeditor-styled-table" style= "border-collapse: collapse;"'); // 添加边框)
156
- }
157
- console.log(html)
309
+ // 创建一个临时的 div 容器来解析 HTML
310
+ const tempDiv = document.createElement('div');
311
+ tempDiv.innerHTML = html;
312
+
313
+ // 处理 table 元素 -- 查找新增的表格并添加默认样式
314
+ const tables = tempDiv.querySelectorAll('table:not([data-processed])');
315
+ tables.forEach(table => {
316
+ const currentStyle = table.getAttribute('style') || '';
317
+ const newStyle = `border-collapse: collapse;border-style: solid; border-color: rgb(204, 204, 204);border-width: 1;${currentStyle}`.trim();
318
+ table.setAttribute('class', 'wangeditor-styled-table');
319
+ table.setAttribute('style', newStyle);
320
+
321
+ // 处理表头
322
+ const thCells = table.querySelectorAll('th');
323
+ thCells.forEach(cell => {
324
+ const cellCurrentStyle = cell.getAttribute('style') || '';
325
+ const cellNewStyle = `background-color: rgb(245, 242, 240);border-style: solid; border-color: rgb(204, 204, 204);border-width: 1;padding: 3px 5px;${cellCurrentStyle}`.trim();
326
+ cell.setAttribute('style', cellNewStyle);
327
+ });
328
+
329
+ // 处理单元格
330
+ const tdCells = table.querySelectorAll('td');
331
+ tdCells.forEach(cell => {
332
+ const cellCurrentStyle = cell.getAttribute('style') || '';
333
+ const cellNewStyle = `border-style: solid; border-color: rgb(204, 204, 204);border-width: 1;padding: 3px 5px;${cellCurrentStyle}`.trim();
334
+ cell.setAttribute('style', cellNewStyle);
335
+ });
336
+
337
+ // 标记已处理
338
+ table.setAttribute('data-processed', 'true');
339
+ });
340
+
341
+ // 处理 hr 元素
342
+ const hrs = tempDiv.querySelectorAll('hr');
343
+ hrs.forEach(hr => {
344
+ const currentStyle = hr.getAttribute('style') || '';
345
+ const newStyle = `${currentStyle}; border: 1px solid rgb(250, 64, 6);`.trim();
346
+ hr.setAttribute('style', newStyle);
347
+ });
348
+ // 更新处理后的 HTML
349
+ html = tempDiv.innerHTML;
158
350
  this.$emit('input', self.valueType === 'html' ? html : editor.getText())
159
351
  this.$emit('on-change', html, editor.getText())
160
352
  },