Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add capability of add custom metaComponents #528

Merged
merged 8 commits into from
May 31, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions designer-demo/src/MetaInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<template>
<span>我是自定义的 meta input</span>
<tiny-input v-model="value" :type="type" :placeholder="placeholder" :rows="rows" @update:modelValue="change">
</tiny-input>
</template>

<script>
import { ref } from 'vue'
import { Input } from '@opentiny/vue'

export default {
name: 'MetaInput',
components: {
TinyInput: Input
},
props: {
modelValue: {
type: String
},
type: {
type: String
},
placeholder: {
type: String
},
suffixIcons: {
type: Array,
default: () => []
},
dataType: {
type: String
},
rows: {
type: Number,
default: 10
}
},
emits: ['update:modelValue'],
setup(props, { emit }) {
const value = ref(props.modelValue)

const change = (val) => {
emit('update:modelValue', props.dataType === 'Array' ? val.split(',') : val)
}

return {
value,
change
}
}
}
</script>

<style lang="less" scoped>
.tiny-svg-size {
margin-left: 10px;
font-size: 16px;
&:hover {
cursor: pointer;
color: var(--ti-lowcode-dialog-font-color);
}
}
</style>
3 changes: 2 additions & 1 deletion designer-demo/src/main.js
Original file line number Diff line number Diff line change
@@ -13,5 +13,6 @@
// 导入@opentiny/tiny-engine时,内部的依赖包也会逐个导入,可能会执行useComplie,此时需要templateHashMap。所以需要先执行一次defineEntry
import { registry } from './defineEntry.js'
import { init } from '@opentiny/tiny-engine'
import { metaComponents } from './metaComponents'

init({ registry })
init({ registry, metaComponents })
12 changes: 12 additions & 0 deletions designer-demo/src/metaComponents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// import { addMetaComponents } from '@opentiny/tiny-engine-entry'
import CustomMetaInput from './MetaInput.vue'
import { MetaInput, MetaSelect } from '@opentiny/tiny-engine-meta-components'

export const metaComponents = [
{
name: 'meta-input-custom',
component: CustomMetaInput
},
MetaInput,
MetaSelect
]
22 changes: 15 additions & 7 deletions packages/common/component/ConfigItem.vue
Original file line number Diff line number Diff line change
@@ -111,6 +111,7 @@ import MultiTypeSelector from './MultiTypeSelector.vue'
import { useHistory, useProperties, useResource, useLayout, useCanvas } from '@opentiny/tiny-engine-controller'
import { generateFunction } from '@opentiny/tiny-engine-controller/utils'
import { SCHEMA_DATA_TYPE, PAGE_STATUS, TYPES } from '@opentiny/tiny-engine-controller/js/constants'
import { getMetaComponent } from '@opentiny/tiny-engine-entry'

const hasRule = (required, rules) => {
if (required) {
@@ -201,13 +202,20 @@ export default {
(props.property?.label?.text?.[locale.value] ?? props.property?.label?.text)
)
const isLinked = computed(() => Boolean(props.property.linked))
const component = computed(() =>
multiType.value
? MultiTypeSelector
: props.metaComponents[widget.value.component] ||
MetaComponents[widget.value.component] ||
MetaComponents['MetaInput']
)
const component = computed(() => {
if (multiType.value) {
return MultiTypeSelector
}

const component = getMetaComponent(widget.value.component)

if (component) {
return component
}

// TODO: 需要弄清楚 props.metaComponents[widget.value.component] 是什么场景
return props.metaComponents[widget.value.component] || getMetaComponent('MetaInput')
})
const bindValue = computed(() => {
let value = props.property?.widget?.props?.modelValue

3 changes: 1 addition & 2 deletions packages/common/index.js
Original file line number Diff line number Diff line change
@@ -146,8 +146,7 @@ export const MetaComponents = {
MetaIpSection,
MetaRelatedEditor,
MetaRelatedColumns,
MetaTableColumns,
SearchEmpty
MetaTableColumns
}

export {
2 changes: 1 addition & 1 deletion packages/design-core/public/mock/bundle.json
Original file line number Diff line number Diff line change
@@ -79,7 +79,7 @@
"labelPosition": "top",
"type": "string",
"widget": {
"component": "MetaInput",
"component": "meta-input-custom",
"props": {}
}
},
5 changes: 4 additions & 1 deletion packages/design-core/src/init.js
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ import { utils } from '@opentiny/tiny-engine-utils'
import { defineEntry } from '@opentiny/tiny-engine-entry'
import App from './App.vue'
import defaultRegistry from '../registry.js'
import { registerMetaComponents } from './registerMetaComponents'

import 'virtual:svg-icons-register'

@@ -80,9 +81,11 @@ const defaultLifeCycles = {
}
}

export const init = ({ selector = '#app', registry = defaultRegistry, lifeCycles = {} } = {}) => {
export const init = ({ selector = '#app', registry = defaultRegistry, lifeCycles = {}, metaComponents = [] } = {}) => {
const { beforeAppCreate, appCreated, appMounted } = lifeCycles

registerMetaComponents(metaComponents)

defaultLifeCycles.beforeAppCreate({ registry })
beforeAppCreate?.({ registry })
const app = createApp(App)
13 changes: 13 additions & 0 deletions packages/design-core/src/registerMetaComponents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// TODO: 抽空 common package 中的 metaComponent
import { MetaComponents } from '@opentiny/tiny-engine-common'
import { addMetaComponents } from '@opentiny/tiny-engine-entry'

/**
* 注册TinyEngine默认的 metaComponents
*/
export const registerMetaComponents = (metaComponents) => {
const { MetaInput, MetaSelect, ...otherComponents } = MetaComponents

addMetaComponents(Object.entries(otherComponents).map(([name, component]) => ({ name, component })))
addMetaComponents(metaComponents)
}
3 changes: 2 additions & 1 deletion packages/design-core/vite.config.js
Original file line number Diff line number Diff line change
@@ -199,7 +199,8 @@ const devAlias = {
'@opentiny/tiny-engine-i18n-host': path.resolve(__dirname, '../i18n/src/lib.js'),
'@opentiny/tiny-engine-builtin-component': path.resolve(__dirname, '../builtinComponent/index.js'),
'@opentiny/tiny-engine-entry': path.resolve(__dirname, '../entry/src/index.js'),
'@opentiny/tiny-engine-layout': path.resolve(__dirname, '../layout/index.js')
'@opentiny/tiny-engine-layout': path.resolve(__dirname, '../layout/index.js'),
'@opentiny/tiny-engine-meta-components': path.resolve(__dirname, '../meta-components/src/index.js')
}

const prodAlias = {
27 changes: 27 additions & 0 deletions packages/entry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# entry(暂定)


## metaComponents

往设计器添加 metaComponent,满足右侧属性面板属性设置器自定义组件的需求

### addMetaComponents

```javascript
addMetaComponents([
{
name: 'MetaInput',
component: MetaInput
},
{
name: 'MetaSelect',
component: MetaSelect
}
])
```

### getMetaComponent

```javascript
getMetaComponent('MetaInput')
```
1 change: 1 addition & 0 deletions packages/entry/src/index.js
Original file line number Diff line number Diff line change
@@ -16,3 +16,4 @@ export { defineEntry, callEntry, beforeCallEntry, afterCallEntry, getMergeRegist
export { getLayoutComponent } from './layoutHash'
export { default as useMessage } from './useMessage'
export { useShareState } from './useShareState'
export { getMetaComponent, addMetaComponents } from './metaComponents'
13 changes: 13 additions & 0 deletions packages/entry/src/metaComponents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const metaComponentsMap = new Map()

export const addMetaComponents = (components) => {
if (Array.isArray(components)) {
for (const { name, component } of components) {
metaComponentsMap.set(name, component)
}
}
}

export const getMetaComponent = (name) => {
return metaComponentsMap.get(name)
}
3 changes: 3 additions & 0 deletions packages/meta-components/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# TinyEngine 的 meta component 组件

右侧属性面板配置物料属性的组件。
29 changes: 29 additions & 0 deletions packages/meta-components/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@opentiny/tiny-engine-meta-components",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"module": "dist/index.js",
"type": "module",
"scripts": {
"build": "vite build",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "OpenTiny Team",
"license": "MIT",
"bugs": {
"url": "https://github.com/opentiny/tiny-engine/issues"
},
"homepage": "https://opentiny.design/tiny-engine",
"devDependencies": {
"vite": "^4.3.7"
},
"peerDependencies": {
"@opentiny/vue": "^3.14.0",
"vue": "^3.4.15"
},
"dependencies": {
"@opentiny/tiny-engine-controller": "workspace:*"
}
}
2 changes: 2 additions & 0 deletions packages/meta-components/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as MetaInput } from './meta-input'
export { default as MetaSelect } from './meta-select'
72 changes: 72 additions & 0 deletions packages/meta-components/src/meta-input/MetaInput.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<template>
<tiny-input v-model="value" :type="type" :placeholder="placeholder" :rows="rows" @update:modelValue="change">
<template #suffix>
<div v-for="item in suffixIcons" :key="item.icon">
<svg-icon v-if="item.icon" :name="item.icon" class="tiny-svg-size" @click="item.onClick.action"></svg-icon>
</div>
</template>
</tiny-input>
</template>

<script>
import { ref, watchEffect } from 'vue'
import { Input } from '@opentiny/vue'
import { useProperties } from '@opentiny/tiny-engine-controller'

export default {
name: 'MetaInput',
components: {
TinyInput: Input
},
props: {
modelValue: {
type: String
},
type: {
type: String
},
placeholder: {
type: String
},
suffixIcons: {
type: Array,
default: () => []
},
dataType: {
type: String
},
rows: {
type: Number,
default: 10
}
},
emits: ['update:modelValue'],
setup(props, { emit }) {
const value = ref(props.modelValue)

const change = (val) => {
emit('update:modelValue', props.dataType === 'Array' ? val.split(',') : val)
}

watchEffect(() => {
value.value = useProperties().translateProp(props.modelValue)
})

return {
value,
change
}
}
}
</script>

<style lang="less" scoped>
.tiny-svg-size {
margin-left: 10px;
font-size: 16px;
&:hover {
cursor: pointer;
color: var(--ti-lowcode-dialog-font-color);
}
}
</style>
6 changes: 6 additions & 0 deletions packages/meta-components/src/meta-input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import MetaInput from './MetaInput.vue'

export default {
name: 'MetaInput',
component: MetaInput
}
Loading