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();
addAnimate(drawFn);
onUnmounted(() => {
removeAnimate(drawFn);
});
}
}
onUnmounted(() => {
removeAnimate(drawFn);
});
onMounted(() => {
c = document.getElementById(`box-animate-${id}`) as HTMLCanvasElement;
ctx = c.getContext('2d')!;

View File

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