@nstc-business/common 0.0.0-stage → 0.1.0
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,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nstc-business/common",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"description": "N20 公共业务组件(含 TinyMCE 富文本等)",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src/components",
|
|
12
|
+
"src/index.js"
|
|
13
|
+
],
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@tinymce/tinymce-vue": "^3.2.8",
|
|
16
|
+
"tinymce": "^5.10.9",
|
|
17
|
+
"tinymce-i18n": "^26.11.2",
|
|
18
|
+
"vue": "^2.6.11"
|
|
19
|
+
},
|
|
20
|
+
"browserslist": [
|
|
21
|
+
"> 1%",
|
|
22
|
+
"last 2 versions",
|
|
23
|
+
"not dead"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="tinymce-editor">
|
|
3
|
+
<editor :value="value" :init="editorInit" :disabled="disabled" @input="handleInput" />
|
|
4
|
+
</div>
|
|
5
|
+
</template>
|
|
6
|
+
|
|
7
|
+
<script>
|
|
8
|
+
/**
|
|
9
|
+
* 方法内容:封装 TinyMCE 5 富文本;v-model 绑定原始 HTML,xmreport.sync 抛出转换后字符串
|
|
10
|
+
* 时间:2026-11-24 14:45:00
|
|
11
|
+
* 人员:AI && 徐红飞
|
|
12
|
+
* 入参:value / xmreport / height / disabled / plugins / toolbar / menubar / tableMode
|
|
13
|
+
* 出参:input、update:xmreport
|
|
14
|
+
*/
|
|
15
|
+
import Editor from '@tinymce/tinymce-vue'
|
|
16
|
+
import tinymce from 'tinymce/tinymce'
|
|
17
|
+
import 'tinymce/themes/silver'
|
|
18
|
+
import 'tinymce/icons/default'
|
|
19
|
+
import 'tinymce/plugins/advlist'
|
|
20
|
+
import 'tinymce/plugins/autolink'
|
|
21
|
+
import 'tinymce/plugins/lists'
|
|
22
|
+
import 'tinymce/plugins/charmap'
|
|
23
|
+
import 'tinymce/plugins/preview'
|
|
24
|
+
import 'tinymce/plugins/anchor'
|
|
25
|
+
import 'tinymce/plugins/searchreplace'
|
|
26
|
+
import 'tinymce/plugins/visualblocks'
|
|
27
|
+
import 'tinymce/plugins/code'
|
|
28
|
+
import 'tinymce/plugins/fullscreen'
|
|
29
|
+
import 'tinymce/plugins/insertdatetime'
|
|
30
|
+
import 'tinymce/plugins/table'
|
|
31
|
+
import 'tinymce/plugins/paste'
|
|
32
|
+
import 'tinymce/plugins/help'
|
|
33
|
+
import 'tinymce/plugins/wordcount'
|
|
34
|
+
import 'tinymce/skins/ui/oxide/skin.min.css'
|
|
35
|
+
import { toXmreportData, toXmreportHtml } from './xmreportHtml'
|
|
36
|
+
import { applyTableSum } from './tableSum'
|
|
37
|
+
|
|
38
|
+
// 自托管:挂到 window,避免 tinymce-vue 走云端 CDN / API Key
|
|
39
|
+
window.tinymce = tinymce
|
|
40
|
+
// 注册中文语言包(不依赖业务工程 public 路径)
|
|
41
|
+
try {
|
|
42
|
+
// eslint-disable-next-line global-require
|
|
43
|
+
require('tinymce-i18n/langs5/zh_CN.js')
|
|
44
|
+
} catch (e) {
|
|
45
|
+
// 语言包缺失时降级为英文,不影响编辑
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 默认不含链接、图片菜单;含表格合计按钮 tableSum
|
|
49
|
+
const DEFAULT_PLUGINS =
|
|
50
|
+
'advlist autolink lists charmap preview anchor searchreplace visualblocks code fullscreen insertdatetime table paste help wordcount'
|
|
51
|
+
const DEFAULT_TOOLBAR =
|
|
52
|
+
'undo redo | formatselect | bold italic underline forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table tableSum | removeformat code fullscreen help'
|
|
53
|
+
|
|
54
|
+
export default {
|
|
55
|
+
name: 'TinymceEditor',
|
|
56
|
+
components: {
|
|
57
|
+
Editor
|
|
58
|
+
},
|
|
59
|
+
props: {
|
|
60
|
+
// v-model:编辑器原始 HTML
|
|
61
|
+
value: {
|
|
62
|
+
type: String,
|
|
63
|
+
default: ''
|
|
64
|
+
},
|
|
65
|
+
// .sync:xmreport 转换后字符串(非 JSON)
|
|
66
|
+
xmreport: {
|
|
67
|
+
type: String,
|
|
68
|
+
default: ''
|
|
69
|
+
},
|
|
70
|
+
height: {
|
|
71
|
+
type: [Number, String],
|
|
72
|
+
default: 400
|
|
73
|
+
},
|
|
74
|
+
disabled: {
|
|
75
|
+
type: Boolean,
|
|
76
|
+
default: false
|
|
77
|
+
},
|
|
78
|
+
plugins: {
|
|
79
|
+
type: String,
|
|
80
|
+
default: DEFAULT_PLUGINS
|
|
81
|
+
},
|
|
82
|
+
toolbar: {
|
|
83
|
+
type: String,
|
|
84
|
+
default: DEFAULT_TOOLBAR
|
|
85
|
+
},
|
|
86
|
+
menubar: {
|
|
87
|
+
type: [Boolean, String],
|
|
88
|
+
default: 'file edit view insert format tools table help'
|
|
89
|
+
},
|
|
90
|
+
// 表格转换模式:html | plain
|
|
91
|
+
tableMode: {
|
|
92
|
+
type: String,
|
|
93
|
+
default: 'html'
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
computed: {
|
|
97
|
+
editorInit() {
|
|
98
|
+
const vm = this
|
|
99
|
+
return {
|
|
100
|
+
height: this.height,
|
|
101
|
+
menubar: this.menubar,
|
|
102
|
+
plugins: this.plugins,
|
|
103
|
+
toolbar: this.toolbar,
|
|
104
|
+
branding: false,
|
|
105
|
+
language: 'zh_CN',
|
|
106
|
+
skin: false,
|
|
107
|
+
content_css: false,
|
|
108
|
+
content_style:
|
|
109
|
+
'body { font-family: Helvetica, Arial, sans-serif; font-size: 14px; } table, td, th { border: 1px solid #000; }',
|
|
110
|
+
table_default_attributes: {
|
|
111
|
+
border: '1'
|
|
112
|
+
},
|
|
113
|
+
table_default_styles: {
|
|
114
|
+
'border-collapse': 'collapse',
|
|
115
|
+
width: '100%'
|
|
116
|
+
},
|
|
117
|
+
convert_urls: false,
|
|
118
|
+
relative_urls: false,
|
|
119
|
+
/**
|
|
120
|
+
* 方法内容:注册「合计」工具栏按钮
|
|
121
|
+
* 时间:2026-11-24 14:55:00
|
|
122
|
+
* 人员:AI && 徐红飞
|
|
123
|
+
* 入参:editor
|
|
124
|
+
* 出参:无
|
|
125
|
+
*/
|
|
126
|
+
setup(editor) {
|
|
127
|
+
editor.ui.registry.addButton('tableSum', {
|
|
128
|
+
text: '合计',
|
|
129
|
+
tooltip: '为当前表格生成/更新合计行',
|
|
130
|
+
onAction() {
|
|
131
|
+
const result = applyTableSum(editor)
|
|
132
|
+
if (!result.ok) {
|
|
133
|
+
// 优先用 element 全局 Message;没有则用 tinymce 通知
|
|
134
|
+
if (vm.$message && typeof vm.$message.warning === 'function') {
|
|
135
|
+
vm.$message.warning(result.msg)
|
|
136
|
+
} else if (editor.notificationManager) {
|
|
137
|
+
editor.notificationManager.open({
|
|
138
|
+
text: result.msg,
|
|
139
|
+
type: 'warning',
|
|
140
|
+
timeout: 2500
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
// DOM 改完后主动同步 v-model / xmreport
|
|
146
|
+
vm.handleInput(editor.getContent())
|
|
147
|
+
if (vm.$message && typeof vm.$message.success === 'function') {
|
|
148
|
+
vm.$message.success(result.msg)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
watch: {
|
|
157
|
+
value: {
|
|
158
|
+
immediate: true,
|
|
159
|
+
handler(val) {
|
|
160
|
+
this.emitXmreport(val)
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
tableMode() {
|
|
164
|
+
this.emitXmreport(this.value)
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
methods: {
|
|
168
|
+
/**
|
|
169
|
+
* 方法内容:编辑器内容变化时同步 v-model 与 xmreport
|
|
170
|
+
* 时间:2026-11-24 14:45:00
|
|
171
|
+
* 人员:AI && 徐红飞
|
|
172
|
+
* 入参:content - 编辑器 HTML
|
|
173
|
+
* 出参:无
|
|
174
|
+
*/
|
|
175
|
+
handleInput(content) {
|
|
176
|
+
const html = content == null ? '' : content
|
|
177
|
+
this.$emit('input', html)
|
|
178
|
+
this.emitXmreport(html)
|
|
179
|
+
},
|
|
180
|
+
/**
|
|
181
|
+
* 方法内容:计算并抛出 xmreport 转换后字符串(update:xmreport)
|
|
182
|
+
* 时间:2026-11-24 14:45:00
|
|
183
|
+
* 人员:AI && 徐红飞
|
|
184
|
+
* 入参:html - 原始 HTML
|
|
185
|
+
* 出参:无
|
|
186
|
+
*/
|
|
187
|
+
emitXmreport(html) {
|
|
188
|
+
const next = toXmreportHtml(html == null ? '' : html, { tableMode: this.tableMode })
|
|
189
|
+
if (next !== this.xmreport) {
|
|
190
|
+
this.$emit('update:xmreport', next)
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
/**
|
|
194
|
+
* 方法内容:产出适合 xmreport 打印的 HTML
|
|
195
|
+
* 时间:2026-11-24 14:45:00
|
|
196
|
+
* 人员:AI && 徐红飞
|
|
197
|
+
* 入参:options
|
|
198
|
+
* 出参:xmreport HTML
|
|
199
|
+
*/
|
|
200
|
+
getPrintHtml(options = {}) {
|
|
201
|
+
return toXmreportHtml(this.value, Object.assign({ tableMode: this.tableMode }, options))
|
|
202
|
+
},
|
|
203
|
+
/**
|
|
204
|
+
* 方法内容:产出 xmreport 打印入参对象
|
|
205
|
+
* 时间:2026-11-24 14:45:00
|
|
206
|
+
* 人员:AI && 徐红飞
|
|
207
|
+
* 入参:fieldName;options
|
|
208
|
+
* 出参:{ fieldName: html }
|
|
209
|
+
*/
|
|
210
|
+
getPrintData(fieldName = 'testText', options = {}) {
|
|
211
|
+
return toXmreportData(this.value, fieldName, Object.assign({ tableMode: this.tableMode }, options))
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
</script>
|
|
216
|
+
|
|
217
|
+
<style scoped>
|
|
218
|
+
.tinymce-editor {
|
|
219
|
+
width: 100%;
|
|
220
|
+
}
|
|
221
|
+
</style>
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TinyMCE 表格「合计」:手动生成/更新合计行
|
|
3
|
+
* 规则(首版):
|
|
4
|
+
* - 光标所在表格生效
|
|
5
|
+
* - 首列写「合计」(或表头像序号/NO 的列)
|
|
6
|
+
* - 可解析为数字的列求和(去千分位、货币符号)
|
|
7
|
+
* - 日期列 / 明显非数字列不求和
|
|
8
|
+
* - 已有合计行则更新,否则追加
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const TOTAL_LABEL = '合计'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 方法内容:判断文本是否像日期
|
|
15
|
+
* 时间:2026-11-24 14:55:00
|
|
16
|
+
* 人员:AI && 徐红飞
|
|
17
|
+
* 入参:text
|
|
18
|
+
* 出参:boolean
|
|
19
|
+
*/
|
|
20
|
+
function looksLikeDate(text) {
|
|
21
|
+
const t = String(text || '').trim()
|
|
22
|
+
if (!t) {
|
|
23
|
+
return false
|
|
24
|
+
}
|
|
25
|
+
return /^\d{4}[-/.年]\d{1,2}([-/.月]\d{1,2})?/.test(t) || /^\d{4}-\d{1,2}-\d{1,2}/.test(t)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 方法内容:解析单元格数字(支持千分位)
|
|
30
|
+
* 时间:2026-11-24 14:55:00
|
|
31
|
+
* 人员:AI && 徐红飞
|
|
32
|
+
* 入参:text
|
|
33
|
+
* 出参:number | null
|
|
34
|
+
*/
|
|
35
|
+
function parseNumber(text) {
|
|
36
|
+
let t = String(text || '')
|
|
37
|
+
.replace(/\s+/g, '')
|
|
38
|
+
.replace(/,/g, '')
|
|
39
|
+
.replace(/,/g, '')
|
|
40
|
+
.replace(/¥|¥|\$|元|万元/g, '')
|
|
41
|
+
if (!t || t === '-' || t === '—') {
|
|
42
|
+
return null
|
|
43
|
+
}
|
|
44
|
+
// 纯数字或小数
|
|
45
|
+
if (!/^-?\d+(\.\d+)?$/.test(t)) {
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
48
|
+
const n = Number(t)
|
|
49
|
+
return Number.isFinite(n) ? n : null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 方法内容:格式化合计数字(保留原小数位倾向)
|
|
54
|
+
* 时间:2026-11-24 14:55:00
|
|
55
|
+
* 人员:AI && 徐红飞
|
|
56
|
+
* 入参:num
|
|
57
|
+
* 出参:展示字符串
|
|
58
|
+
*/
|
|
59
|
+
function formatNumber(num) {
|
|
60
|
+
if (num == null || !Number.isFinite(num)) {
|
|
61
|
+
return ''
|
|
62
|
+
}
|
|
63
|
+
// 整数不带小数;有小数最多保留 4 位去尾零
|
|
64
|
+
if (Math.abs(num - Math.round(num)) < 1e-9) {
|
|
65
|
+
return String(Math.round(num))
|
|
66
|
+
}
|
|
67
|
+
return String(parseFloat(num.toFixed(4)))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 方法内容:取单元格纯文本
|
|
72
|
+
*/
|
|
73
|
+
function cellText(cell) {
|
|
74
|
+
return (cell && (cell.textContent || '')).replace(/\s+/g, ' ').trim()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 方法内容:判断是否合计行
|
|
79
|
+
*/
|
|
80
|
+
function isTotalRow(row) {
|
|
81
|
+
const cells = row ? Array.from(row.querySelectorAll('th,td')) : []
|
|
82
|
+
if (!cells.length) {
|
|
83
|
+
return false
|
|
84
|
+
}
|
|
85
|
+
return cells.some(c => cellText(c) === TOTAL_LABEL)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 方法内容:为当前选中表格生成或更新合计行
|
|
90
|
+
* 时间:2026-11-24 14:55:00
|
|
91
|
+
* 人员:AI && 徐红飞
|
|
92
|
+
* 入参:editor - TinyMCE 实例
|
|
93
|
+
* 出参:{ ok, msg }
|
|
94
|
+
*/
|
|
95
|
+
export function applyTableSum(editor) {
|
|
96
|
+
if (!editor) {
|
|
97
|
+
return { ok: false, msg: '编辑器未就绪' }
|
|
98
|
+
}
|
|
99
|
+
const table = editor.dom.getParent(editor.selection.getNode(), 'table')
|
|
100
|
+
if (!table) {
|
|
101
|
+
return { ok: false, msg: '请先将光标放在表格内' }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const allRows = Array.from(table.querySelectorAll('tr'))
|
|
105
|
+
if (allRows.length < 1) {
|
|
106
|
+
return { ok: false, msg: '表格无可用行' }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 去掉旧合计行后再计算
|
|
110
|
+
const dataRows = allRows.filter(r => !isTotalRow(r))
|
|
111
|
+
if (!dataRows.length) {
|
|
112
|
+
return { ok: false, msg: '没有可合计的数据行' }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 以列数最多的行作为列基准
|
|
116
|
+
let colCount = 0
|
|
117
|
+
dataRows.forEach(r => {
|
|
118
|
+
const n = r.querySelectorAll('th,td').length
|
|
119
|
+
if (n > colCount) {
|
|
120
|
+
colCount = n
|
|
121
|
+
}
|
|
122
|
+
})
|
|
123
|
+
if (colCount < 1) {
|
|
124
|
+
return { ok: false, msg: '表格无可用列' }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 首行若含 th 或表头语义,视为表头,不参与求和
|
|
128
|
+
const first = dataRows[0]
|
|
129
|
+
const firstCells = Array.from(first.querySelectorAll('th,td'))
|
|
130
|
+
const headerLike =
|
|
131
|
+
first.querySelector('th') ||
|
|
132
|
+
firstCells.some(c => /授信|额度|日期|序号|NO|编号|名称|单位/.test(cellText(c)))
|
|
133
|
+
const bodyRows = headerLike && dataRows.length > 1 ? dataRows.slice(1) : dataRows
|
|
134
|
+
if (!bodyRows.length) {
|
|
135
|
+
return { ok: false, msg: '没有可合计的数据行' }
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const sums = []
|
|
139
|
+
const summable = []
|
|
140
|
+
for (let c = 0; c < colCount; c++) {
|
|
141
|
+
let numericCount = 0
|
|
142
|
+
let dateCount = 0
|
|
143
|
+
let emptyCount = 0
|
|
144
|
+
let sum = 0
|
|
145
|
+
bodyRows.forEach(row => {
|
|
146
|
+
const cells = Array.from(row.querySelectorAll('th,td'))
|
|
147
|
+
const text = cellText(cells[c])
|
|
148
|
+
if (!text) {
|
|
149
|
+
emptyCount += 1
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
if (looksLikeDate(text)) {
|
|
153
|
+
dateCount += 1
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
const n = parseNumber(text)
|
|
157
|
+
if (n != null) {
|
|
158
|
+
numericCount += 1
|
|
159
|
+
sum += n
|
|
160
|
+
}
|
|
161
|
+
})
|
|
162
|
+
const samples = bodyRows.length - emptyCount
|
|
163
|
+
const canSum = samples > 0 && dateCount === 0 && numericCount >= Math.ceil(samples * 0.6)
|
|
164
|
+
summable[c] = canSum
|
|
165
|
+
sums[c] = canSum ? sum : null
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 合计标签列:优先第 0 列;若第 0 列也可求和则仍放第 0 列写「合计」
|
|
169
|
+
const labelCol = 0
|
|
170
|
+
|
|
171
|
+
editor.undoManager.transact(() => {
|
|
172
|
+
// 删除已有合计行
|
|
173
|
+
Array.from(table.querySelectorAll('tr')).forEach(row => {
|
|
174
|
+
if (isTotalRow(row)) {
|
|
175
|
+
editor.dom.remove(row)
|
|
176
|
+
}
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
const tr = editor.dom.create('tr')
|
|
180
|
+
for (let c = 0; c < colCount; c++) {
|
|
181
|
+
const td = editor.dom.create('td', {
|
|
182
|
+
style: 'text-align: center; border: 1px solid #000;'
|
|
183
|
+
})
|
|
184
|
+
if (c === labelCol) {
|
|
185
|
+
td.innerHTML = TOTAL_LABEL
|
|
186
|
+
} else if (summable[c] && sums[c] != null) {
|
|
187
|
+
td.innerHTML = formatNumber(sums[c])
|
|
188
|
+
} else {
|
|
189
|
+
td.innerHTML = ''
|
|
190
|
+
}
|
|
191
|
+
tr.appendChild(td)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const tbody = table.querySelector('tbody') || table
|
|
195
|
+
tbody.appendChild(tr)
|
|
196
|
+
|
|
197
|
+
editor.nodeChanged()
|
|
198
|
+
editor.fire('change')
|
|
199
|
+
// 触发 tinymce-vue v-model
|
|
200
|
+
editor.fire('input')
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
return { ok: true, msg: '已生成合计行' }
|
|
204
|
+
}
|
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TinyMCE HTML → xmreport 富文本格式
|
|
3
|
+
*
|
|
4
|
+
* xmreport 示例:
|
|
5
|
+
* <p style="display:block; ">
|
|
6
|
+
* <span style="color:rgba(244,116,11,1); highlight:rgba(73,123,182,1); bold:true; italic:true; delete-line:true; under-line:true; over-line:true; ">Text</span>
|
|
7
|
+
* </p>
|
|
8
|
+
*
|
|
9
|
+
* 标准标签/CSS 需转成上述自定义 style 属性,色值用 rgba,避免 #hex。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const NAMED_COLORS = {
|
|
13
|
+
black: [0, 0, 0, 1],
|
|
14
|
+
white: [255, 255, 255, 1],
|
|
15
|
+
red: [255, 0, 0, 1],
|
|
16
|
+
green: [0, 128, 0, 1],
|
|
17
|
+
blue: [0, 0, 255, 1],
|
|
18
|
+
yellow: [255, 255, 0, 1],
|
|
19
|
+
orange: [255, 165, 0, 1],
|
|
20
|
+
gray: [128, 128, 128, 1],
|
|
21
|
+
grey: [128, 128, 128, 1],
|
|
22
|
+
silver: [192, 192, 192, 1],
|
|
23
|
+
maroon: [128, 0, 0, 1],
|
|
24
|
+
purple: [128, 0, 128, 1],
|
|
25
|
+
fuchsia: [255, 0, 255, 1],
|
|
26
|
+
lime: [0, 255, 0, 1],
|
|
27
|
+
olive: [128, 128, 0, 1],
|
|
28
|
+
navy: [0, 0, 128, 1],
|
|
29
|
+
teal: [0, 128, 128, 1],
|
|
30
|
+
aqua: [0, 255, 255, 1],
|
|
31
|
+
cyan: [0, 255, 255, 1],
|
|
32
|
+
magenta: [255, 0, 255, 1]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const BLOCK_TAGS = {
|
|
36
|
+
P: true,
|
|
37
|
+
DIV: true,
|
|
38
|
+
H1: true,
|
|
39
|
+
H2: true,
|
|
40
|
+
H3: true,
|
|
41
|
+
H4: true,
|
|
42
|
+
H5: true,
|
|
43
|
+
H6: true,
|
|
44
|
+
LI: true,
|
|
45
|
+
BLOCKQUOTE: true,
|
|
46
|
+
SECTION: true,
|
|
47
|
+
ARTICLE: true
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 方法内容:颜色值转 rgba(r,g,b,a) 字符串
|
|
52
|
+
* 时间:2026-11-24 13:55:00
|
|
53
|
+
* 人员:AI && 徐红飞
|
|
54
|
+
* 入参:color - hex/rgb/rgba/英文色名
|
|
55
|
+
* 出参:rgba 字符串;无法解析时返回空
|
|
56
|
+
*/
|
|
57
|
+
function toRgba(color) {
|
|
58
|
+
if (!color) {
|
|
59
|
+
return ''
|
|
60
|
+
}
|
|
61
|
+
const raw = String(color).trim().toLowerCase()
|
|
62
|
+
if (raw.startsWith('rgba(')) {
|
|
63
|
+
return raw.replace(/\s+/g, '')
|
|
64
|
+
}
|
|
65
|
+
if (raw.startsWith('rgb(')) {
|
|
66
|
+
const m = raw.match(/rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/)
|
|
67
|
+
if (m) {
|
|
68
|
+
return `rgba(${m[1]},${m[2]},${m[3]},1)`
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (raw.startsWith('#')) {
|
|
72
|
+
let hex = raw.slice(1)
|
|
73
|
+
if (hex.length === 3) {
|
|
74
|
+
hex = hex
|
|
75
|
+
.split('')
|
|
76
|
+
.map(c => c + c)
|
|
77
|
+
.join('')
|
|
78
|
+
}
|
|
79
|
+
if (hex.length === 6 && /^[0-9a-f]+$/i.test(hex)) {
|
|
80
|
+
const r = parseInt(hex.slice(0, 2), 16)
|
|
81
|
+
const g = parseInt(hex.slice(2, 4), 16)
|
|
82
|
+
const b = parseInt(hex.slice(4, 6), 16)
|
|
83
|
+
return `rgba(${r},${g},${b},1)`
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (NAMED_COLORS[raw]) {
|
|
87
|
+
const [r, g, b, a] = NAMED_COLORS[raw]
|
|
88
|
+
return `rgba(${r},${g},${b},${a})`
|
|
89
|
+
}
|
|
90
|
+
return ''
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 方法内容:解析 style 属性为键值对象
|
|
95
|
+
* 时间:2026-11-24 13:55:00
|
|
96
|
+
* 人员:AI && 徐红飞
|
|
97
|
+
* 入参:styleText
|
|
98
|
+
* 出参:{ key: value }
|
|
99
|
+
*/
|
|
100
|
+
function parseStyle(styleText) {
|
|
101
|
+
const map = {}
|
|
102
|
+
if (!styleText) {
|
|
103
|
+
return map
|
|
104
|
+
}
|
|
105
|
+
String(styleText)
|
|
106
|
+
.split(';')
|
|
107
|
+
.forEach(part => {
|
|
108
|
+
const idx = part.indexOf(':')
|
|
109
|
+
if (idx < 0) {
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
const key = part.slice(0, idx).trim().toLowerCase()
|
|
113
|
+
const val = part.slice(idx + 1).trim()
|
|
114
|
+
if (key) {
|
|
115
|
+
map[key] = val
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
return map
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 方法内容:根据标签名与 CSS 合并出 xmreport 文本样式
|
|
123
|
+
* 时间:2026-11-24 13:55:00
|
|
124
|
+
* 人员:AI && 徐红飞
|
|
125
|
+
* 入参:tagName、cssMap、baseStyles
|
|
126
|
+
* 出参:合并后的样式对象
|
|
127
|
+
*/
|
|
128
|
+
function mergeTextStyles(tagName, cssMap, baseStyles) {
|
|
129
|
+
const styles = Object.assign({}, baseStyles || {})
|
|
130
|
+
const tag = (tagName || '').toUpperCase()
|
|
131
|
+
|
|
132
|
+
if (tag === 'STRONG' || tag === 'B') {
|
|
133
|
+
styles.bold = true
|
|
134
|
+
}
|
|
135
|
+
if (tag === 'EM' || tag === 'I') {
|
|
136
|
+
styles.italic = true
|
|
137
|
+
}
|
|
138
|
+
if (tag === 'U') {
|
|
139
|
+
styles['under-line'] = true
|
|
140
|
+
}
|
|
141
|
+
if (tag === 'S' || tag === 'STRIKE' || tag === 'DEL') {
|
|
142
|
+
styles['delete-line'] = true
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const fontWeight = cssMap['font-weight']
|
|
146
|
+
if (fontWeight && (fontWeight === 'bold' || parseInt(fontWeight, 10) >= 600)) {
|
|
147
|
+
styles.bold = true
|
|
148
|
+
}
|
|
149
|
+
if (cssMap['font-style'] === 'italic' || cssMap['font-style'] === 'oblique') {
|
|
150
|
+
styles.italic = true
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const deco = `${cssMap['text-decoration'] || ''} ${cssMap['text-decoration-line'] || ''}`.toLowerCase()
|
|
154
|
+
if (deco.indexOf('underline') >= 0) {
|
|
155
|
+
styles['under-line'] = true
|
|
156
|
+
}
|
|
157
|
+
if (deco.indexOf('line-through') >= 0) {
|
|
158
|
+
styles['delete-line'] = true
|
|
159
|
+
}
|
|
160
|
+
if (deco.indexOf('overline') >= 0) {
|
|
161
|
+
styles['over-line'] = true
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const color = toRgba(cssMap.color)
|
|
165
|
+
if (color) {
|
|
166
|
+
styles.color = color
|
|
167
|
+
}
|
|
168
|
+
const highlight = toRgba(cssMap['background-color'] || cssMap.background || cssMap.highlight)
|
|
169
|
+
if (highlight) {
|
|
170
|
+
styles.highlight = highlight
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// 已是 xmreport 自定义属性时透传
|
|
174
|
+
;['bold', 'italic', 'under-line', 'delete-line', 'over-line'].forEach(k => {
|
|
175
|
+
if (cssMap[k] === 'true' || cssMap[k] === true) {
|
|
176
|
+
styles[k] = true
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
if (cssMap.color && String(cssMap.color).indexOf('rgba') === 0) {
|
|
180
|
+
styles.color = cssMap.color.replace(/\s+/g, '')
|
|
181
|
+
}
|
|
182
|
+
if (cssMap.highlight) {
|
|
183
|
+
const h = toRgba(cssMap.highlight) || String(cssMap.highlight).replace(/\s+/g, '')
|
|
184
|
+
if (h) {
|
|
185
|
+
styles.highlight = h
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// text-align:继承或当前节点(表格对齐落在 span)
|
|
190
|
+
const allowAlign = { left: 1, center: 1, right: 1, justify: 1 }
|
|
191
|
+
const cssAlign = (cssMap['text-align'] || '').toLowerCase()
|
|
192
|
+
if (allowAlign[cssAlign]) {
|
|
193
|
+
styles['text-align'] = cssAlign
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return styles
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 方法内容:把样式对象序列化为 xmreport span style
|
|
201
|
+
* 时间:2026-11-24 13:55:00
|
|
202
|
+
* 人员:AI && 徐红飞
|
|
203
|
+
* 入参:styles
|
|
204
|
+
* 出参:style 字符串(末尾带空格,贴近官方示例)
|
|
205
|
+
*/
|
|
206
|
+
function buildSpanStyle(styles) {
|
|
207
|
+
if (!styles) {
|
|
208
|
+
return ''
|
|
209
|
+
}
|
|
210
|
+
const parts = []
|
|
211
|
+
// 表格单元格对齐写在 span 上(不要写在 td)
|
|
212
|
+
if (styles['text-align']) {
|
|
213
|
+
parts.push(`text-align:${styles['text-align']}`)
|
|
214
|
+
}
|
|
215
|
+
if (styles.color) {
|
|
216
|
+
parts.push(`color:${styles.color}`)
|
|
217
|
+
}
|
|
218
|
+
if (styles.highlight) {
|
|
219
|
+
parts.push(`highlight:${styles.highlight}`)
|
|
220
|
+
}
|
|
221
|
+
if (styles.bold) {
|
|
222
|
+
parts.push('bold:true')
|
|
223
|
+
}
|
|
224
|
+
if (styles.italic) {
|
|
225
|
+
parts.push('italic:true')
|
|
226
|
+
}
|
|
227
|
+
if (styles['delete-line']) {
|
|
228
|
+
parts.push('delete-line:true')
|
|
229
|
+
}
|
|
230
|
+
if (styles['under-line']) {
|
|
231
|
+
parts.push('under-line:true')
|
|
232
|
+
}
|
|
233
|
+
if (styles['over-line']) {
|
|
234
|
+
parts.push('over-line:true')
|
|
235
|
+
}
|
|
236
|
+
if (!parts.length) {
|
|
237
|
+
return ''
|
|
238
|
+
}
|
|
239
|
+
return `${parts.join('; ')}; `
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 方法内容:样式是否为空
|
|
244
|
+
*/
|
|
245
|
+
function hasTextStyles(styles) {
|
|
246
|
+
return !!(
|
|
247
|
+
styles &&
|
|
248
|
+
(styles['text-align'] ||
|
|
249
|
+
styles.color ||
|
|
250
|
+
styles.highlight ||
|
|
251
|
+
styles.bold ||
|
|
252
|
+
styles.italic ||
|
|
253
|
+
styles['delete-line'] ||
|
|
254
|
+
styles['under-line'] ||
|
|
255
|
+
styles['over-line'])
|
|
256
|
+
)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 方法内容:转义文本节点
|
|
261
|
+
*/
|
|
262
|
+
function escapeText(text) {
|
|
263
|
+
return String(text)
|
|
264
|
+
.replace(/&/g, '&')
|
|
265
|
+
.replace(/</g, '<')
|
|
266
|
+
.replace(/>/g, '>')
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* 方法内容:从块级节点提取对齐信息(text-align / text-vertical-align)
|
|
271
|
+
* 时间:2026-11-24 14:00:00
|
|
272
|
+
* 人员:AI && 徐红飞
|
|
273
|
+
* 入参:el - 块级 DOM 元素
|
|
274
|
+
* 出参:{ textAlign, textVerticalAlign }
|
|
275
|
+
*/
|
|
276
|
+
function extractBlockAlign(el) {
|
|
277
|
+
const cssMap = parseStyle(el && el.getAttribute ? el.getAttribute('style') : '')
|
|
278
|
+
let textAlign = (cssMap['text-align'] || '').toLowerCase()
|
|
279
|
+
// TinyMCE 偶发用 align 属性
|
|
280
|
+
if (!textAlign && el && el.getAttribute) {
|
|
281
|
+
textAlign = (el.getAttribute('align') || '').toLowerCase()
|
|
282
|
+
}
|
|
283
|
+
// 兼容 class:ql/mce 常见命名
|
|
284
|
+
const className = (el && el.className ? String(el.className) : '').toLowerCase()
|
|
285
|
+
if (!textAlign) {
|
|
286
|
+
if (className.indexOf('align-left') >= 0 || className.indexOf('aleft') >= 0) {
|
|
287
|
+
textAlign = 'left'
|
|
288
|
+
} else if (className.indexOf('align-center') >= 0 || className.indexOf('acenter') >= 0) {
|
|
289
|
+
textAlign = 'center'
|
|
290
|
+
} else if (className.indexOf('align-right') >= 0 || className.indexOf('aright') >= 0) {
|
|
291
|
+
textAlign = 'right'
|
|
292
|
+
} else if (className.indexOf('align-justify') >= 0 || className.indexOf('ajustify') >= 0) {
|
|
293
|
+
textAlign = 'justify'
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const allow = { left: 1, center: 1, right: 1, justify: 1 }
|
|
297
|
+
if (!allow[textAlign]) {
|
|
298
|
+
textAlign = ''
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
let textVerticalAlign = (
|
|
302
|
+
cssMap['text-vertical-align'] ||
|
|
303
|
+
cssMap['vertical-align'] ||
|
|
304
|
+
''
|
|
305
|
+
).toLowerCase()
|
|
306
|
+
const vAllow = { top: 1, middle: 1, bottom: 1, baseline: 1 }
|
|
307
|
+
if (!vAllow[textVerticalAlign]) {
|
|
308
|
+
textVerticalAlign = ''
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return { textAlign, textVerticalAlign }
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* 方法内容:把内联结果规范成至少包一层 span(xmreport 示例要求)
|
|
316
|
+
* 时间:2026-11-24 14:00:00
|
|
317
|
+
* 人员:AI && 徐红飞
|
|
318
|
+
* 入参:innerHtml
|
|
319
|
+
* 出参:带 span 的内联 HTML
|
|
320
|
+
*/
|
|
321
|
+
function ensureSpanWrapped(innerHtml) {
|
|
322
|
+
const raw = innerHtml == null ? '' : String(innerHtml)
|
|
323
|
+
const trimmed = raw.trim()
|
|
324
|
+
if (!trimmed) {
|
|
325
|
+
return '<span></span>'
|
|
326
|
+
}
|
|
327
|
+
// 已是单一/多个 span 或含 br 的组合时,若纯文本未包 span 则包一层
|
|
328
|
+
if (/^<span[\s>]/i.test(trimmed) || trimmed.indexOf('<span') >= 0 || trimmed.indexOf('<br') >= 0) {
|
|
329
|
+
// 纯文本片段与 span 混排时不再强行外包,避免破坏已有结构
|
|
330
|
+
if (trimmed.indexOf('<span') >= 0 || trimmed.indexOf('<br') >= 0) {
|
|
331
|
+
return raw
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// 转义后的纯文本
|
|
335
|
+
if (trimmed.indexOf('<') < 0) {
|
|
336
|
+
return `<span>${trimmed}</span>`
|
|
337
|
+
}
|
|
338
|
+
return raw
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* 方法内容:递归把节点转成 xmreport 内联 HTML
|
|
343
|
+
* 时间:2026-11-24 13:55:00
|
|
344
|
+
* 人员:AI && 徐红飞
|
|
345
|
+
* 入参:node、inheritedStyles、doc
|
|
346
|
+
* 出参:HTML 字符串
|
|
347
|
+
*/
|
|
348
|
+
function convertInline(node, inheritedStyles, doc) {
|
|
349
|
+
if (!node) {
|
|
350
|
+
return ''
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
354
|
+
const text = node.nodeValue
|
|
355
|
+
if (text == null || text === '') {
|
|
356
|
+
return ''
|
|
357
|
+
}
|
|
358
|
+
// 空白文本:有样式时保留,无样式则忽略(避免多余空 span)
|
|
359
|
+
if (text.replace(/\s+/g, '') === '' && !hasTextStyles(inheritedStyles)) {
|
|
360
|
+
return ''
|
|
361
|
+
}
|
|
362
|
+
const escaped = escapeText(text)
|
|
363
|
+
// xmreport 示例:即使无样式也用 <span>text</span>
|
|
364
|
+
const style = buildSpanStyle(inheritedStyles)
|
|
365
|
+
if (style) {
|
|
366
|
+
return `<span style="${style}">${escaped}</span>`
|
|
367
|
+
}
|
|
368
|
+
return `<span>${escaped}</span>`
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
372
|
+
return ''
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const el = node
|
|
376
|
+
const tag = el.tagName.toUpperCase()
|
|
377
|
+
|
|
378
|
+
if (tag === 'BR') {
|
|
379
|
+
return '<br/>'
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// 表格在外层单独处理,内联阶段跳过
|
|
383
|
+
if (tag === 'TABLE') {
|
|
384
|
+
return ''
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const cssMap = parseStyle(el.getAttribute('style'))
|
|
388
|
+
const nextStyles = mergeTextStyles(tag, cssMap, inheritedStyles)
|
|
389
|
+
|
|
390
|
+
// 块级在 convertBlocks 处理;这里若嵌套块,继续下钻
|
|
391
|
+
if (BLOCK_TAGS[tag]) {
|
|
392
|
+
return convertChildrenInline(el, nextStyles, doc)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (tag === 'A') {
|
|
396
|
+
// 链接只保留文本样式
|
|
397
|
+
return convertChildrenInline(el, nextStyles, doc)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (tag === 'SPAN' || tag === 'FONT' || tag === 'STRONG' || tag === 'B' || tag === 'EM' || tag === 'I' || tag === 'U' || tag === 'S' || tag === 'STRIKE' || tag === 'DEL' || tag === 'SUB' || tag === 'SUP') {
|
|
401
|
+
return convertChildrenInline(el, nextStyles, doc)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return convertChildrenInline(el, nextStyles, doc)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function convertChildrenInline(el, styles, doc) {
|
|
408
|
+
let html = ''
|
|
409
|
+
Array.from(el.childNodes).forEach(child => {
|
|
410
|
+
html += convertInline(child, styles, doc)
|
|
411
|
+
})
|
|
412
|
+
return html
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* 方法内容:生成 xmreport 段落(含对齐)
|
|
417
|
+
* 时间:2026-11-24 14:00:00
|
|
418
|
+
* 人员:AI && 徐红飞
|
|
419
|
+
* 入参:innerHtml;align - { textAlign, textVerticalAlign }
|
|
420
|
+
* 出参:p 段落 HTML
|
|
421
|
+
*
|
|
422
|
+
* 对齐示例:
|
|
423
|
+
* text-align:left|center|right|justify; display:block;
|
|
424
|
+
* text-align:center; display:block; text-vertical-align:top;
|
|
425
|
+
*/
|
|
426
|
+
function wrapBlock(innerHtml, align = {}) {
|
|
427
|
+
const content = ensureSpanWrapped(innerHtml)
|
|
428
|
+
const parts = []
|
|
429
|
+
// 编辑器默认居左;xmreport 未写 text-align 时会居中,故缺省显式补 left
|
|
430
|
+
const textAlign = align.textAlign || 'left'
|
|
431
|
+
parts.push(`text-align:${textAlign}`)
|
|
432
|
+
parts.push('display:block')
|
|
433
|
+
if (align.textVerticalAlign) {
|
|
434
|
+
parts.push(`text-vertical-align:${align.textVerticalAlign}`)
|
|
435
|
+
}
|
|
436
|
+
// 末尾保留空格,贴近官方示例 "display:block; "
|
|
437
|
+
const style = `${parts.join('; ')}; `
|
|
438
|
+
return `<p style="${style}">${content}</p>`
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* 方法内容:把 table 转成若干段落(每行一个),单元格样式走 span 规则
|
|
443
|
+
* 时间:2026-11-24 13:55:00
|
|
444
|
+
* 人员:AI && 徐红飞
|
|
445
|
+
* 入参:table、doc、tableMode
|
|
446
|
+
* 出参:HTML 字符串
|
|
447
|
+
*/
|
|
448
|
+
function convertTable(table, doc, tableMode) {
|
|
449
|
+
const rows = Array.from(table.querySelectorAll('tr'))
|
|
450
|
+
if (!rows.length) {
|
|
451
|
+
return ''
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
if (tableMode === 'plain') {
|
|
455
|
+
const lines = rows.map(tr => {
|
|
456
|
+
const cells = Array.from(tr.querySelectorAll('th,td')).map(cell =>
|
|
457
|
+
(cell.textContent || '').replace(/\s+/g, ' ').trim()
|
|
458
|
+
)
|
|
459
|
+
return escapeText(cells.join(' | '))
|
|
460
|
+
})
|
|
461
|
+
return wrapBlock(lines.join('<br/>'), {})
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// 保留表格骨架:对齐写在 span 上,td 只保留边框
|
|
465
|
+
const bodyParts = []
|
|
466
|
+
rows.forEach(tr => {
|
|
467
|
+
const tds = Array.from(tr.querySelectorAll('th,td'))
|
|
468
|
+
.map(cell => {
|
|
469
|
+
const align = extractBlockAlign(cell)
|
|
470
|
+
const textAlign = align.textAlign || 'left'
|
|
471
|
+
// 把单元格 text-align 传入内联样式,落到 span,不写到 td
|
|
472
|
+
let inner = convertChildrenInline(cell, { 'text-align': textAlign }, doc)
|
|
473
|
+
if (!inner || !String(inner).trim()) {
|
|
474
|
+
inner = `<span style="text-align:${textAlign}; "></span>`
|
|
475
|
+
} else {
|
|
476
|
+
inner = ensureSpanWrapped(inner)
|
|
477
|
+
}
|
|
478
|
+
return `<td style="border:1px solid black;">${inner}</td>`
|
|
479
|
+
})
|
|
480
|
+
.join('')
|
|
481
|
+
bodyParts.push(`<tr>${tds}</tr>`)
|
|
482
|
+
})
|
|
483
|
+
return `<table border="1" cellspacing="0" cellpadding="4" style="width:100%;border:1px solid black;"><tbody>${bodyParts.join(
|
|
484
|
+
''
|
|
485
|
+
)}</tbody></table>`
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* 方法内容:按块级结构把根节点转成 xmreport HTML
|
|
490
|
+
* 时间:2026-11-24 13:55:00
|
|
491
|
+
* 人员:AI && 徐红飞
|
|
492
|
+
* 入参:root、doc、tableMode
|
|
493
|
+
* 出参:转换后 HTML
|
|
494
|
+
*/
|
|
495
|
+
function convertRoot(root, doc, tableMode) {
|
|
496
|
+
const parts = []
|
|
497
|
+
let pendingInline = ''
|
|
498
|
+
|
|
499
|
+
const flushInline = () => {
|
|
500
|
+
if (!pendingInline || !String(pendingInline).replace(/\s| /g, '')) {
|
|
501
|
+
// 只有空白则不单独成段,避免多余空行
|
|
502
|
+
if (pendingInline && pendingInline.indexOf('<br') >= 0) {
|
|
503
|
+
parts.push(wrapBlock(pendingInline, {}))
|
|
504
|
+
}
|
|
505
|
+
pendingInline = ''
|
|
506
|
+
return
|
|
507
|
+
}
|
|
508
|
+
parts.push(wrapBlock(pendingInline, {}))
|
|
509
|
+
pendingInline = ''
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
Array.from(root.childNodes).forEach(node => {
|
|
513
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
514
|
+
const t = node.nodeValue || ''
|
|
515
|
+
if (t.replace(/\s+/g, '') === '') {
|
|
516
|
+
return
|
|
517
|
+
}
|
|
518
|
+
pendingInline += convertInline(node, {}, doc)
|
|
519
|
+
return
|
|
520
|
+
}
|
|
521
|
+
if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
522
|
+
return
|
|
523
|
+
}
|
|
524
|
+
const tag = node.tagName.toUpperCase()
|
|
525
|
+
|
|
526
|
+
if (tag === 'TABLE') {
|
|
527
|
+
flushInline()
|
|
528
|
+
parts.push(convertTable(node, doc, tableMode))
|
|
529
|
+
return
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (tag === 'UL' || tag === 'OL') {
|
|
533
|
+
flushInline()
|
|
534
|
+
Array.from(node.children).forEach(li => {
|
|
535
|
+
if (li.tagName && li.tagName.toUpperCase() === 'LI') {
|
|
536
|
+
const cssMap = parseStyle(li.getAttribute('style'))
|
|
537
|
+
const styles = mergeTextStyles('LI', cssMap, {})
|
|
538
|
+
const align = extractBlockAlign(li)
|
|
539
|
+
parts.push(wrapBlock(convertChildrenInline(li, styles, doc), align))
|
|
540
|
+
}
|
|
541
|
+
})
|
|
542
|
+
return
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
if (tag === 'BR') {
|
|
546
|
+
pendingInline += '<br/>'
|
|
547
|
+
return
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
if (BLOCK_TAGS[tag]) {
|
|
551
|
+
flushInline()
|
|
552
|
+
const cssMap = parseStyle(node.getAttribute('style'))
|
|
553
|
+
const styles = mergeTextStyles(tag, cssMap, {})
|
|
554
|
+
const align = extractBlockAlign(node)
|
|
555
|
+
// 块内若还有嵌套 table,先拆开处理
|
|
556
|
+
const hasTable = node.querySelector && node.querySelector('table')
|
|
557
|
+
if (hasTable) {
|
|
558
|
+
Array.from(node.childNodes).forEach(child => {
|
|
559
|
+
if (child.nodeType === Node.ELEMENT_NODE && child.tagName.toUpperCase() === 'TABLE') {
|
|
560
|
+
parts.push(convertTable(child, doc, tableMode))
|
|
561
|
+
} else {
|
|
562
|
+
const piece = convertInline(child, styles, doc)
|
|
563
|
+
if (piece) {
|
|
564
|
+
parts.push(wrapBlock(piece, align))
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
})
|
|
568
|
+
} else {
|
|
569
|
+
parts.push(wrapBlock(convertChildrenInline(node, styles, doc), align))
|
|
570
|
+
}
|
|
571
|
+
return
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// 其他内联/未知标签并入当前段
|
|
575
|
+
pendingInline += convertInline(node, {}, doc)
|
|
576
|
+
})
|
|
577
|
+
|
|
578
|
+
flushInline()
|
|
579
|
+
return parts.join('')
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* 方法内容:将 TinyMCE HTML 转为 xmreport 自定义富文本 HTML
|
|
584
|
+
* 时间:2026-11-24 13:55:00
|
|
585
|
+
* 人员:AI && 徐红飞
|
|
586
|
+
* 入参:html;options.tableMode - html|plain
|
|
587
|
+
* 出参:xmreport 可显示的 HTML
|
|
588
|
+
*/
|
|
589
|
+
export function toXmreportHtml(html, options = {}) {
|
|
590
|
+
if (html == null || String(html).trim() === '') {
|
|
591
|
+
return ''
|
|
592
|
+
}
|
|
593
|
+
if (typeof document === 'undefined') {
|
|
594
|
+
return String(html)
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
const tableMode = options.tableMode === 'plain' ? 'plain' : 'html'
|
|
598
|
+
const parser = new DOMParser()
|
|
599
|
+
const doc = parser.parseFromString(`<div id="xm-root">${html}</div>`, 'text/html')
|
|
600
|
+
const root = doc.getElementById('xm-root')
|
|
601
|
+
if (!root) {
|
|
602
|
+
return String(html)
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
root.querySelectorAll('[data-mce-style]').forEach(el => {
|
|
606
|
+
el.removeAttribute('data-mce-style')
|
|
607
|
+
})
|
|
608
|
+
|
|
609
|
+
return convertRoot(root, doc, tableMode)
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* 方法内容:组装 xmreport 打印入参对象(字段名默认 testText)
|
|
614
|
+
* 时间:2026-11-24 13:40:00
|
|
615
|
+
* 人员:AI && 徐红飞
|
|
616
|
+
* 入参:html;fieldName;options
|
|
617
|
+
* 出参:{ [fieldName]: html }
|
|
618
|
+
*/
|
|
619
|
+
export function toXmreportData(html, fieldName = 'testText', options = {}) {
|
|
620
|
+
const key = fieldName || 'testText'
|
|
621
|
+
return {
|
|
622
|
+
[key]: toXmreportHtml(html, options)
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* 方法内容:组装 xmreport 打印入参 JSON 字符串
|
|
628
|
+
* 时间:2026-11-24 13:40:00
|
|
629
|
+
* 人员:AI && 徐红飞
|
|
630
|
+
* 入参:html;fieldName;options
|
|
631
|
+
* 出参:格式化 JSON
|
|
632
|
+
*/
|
|
633
|
+
export function toXmreportJson(html, fieldName = 'testText', options = {}) {
|
|
634
|
+
return JSON.stringify(toXmreportData(html, fieldName, options), null, 2)
|
|
635
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @nstc-business/common 公共组件入口
|
|
3
|
+
*/
|
|
4
|
+
import TinymceEditor from './components/TinymceEditor/index.vue'
|
|
5
|
+
import { toXmreportData, toXmreportHtml, toXmreportJson } from './components/TinymceEditor/xmreportHtml'
|
|
6
|
+
|
|
7
|
+
const components = [TinymceEditor]
|
|
8
|
+
|
|
9
|
+
const install = function (Vue, options = { prefix: 'N' }) {
|
|
10
|
+
components.forEach(component => {
|
|
11
|
+
let name = component.name
|
|
12
|
+
if (!name) {
|
|
13
|
+
// eslint-disable-next-line no-console
|
|
14
|
+
console.error('必须设置组件名称:', component)
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
name = options.prefix + name.replace(name[0], name[0].toUpperCase())
|
|
18
|
+
Vue.component(name, component)
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/* istanbul ignore if */
|
|
23
|
+
if (typeof window !== 'undefined' && window.Vue) {
|
|
24
|
+
install(window.Vue)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default {
|
|
28
|
+
version: '0.1.0',
|
|
29
|
+
install,
|
|
30
|
+
TinymceEditor,
|
|
31
|
+
toXmreportHtml,
|
|
32
|
+
toXmreportData,
|
|
33
|
+
toXmreportJson
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { TinymceEditor, toXmreportHtml, toXmreportData, toXmreportJson }
|
package/README.md
DELETED