Skip to content

Commit 1baf01e

Browse files
committed
types: Simplify types
1 parent 5b27477 commit 1baf01e

2 files changed

Lines changed: 33 additions & 76 deletions

File tree

packages/plugins/materials/src/composable/types.ts

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ export interface Assets {
4646
styles: string[]
4747
}
4848

49+
export type Resource = Omit<Component, 'component'> & { type: string; component?: string; item?: string }
50+
4951
export interface BlockResource {
5052
componentName: string
5153
fileName: string
5254
css?: string
5355
props: Record<string, any>
5456
dataSource?: Record<string, any>
55-
schema: ContentSchema
56-
children: BlockContentChildren[]
57+
schema: Schema
58+
children: Schema[]
5759
state?: Record<string, any>
5860
methods: {
5961
[key: string]: TypeValuePair
@@ -70,69 +72,22 @@ export interface BlockResource {
7072
blockName?: string
7173
}
7274

73-
export interface BlockContentChildren {
74-
componentName: string
75-
props?: Record<string, any>
76-
children?: BlockContentChildren[]
77-
id: string
78-
condition?: boolean
79-
fileName?: string
80-
}
81-
8275
export interface TypeValuePair {
8376
type: DataTypeEnum
8477
value: string
8578
}
8679

8780
export type DataTypeEnum = 'JSExpression' | 'JSFunction' | 'JSResource' | 'JSResouce'
8881

89-
export interface ContentSchema {
90-
properties?: Property[]
91-
events?: Record<string, any>
92-
slots?: unknown
93-
lifeCycles?: Record<string, TypeValuePair>
94-
}
95-
9682
export interface Locale {
9783
zh_CN?: string
9884
}
9985

100-
export interface Property {
101-
label: Locale
102-
description: Locale
103-
collapse: Collapse
104-
content?: BlockProperty[]
105-
defaultValue?: any[]
106-
}
107-
10886
export interface Collapse {
10987
number: number
11088
text: Locale
11189
}
11290

113-
export interface BlockProperty {
114-
property: string
115-
type?: string
116-
defaultValue: unknown
117-
label: {
118-
text: Locale
119-
}
120-
description?: Locale
121-
widget?: {
122-
component: string
123-
props?: Record<string, any>
124-
}
125-
cols?: number
126-
rules?: any[]
127-
linked?: Linked
128-
handle?: Record<string, any>
129-
hidden?: boolean
130-
required?: boolean
131-
readOnly?: boolean
132-
disabled?: boolean
133-
labelPosition?: string
134-
}
135-
13691
export interface Linked {
13792
componentName: string
13893
property: string
@@ -207,7 +162,7 @@ export interface Component {
207162
slots?: unknown
208163
}
209164
library?: number
210-
schema: ComponentSchema
165+
schema: Schema
211166
}
212167

213168
export interface Configure {
@@ -236,13 +191,7 @@ export interface ContextMenu {
236191
disable: string[]
237192
}
238193

239-
export interface ComponentSchema {
240-
properties?: ComponentProperty[]
241-
events?: Record<string, any>
242-
slots?: unknown
243-
}
244-
245-
export interface ComponentProperty {
194+
export interface Property {
246195
label: Locale
247196
description?: Locale
248197
collapse?: Collapse
@@ -268,9 +217,12 @@ export interface ComponentProperty {
268217
device?: any[]
269218
onChange?: string
270219
properties?: ContentProperty[]
220+
linked?: Linked
221+
handle?: Record<string, any>
271222
}[]
272223
name?: string
273224
group?: string
225+
defaultValue?: unknown
274226
}
275227

276228
export interface ContentProperty {
@@ -313,7 +265,7 @@ export interface Snippet {
313265
export interface SnippetChild {
314266
icon?: string
315267
name?: Locale
316-
schema?: SnippetSchema
268+
schema?: Schema
317269
screenshot?: string
318270
snippetName?: string
319271
configure?: Configure
@@ -349,14 +301,18 @@ export interface NestingRule {
349301
ancestorWhitelist?: string[]
350302
}
351303

352-
export interface SnippetSchema {
304+
export interface Schema {
353305
props?: Record<string, any>
354-
children?: SnippetSchema[]
306+
children?: Schema[]
355307
componentName?: string
356308
componentType?: string
357-
properties?: unknown[]
358-
events?: unknown
309+
properties?: Property[]
310+
events?: Record<string, any>
359311
slots?: unknown
312+
lifeCycles?: Record<string, TypeValuePair>
313+
id?: string
314+
condition?: boolean
315+
fileName?: string
360316
}
361317

362318
export interface MaterialState {

packages/plugins/materials/src/composable/useMaterial.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,24 @@ import meta from '../../meta'
2828
import { getBlockCompileRes, getBlockByName, updateBlockCompileCache } from './block-compile'
2929
import type {
3030
Block,
31-
Material,
32-
MaterialState,
33-
SnippetSchema,
34-
Snippet,
31+
BlockResource,
3532
Component,
36-
InitMaterialOptions,
3733
ComponentMap,
38-
BlockResource,
39-
ComponentProperty,
40-
Dependency
34+
Dependency,
35+
InitMaterialOptions,
36+
Material,
37+
MaterialState,
38+
Property,
39+
Resource,
40+
Schema,
41+
Snippet
4142
} from './types'
4243

4344
const { camelize, capitalize, deepClone } = utils
4445
const { MATERIAL_TYPE } = constants
4546

4647
// 这里存放所有TinyVue组件、原生HTML、内置组件的缓存,包含了物料插件面板里所有显示的组件,也包含了没显示的一些联动组件
47-
const resource = new Map<string, Omit<Component, 'component'> & { type: string; component?: string; item?: string }>()
48+
const resource = new Map<string, Resource>()
4849

4950
// 这里涉及到区块发布后的更新问题,所以需要单独缓存区块
5051
const blockResource = new Map<string, BlockResource>()
@@ -60,7 +61,7 @@ const componentState = reactive<{ componentsMap: Record<string, ComponentMap> }>
6061
componentsMap: {}
6162
})
6263
const getSnippet = (component: string) => {
63-
let schema: SnippetSchema = {}
64+
let schema: Schema = {}
6465
materialState.components.some(({ children }) => {
6566
const child = children.find(({ snippetName }) => snippetName === component)
6667
if (child?.schema) {
@@ -76,7 +77,7 @@ const getSnippet = (component: string) => {
7677
const generateNode = ({ type, component }: { type: string; component: string }) => {
7778
const snippet = getSnippet(component) || {}
7879

79-
const schema: SnippetSchema & Required<Pick<SnippetSchema, 'props'>> = {
80+
const schema: Schema & Required<Pick<Schema, 'props'>> = {
8081
componentName: component,
8182
...snippet,
8283
props: {
@@ -109,7 +110,7 @@ const getConfigureMap = () => {
109110
* @param schemaProperties
110111
* @returns
111112
*/
112-
const patchBaseProps = (schemaProperties?: ComponentProperty[]) => {
113+
const patchBaseProps = (schemaProperties?: Property[]) => {
113114
if (!Array.isArray(schemaProperties)) {
114115
return
115116
}
@@ -408,7 +409,7 @@ const addMaterials = (materials: Material) => {
408409
addBlocks(materials.blocks)
409410
}
410411

411-
const getMaterial = (name: string) => {
412+
const getMaterial = (name: string): Resource | BlockResource | object => {
412413
if (name) {
413414
// 先读取组件缓存,再读取区块缓存
414415
return (
@@ -423,7 +424,7 @@ const getMaterial = (name: string) => {
423424
}
424425
}
425426

426-
const setMaterial = (name: string, data: any) => {
427+
const setMaterial = (name: string, data: Resource) => {
427428
resource.set(name, data)
428429
}
429430

0 commit comments

Comments
 (0)