fix(editor): 优化代码逻辑

This commit is contained in:
parisma 2022-10-26 19:00:46 +08:00
parent 49c0ec9dd9
commit f8d7eaea5e

View File

@ -78,19 +78,19 @@ const selectConfig = computed(() => {
const fieldKey = ref(''); const fieldKey = ref('');
const multiple = ref(true); const multiple = ref(true);
let lastTagSnapshot = cloneDeep(props.model[props.name]) || []; let lastTagSnapshot = cloneDeep(props.model[props.name]) || [];
let shouldWatch = true;
// editorshouldWatchlastTagSnapshot // editorsetTimeoutlastTagSnapshot
watch( watch(
() => props.model[props.name], () => props.model[props.name],
(selectValue) => { (selectValue) => {
if (shouldWatch) { setTimeout(() => {
lastTagSnapshot = cloneDeep(selectValue) || []; lastTagSnapshot = cloneDeep(selectValue) || [];
} });
}, },
); );
watchEffect(async () => { watchEffect(async () => {
if (!props.model[props.name]) return;
const combineNames = await Promise.all( const combineNames = await Promise.all(
props.model[props.name].map(async (id: string) => { props.model[props.name].map(async (id: string) => {
const { name = '' } = (await services?.codeBlockService.getCodeContentById(id)) || {}; const { name = '' } = (await services?.codeBlockService.getCodeContentById(id)) || {};
@ -101,11 +101,9 @@ watchEffect(async () => {
}); });
const changeHandler = async (value: any) => { const changeHandler = async (value: any) => {
shouldWatch = false;
let codeIds = value; let codeIds = value;
if (typeof value === 'string') { if (typeof value === 'string') {
multiple.value = false; multiple.value = false;
lastTagSnapshot = [lastTagSnapshot];
codeIds = value ? [value] : []; codeIds = value ? [value] : [];
} }
await setCombineRelation(codeIds); await setCombineRelation(codeIds);
@ -124,11 +122,8 @@ const setCombineRelation = async (codeIds: string[]) => {
opFlag = codeIds.length < lastTagSnapshot.length ? CodeSelectOp.DELETE : CodeSelectOp.ADD; opFlag = codeIds.length < lastTagSnapshot.length ? CodeSelectOp.DELETE : CodeSelectOp.ADD;
diffValues = xor(codeIds, lastTagSnapshot) as string[]; diffValues = xor(codeIds, lastTagSnapshot) as string[];
} }
// //
await services?.codeBlockService.setCombineRelation(id, diffValues, opFlag, props.prop); await services?.codeBlockService.setCombineRelation(id, diffValues, opFlag, props.prop);
lastTagSnapshot = codeIds;
shouldWatch = true;
}; };
const viewHandler = async () => { const viewHandler = async () => {