fix(tmagic-form-runtime): 完善form-runtime

This commit is contained in:
roymondchen 2024-01-31 17:09:04 +08:00
parent 7db8d8f45f
commit 8d1ba220c1
5 changed files with 56 additions and 29 deletions

View File

@ -1,5 +1,5 @@
{ {
"version": "1.0.0", "version": "1.0.2",
"name": "@tmagic/tmagic-form-runtime", "name": "@tmagic/tmagic-form-runtime",
"type": "module", "type": "module",
"sideEffects": [ "sideEffects": [

View File

@ -1,20 +1,25 @@
<template> <template>
<MForm ref="mForm" :id="config?.id" :data-magic-id="config?.id" :config="formConfig" :init-values="values"></MForm> <MForm
ref="mForm"
:key="config?.id"
:id="config?.id"
:data-magic-id="config?.id"
:config="formConfig"
:init-values="values"
></MForm>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { watch } from 'vue'; import { watch } from 'vue';
import { MForm } from '@tmagic/form'; import { MForm } from '@tmagic/form';
import type StageCore from '@tmagic/stage';
import { AppProps } from './types';
import { useFormConfig } from './useFormConfig'; import { useFormConfig } from './useFormConfig';
const props = defineProps<{ const props = defineProps<AppProps>();
stage: StageCore;
}>();
const { mForm, formConfig, config, values } = useFormConfig(props.stage.renderer.contentWindow); const { mForm, formConfig, config, values } = useFormConfig(props);
watch(formConfig, async () => { watch(formConfig, async () => {
setTimeout(() => { setTimeout(() => {

View File

@ -1,4 +1,4 @@
import { createApp, onBeforeUnmount } from 'vue'; import { createApp, onBeforeUnmount, Plugin } from 'vue';
import cssStyle from 'element-plus/dist/index.css?raw'; import cssStyle from 'element-plus/dist/index.css?raw';
import { editorService, Layout, propsService, uiService } from '@tmagic/editor'; import { editorService, Layout, propsService, uiService } from '@tmagic/editor';
@ -16,13 +16,18 @@ export const propsConfigs = formConfigs;
export const canSelect = (el: HTMLElement) => Boolean(el.dataset.magicId); export const canSelect = (el: HTMLElement) => Boolean(el.dataset.magicId);
export const useRuntime = () => { export const useRuntime = ({
plugins = [],
fillConfig = (config) => config,
}: {
plugins?: Plugin[];
fillConfig?: (config: FormConfig) => FormConfig;
}) => {
const render = (stage: StageCore) => { const render = (stage: StageCore) => {
injectStyle(stage.renderer.getDocument()!, cssStyle); injectStyle(stage.renderer.getDocument()!, cssStyle);
injectStyle( injectStyle(
stage.renderer.getDocument()!, stage.renderer.getDocument()!,
` `html,
html,
body, body,
#app { #app {
width: 100%; width: 100%;
@ -39,11 +44,13 @@ export const useRuntime = () => {
el.id = 'app'; el.id = 'app';
el.style.overflow = 'auto'; el.style.overflow = 'auto';
createApp(App, { const vueApp = createApp(App, {
stage, stage,
}) fillConfig,
.use(MagicForm) });
.mount(el); vueApp.use(MagicForm);
plugins.forEach((plugin) => vueApp.use(plugin));
vueApp.mount(el);
setTimeout(() => { setTimeout(() => {
uiService.set('showRule', false); uiService.set('showRule', false);

View File

@ -0,0 +1,9 @@
import type { Ref } from 'vue';
import { type FormConfig, MForm } from '@tmagic/form';
import type StageCore from '@tmagic/stage';
export interface AppProps {
stage: StageCore;
fillConfig: (config: FormConfig, mForm: Ref<InstanceType<typeof MForm>>) => FormConfig;
}

View File

@ -3,10 +3,13 @@ import { computed, nextTick, onBeforeUnmount, reactive, ref } from 'vue';
import Core from '@tmagic/core'; import Core from '@tmagic/core';
import { type FormConfig, initValue, MForm } from '@tmagic/form'; import { type FormConfig, initValue, MForm } from '@tmagic/form';
import type { Id, MApp, MNode } from '@tmagic/schema'; import type { Id, MApp, MNode } from '@tmagic/schema';
import type { RemoveData, RuntimeWindow, UpdateData } from '@tmagic/stage'; import type { RemoveData, UpdateData } from '@tmagic/stage';
import { getNodePath, replaceChildNode } from '@tmagic/utils'; import { getNodePath, replaceChildNode } from '@tmagic/utils';
export const useFormConfig = (contentWindow: RuntimeWindow | null) => { import { AppProps } from './types';
export const useFormConfig = (props: AppProps) => {
const { contentWindow } = props.stage.renderer;
const mForm = ref<InstanceType<typeof MForm>>(); const mForm = ref<InstanceType<typeof MForm>>();
const root = ref<MApp>(); const root = ref<MApp>();
@ -17,14 +20,17 @@ export const useFormConfig = (contentWindow: RuntimeWindow | null) => {
const config = computed( const config = computed(
() => root.value?.items?.find((item: MNode) => item.id === curPageId.value) || root.value?.items?.[0], () => root.value?.items?.find((item: MNode) => item.id === curPageId.value) || root.value?.items?.[0],
); );
// @ts-ignore
const formConfig = computed(() => (config.value?.items || []) as FormConfig); const formConfig = computed(() => props.fillConfig((config.value?.items || []) as FormConfig, mForm));
const app = new Core({ const app = new Core({
ua: contentWindow?.navigator.userAgent, ua: contentWindow?.navigator.userAgent,
platform: 'editor', platform: 'editor',
}); });
// @ts-ignore
app.mForm = mForm;
const resetValues = () => { const resetValues = () => {
initValue(mForm.value?.formState, { initValue(mForm.value?.formState, {
initValues: {}, initValues: {},