Skip to content

Commit

Permalink
fix(tmagic-form-runtime): 完善form-runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Jan 31, 2024
1 parent 7db8d8f commit 8d1ba22
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 29 deletions.
2 changes: 1 addition & 1 deletion runtime/tmagic-form/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.0",
"version": "1.0.2",
"name": "@tmagic/tmagic-form-runtime",
"type": "module",
"sideEffects": [
Expand Down
17 changes: 11 additions & 6 deletions runtime/tmagic-form/src/App.vue
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down
43 changes: 25 additions & 18 deletions runtime/tmagic-form/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions runtime/tmagic-form/src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}
14 changes: 10 additions & 4 deletions runtime/tmagic-form/src/useFormConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand All @@ -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: {},
Expand Down

0 comments on commit 8d1ba22

Please sign in to comment.