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/src/core/index.js CHANGED
@@ -1,26 +1,26 @@
1
- /**
2
- * Headless entry: form schema, variables, condition operators and file helpers.
3
- * Imports nothing from Vue/Element Plus, so workflow runtimes can use it in a
4
- * worker or plain Node.
5
- */
6
- export * from './schema.js'
7
- export * from './fileIO.js'
8
- export * from './FormVariable.js'
9
- export * from './dynamicFormUtils.js'
10
- export * from './formVariableTypeSystem.js'
11
-
12
- export {
13
- COMPONENT_TYPES,
14
- COMPONENT_TYPE_LABELS,
15
- OPTION_COMPONENT_TYPES
16
- } from '../design/constants/componentTypes.js'
17
-
18
- export {
19
- generateComponentId,
20
- ensureComponentId,
21
- normalizeNumberValue,
22
- isEmpty,
23
- normalizeOptions,
24
- parseWidth,
25
- getFieldName
26
- } from '../design/utils/componentUtils.js'
1
+ /**
2
+ * Headless entry: form schema, variables, condition operators and file helpers.
3
+ * Imports nothing from Vue/Element Plus, so workflow runtimes can use it in a
4
+ * worker or plain Node.
5
+ */
6
+ export * from './schema.js'
7
+ export * from './fileIO.js'
8
+ export * from './FormVariable.js'
9
+ export * from './dynamicFormUtils.js'
10
+ export * from './formVariableTypeSystem.js'
11
+
12
+ export {
13
+ COMPONENT_TYPES,
14
+ COMPONENT_TYPE_LABELS,
15
+ OPTION_COMPONENT_TYPES
16
+ } from '../design/constants/componentTypes.js'
17
+
18
+ export {
19
+ generateComponentId,
20
+ ensureComponentId,
21
+ normalizeNumberValue,
22
+ isEmpty,
23
+ normalizeOptions,
24
+ parseWidth,
25
+ getFieldName
26
+ } from '../design/utils/componentUtils.js'
@@ -1,193 +1,193 @@
1
- import { COMPONENT_TYPES } from '../design/constants/componentTypes.js'
2
- import { generateComponentId, normalizeOptions } from '../design/utils/componentUtils.js'
3
-
4
- export const SCHEMA_VERSION = '1.0.0'
5
- export const FORM_KIND = 'gbflow-form'
6
-
7
- function isPlainObject(value) {
8
- return !!value && typeof value === 'object' && !Array.isArray(value)
9
- }
10
-
11
- function clone(value) {
12
- if (value === undefined) return undefined
13
- return JSON.parse(JSON.stringify(value))
14
- }
15
-
16
- function parseJSONIfNeeded(input) {
17
- if (typeof input !== 'string') return input
18
- const text = input.replace(/^\uFEFF/, '').trim()
19
- if (!text) throw new Error('导入内容为空')
20
- try {
21
- return JSON.parse(text)
22
- } catch (error) {
23
- throw new Error(`JSON 解析失败:${error.message}`)
24
- }
25
- }
26
-
27
- /**
28
- * Normalizes a component list (or a { components, options } container).
29
- * Returns { components, options, repairs }.
30
- */
31
- export function normalizeForm(form) {
32
- const input = Array.isArray(form) ? form : form?.components
33
- const rawOptions = Array.isArray(form) ? {} : form?.options || {}
34
- if (!Array.isArray(input)) return { components: [], options: { ...rawOptions }, repairs: [] }
35
- const repairs = []
36
-
37
- const normalizeComponents = (components) =>
38
- components
39
- .filter((component) => {
40
- if (isPlainObject(component)) return true
41
- repairs.push(`已忽略无效的表单组件配置:${JSON.stringify(component)}`)
42
- return false
43
- })
44
- .map((component) => {
45
- const item = clone(component)
46
- if (!item.id) item.id = generateComponentId()
47
- if (!item.type) {
48
- item.type = COMPONENT_TYPES.INPUT
49
- repairs.push('表单组件缺少 type,已按文本输入框处理')
50
- }
51
- if (!isPlainObject(item.props)) item.props = {}
52
- if (item.props.label === undefined) item.props.label = item.type
53
- if (item.props.variableName === undefined) item.props.variableName = ''
54
- if (item.props.options !== undefined) {
55
- item.props.options = normalizeOptions(item.props.options)
56
- }
57
- if (item.type === COMPONENT_TYPES.ROW) {
58
- const children = Array.isArray(item.children) ? item.children : []
59
- item.children = children.map((column) => normalizeComponents(Array.isArray(column) ? column : []))
60
- if (!item.children.length) item.children = [[], []]
61
- if (!item.props.columns) {
62
- item.props.columns = item.children.map(() => ({ span: 24 / item.children.length }))
63
- }
64
- }
65
- return item
66
- })
67
-
68
- return {
69
- components: normalizeComponents(input),
70
- options: {
71
- name: rawOptions.name || '',
72
- description: rawOptions.description || '',
73
- labelWidth: rawOptions.labelWidth || '120px',
74
- size: rawOptions.size || 'default',
75
- ...rawOptions
76
- },
77
- repairs
78
- }
79
- }
80
-
81
- export function validateForm(form) {
82
- const components = Array.isArray(form) ? form : form?.components || []
83
- const errors = []
84
- const warnings = []
85
- const names = new Set()
86
-
87
- const visit = (list) => {
88
- list.forEach((component) => {
89
- if (!component || typeof component !== 'object') return
90
- if (component.type === COMPONENT_TYPES.ROW) {
91
- component.children?.forEach(visit)
92
- return
93
- }
94
- const name = component.props?.variableName
95
- if (name) {
96
- if (names.has(name)) {
97
- errors.push({
98
- code: 'DUPLICATE_VARIABLE',
99
- message: `表单变量名重复:${name}`,
100
- componentId: component.id
101
- })
102
- }
103
- names.add(name)
104
- } else {
105
- warnings.push({
106
- code: 'MISSING_VARIABLE',
107
- message: `表单项「${component.props?.label || component.type}」尚未设置变量名`,
108
- componentId: component.id
109
- })
110
- }
111
- if (!component.props?.label) {
112
- warnings.push({
113
- code: 'MISSING_LABEL',
114
- message: `表单项「${component.id}」尚未设置标签`,
115
- componentId: component.id
116
- })
117
- }
118
- if (['select', 'radio-group', 'checkbox-group'].includes(component.type)) {
119
- if (!Array.isArray(component.props?.options) || component.props.options.length === 0) {
120
- warnings.push({
121
- code: 'MISSING_OPTIONS',
122
- message: `选项组件「${component.props?.label || component.type}」还没有选项`,
123
- componentId: component.id
124
- })
125
- }
126
- }
127
- })
128
- }
129
-
130
- visit(components)
131
- return { valid: errors.length === 0, errors, warnings }
132
- }
133
-
134
- function stripUndefined(meta) {
135
- return Object.keys(meta || {}).reduce((result, key) => {
136
- if (meta[key] !== undefined && meta[key] !== null) result[key] = meta[key]
137
- return result
138
- }, {})
139
- }
140
-
141
- /**
142
- * Payload/JSON export for a standalone form.
143
- */
144
- export function exportForm(form, meta = {}, options = {}) {
145
- const result = normalizeForm(form)
146
- const payload = {
147
- schemaVersion: SCHEMA_VERSION,
148
- kind: FORM_KIND,
149
- meta: stripUndefined(meta),
150
- form: { components: result.components, options: result.options }
151
- }
152
- return options.raw ? payload : JSON.stringify(payload, null, options.space ?? 2)
153
- }
154
-
155
- /**
156
- * Imports a standalone form from GBflow documents, bare arrays,
157
- * `{ components }` containers, form-create style `{ rule, options }`
158
- * and RuoYi `formStructure` records.
159
- */
160
- export function importForm(input) {
161
- const raw = parseJSONIfNeeded(input)
162
- if (Array.isArray(raw)) {
163
- const result = normalizeForm(raw)
164
- return {
165
- components: result.components,
166
- options: result.options,
167
- repairs: result.repairs,
168
- validation: validateForm({ components: result.components })
169
- }
170
- }
171
- if (!isPlainObject(raw)) throw new Error('无法识别的表单导入内容')
172
-
173
- const source =
174
- raw.form ||
175
- raw.formStructure ||
176
- raw.formConfig ||
177
- raw.rule ||
178
- (raw.components ? raw : null)
179
- if (!source) throw new Error('导入内容中未找到表单(components)')
180
-
181
- const result = normalizeForm(source)
182
- const options = {
183
- ...result.options,
184
- ...(isPlainObject(raw.options) ? raw.options : {})
185
- }
186
- return {
187
- components: result.components,
188
- options,
189
- repairs: result.repairs,
190
- meta: isPlainObject(raw.meta) ? raw.meta : undefined,
191
- validation: validateForm({ components: result.components })
192
- }
1
+ import { COMPONENT_TYPES } from '../design/constants/componentTypes.js'
2
+ import { generateComponentId, normalizeOptions } from '../design/utils/componentUtils.js'
3
+
4
+ export const SCHEMA_VERSION = '1.0.0'
5
+ export const FORM_KIND = 'gbflow-form'
6
+
7
+ function isPlainObject(value) {
8
+ return !!value && typeof value === 'object' && !Array.isArray(value)
9
+ }
10
+
11
+ function clone(value) {
12
+ if (value === undefined) return undefined
13
+ return JSON.parse(JSON.stringify(value))
14
+ }
15
+
16
+ function parseJSONIfNeeded(input) {
17
+ if (typeof input !== 'string') return input
18
+ const text = input.replace(/^\uFEFF/, '').trim()
19
+ if (!text) throw new Error('导入内容为空')
20
+ try {
21
+ return JSON.parse(text)
22
+ } catch (error) {
23
+ throw new Error(`JSON 解析失败:${error.message}`)
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Normalizes a component list (or a { components, options } container).
29
+ * Returns { components, options, repairs }.
30
+ */
31
+ export function normalizeForm(form) {
32
+ const input = Array.isArray(form) ? form : form?.components
33
+ const rawOptions = Array.isArray(form) ? {} : form?.options || {}
34
+ if (!Array.isArray(input)) return { components: [], options: { ...rawOptions }, repairs: [] }
35
+ const repairs = []
36
+
37
+ const normalizeComponents = (components) =>
38
+ components
39
+ .filter((component) => {
40
+ if (isPlainObject(component)) return true
41
+ repairs.push(`已忽略无效的表单组件配置:${JSON.stringify(component)}`)
42
+ return false
43
+ })
44
+ .map((component) => {
45
+ const item = clone(component)
46
+ if (!item.id) item.id = generateComponentId()
47
+ if (!item.type) {
48
+ item.type = COMPONENT_TYPES.INPUT
49
+ repairs.push('表单组件缺少 type,已按文本输入框处理')
50
+ }
51
+ if (!isPlainObject(item.props)) item.props = {}
52
+ if (item.props.label === undefined) item.props.label = item.type
53
+ if (item.props.variableName === undefined) item.props.variableName = ''
54
+ if (item.props.options !== undefined) {
55
+ item.props.options = normalizeOptions(item.props.options)
56
+ }
57
+ if (item.type === COMPONENT_TYPES.ROW) {
58
+ const children = Array.isArray(item.children) ? item.children : []
59
+ item.children = children.map((column) => normalizeComponents(Array.isArray(column) ? column : []))
60
+ if (!item.children.length) item.children = [[], []]
61
+ if (!item.props.columns) {
62
+ item.props.columns = item.children.map(() => ({ span: 24 / item.children.length }))
63
+ }
64
+ }
65
+ return item
66
+ })
67
+
68
+ return {
69
+ components: normalizeComponents(input),
70
+ options: {
71
+ name: rawOptions.name || '',
72
+ description: rawOptions.description || '',
73
+ labelWidth: rawOptions.labelWidth || '120px',
74
+ size: rawOptions.size || 'default',
75
+ ...rawOptions
76
+ },
77
+ repairs
78
+ }
79
+ }
80
+
81
+ export function validateForm(form) {
82
+ const components = Array.isArray(form) ? form : form?.components || []
83
+ const errors = []
84
+ const warnings = []
85
+ const names = new Set()
86
+
87
+ const visit = (list) => {
88
+ list.forEach((component) => {
89
+ if (!component || typeof component !== 'object') return
90
+ if (component.type === COMPONENT_TYPES.ROW) {
91
+ component.children?.forEach(visit)
92
+ return
93
+ }
94
+ const name = component.props?.variableName
95
+ if (name) {
96
+ if (names.has(name)) {
97
+ errors.push({
98
+ code: 'DUPLICATE_VARIABLE',
99
+ message: `表单变量名重复:${name}`,
100
+ componentId: component.id
101
+ })
102
+ }
103
+ names.add(name)
104
+ } else {
105
+ warnings.push({
106
+ code: 'MISSING_VARIABLE',
107
+ message: `表单项「${component.props?.label || component.type}」尚未设置变量名`,
108
+ componentId: component.id
109
+ })
110
+ }
111
+ if (!component.props?.label) {
112
+ warnings.push({
113
+ code: 'MISSING_LABEL',
114
+ message: `表单项「${component.id}」尚未设置标签`,
115
+ componentId: component.id
116
+ })
117
+ }
118
+ if (['select', 'radio-group', 'checkbox-group'].includes(component.type)) {
119
+ if (!Array.isArray(component.props?.options) || component.props.options.length === 0) {
120
+ warnings.push({
121
+ code: 'MISSING_OPTIONS',
122
+ message: `选项组件「${component.props?.label || component.type}」还没有选项`,
123
+ componentId: component.id
124
+ })
125
+ }
126
+ }
127
+ })
128
+ }
129
+
130
+ visit(components)
131
+ return { valid: errors.length === 0, errors, warnings }
132
+ }
133
+
134
+ function stripUndefined(meta) {
135
+ return Object.keys(meta || {}).reduce((result, key) => {
136
+ if (meta[key] !== undefined && meta[key] !== null) result[key] = meta[key]
137
+ return result
138
+ }, {})
139
+ }
140
+
141
+ /**
142
+ * Payload/JSON export for a standalone form.
143
+ */
144
+ export function exportForm(form, meta = {}, options = {}) {
145
+ const result = normalizeForm(form)
146
+ const payload = {
147
+ schemaVersion: SCHEMA_VERSION,
148
+ kind: FORM_KIND,
149
+ meta: stripUndefined(meta),
150
+ form: { components: result.components, options: result.options }
151
+ }
152
+ return options.raw ? payload : JSON.stringify(payload, null, options.space ?? 2)
153
+ }
154
+
155
+ /**
156
+ * Imports a standalone form from GBflow documents, bare arrays,
157
+ * `{ components }` containers, form-create style `{ rule, options }`
158
+ * and RuoYi `formStructure` records.
159
+ */
160
+ export function importForm(input) {
161
+ const raw = parseJSONIfNeeded(input)
162
+ if (Array.isArray(raw)) {
163
+ const result = normalizeForm(raw)
164
+ return {
165
+ components: result.components,
166
+ options: result.options,
167
+ repairs: result.repairs,
168
+ validation: validateForm({ components: result.components })
169
+ }
170
+ }
171
+ if (!isPlainObject(raw)) throw new Error('无法识别的表单导入内容')
172
+
173
+ const source =
174
+ raw.form ||
175
+ raw.formStructure ||
176
+ raw.formConfig ||
177
+ raw.rule ||
178
+ (raw.components ? raw : null)
179
+ if (!source) throw new Error('导入内容中未找到表单(components)')
180
+
181
+ const result = normalizeForm(source)
182
+ const options = {
183
+ ...result.options,
184
+ ...(isPlainObject(raw.options) ? raw.options : {})
185
+ }
186
+ return {
187
+ components: result.components,
188
+ options,
189
+ repairs: result.repairs,
190
+ meta: isPlainObject(raw.meta) ? raw.meta : undefined,
191
+ validation: validateForm({ components: result.components })
192
+ }
193
193
  }
@@ -1,55 +1,55 @@
1
- /**
2
- * 默认值处理的组合式函数
3
- */
4
- import { isEmpty, normalizeNumberValue } from '../utils/componentUtils.js'
5
- import { COMPONENT_TYPES, DATE_DEFAULT_OPTIONS } from '../constants/componentTypes.js'
6
-
7
- /**
8
- * 获取组件的默认值
9
- * @param {Object} component - 组件对象
10
- * @returns {any} 默认值
11
- */
12
- export const getDefaultValue = (component) => {
13
- const defaultValue = component.defaultValue
14
- if (isEmpty(defaultValue)) return null
15
-
16
- if (component.type === COMPONENT_TYPES.DATE_PICKER) {
17
- if (defaultValue === DATE_DEFAULT_OPTIONS.TODAY) {
18
- return new Date()
19
- }
20
- if (defaultValue === DATE_DEFAULT_OPTIONS.TOMORROW) {
21
- const tomorrow = new Date()
22
- tomorrow.setDate(tomorrow.getDate() + 1)
23
- return tomorrow
24
- }
25
- return null
26
- }
27
-
28
- if (component.type === COMPONENT_TYPES.INPUT_NUMBER) {
29
- return normalizeNumberValue(defaultValue)
30
- }
31
-
32
- if (component.type === COMPONENT_TYPES.CHECKBOX_GROUP) {
33
- return Array.isArray(defaultValue) ? [...defaultValue] : []
34
- }
35
-
36
- return defaultValue
37
- }
38
-
39
- /**
40
- * 应用默认值到组件
41
- * @param {Object} component - 组件对象
42
- */
43
- export const applyDefaultValue = (component) => {
44
- if (component.type === COMPONENT_TYPES.INPUT_NUMBER) {
45
- component.value = normalizeNumberValue(component.value)
46
- }
47
-
48
- if (isEmpty(component.value) && component.defaultValue !== undefined && component.defaultValue !== null) {
49
- const defaultValue = getDefaultValue(component)
50
- if (defaultValue !== null && defaultValue !== undefined) {
51
- component.value = defaultValue
52
- }
53
- }
54
- }
55
-
1
+ /**
2
+ * 默认值处理的组合式函数
3
+ */
4
+ import { isEmpty, normalizeNumberValue } from '../utils/componentUtils.js'
5
+ import { COMPONENT_TYPES, DATE_DEFAULT_OPTIONS } from '../constants/componentTypes.js'
6
+
7
+ /**
8
+ * 获取组件的默认值
9
+ * @param {Object} component - 组件对象
10
+ * @returns {any} 默认值
11
+ */
12
+ export const getDefaultValue = (component) => {
13
+ const defaultValue = component.defaultValue
14
+ if (isEmpty(defaultValue)) return null
15
+
16
+ if (component.type === COMPONENT_TYPES.DATE_PICKER) {
17
+ if (defaultValue === DATE_DEFAULT_OPTIONS.TODAY) {
18
+ return new Date()
19
+ }
20
+ if (defaultValue === DATE_DEFAULT_OPTIONS.TOMORROW) {
21
+ const tomorrow = new Date()
22
+ tomorrow.setDate(tomorrow.getDate() + 1)
23
+ return tomorrow
24
+ }
25
+ return null
26
+ }
27
+
28
+ if (component.type === COMPONENT_TYPES.INPUT_NUMBER) {
29
+ return normalizeNumberValue(defaultValue)
30
+ }
31
+
32
+ if (component.type === COMPONENT_TYPES.CHECKBOX_GROUP) {
33
+ return Array.isArray(defaultValue) ? [...defaultValue] : []
34
+ }
35
+
36
+ return defaultValue
37
+ }
38
+
39
+ /**
40
+ * 应用默认值到组件
41
+ * @param {Object} component - 组件对象
42
+ */
43
+ export const applyDefaultValue = (component) => {
44
+ if (component.type === COMPONENT_TYPES.INPUT_NUMBER) {
45
+ component.value = normalizeNumberValue(component.value)
46
+ }
47
+
48
+ if (isEmpty(component.value) && component.defaultValue !== undefined && component.defaultValue !== null) {
49
+ const defaultValue = getDefaultValue(component)
50
+ if (defaultValue !== null && defaultValue !== undefined) {
51
+ component.value = defaultValue
52
+ }
53
+ }
54
+ }
55
+