fix: 使用form.initValues解决代码块绑定关系更新不及时的问题

This commit is contained in:
parisma 2022-10-28 14:58:30 +08:00
parent f8d7eaea5e
commit 0ca0abf2da

View File

@ -33,16 +33,16 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, defineEmits, defineProps, inject, ref, watch, watchEffect } from 'vue'; import { computed, defineEmits, defineProps, inject, ref, watchEffect } from 'vue';
import { cloneDeep, map, xor } from 'lodash-es'; import { map, xor } from 'lodash-es';
import { TMagicCard, tMagicMessage, TMagicTooltip } from '@tmagic/design'; import { TMagicCard, tMagicMessage, TMagicTooltip } from '@tmagic/design';
import { SelectConfig } from '@tmagic/form'; import { FormState, SelectConfig } from '@tmagic/form';
import type { Services } from '../type'; import type { Services } from '../type';
import { CodeEditorMode, CodeSelectOp } from '../type'; import { CodeEditorMode, CodeSelectOp } from '../type';
const services = inject<Services>('services'); const services = inject<Services>('services');
const form = inject<FormState>('mForm');
const emit = defineEmits(['change']); const emit = defineEmits(['change']);
const props = defineProps<{ const props = defineProps<{
@ -77,17 +77,7 @@ const selectConfig = computed(() => {
}); });
const fieldKey = ref(''); const fieldKey = ref('');
const multiple = ref(true); const multiple = ref(true);
let lastTagSnapshot = cloneDeep(props.model[props.name]) || []; const lastTagSnapshot = ref<string[]>([]);
// editorsetTimeoutlastTagSnapshot
watch(
() => props.model[props.name],
(selectValue) => {
setTimeout(() => {
lastTagSnapshot = cloneDeep(selectValue) || [];
});
},
);
watchEffect(async () => { watchEffect(async () => {
if (!props.model[props.name]) return; if (!props.model[props.name]) return;
@ -119,8 +109,10 @@ const setCombineRelation = async (codeIds: string[]) => {
let opFlag = CodeSelectOp.CHANGE; let opFlag = CodeSelectOp.CHANGE;
let diffValues = codeIds; let diffValues = codeIds;
if (multiple.value) { if (multiple.value) {
opFlag = codeIds.length < lastTagSnapshot.length ? CodeSelectOp.DELETE : CodeSelectOp.ADD; // initValuesinitValues
diffValues = xor(codeIds, lastTagSnapshot) as string[]; lastTagSnapshot.value = form?.initValues[props.name] || [];
opFlag = codeIds.length < lastTagSnapshot.value.length ? CodeSelectOp.DELETE : CodeSelectOp.ADD;
diffValues = xor(codeIds, lastTagSnapshot.value) as string[];
} }
// //
await services?.codeBlockService.setCombineRelation(id, diffValues, opFlag, props.prop); await services?.codeBlockService.setCombineRelation(id, diffValues, opFlag, props.prop);