gbformdesign 0.1.0 → 0.1.1
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/LICENSE +21 -21
- package/README.md +84 -84
- package/dist/gbformdesign.css +1 -1
- package/dist/gbformdesign.js +1146 -1051
- package/dist/gbformdesign.js.map +1 -1
- package/dist/gbformdesign.umd.cjs +6 -6
- package/dist/gbformdesign.umd.cjs.map +1 -1
- package/package.json +58 -57
- package/src/core/FormVariable.js +328 -328
- package/src/core/dynamicFormUtils.js +139 -139
- package/src/core/fileIO.js +90 -90
- package/src/core/formVariableTypeSystem.js +327 -327
- package/src/core/index.js +26 -26
- package/src/core/schema.js +192 -192
- package/src/design/composables/useDefaultValue.js +55 -55
- package/src/design/constants/componentTemplates.js +181 -190
- package/src/design/constants/componentTypes.js +70 -70
- package/src/design/utils/componentUtils.js +75 -75
- package/src/designer/fc/FcCanvasField.vue +277 -276
- package/src/designer/fc/FcJsonPanel.vue +113 -113
- package/src/designer/fc/FcPreviewDialog.vue +62 -62
- package/src/designer/fc/FcPropsPanel.vue +373 -373
- package/src/designer/fc/FormDesigner.vue +633 -633
- package/src/designer/fc/codegen.js +120 -120
- package/src/index.js +48 -48
- package/src/styles/config.scss +102 -102
- package/src/styles/fc/designer.css +837 -837
- package/src/styles/fc/icon.css +1053 -1053
- package/src/styles/index.scss +4 -4
- package/src/view/FormField.vue +301 -301
- package/src/view/FormViewer.vue +216 -216
- package/src/view/composables/useFormValidation.js +201 -201
|
@@ -1,139 +1,139 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 动态表单工具
|
|
3
|
-
* 提供表单组件的过滤、转换、验证等功能
|
|
4
|
-
* 支持将普通组件转换为带类型信息的FormVariable对象
|
|
5
|
-
*
|
|
6
|
-
* 统一格式:使用 props.label 和 props.variableName
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { FormVariable } from './FormVariable.js'
|
|
10
|
-
import { computeDataType } from './formVariableTypeSystem.js'
|
|
11
|
-
|
|
12
|
-
// ==================== 组件过滤和提取 ====================
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 过滤有效的表单组件
|
|
16
|
-
* @param {Object} formData - 表单数据对象
|
|
17
|
-
* @returns {Array} 有效的组件列表
|
|
18
|
-
*/
|
|
19
|
-
export function filterValidComponents(formData) {
|
|
20
|
-
if (!formData?.components) return []
|
|
21
|
-
|
|
22
|
-
return formData.components.filter(c =>
|
|
23
|
-
c?.type === 'row' || (c?.props?.variableName && c?.props?.label)
|
|
24
|
-
)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 递归获取所有表单组件(包括嵌套在布局中的)
|
|
29
|
-
* @param {Array} components - 组件配置数组
|
|
30
|
-
* @returns {Array} 展平后的组件数组
|
|
31
|
-
*/
|
|
32
|
-
export function getAllFormComponents(components) {
|
|
33
|
-
if (!Array.isArray(components)) return []
|
|
34
|
-
|
|
35
|
-
return components.reduce((result, component) => {
|
|
36
|
-
if (!component) return result
|
|
37
|
-
|
|
38
|
-
// 处理分栏布局组件(新格式:row)
|
|
39
|
-
if (component.type === 'row' && component.children) {
|
|
40
|
-
component.children.forEach(col => {
|
|
41
|
-
if (Array.isArray(col)) {
|
|
42
|
-
result.push(...getAllFormComponents(col))
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
}
|
|
46
|
-
// 处理普通表单组件(统一格式:使用 props.variableName)
|
|
47
|
-
else if (component.props?.variableName) {
|
|
48
|
-
result.push(component)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return result
|
|
52
|
-
}, [])
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// ==================== FormVariable转换 ====================
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 将组件列表转换为FormVariable数组
|
|
59
|
-
* FormVariable是增强的表单变量对象,包含完整的类型信息,可直接用于条件判断
|
|
60
|
-
* @param {Array} components - 组件配置数组或FormVariable数组
|
|
61
|
-
* @returns {Array<FormVariable>} FormVariable数组
|
|
62
|
-
*/
|
|
63
|
-
export function enrichFormVariables(components) {
|
|
64
|
-
if (!Array.isArray(components)) return []
|
|
65
|
-
|
|
66
|
-
// 避免重复转换
|
|
67
|
-
if (components.length > 0 && components[0] instanceof FormVariable) {
|
|
68
|
-
return components
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const allComponents = getAllFormComponents(components)
|
|
72
|
-
return FormVariable.fromComponents(allComponents)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* 从FormVariable数组中查找指定变量
|
|
77
|
-
* @param {Array<FormVariable>} formVariables - FormVariable数组
|
|
78
|
-
* @param {string} variableName - 变量名
|
|
79
|
-
* @returns {FormVariable|null} 找到的FormVariable或null
|
|
80
|
-
*/
|
|
81
|
-
export function findFormVariable(formVariables, variableName) {
|
|
82
|
-
if (!Array.isArray(formVariables)) return null
|
|
83
|
-
return formVariables.find(v => v.variableName === variableName) || null
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// ==================== 组件验证 ====================
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* 验证表单组件配置的完整性
|
|
90
|
-
* @param {Object} component - 组件配置
|
|
91
|
-
* @returns {{valid: boolean, errors: Array<string>}} 验证结果
|
|
92
|
-
*/
|
|
93
|
-
export function validateComponentConfig(component) {
|
|
94
|
-
const errors = []
|
|
95
|
-
|
|
96
|
-
if (!component) {
|
|
97
|
-
errors.push('组件配置不能为空')
|
|
98
|
-
return { valid: false, errors }
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (!component.type) {
|
|
102
|
-
errors.push('组件类型不能为空')
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (!component.props) {
|
|
106
|
-
errors.push('组件属性不能为空')
|
|
107
|
-
} else {
|
|
108
|
-
if (!component.props.variableName) {
|
|
109
|
-
errors.push('变量名不能为空')
|
|
110
|
-
}
|
|
111
|
-
if (!component.props.label) {
|
|
112
|
-
errors.push('标签不能为空')
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// 选择类组件必须配置选项
|
|
116
|
-
const selectTypes = ['select', 'radio', 'checkbox']
|
|
117
|
-
if (selectTypes.includes(component.type)) {
|
|
118
|
-
const hasOptions = Array.isArray(component.props.options) && component.props.options.length > 0
|
|
119
|
-
if (!hasOptions) {
|
|
120
|
-
errors.push(`${component.type}类型必须配置选项列表`)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return {
|
|
126
|
-
valid: errors.length === 0,
|
|
127
|
-
errors
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* 统计叶子表单字段数量(不包含分栏容器)
|
|
134
|
-
* @param {Array} components
|
|
135
|
-
* @returns {Number}
|
|
136
|
-
*/
|
|
137
|
-
export function countFormComponents(components) {
|
|
138
|
-
return getAllFormComponents(components).length
|
|
139
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 动态表单工具
|
|
3
|
+
* 提供表单组件的过滤、转换、验证等功能
|
|
4
|
+
* 支持将普通组件转换为带类型信息的FormVariable对象
|
|
5
|
+
*
|
|
6
|
+
* 统一格式:使用 props.label 和 props.variableName
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { FormVariable } from './FormVariable.js'
|
|
10
|
+
import { computeDataType } from './formVariableTypeSystem.js'
|
|
11
|
+
|
|
12
|
+
// ==================== 组件过滤和提取 ====================
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 过滤有效的表单组件
|
|
16
|
+
* @param {Object} formData - 表单数据对象
|
|
17
|
+
* @returns {Array} 有效的组件列表
|
|
18
|
+
*/
|
|
19
|
+
export function filterValidComponents(formData) {
|
|
20
|
+
if (!formData?.components) return []
|
|
21
|
+
|
|
22
|
+
return formData.components.filter(c =>
|
|
23
|
+
c?.type === 'row' || (c?.props?.variableName && c?.props?.label)
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 递归获取所有表单组件(包括嵌套在布局中的)
|
|
29
|
+
* @param {Array} components - 组件配置数组
|
|
30
|
+
* @returns {Array} 展平后的组件数组
|
|
31
|
+
*/
|
|
32
|
+
export function getAllFormComponents(components) {
|
|
33
|
+
if (!Array.isArray(components)) return []
|
|
34
|
+
|
|
35
|
+
return components.reduce((result, component) => {
|
|
36
|
+
if (!component) return result
|
|
37
|
+
|
|
38
|
+
// 处理分栏布局组件(新格式:row)
|
|
39
|
+
if (component.type === 'row' && component.children) {
|
|
40
|
+
component.children.forEach(col => {
|
|
41
|
+
if (Array.isArray(col)) {
|
|
42
|
+
result.push(...getAllFormComponents(col))
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
// 处理普通表单组件(统一格式:使用 props.variableName)
|
|
47
|
+
else if (component.props?.variableName) {
|
|
48
|
+
result.push(component)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return result
|
|
52
|
+
}, [])
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ==================== FormVariable转换 ====================
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 将组件列表转换为FormVariable数组
|
|
59
|
+
* FormVariable是增强的表单变量对象,包含完整的类型信息,可直接用于条件判断
|
|
60
|
+
* @param {Array} components - 组件配置数组或FormVariable数组
|
|
61
|
+
* @returns {Array<FormVariable>} FormVariable数组
|
|
62
|
+
*/
|
|
63
|
+
export function enrichFormVariables(components) {
|
|
64
|
+
if (!Array.isArray(components)) return []
|
|
65
|
+
|
|
66
|
+
// 避免重复转换
|
|
67
|
+
if (components.length > 0 && components[0] instanceof FormVariable) {
|
|
68
|
+
return components
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const allComponents = getAllFormComponents(components)
|
|
72
|
+
return FormVariable.fromComponents(allComponents)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 从FormVariable数组中查找指定变量
|
|
77
|
+
* @param {Array<FormVariable>} formVariables - FormVariable数组
|
|
78
|
+
* @param {string} variableName - 变量名
|
|
79
|
+
* @returns {FormVariable|null} 找到的FormVariable或null
|
|
80
|
+
*/
|
|
81
|
+
export function findFormVariable(formVariables, variableName) {
|
|
82
|
+
if (!Array.isArray(formVariables)) return null
|
|
83
|
+
return formVariables.find(v => v.variableName === variableName) || null
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ==================== 组件验证 ====================
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 验证表单组件配置的完整性
|
|
90
|
+
* @param {Object} component - 组件配置
|
|
91
|
+
* @returns {{valid: boolean, errors: Array<string>}} 验证结果
|
|
92
|
+
*/
|
|
93
|
+
export function validateComponentConfig(component) {
|
|
94
|
+
const errors = []
|
|
95
|
+
|
|
96
|
+
if (!component) {
|
|
97
|
+
errors.push('组件配置不能为空')
|
|
98
|
+
return { valid: false, errors }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!component.type) {
|
|
102
|
+
errors.push('组件类型不能为空')
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!component.props) {
|
|
106
|
+
errors.push('组件属性不能为空')
|
|
107
|
+
} else {
|
|
108
|
+
if (!component.props.variableName) {
|
|
109
|
+
errors.push('变量名不能为空')
|
|
110
|
+
}
|
|
111
|
+
if (!component.props.label) {
|
|
112
|
+
errors.push('标签不能为空')
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 选择类组件必须配置选项
|
|
116
|
+
const selectTypes = ['select', 'radio', 'checkbox']
|
|
117
|
+
if (selectTypes.includes(component.type)) {
|
|
118
|
+
const hasOptions = Array.isArray(component.props.options) && component.props.options.length > 0
|
|
119
|
+
if (!hasOptions) {
|
|
120
|
+
errors.push(`${component.type}类型必须配置选项列表`)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
valid: errors.length === 0,
|
|
127
|
+
errors
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 统计叶子表单字段数量(不包含分栏容器)
|
|
134
|
+
* @param {Array} components
|
|
135
|
+
* @returns {Number}
|
|
136
|
+
*/
|
|
137
|
+
export function countFormComponents(components) {
|
|
138
|
+
return getAllFormComponents(components).length
|
|
139
|
+
}
|
package/src/core/fileIO.js
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
export function sanitizeFileName(name, fallback = 'flow') {
|
|
2
|
-
const value = String(name || fallback)
|
|
3
|
-
.trim()
|
|
4
|
-
.replace(/[\\/:*?"<>|]/g, '-')
|
|
5
|
-
.replace(/\s+/g, '-')
|
|
6
|
-
return value || fallback
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function downloadText(content, fileName, mime = 'application/json;charset=utf-8') {
|
|
10
|
-
if (typeof document === 'undefined') {
|
|
11
|
-
throw new Error('downloadText 只能在浏览器环境使用')
|
|
12
|
-
}
|
|
13
|
-
const blob = new Blob([content], { type: mime })
|
|
14
|
-
const url = URL.createObjectURL(blob)
|
|
15
|
-
const link = document.createElement('a')
|
|
16
|
-
link.href = url
|
|
17
|
-
link.download = fileName
|
|
18
|
-
link.style.display = 'none'
|
|
19
|
-
document.body.appendChild(link)
|
|
20
|
-
link.click()
|
|
21
|
-
document.body.removeChild(link)
|
|
22
|
-
setTimeout(() => URL.revokeObjectURL(url), 1000)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function downloadJSON(data, fileName = 'flow.json', space = 2) {
|
|
26
|
-
const content = typeof data === 'string' ? data : JSON.stringify(data, null, space)
|
|
27
|
-
const name = fileName.endsWith('.json') ? fileName : `${fileName}.json`
|
|
28
|
-
downloadText(content, name)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function readTextFile(file) {
|
|
32
|
-
if (!file) return Promise.reject(new Error('未选择文件'))
|
|
33
|
-
if (typeof file.text === 'function') return file.text()
|
|
34
|
-
return new Promise((resolve, reject) => {
|
|
35
|
-
const reader = new FileReader()
|
|
36
|
-
reader.onload = () => resolve(String(reader.result || ''))
|
|
37
|
-
reader.onerror = () => reject(reader.error || new Error('读取文件失败'))
|
|
38
|
-
reader.readAsText(file, 'utf-8')
|
|
39
|
-
})
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Opens the native file picker and resolves with { file, text }.
|
|
44
|
-
* Resolves with null when the user cancels.
|
|
45
|
-
*/
|
|
46
|
-
export function pickJSONFile() {
|
|
47
|
-
return new Promise((resolve) => {
|
|
48
|
-
if (typeof document === 'undefined') {
|
|
49
|
-
rejectInBrowser()
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
const input = document.createElement('input')
|
|
53
|
-
input.type = 'file'
|
|
54
|
-
input.accept = '.json,.gbflow,application/json'
|
|
55
|
-
input.style.display = 'none'
|
|
56
|
-
document.body.appendChild(input)
|
|
57
|
-
|
|
58
|
-
const cleanup = () => {
|
|
59
|
-
if (input.parentNode) input.parentNode.removeChild(input)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
input.addEventListener('change', async () => {
|
|
63
|
-
const file = input.files?.[0]
|
|
64
|
-
if (!file) {
|
|
65
|
-
cleanup()
|
|
66
|
-
resolve(null)
|
|
67
|
-
return
|
|
68
|
-
}
|
|
69
|
-
try {
|
|
70
|
-
const text = await readTextFile(file)
|
|
71
|
-
cleanup()
|
|
72
|
-
resolve({ file, text })
|
|
73
|
-
} catch (error) {
|
|
74
|
-
cleanup()
|
|
75
|
-
resolve({ file, text: '', error })
|
|
76
|
-
}
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
input.addEventListener('cancel', () => {
|
|
80
|
-
cleanup()
|
|
81
|
-
resolve(null)
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
input.click()
|
|
85
|
-
})
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function rejectInBrowser() {
|
|
89
|
-
throw new Error('pickJSONFile 只能在浏览器环境使用')
|
|
90
|
-
}
|
|
1
|
+
export function sanitizeFileName(name, fallback = 'flow') {
|
|
2
|
+
const value = String(name || fallback)
|
|
3
|
+
.trim()
|
|
4
|
+
.replace(/[\\/:*?"<>|]/g, '-')
|
|
5
|
+
.replace(/\s+/g, '-')
|
|
6
|
+
return value || fallback
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function downloadText(content, fileName, mime = 'application/json;charset=utf-8') {
|
|
10
|
+
if (typeof document === 'undefined') {
|
|
11
|
+
throw new Error('downloadText 只能在浏览器环境使用')
|
|
12
|
+
}
|
|
13
|
+
const blob = new Blob([content], { type: mime })
|
|
14
|
+
const url = URL.createObjectURL(blob)
|
|
15
|
+
const link = document.createElement('a')
|
|
16
|
+
link.href = url
|
|
17
|
+
link.download = fileName
|
|
18
|
+
link.style.display = 'none'
|
|
19
|
+
document.body.appendChild(link)
|
|
20
|
+
link.click()
|
|
21
|
+
document.body.removeChild(link)
|
|
22
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function downloadJSON(data, fileName = 'flow.json', space = 2) {
|
|
26
|
+
const content = typeof data === 'string' ? data : JSON.stringify(data, null, space)
|
|
27
|
+
const name = fileName.endsWith('.json') ? fileName : `${fileName}.json`
|
|
28
|
+
downloadText(content, name)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function readTextFile(file) {
|
|
32
|
+
if (!file) return Promise.reject(new Error('未选择文件'))
|
|
33
|
+
if (typeof file.text === 'function') return file.text()
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const reader = new FileReader()
|
|
36
|
+
reader.onload = () => resolve(String(reader.result || ''))
|
|
37
|
+
reader.onerror = () => reject(reader.error || new Error('读取文件失败'))
|
|
38
|
+
reader.readAsText(file, 'utf-8')
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Opens the native file picker and resolves with { file, text }.
|
|
44
|
+
* Resolves with null when the user cancels.
|
|
45
|
+
*/
|
|
46
|
+
export function pickJSONFile() {
|
|
47
|
+
return new Promise((resolve) => {
|
|
48
|
+
if (typeof document === 'undefined') {
|
|
49
|
+
rejectInBrowser()
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
const input = document.createElement('input')
|
|
53
|
+
input.type = 'file'
|
|
54
|
+
input.accept = '.json,.gbflow,application/json'
|
|
55
|
+
input.style.display = 'none'
|
|
56
|
+
document.body.appendChild(input)
|
|
57
|
+
|
|
58
|
+
const cleanup = () => {
|
|
59
|
+
if (input.parentNode) input.parentNode.removeChild(input)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
input.addEventListener('change', async () => {
|
|
63
|
+
const file = input.files?.[0]
|
|
64
|
+
if (!file) {
|
|
65
|
+
cleanup()
|
|
66
|
+
resolve(null)
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const text = await readTextFile(file)
|
|
71
|
+
cleanup()
|
|
72
|
+
resolve({ file, text })
|
|
73
|
+
} catch (error) {
|
|
74
|
+
cleanup()
|
|
75
|
+
resolve({ file, text: '', error })
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
input.addEventListener('cancel', () => {
|
|
80
|
+
cleanup()
|
|
81
|
+
resolve(null)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
input.click()
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function rejectInBrowser() {
|
|
89
|
+
throw new Error('pickJSONFile 只能在浏览器环境使用')
|
|
90
|
+
}
|