|
|
@@ -7,7 +7,7 @@ import type {
|
|
|
} from '@/types/event'
|
|
|
import type { OptionType } from './type'
|
|
|
|
|
|
-import { get } from 'lodash-es'
|
|
|
+import { get, set } from 'lodash-es'
|
|
|
import { klona } from 'klona'
|
|
|
import { bfsWalk } from 'simple-mind-map/src/utils'
|
|
|
|
|
|
@@ -839,9 +839,117 @@ function createSchemaDefaultValue(defaultRoot: any, schema: ComponentSchema) {
|
|
|
if (currentValue !== undefined) return klona(currentValue)
|
|
|
}
|
|
|
|
|
|
+ if (schema.valueType === 'group') {
|
|
|
+ return createGroupDefaultValue(defaultRoot, schema)
|
|
|
+ }
|
|
|
+
|
|
|
return createDefaultValue(schema)
|
|
|
}
|
|
|
|
|
|
+function splitFieldPath(field?: string): string[] {
|
|
|
+ return String(field || '')
|
|
|
+ .split('.')
|
|
|
+ .map((item) => item.trim())
|
|
|
+ .filter(Boolean)
|
|
|
+}
|
|
|
+
|
|
|
+function joinFieldPath(paths: string[]): string {
|
|
|
+ return paths.filter(Boolean).join('.')
|
|
|
+}
|
|
|
+
|
|
|
+function getCommonFieldPrefix(fields: string[]): string[] {
|
|
|
+ const pathList = fields.map((item) => splitFieldPath(item)).filter((item) => item.length)
|
|
|
+ if (!pathList.length) return []
|
|
|
+
|
|
|
+ const [firstPath, ...restPaths] = pathList
|
|
|
+ let length = firstPath.length
|
|
|
+
|
|
|
+ restPaths.forEach((paths) => {
|
|
|
+ length = Math.min(length, paths.length)
|
|
|
+ for (let i = 0; i < length; i += 1) {
|
|
|
+ if (firstPath[i] !== paths[i]) {
|
|
|
+ length = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ return firstPath.slice(0, length)
|
|
|
+}
|
|
|
+
|
|
|
+function collectSchemaFields(schema?: ComponentSchema): string[] {
|
|
|
+ if (!schema) return []
|
|
|
+
|
|
|
+ const fields: string[] = []
|
|
|
+ if (schema.field) fields.push(schema.field)
|
|
|
+ ;(schema.children || []).forEach((child) => {
|
|
|
+ fields.push(...collectSchemaFields(child))
|
|
|
+ })
|
|
|
+
|
|
|
+ return fields
|
|
|
+}
|
|
|
+
|
|
|
+function getSchemaSourceBase(schema: ComponentSchema): string[] {
|
|
|
+ if (schema.field) return splitFieldPath(schema.field)
|
|
|
+ return getCommonFieldPrefix(collectSchemaFields(schema))
|
|
|
+}
|
|
|
+
|
|
|
+function getRelativeFieldPath(field: string | undefined, sourceBase: string[]): string[] {
|
|
|
+ const fieldPaths = splitFieldPath(field)
|
|
|
+ if (!sourceBase.length) return fieldPaths
|
|
|
+
|
|
|
+ const isSourceBaseMatched = sourceBase.every((item, index) => fieldPaths[index] === item)
|
|
|
+ return isSourceBaseMatched ? fieldPaths.slice(sourceBase.length) : fieldPaths
|
|
|
+}
|
|
|
+
|
|
|
+function getActionField(
|
|
|
+ field: string | undefined,
|
|
|
+ targetField: string,
|
|
|
+ sourceBase: string[]
|
|
|
+): string {
|
|
|
+ return joinFieldPath([targetField, ...getRelativeFieldPath(field, sourceBase)]) || targetField
|
|
|
+}
|
|
|
+
|
|
|
+function createGroupDefaultValue(defaultRoot: any, schema: ComponentSchema): Record<string, any> {
|
|
|
+ const result: Record<string, any> = {}
|
|
|
+ const sourceBase = getSchemaSourceBase(schema)
|
|
|
+
|
|
|
+ const appendValue = (item?: EventSettableSchema) => {
|
|
|
+ if (!item) return
|
|
|
+
|
|
|
+ if (item.valueType === 'dependency' && typeof item.dependency === 'function') {
|
|
|
+ const dependencySchemas = item.dependency(buildDependencyValues(item.name, defaultRoot))
|
|
|
+ const schemaList = Array.isArray(dependencySchemas) ? dependencySchemas : [dependencySchemas]
|
|
|
+ schemaList.forEach((schemaItem) => appendValue(schemaItem as EventSettableSchema))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if (item.valueType === 'group') {
|
|
|
+ const groupValue = createSchemaDefaultValue(defaultRoot, item)
|
|
|
+ const groupPath = item.field ? getRelativeFieldPath(item.field, sourceBase) : []
|
|
|
+
|
|
|
+ if (!groupPath.length) {
|
|
|
+ Object.assign(result, groupValue)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ set(result, joinFieldPath(groupPath), groupValue)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!item.field) return
|
|
|
+
|
|
|
+ const nextValue = createSchemaDefaultValue(defaultRoot, item)
|
|
|
+ const targetPath = getRelativeFieldPath(item.field, sourceBase)
|
|
|
+
|
|
|
+ if (!targetPath.length) return
|
|
|
+ set(result, joinFieldPath(targetPath), nextValue)
|
|
|
+ }
|
|
|
+
|
|
|
+ ;(schema.children || []).forEach((item) => appendValue(item as EventSettableSchema))
|
|
|
+ return result
|
|
|
+}
|
|
|
+
|
|
|
function cloneActionSchema(schema: ComponentSchema, targetField = 'payload'): ComponentSchema {
|
|
|
const cloned = klona(schema)
|
|
|
cloned.field = targetField
|
|
|
@@ -859,18 +967,75 @@ function buildDependencyValues(names: string[] = [], defaultRoot: any) {
|
|
|
}, {})
|
|
|
}
|
|
|
|
|
|
-function flattenEventSettableSchemas(
|
|
|
+function cloneEventActionSchema(
|
|
|
+ schema: ComponentSchema,
|
|
|
+ targetField = 'payload',
|
|
|
+ sourceBase = getSchemaSourceBase(schema)
|
|
|
+): ComponentSchema {
|
|
|
+ const rewriteSchema = (item: ComponentSchema): ComponentSchema => {
|
|
|
+ const cloned = klona(item)
|
|
|
+
|
|
|
+ if (cloned.field) {
|
|
|
+ cloned.field = getActionField(cloned.field, targetField, sourceBase)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (cloned.valueType === 'dependency') {
|
|
|
+ const dependencySchema = cloned as EventSettableSchema
|
|
|
+ if (Array.isArray(dependencySchema.name)) {
|
|
|
+ dependencySchema.name = dependencySchema.name.map((name) =>
|
|
|
+ getActionField(name, targetField, sourceBase)
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (cloned.children?.length) {
|
|
|
+ cloned.children = cloned.children.map((child) => rewriteSchema(child))
|
|
|
+ }
|
|
|
+
|
|
|
+ if (cloned.componentProps?.defaultCollapsed) {
|
|
|
+ cloned.componentProps.defaultCollapsed = false
|
|
|
+ }
|
|
|
+
|
|
|
+ return cloned
|
|
|
+ }
|
|
|
+
|
|
|
+ return rewriteSchema(schema)
|
|
|
+}
|
|
|
+
|
|
|
+function flattenEventActionSchemas(
|
|
|
items: EventSettableSchema[] = [],
|
|
|
- defaultRoot: any
|
|
|
+ defaultRoot: any,
|
|
|
+ pathTokens: string[] = []
|
|
|
): SchemaActionDescriptor[] {
|
|
|
const result: SchemaActionDescriptor[] = []
|
|
|
|
|
|
- items.forEach((item) => {
|
|
|
+ items.forEach((item, index) => {
|
|
|
if (!item) return
|
|
|
|
|
|
+ const nextPathTokens = [
|
|
|
+ ...pathTokens,
|
|
|
+ `${item.field || getSchemaLabel(item) || 'item'}_${index}`
|
|
|
+ ]
|
|
|
+
|
|
|
if (item.valueType === 'group') {
|
|
|
+ if (item.canUseEventSet === true) {
|
|
|
+ result.push({
|
|
|
+ actionKey: item.field || `group.${nextPathTokens.join('.')}`,
|
|
|
+ label: getSchemaLabel(item),
|
|
|
+ groupKey: 'property',
|
|
|
+ groupLabel: 'Property',
|
|
|
+ groupOrder: 1,
|
|
|
+ schemas: [cloneEventActionSchema(item)],
|
|
|
+ defaultValue: createSchemaDefaultValue(defaultRoot, item)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
result.push(
|
|
|
- ...flattenEventSettableSchemas((item.children || []) as EventSettableSchema[], defaultRoot)
|
|
|
+ ...flattenEventActionSchemas(
|
|
|
+ (item.children || []) as EventSettableSchema[],
|
|
|
+ defaultRoot,
|
|
|
+ nextPathTokens
|
|
|
+ )
|
|
|
)
|
|
|
return
|
|
|
}
|
|
|
@@ -878,7 +1043,13 @@ function flattenEventSettableSchemas(
|
|
|
if (item.valueType === 'dependency' && typeof item.dependency === 'function') {
|
|
|
const dependencySchemas = item.dependency(buildDependencyValues(item.name, defaultRoot))
|
|
|
const schemaList = Array.isArray(dependencySchemas) ? dependencySchemas : [dependencySchemas]
|
|
|
- result.push(...flattenEventSettableSchemas(schemaList as EventSettableSchema[], defaultRoot))
|
|
|
+ result.push(
|
|
|
+ ...flattenEventActionSchemas(
|
|
|
+ schemaList as EventSettableSchema[],
|
|
|
+ defaultRoot,
|
|
|
+ nextPathTokens
|
|
|
+ )
|
|
|
+ )
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -889,9 +1060,9 @@ function flattenEventSettableSchemas(
|
|
|
actionKey: item.field!,
|
|
|
label: getSchemaLabel(item),
|
|
|
groupKey: 'property',
|
|
|
- groupLabel: '属性',
|
|
|
+ groupLabel: 'Property',
|
|
|
groupOrder: 1,
|
|
|
- schemas: [cloneActionSchema(item)],
|
|
|
+ schemas: [cloneEventActionSchema(item)],
|
|
|
defaultValue: createSchemaDefaultValue(defaultRoot, item)
|
|
|
})
|
|
|
})
|
|
|
@@ -954,11 +1125,11 @@ function getSchemaActionDescriptors(targetData?: WidgetEventTargetData): SchemaA
|
|
|
if (!model) return []
|
|
|
|
|
|
return [
|
|
|
- ...flattenEventSettableSchemas(
|
|
|
+ ...flattenEventActionSchemas(
|
|
|
(model.config.props || []) as EventSettableSchema[],
|
|
|
model.defaultSchema
|
|
|
),
|
|
|
- ...flattenEventSettableSchemas(
|
|
|
+ ...flattenEventActionSchemas(
|
|
|
(model.config.coreProps || []) as EventSettableSchema[],
|
|
|
model.defaultSchema
|
|
|
),
|
|
|
@@ -1234,10 +1405,11 @@ export function getTargetOptions(project?: IProject): OptionType[] {
|
|
|
|
|
|
bfsWalk(page, (child) => {
|
|
|
if (child.id === page.id) return
|
|
|
+ if (!child.id) return
|
|
|
|
|
|
options.push({
|
|
|
label: `[${screen.name}] [${page.name}] ${child.name}`,
|
|
|
- value: `widget:${child.id}`,
|
|
|
+ value: `${child.id}`,
|
|
|
data: {
|
|
|
kind: 'widget',
|
|
|
pageId: page.id,
|