1313import { toRaw , shallowReactive , ref } from 'vue'
1414import { constants } from '@opentiny/tiny-engine-utils'
1515import { useCanvas , useMaterial , useTranslate } from '@opentiny/tiny-engine-meta-register'
16+ import type { Property , Schema } from '@opentiny/tiny-engine-plugin-materials'
1617
1718const { COMPONENT_NAME } = constants
1819const propsUpdateKey = ref ( 0 )
1920
20- const getSlotSwitch = ( properties , slots = { } ) => {
21+ const getSlotSwitch = ( properties : Property [ ] , slots : Record < string , any > = { } ) => {
2122 if ( Object . keys ( slots ) . length ) {
2223 properties . push ( {
2324 label : {
@@ -52,7 +53,7 @@ const getSlotSwitch = (properties, slots = {}) => {
5253 * @param {* } groups 组件元数据
5354 * @returns
5455 */
55- const mergeProps = ( pageProps = { } , groups = [ ] ) => {
56+ const mergeProps = ( pageProps : Record < string , any > = { } , groups : Property [ ] = [ ] ) => {
5657 const group = groups . map ( ( { content = [ ] , ...group } ) => {
5758 return {
5859 ...group ,
@@ -71,7 +72,7 @@ const mergeProps = (pageProps = {}, groups = []) => {
7172 return group
7273}
7374
74- const translateProp = ( value ) => {
75+ const translateProp = ( value : { type : string } ) => {
7576 if ( value ?. type === 'i18n' ) {
7677 return useTranslate ( ) . translate ( value )
7778 }
@@ -84,14 +85,15 @@ const translateProp = (value) => {
8485 * @param {* } instance 画布上当前选中节点信息
8586 */
8687
87- const properties = shallowReactive ( {
88+ const properties = shallowReactive < { schema : Schema | null ; parent : Schema | null } > ( {
8889 schema : null ,
8990 parent : null
9091} )
9192
92- const isPageOrBlock = ( schema ) => [ COMPONENT_NAME . Block , COMPONENT_NAME . Page ] . includes ( schema ?. componentName )
93+ const isPageOrBlock = ( schema : Schema ) =>
94+ [ COMPONENT_NAME . Block , COMPONENT_NAME . Page ] . includes ( schema ?. componentName || '' )
9395
94- const getProps = ( schema , parent ) => {
96+ const getProps = ( schema : Schema , parent : Schema ) => {
9597 // 1 现在选中的节点和当前节点一样,不需要重新计算, 2 默认进来由于scheme和properities.schema相等,因此判断如果是“页面或者区块”需要进入if判断
9698 if ( schema && ( properties . schema !== schema || isPageOrBlock ( schema ) ) ) {
9799 const { props, componentName } = schema
@@ -114,7 +116,7 @@ const getProps = (schema, parent) => {
114116 properties . parent = parent
115117}
116118
117- const setProp = ( name , value , type ) => {
119+ const setProp = ( name : string , value : unknown , type ?: unknown ) => {
118120 if ( ! properties . schema ) {
119121 return
120122 }
@@ -134,37 +136,37 @@ const setProp = (name, value, type) => {
134136
135137 useCanvas ( ) . operateNode ( {
136138 type : 'changeProps' ,
137- id : properties . schema . id ,
139+ id : properties . schema . id || '' ,
138140 value : { props : newProps } ,
139141 option : { overwrite }
140142 } )
141143
142144 propsUpdateKey . value ++
143145}
144146
145- const getProp = ( key ) => {
146- return ( properties . schema . props || { } ) [ key ]
147+ const getProp = ( key : string ) => {
148+ return ( properties . schema ? .props || { } ) [ key ]
147149}
148150
149- const delProp = ( name ) => {
150- const newProps = { ...( properties . schema . props || { } ) }
151+ const delProp = ( name : string ) => {
152+ const newProps = { ...( properties . schema ? .props || { } ) }
151153
152154 delete newProps [ name ]
153155
154156 useCanvas ( ) . operateNode ( {
155157 type : 'changeProps' ,
156- id : properties . schema . id ,
158+ id : properties . schema ? .id || '' ,
157159 value : { props : newProps } ,
158160 option : { overwrite : true }
159161 } )
160162 propsUpdateKey . value ++
161163}
162164
163- const setProps = ( schema ) => {
165+ const setProps = ( schema : Schema ) => {
164166 Object . entries ( schema . props || { } ) . map ( ( [ key , value ] ) => setProp ( key , value ) )
165167}
166168
167- const getSchema = ( parent ) => {
169+ const getSchema = ( parent : boolean ) => {
168170 return parent ? properties : properties . schema
169171}
170172
0 commit comments