Skip to content

Commit 53e3296

Browse files
authored
types: add types to useProperties, useProperty and useSaveLocal (#1265)
1 parent 126c9da commit 53e3296

6 files changed

Lines changed: 51 additions & 27 deletions

File tree

packages/canvas/DesignCanvas/src/api/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ export type PageSchema = RootNode
44

55
export interface PageState {
66
currentVm?: unknown
7-
currentSchema?: unknown
7+
currentSchema?: { [x: string]: any; id: string }
88
currentType?: unknown
9-
currentPage?: unknown
9+
currentPage?: { [x: string]: any; id: string; name: string } | null
10+
currentPageId?: string
11+
currentPageName?: string
1012
hoverVm?: unknown
1113
pageSchema: RootNode | null
1214
properties?: unknown

packages/canvas/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ export default {
2929
layout: CanvasLayout,
3030
metas: [CanvasContainer]
3131
}
32+
33+
export * from './DesignCanvas/src/api/types'

packages/plugins/materials/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ export default {
4646
}
4747

4848
export { entry, ResourceService, MaterialService }
49+
export * from './src/composable/types'

packages/settings/props/src/composable/useProperties.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
import { toRaw, shallowReactive, ref } from 'vue'
1414
import { constants } from '@opentiny/tiny-engine-utils'
1515
import { useCanvas, useMaterial, useTranslate } from '@opentiny/tiny-engine-meta-register'
16+
import type { Property, Schema } from '@opentiny/tiny-engine-plugin-materials'
1617

1718
const { COMPONENT_NAME } = constants
1819
const 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

packages/settings/props/src/composable/useProperty.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@
1313
import { computed } from 'vue'
1414
import { constants } from '@opentiny/tiny-engine-utils'
1515
import { useBlock } from '@opentiny/tiny-engine-meta-register'
16+
import type { PageState } from '@opentiny/tiny-engine-canvas'
17+
import type { Property, Linked } from '@opentiny/tiny-engine-plugin-materials'
1618

1719
const { SCHEMA_DATA_TYPE } = constants
1820

21+
interface AddPropertyLinksOptions {
22+
linked: Linked
23+
propertyName: string
24+
componentProperties: Property[]
25+
defaultValue: unknown
26+
}
27+
1928
// 给组件属性添加关联信息
20-
const addPropertyLinks = ({ linked, propertyName, componentProperties }) => {
29+
const addPropertyLinks = ({ linked, propertyName, componentProperties, defaultValue: _ }: AddPropertyLinksOptions) => {
2130
for (let i = 0; i < componentProperties.length; i++) {
2231
const propertyList = componentProperties[i].content
2332

@@ -41,8 +50,16 @@ const addPropertyLinks = ({ linked, propertyName, componentProperties }) => {
4150
}
4251
}
4352

53+
interface FindLinkedOptions {
54+
componentProperties: Property[]
55+
componentId: string
56+
blockProperties: Property['content'][number][]
57+
}
58+
4459
// 遍历区块属性,查找已关联的组件属性
45-
const findLinked = ({ componentProperties, componentId, blockProperties }) => {
60+
const findLinked = (options: FindLinkedOptions) => {
61+
const { componentProperties, componentId, blockProperties } = options
62+
4663
for (let i = 0; i < blockProperties.length; i++) {
4764
const property = blockProperties[i]
4865

@@ -58,7 +75,7 @@ const findLinked = ({ componentProperties, componentId, blockProperties }) => {
5875
}
5976

6077
// 重置组件属性的关联信息
61-
const resetLink = (properties) => {
78+
const resetLink = (properties: Property[]) => {
6279
if (properties && Array.isArray(properties)) {
6380
properties.forEach((group) => {
6481
if (group?.content && Array.isArray(group.content)) {
@@ -70,16 +87,16 @@ const resetLink = (properties) => {
7087
}
7188
}
7289

73-
const getProperty = ({ pageState }) => {
90+
const getProperty = ({ pageState }: { pageState: PageState }) => {
7491
const { getCurrentBlock, getBlockProperties } = useBlock()
7592

7693
const properties = computed(() => {
7794
// 区块消费时区块属性有关联信息需要重置
78-
resetLink(pageState.properties)
95+
resetLink(pageState.properties as Property[])
7996
// 区块编辑态下设置组件关联信息
8097
if (pageState.isBlock && pageState.currentSchema?.id) {
8198
findLinked({
82-
componentProperties: pageState.properties,
99+
componentProperties: pageState.properties as Property[],
83100
componentId: pageState.currentSchema.id,
84101
blockProperties: getBlockProperties(getCurrentBlock())
85102
})

packages/toolbars/generate-code/src/composable/useSaveLocal.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ const bridge = window.vscodeBridge
1919

2020
const confirmSaveLocal = async () => {
2121
const { pageState, setSaved } = useCanvas()
22-
const currentPageId = pageState.currentPageId || pageState.currentPage.id
23-
const currentPageName = pageState.currentPageName || pageState.currentPage.name
22+
const currentPageId = pageState.currentPageId || pageState.currentPage?.id
23+
const currentPageName = pageState.currentPageName || pageState.currentPage?.name
2424

2525
const { VITE_ORIGIN } = useEnv()
2626
const savePage = await bridge.callHandler('save-page', {
@@ -52,8 +52,8 @@ const savePageLocal = async () => {
5252
// 查询本地页面文件是否存在
5353
const { currentPageId, currentPageName, currentPage } = useCanvas().pageState
5454
const fileExistRes = await bridge.callHandler('page-is-exist', {
55-
pageName: currentPageName || currentPage.name,
56-
pageId: currentPageId || currentPage.id,
55+
pageName: currentPageName || currentPage?.name,
56+
pageId: currentPageId || currentPage?.id,
5757
platformId: getMergeMeta('engine.config')?.platformId
5858
})
5959

0 commit comments

Comments
 (0)