feat: defineProps & defineEmits

This commit is contained in:
unanmed 2024-02-03 16:07:47 +08:00
parent 09118b162c
commit 72f8af1a1c
2 changed files with 76 additions and 32 deletions

View File

@ -63,13 +63,13 @@ function draw() {
drawFn(); drawFn();
addAnimate(drawFn); addAnimate(drawFn);
onUnmounted(() => {
removeAnimate(drawFn);
});
} }
} }
onUnmounted(() => {
removeAnimate(drawFn);
});
onMounted(() => { onMounted(() => {
c = document.getElementById(`box-animate-${id}`) as HTMLCanvasElement; c = document.getElementById(`box-animate-${id}`) as HTMLCanvasElement;
ctx = c.getContext('2d')!; ctx = c.getContext('2d')!;

View File

@ -2,12 +2,16 @@ import {
Component, Component,
RenderFunction, RenderFunction,
SetupContext, SetupContext,
SlotsType,
VNode, VNode,
VNodeChild, VNodeChild,
defineComponent, defineComponent,
h, h,
onMounted onMounted
} from 'vue'; } from 'vue';
import BoxAnimate from '@/components/boxAnimate.vue';
import { mainUi } from './init/ui';
import { GameUi } from './custom/ui';
interface VForRenderer { interface VForRenderer {
type: '@v-for'; type: '@v-for';
@ -54,22 +58,32 @@ export class MComponent {
static mountNum: number = 0; static mountNum: number = 0;
content: (MotaComponent | VForRenderer)[] = []; content: (MotaComponent | VForRenderer)[] = [];
/** 渲染插槽 */
slots: Record<string, Record<string, any>> = {};
private onSetupFn?: OnSetupFunction; private onSetupFn?: OnSetupFunction;
private setupFn?: SetupFunction; private setupFn?: SetupFunction;
private onMountedFn?: OnMountedFunction; private onMountedFn?: OnMountedFunction;
private retFn?: RetFunction; private retFn?: RetFunction;
private propsDef: Record<string, any> = {};
private emitsDef: string[] = [];
/** /**
* props可选props传入被渲染的组件 * propsprops名称例如num: Number
* @param name * `UiController.open`ui
* @param props props * - num: ui的唯一标识符Number
* - ui: 对于的GameUi实例GameUi
* @param props props
*/ */
slot(name: string, props?: Record<string, any>): this { defineProps(props: Record<string, any>) {
this.slots[name] = props ?? {}; this.propsDef = props;
return this; }
/**
* emitsemits的名称
* @param emits emits
*/
defineEmits(emits: string[]) {
this.emitsDef = emits;
} }
/** /**
@ -239,29 +253,40 @@ export class MComponent {
*/ */
export() { export() {
if (!this.setupFn) { if (!this.setupFn) {
return defineComponent((props, ctx) => { return defineComponent(
const mountNum = MComponent.mountNum++; (props, ctx) => {
this.onSetupFn?.(props); const mountNum = MComponent.mountNum++;
this.onSetupFn?.(props);
onMounted(() => { onMounted(() => {
this.onMountedFn?.( this.onMountedFn?.(
props, props,
Array.from( Array.from(
document.getElementsByClassName( document.getElementsByClassName(
`--mota-component-canvas-${mountNum}` `--mota-component-canvas-${mountNum}`
) as HTMLCollectionOf<HTMLCanvasElement> ) as HTMLCollectionOf<HTMLCanvasElement>
) )
); );
}); });
if (this.retFn) return () => this.retFn!(props, ctx); if (this.retFn) return () => this.retFn!(props, ctx);
else { else {
return () => { return () => {
const vNodes = MComponent.vNode(this.content, mountNum); console.log(ctx.slots.default);
return vNodes;
}; const vNodes = MComponent.vNode(
this.content,
mountNum
);
return vNodes;
};
}
},
{
emits: this.emitsDef,
props: this.propsDef
} }
}); );
} else { } else {
return defineComponent((props, ctx) => this.setupFn!(props, ctx)); return defineComponent((props, ctx) => this.setupFn!(props, ctx));
} }
@ -395,3 +420,22 @@ export class MComponent {
export function m() { export function m() {
return new MComponent(); return new MComponent();
} }
/**
* VNode
* @param id id
* @param width
* @param height
* @param noBoarder
*/
export function icon(
id: AllIds,
width?: number,
height?: number,
noBoarder?: number
) {
return h(BoxAnimate, { id, width, height, noBoarder });
}
mainUi.register(new GameUi('test', m().export()));
mainUi.open('test');