refactor(editor): 抽取 serializeConfig 工具统一序列化配置

将分散在 CodeLink、CodeEditor 及 playground 中重复的 serialize-javascript
序列化逻辑收敛为 @editor/utils/editor 的 serializeConfig 并对外导出复用。
This commit is contained in:
roymondchen 2026-06-02 16:32:28 +08:00
parent 64d35d5363
commit 1b66ab1b88
3 changed files with 13 additions and 19 deletions

View File

@ -4,11 +4,11 @@
<script lang="ts" setup>
import { computed, reactive, watch } from 'vue';
import serialize from 'serialize-javascript';
import type { CodeLinkConfig, FieldProps, MLink } from '@tmagic/form';
import { getEditorConfig } from '@editor/utils/config';
import { serializeConfig } from '@editor/utils/editor';
defineOptions({
name: 'MFieldsCodeLink',
@ -47,10 +47,7 @@ watch(
() => props.model[props.name],
(value) => {
modelValue.form = {
[props.name]: serialize(value, {
space: 2,
unsafe: true,
}).replace(/"(\w+)":\s/g, '$1: '),
[props.name]: serializeConfig(value),
};
},
{

View File

@ -21,12 +21,12 @@ import { computed, nextTick, onBeforeUnmount, onMounted, onUnmounted, ref, useTe
import { FullScreen } from '@element-plus/icons-vue';
import { throttle } from 'lodash-es';
import type * as Monaco from 'monaco-editor';
import serialize from 'serialize-javascript';
import { TMagicButton } from '@tmagic/design';
import MIcon from '@editor/components/Icon.vue';
import { getEditorConfig } from '@editor/utils/config';
import { serializeConfig } from '@editor/utils/editor';
import loadMonaco from '@editor/utils/monaco-editor';
defineOptions({
@ -163,10 +163,7 @@ const toString = (v: string | any, language: string): string => {
if (language === 'json') {
value = JSON.stringify(v, null, 2);
} else {
value = serialize(v, {
space: 2,
unsafe: true,
}).replace(/"(\w+)":\s/g, '$1: ');
value = serializeConfig(v);
}
} else {
value = v;

View File

@ -48,11 +48,17 @@
<script lang="ts" setup>
import { computed, onBeforeUnmount, ref, shallowRef, toRaw } from 'vue';
import serialize from 'serialize-javascript';
import type { MApp, MContainer, MNode } from '@tmagic/core';
import type { DatasourceTypeOption } from '@tmagic/editor';
import { editorService, propsService, TMagicDialog, TMagicEditor, tMagicMessage } from '@tmagic/editor';
import {
editorService,
propsService,
serializeConfig,
TMagicDialog,
TMagicEditor,
tMagicMessage,
} from '@tmagic/editor';
import DeviceGroup from '../components/DeviceGroup.vue';
import componentGroupList from '../configs/componentGroupList';
@ -88,13 +94,7 @@ const previewUrl = computed(
const { moveableOptions } = useEditorMoveableOptions(editor);
const save = () => {
localStorage.setItem(
'magicDSL',
serialize(toRaw(value.value), {
space: 2,
unsafe: true,
}).replace(/"(\w+)":\s/g, '$1: '),
);
localStorage.setItem('magicDSL', serializeConfig(toRaw(value.value)));
editor.value?.editorService.resetModifiedNodeId();
};