mirror of
				https://github.com/Tencent/tmagic-editor.git
				synced 2025-11-04 10:49:51 +08:00 
			
		
		
		
	feat(vue-container): 支持自定义render
This commit is contained in:
		
							parent
							
								
									2114b71d47
								
							
						
					
					
						commit
						91cde30d75
					
				@ -1,5 +1,5 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  "version": "0.0.8",
 | 
					  "version": "1.0.0",
 | 
				
			||||||
  "name": "@tmagic/vue-container",
 | 
					  "name": "@tmagic/vue-container",
 | 
				
			||||||
  "type": "module",
 | 
					  "type": "module",
 | 
				
			||||||
  "main": "src/index.ts",
 | 
					  "main": "src/index.ts",
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										54
									
								
								vue-components/container/src/Component.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								vue-components/container/src/Component.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,54 @@
 | 
				
			|||||||
 | 
					import { defineComponent, h, inject, type PropType } from 'vue-demi';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import type TMagicApp from '@tmagic/core';
 | 
				
			||||||
 | 
					import { Id, IS_DSL_NODE_KEY, MComponent, toLine } from '@tmagic/core';
 | 
				
			||||||
 | 
					import { useComponent, type UserRenderFunction } from '@tmagic/vue-runtime-help';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default defineComponent({
 | 
				
			||||||
 | 
					  props: {
 | 
				
			||||||
 | 
					    config: {
 | 
				
			||||||
 | 
					      type: Object as PropType<MComponent>,
 | 
				
			||||||
 | 
					      required: true,
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    index: Number,
 | 
				
			||||||
 | 
					    iteratorIndex: {
 | 
				
			||||||
 | 
					      type: Array as PropType<number[]>,
 | 
				
			||||||
 | 
					      default: () => [],
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    iteratorContainerId: {
 | 
				
			||||||
 | 
					      type: Array as PropType<Id[]>,
 | 
				
			||||||
 | 
					      default: () => [],
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  setup(props) {
 | 
				
			||||||
 | 
					    const userRender = inject<UserRenderFunction>('userRender', ({ h, type, props, attrs, style, className }) =>
 | 
				
			||||||
 | 
					      h(type, { ...props, ...attrs, style, class: className }),
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const app = inject<TMagicApp>('app');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return () =>
 | 
				
			||||||
 | 
					      userRender({
 | 
				
			||||||
 | 
					        h,
 | 
				
			||||||
 | 
					        config: props.config,
 | 
				
			||||||
 | 
					        type: useComponent({ componentType: props.config.type, app }),
 | 
				
			||||||
 | 
					        style: app?.transformStyle(props.config.style || {}),
 | 
				
			||||||
 | 
					        className: props.config.className
 | 
				
			||||||
 | 
					          ? `${props.config.className} magic-ui-${toLine(props.config.type)}`
 | 
				
			||||||
 | 
					          : `magic-ui-${toLine(props.config.type)}`,
 | 
				
			||||||
 | 
					        props: {
 | 
				
			||||||
 | 
					          config: { ...props.config, [IS_DSL_NODE_KEY]: true },
 | 
				
			||||||
 | 
					          containerIndex: props.index,
 | 
				
			||||||
 | 
					          iteratorIndex: props.iteratorIndex,
 | 
				
			||||||
 | 
					          iteratorContainerId: props.iteratorContainerId,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        attrs: {
 | 
				
			||||||
 | 
					          'data-tmagic-id': props.config.id,
 | 
				
			||||||
 | 
					          'data-tmagic-iterator-index': props.iteratorIndex,
 | 
				
			||||||
 | 
					          'data-tmagic-iterator-container-id': props.iteratorContainerId,
 | 
				
			||||||
 | 
					          'data-container-index': props.index,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@ -2,21 +2,14 @@
 | 
				
			|||||||
  <div v-if="display(config)" :class="className" :style="style">
 | 
					  <div v-if="display(config)" :class="className" :style="style">
 | 
				
			||||||
    <slot>
 | 
					    <slot>
 | 
				
			||||||
      <template v-for="(item, index) in config.items">
 | 
					      <template v-for="(item, index) in config.items">
 | 
				
			||||||
        <component
 | 
					        <ItemComponent
 | 
				
			||||||
          v-if="display(item)"
 | 
					          v-if="display(item)"
 | 
				
			||||||
          :key="item.id"
 | 
					          :key="item.id"
 | 
				
			||||||
          :is="useComponent({ componentType: item.type, app })"
 | 
					          :config="item"
 | 
				
			||||||
          :data-tmagic-id="item.id"
 | 
					          :index="index"
 | 
				
			||||||
          :data-tmagic-iterator-index="iteratorIndex"
 | 
					 | 
				
			||||||
          :data-tmagic-iterator-container-id="iteratorContainerId"
 | 
					 | 
				
			||||||
          :data-container-index="index"
 | 
					 | 
				
			||||||
          :class="item.className ? `${item.className} magic-ui-${toLine(item.type)}` : `magic-ui-${toLine(item.type)}`"
 | 
					 | 
				
			||||||
          :style="app?.transformStyle(item.style || {})"
 | 
					 | 
				
			||||||
          :config="{ ...item, [IS_DSL_NODE_KEY]: true }"
 | 
					 | 
				
			||||||
          :container-index="index"
 | 
					 | 
				
			||||||
          :iterator-index="iteratorIndex"
 | 
					          :iterator-index="iteratorIndex"
 | 
				
			||||||
          :iterator-container-id="iteratorContainerId"
 | 
					          :iterator-container-id="iteratorContainerId"
 | 
				
			||||||
        ></component>
 | 
					        ></ItemComponent>
 | 
				
			||||||
      </template>
 | 
					      </template>
 | 
				
			||||||
    </slot>
 | 
					    </slot>
 | 
				
			||||||
  </div>
 | 
					  </div>
 | 
				
			||||||
@ -26,8 +19,10 @@
 | 
				
			|||||||
import { computed, defineComponent, type PropType } from 'vue-demi';
 | 
					import { computed, defineComponent, type PropType } from 'vue-demi';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import type { Id, MContainer } from '@tmagic/core';
 | 
					import type { Id, MContainer } from '@tmagic/core';
 | 
				
			||||||
import { IS_DSL_NODE_KEY, toLine } from '@tmagic/core';
 | 
					import { IS_DSL_NODE_KEY } from '@tmagic/core';
 | 
				
			||||||
import { useApp, useComponent } from '@tmagic/vue-runtime-help';
 | 
					import { useApp } from '@tmagic/vue-runtime-help';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import ItemComponent from './Component';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface ContainerSchema extends Omit<MContainer, 'id'> {
 | 
					interface ContainerSchema extends Omit<MContainer, 'id'> {
 | 
				
			||||||
  id?: Id;
 | 
					  id?: Id;
 | 
				
			||||||
@ -35,19 +30,29 @@ interface ContainerSchema extends Omit<MContainer, 'id'> {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default defineComponent({
 | 
					export default defineComponent({
 | 
				
			||||||
 | 
					  name: 'tmagic-container',
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  props: {
 | 
					  props: {
 | 
				
			||||||
    config: {
 | 
					    config: {
 | 
				
			||||||
      type: Object as PropType<ContainerSchema>,
 | 
					      type: Object as PropType<ContainerSchema>,
 | 
				
			||||||
      required: true,
 | 
					      required: true,
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    iteratorIndex: Array as PropType<number[]>,
 | 
					    iteratorIndex: {
 | 
				
			||||||
    iteratorContainerId: Array as PropType<Id[]>,
 | 
					      type: Array as PropType<number[]>,
 | 
				
			||||||
 | 
					      default: () => [],
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    iteratorContainerId: {
 | 
				
			||||||
 | 
					      type: Array as PropType<Id[]>,
 | 
				
			||||||
 | 
					      default: () => [],
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
    model: {
 | 
					    model: {
 | 
				
			||||||
      type: Object,
 | 
					      type: Object,
 | 
				
			||||||
      default: () => ({}),
 | 
					      default: () => ({}),
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  components: { ItemComponent },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  setup(props) {
 | 
					  setup(props) {
 | 
				
			||||||
    const { display, app } = useApp({
 | 
					    const { display, app } = useApp({
 | 
				
			||||||
      config: props.config,
 | 
					      config: props.config,
 | 
				
			||||||
@ -78,11 +83,8 @@ export default defineComponent({
 | 
				
			|||||||
      app,
 | 
					      app,
 | 
				
			||||||
      className,
 | 
					      className,
 | 
				
			||||||
      style,
 | 
					      style,
 | 
				
			||||||
      IS_DSL_NODE_KEY,
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      display,
 | 
					      display,
 | 
				
			||||||
      toLine,
 | 
					 | 
				
			||||||
      useComponent,
 | 
					 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user