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",
"type": "module",
"sideEffects": [

View File

@ -1,20 +1,25 @@
<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>
<script setup lang="ts">
import { watch } from 'vue';
import { MForm } from '@tmagic/form';
import type StageCore from '@tmagic/stage';
import { AppProps } from './types';
import { useFormConfig } from './useFormConfig';
const props = defineProps<{
stage: StageCore;
}>();
const props = defineProps<AppProps>();
const { mForm, formConfig, config, values } = useFormConfig(props.stage.renderer.contentWindow);
const { mForm, formConfig, config, values } = useFormConfig(props);
watch(formConfig, async () => {
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 { editorService, Layout, propsService, uiService } from '@tmagic/editor';
@ -16,34 +16,41 @@ export const propsConfigs = formConfigs;
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) => {
injectStyle(stage.renderer.getDocument()!, cssStyle);
injectStyle(
stage.renderer.getDocument()!,
`
html,
body,
#app {
width: 100%;
height: 100%;
margin: 0;
}
::-webkit-scrollbar {
width: 0;
}
`,
`html,
body,
#app {
width: 100%;
height: 100%;
margin: 0;
}
::-webkit-scrollbar {
width: 0;
}
`,
);
const el: HTMLDivElement = globalThis.document.createElement('div');
el.id = 'app';
el.style.overflow = 'auto';
createApp(App, {
const vueApp = createApp(App, {
stage,
})
.use(MagicForm)
.mount(el);
fillConfig,
});
vueApp.use(MagicForm);
plugins.forEach((plugin) => vueApp.use(plugin));
vueApp.mount(el);
setTimeout(() => {
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 { type FormConfig, initValue, MForm } from '@tmagic/form';
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';
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 root = ref<MApp>();
@ -17,14 +20,17 @@ export const useFormConfig = (contentWindow: RuntimeWindow | null) => {
const config = computed(
() => root.value?.items?.find((item: MNode) => item.id === curPageId.value) || root.value?.items?.[0],
);
const formConfig = computed(() => (config.value?.items || []) as FormConfig);
// @ts-ignore
const formConfig = computed(() => props.fillConfig((config.value?.items || []) as FormConfig, mForm));
const app = new Core({
ua: contentWindow?.navigator.userAgent,
platform: 'editor',
});
// @ts-ignore
app.mForm = mForm;
const resetValues = () => {
initValue(mForm.value?.formState, {
initValues: {},