@longhongguo/form-create-ant-design-vue 3.3.9 → 3.3.11

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": "@longhongguo/form-create-ant-design-vue",
3
- "version": "3.3.09",
3
+ "version": "3.3.11",
4
4
  "description": "AntDesignVue版本低代码表单|FormCreate 是一个可以通过 JSON 生成具有动态渲染、数据收集、验证和提交功能的低代码表单生成组件。支持6个UI框架,适配移动端,并且支持生成任何 Vue 组件。内置20种常用表单组件和自定义组件,再复杂的表单都可以轻松搞定。",
5
5
  "main": "./dist/form-create.min.js",
6
6
  "module": "./dist/form-create.esm.js",
@@ -36,7 +36,15 @@ export default defineComponent({
36
36
  uploadImgCustomInsert: [String, Function],
37
37
  withCredentials: Boolean,
38
38
  // form-create 注入的 API
39
- formCreateInject: Object
39
+ formCreateInject: Object,
40
+ placeholder: {
41
+ type: String,
42
+ default: '请输入正文'
43
+ },
44
+ height: {
45
+ type: [String, Number],
46
+ default: 300
47
+ }
40
48
  },
41
49
  emits: ['update:modelValue'],
42
50
  watch: {
@@ -51,6 +59,31 @@ export default defineComponent({
51
59
  editorConfig() {
52
60
  const config = {}
53
61
 
62
+ // 设置 placeholder
63
+ if (this.placeholder) {
64
+ config.placeholder = this.placeholder
65
+ }
66
+
67
+ // 设置高度
68
+ if (this.height) {
69
+ // 如果是数字,转换为像素字符串;如果是字符串,直接使用
70
+ config.height = this.height
71
+ }
72
+
73
+ // 排除不需要的工具栏菜单项
74
+ // 移除:全屏、代码、表情、删除线、缩进、待办事项、引用、分割线、斜体
75
+ config.excludeMenus = [
76
+ 'fullScreen', // 全屏
77
+ 'code', // 代码
78
+ 'emoticon', // 表情
79
+ 'strikeThrough', // 删除线
80
+ 'indent', // 缩进
81
+ 'todo', // 待办事项
82
+ 'quote', // 引用
83
+ 'splitLine', // 分割线
84
+ 'italic' // 斜体
85
+ ]
86
+
54
87
  // 如果设置了 readOnly,配置只读模式
55
88
  if (this.readOnly) {
56
89
  // readOnly: true // 启用只读模式
@@ -61,6 +94,8 @@ export default defineComponent({
61
94
  // 禁用自动聚焦,避免在只读模式下获得焦点
62
95
  config.focus = false
63
96
  config.autoFocus = false
97
+ // 只读模式下隐藏 placeholder
98
+ delete config.placeholder
64
99
  }
65
100
 
66
101
  // 如果设置了 uploadImgServer,配置图片上传
@@ -105,6 +105,8 @@ export default {
105
105
  // 只读模式允许复制和点击链接,但禁止编辑
106
106
  if (ctx.prop.props) {
107
107
  ctx.prop.props.readOnly = true
108
+ // 预览模式和只读模式下隐藏 placeholder
109
+ delete ctx.prop.props.placeholder
108
110
  }
109
111
  } else if (
110
112
  ctx.rule.type === 'input' &&
@@ -118,6 +120,10 @@ export default {
118
120
  ctx.prop.props.autoSize = true
119
121
  // 移除 rows 属性,让 autoSize 完全控制高度
120
122
  delete ctx.prop.props.rows
123
+ // 预览模式和只读模式下隐藏 placeholder
124
+ delete ctx.prop.props.placeholder
125
+ // 预览模式和只读模式下隐藏字符计数
126
+ ctx.prop.props.showCount = false
121
127
  }
122
128
  } else if (ctx.rule.type === 'select') {
123
129
  // select 组件在预览模式下使用 disabled 实现只读效果
@@ -126,6 +132,8 @@ export default {
126
132
  // 强制设置 disabled = true,覆盖用户可能设置的 disabled: false
127
133
  if (ctx.prop.props) {
128
134
  ctx.prop.props.disabled = true
135
+ // 预览模式和只读模式下隐藏 placeholder
136
+ delete ctx.prop.props.placeholder
129
137
  // 如果原本设置了 readOnly,保留这个标记以便 CSS 识别
130
138
  if (isReadOnly) {
131
139
  // 可以在这里添加一个标记,但 Select 组件不支持 readOnly
@@ -137,6 +145,30 @@ export default {
137
145
  // 如果设置了 readOnly,直接使用组件的 readOnly 属性
138
146
  if (ctx.prop.props && isReadOnly) {
139
147
  ctx.prop.props.readOnly = true
148
+ // 预览模式和只读模式下隐藏 placeholder
149
+ delete ctx.prop.props.placeholder
150
+ // 预览模式和只读模式下隐藏字符计数
151
+ ctx.prop.props.showCount = false
152
+ } else if (ctx.prop.props && isPreviewMode) {
153
+ // 全局预览模式下也隐藏 placeholder
154
+ delete ctx.prop.props.placeholder
155
+ // 全局预览模式下隐藏字符计数
156
+ ctx.prop.props.showCount = false
157
+ }
158
+ } else if (
159
+ ctx.rule.type === 'datePicker' ||
160
+ ctx.rule.type === 'timePicker' ||
161
+ ctx.rule.type === 'timeRangePicker' ||
162
+ ctx.rule.type === 'rangePicker' ||
163
+ ctx.rule.type === 'cascader'
164
+ ) {
165
+ // 日期选择器、时间选择器、级联选择器等组件
166
+ // 预览模式和只读模式下隐藏 placeholder
167
+ if (ctx.prop.props) {
168
+ if (ctx.rule.type === 'cascader') {
169
+ ctx.prop.props.disabled = true
170
+ }
171
+ delete ctx.prop.props.placeholder
140
172
  }
141
173
  }
142
174
  }
@@ -49,11 +49,17 @@ export default {
49
49
  )
50
50
  }
51
51
  } catch (e) {
52
- console.error('Failed to parse loadData:', e)
52
+ // 解析 loadData 失败,静默处理
53
53
  }
54
54
  }
55
55
 
56
+ // 保存原始的 parsedFn,用于回显时调用(需要在 if 块外定义)
57
+ let originalLoadDataFn = null
58
+
56
59
  if (is.Function(parsedFn)) {
60
+ // 保存原始的 parsedFn,用于回显时调用
61
+ originalLoadDataFn = parsedFn
62
+
57
63
  // 包装 loadData 函数
58
64
  props.loadData = function (selectedOptions) {
59
65
  if (!selectedOptions || selectedOptions.length === 0) return
@@ -75,27 +81,38 @@ export default {
75
81
 
76
82
  // 递归查找并更新选项的函数
77
83
  // 通过 value 路径来查找,避免引用问题
78
- const updateOptionRecursive = (optionsList, pathValues, depth, newChildren) => {
84
+ const updateOptionRecursive = (
85
+ optionsList,
86
+ pathValues,
87
+ depth,
88
+ newChildren
89
+ ) => {
79
90
  if (!pathValues || pathValues.length === 0) {
80
91
  // 如果没有路径,直接返回深拷贝的选项
81
- return optionsList.map(item => ({
92
+ return optionsList.map((item) => ({
82
93
  ...item,
83
- children: item.children ? item.children.map(child => ({ ...child })) : undefined
94
+ children: item.children
95
+ ? item.children.map((child) => ({ ...child }))
96
+ : undefined
84
97
  }))
85
98
  }
86
-
99
+
87
100
  const currentValue = pathValues[depth]
88
101
  const isLast = depth === pathValues.length - 1
89
-
90
- return optionsList.map(item => {
102
+
103
+ return optionsList.map((item) => {
91
104
  const newItem = { ...item }
92
-
105
+
93
106
  // 如果当前项匹配路径的当前层级值
94
- if ((item.value !== undefined && item.value === currentValue) ||
95
- (item.id !== undefined && item.id === currentValue)) {
107
+ if (
108
+ (item.value !== undefined && item.value === currentValue) ||
109
+ (item.id !== undefined && item.id === currentValue)
110
+ ) {
96
111
  if (isLast) {
97
112
  // 是最后一个值(目标项),更新 children
98
- newItem.children = newChildren ? [...newChildren] : undefined
113
+ newItem.children = newChildren
114
+ ? [...newChildren]
115
+ : undefined
99
116
  newItem.loading = false
100
117
  // 保持其他属性不变,但深拷贝 children
101
118
  return newItem
@@ -116,7 +133,7 @@ export default {
116
133
  return newItem
117
134
  }
118
135
  }
119
-
136
+
120
137
  // 不匹配的项,也需要递归处理其 children(以防路径在其他分支)
121
138
  if (item.children && item.children.length > 0) {
122
139
  newItem.children = updateOptionRecursive(
@@ -129,14 +146,14 @@ export default {
129
146
  // 如果没有 children,深拷贝基本结构
130
147
  newItem.children = undefined
131
148
  }
132
-
149
+
133
150
  return newItem
134
151
  })
135
152
  }
136
153
 
137
154
  // 从 selectedOptions 中提取 value 路径
138
- const pathValues = selectedOptions.map(opt => opt.value || opt.id)
139
-
155
+ const pathValues = selectedOptions.map((opt) => opt.value || opt.id)
156
+
140
157
  // 使用递归函数更新选项(从第0层开始)
141
158
  const newOptions = updateOptionRecursive(
142
159
  currentOptions,
@@ -206,6 +223,159 @@ export default {
206
223
  return result
207
224
  }
208
225
  }
226
+
227
+ // 处理回显时的数据加载 - 当 value 变化时触发(包括初始值和 setValue)
228
+ // 使用一个函数来处理,可以在多个地方调用
229
+ const triggerLoadDataForValue = (valueToLoad) => {
230
+ if (
231
+ !valueToLoad ||
232
+ !Array.isArray(valueToLoad) ||
233
+ valueToLoad.length <= 1 ||
234
+ !props.loadData ||
235
+ typeof props.loadData !== 'function'
236
+ ) {
237
+ return
238
+ }
239
+
240
+ // 延迟执行,等待 effect.fetch 完成第一级数据加载
241
+ const timerKey = `_loadDataTimer_${ctx.rule._fc_id || Date.now()}`
242
+ if (ctx.rule[timerKey]) {
243
+ clearTimeout(ctx.rule[timerKey])
244
+ }
245
+
246
+ ctx.rule[timerKey] = setTimeout(() => {
247
+ let retryCount = 0
248
+ const maxRetries = 50 // 最多重试50次,即5秒
249
+
250
+ const loadInitialData = async () => {
251
+ retryCount++
252
+ const fetchData = ctxRef.effectData('fetch')
253
+
254
+ // 如果超过最大重试次数,停止
255
+ if (retryCount > maxRetries) {
256
+ return
257
+ }
258
+
259
+ // 如果还在加载第一级数据,等待
260
+ if (fetchData && fetchData.loading === true) {
261
+ setTimeout(loadInitialData, 100)
262
+ return
263
+ }
264
+
265
+ let currentOptions = props.options || []
266
+
267
+ if (!currentOptions || currentOptions.length === 0) {
268
+ // 检查 fetchData 中是否有 options
269
+ if (fetchData && fetchData.props && fetchData.props.options) {
270
+ currentOptions = fetchData.props.options
271
+ props.options = currentOptions
272
+ } else {
273
+ setTimeout(loadInitialData, 100)
274
+ return
275
+ }
276
+ }
277
+
278
+ if (!currentOptions || currentOptions.length === 0) {
279
+ return
280
+ }
281
+
282
+ // 递归加载缺失的数据
283
+ const loadMissingData = async (pathValues, startDepth) => {
284
+ if (startDepth >= pathValues.length - 1) {
285
+ return
286
+ }
287
+
288
+ // 重新获取最新的 options
289
+ currentOptions = props.options || []
290
+
291
+ // 构建 selectedOptions(从根到当前层级)
292
+ const selectedOptions = []
293
+ let currentLevelOptions = currentOptions
294
+ for (let i = 0; i <= startDepth; i++) {
295
+ const val = pathValues[i]
296
+ const option = currentLevelOptions.find(
297
+ (opt) => opt.value === val || opt.id === val
298
+ )
299
+ if (option) {
300
+ selectedOptions.push(option)
301
+ currentLevelOptions = option.children || []
302
+ } else {
303
+ return
304
+ }
305
+ }
306
+
307
+ const currentOption = selectedOptions[startDepth]
308
+
309
+ // 如果已经是叶子节点,不需要加载
310
+ if (currentOption.isLeaf === true) {
311
+ return
312
+ }
313
+
314
+ // 检查是否需要加载子级数据
315
+ const nextValue = pathValues[startDepth + 1]
316
+ if (nextValue === undefined || nextValue === null) {
317
+ return
318
+ }
319
+
320
+ // 检查是否已有子级数据
321
+ const hasChildren =
322
+ currentOption.children &&
323
+ Array.isArray(currentOption.children) &&
324
+ currentOption.children.length > 0
325
+
326
+ if (!hasChildren) {
327
+ // 需要触发 loadData 加载
328
+ // 设置 loading 状态
329
+ currentOption.loading = true
330
+
331
+ try {
332
+ const result = props.loadData(selectedOptions)
333
+
334
+ // 如果返回 Promise,等待完成
335
+ if (result && typeof result.then === 'function') {
336
+ await result
337
+ }
338
+
339
+ // 等待数据更新
340
+ await new Promise((resolve) => setTimeout(resolve, 300))
341
+
342
+ // 继续加载下一级
343
+ if (startDepth + 1 < pathValues.length - 1) {
344
+ await loadMissingData(pathValues, startDepth + 1)
345
+ }
346
+ } catch (error) {
347
+ currentOption.loading = false
348
+ }
349
+ } else {
350
+ // 已经有 children,直接继续加载下一级
351
+ await loadMissingData(pathValues, startDepth + 1)
352
+ }
353
+ }
354
+
355
+ // 开始加载数据
356
+ loadMissingData(valueToLoad, 0).catch(() => {
357
+ // 静默处理错误
358
+ })
359
+ }
360
+
361
+ loadInitialData()
362
+ }, 500)
363
+ }
364
+
365
+ // 在 mergeProp 中触发(处理初始值和 setValue 的情况)
366
+ const currentValue = ctx.rule.value
367
+ // 检查 value 是否变化(通过比较字符串化的值)
368
+ const valueKey = JSON.stringify(currentValue || [])
369
+ const lastValueKey = ctx.rule._lastLoadDataValueKey
370
+
371
+ if (
372
+ valueKey !== lastValueKey &&
373
+ currentValue &&
374
+ Array.isArray(currentValue)
375
+ ) {
376
+ ctx.rule._lastLoadDataValueKey = valueKey
377
+ triggerLoadDataForValue(currentValue)
378
+ }
209
379
  }
210
380
  },
211
381
  render(children, ctx) {
@@ -223,5 +393,22 @@ export default {
223
393
 
224
394
  // 调用默认渲染
225
395
  return ctx.$render.defaultRender(ctx, children)
396
+ },
397
+ mounted(ctx) {
398
+ // mounted 时也触发一次,确保初始值能正确加载
399
+ const value = ctx.rule.value
400
+ const props = ctx.prop.props
401
+
402
+ if (value && Array.isArray(value) && value.length > 1 && props.loadData) {
403
+ // 延迟一点,确保 mergeProp 已完成
404
+ setTimeout(() => {
405
+ const loadDataFn = ctx.prop.props.loadData
406
+ if (loadDataFn && typeof loadDataFn === 'function') {
407
+ // 重新触发一次加载逻辑(会通过 mergeProp 中的逻辑处理)
408
+ // 通过触发 value 变化来触发加载
409
+ ctx.rule._lastLoadDataValueKey = null
410
+ }
411
+ }, 100)
412
+ }
226
413
  }
227
414
  }