Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/webgal/src/store/stageReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import cloneDeep from 'lodash/cloneDeep';
import { commandType } from '@/Core/controller/scene/sceneInterface';
import { STAGE_KEYS } from '@/Core/constants';
import { baseBlinkParam, baseFocusParam } from '@/Core/live2DCore';

// 初始化舞台数据

Expand Down Expand Up @@ -275,10 +276,12 @@ const stageSlice = createSlice({
const index = state.live2dBlink.findIndex((e) => e.target === target);
if (index < 0) {
// Add a new blink
state.live2dBlink.push({ target, blink });
const fullBlink = { ...baseBlinkParam, ...blink };
state.live2dBlink.push({ target, blink: fullBlink });
} else {
// Update the existing blink
state.live2dBlink[index].blink = blink;
const fullBlink = { ...state.live2dBlink[index].blink, ...blink };
state.live2dBlink[index].blink = fullBlink;
}
Comment on lines 277 to 285

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

这部分用于添加或更新 live2dBlink 的逻辑与 setLive2dFocus reducer (291-299行) 中的逻辑几乎完全相同。

为了提高代码的可维护性并遵循 DRY (Don't Repeat Yourself) 原则,建议将此通用逻辑提取到一个可重用的辅助函数中。这样,如果未来需要修改此逻辑,只需在一个地方进行更改。

虽然当前实现是正确的,但进行此重构将使代码库更整洁。

},
setLive2dFocus: (state, action: PayloadAction<ILive2DFocus>) => {
Expand All @@ -287,10 +290,12 @@ const stageSlice = createSlice({
const index = state.live2dFocus.findIndex((e) => e.target === target);
if (index < 0) {
// Add a new focus
state.live2dFocus.push({ target, focus });
const fullFocus = { ...baseFocusParam, ...focus };
state.live2dFocus.push({ target, focus: fullFocus });
} else {
// Update the existing focus
state.live2dFocus[index].focus = focus;
const fullFocus = { ...state.live2dFocus[index].focus, ...focus };
state.live2dFocus[index].focus = fullFocus;
}
},
replaceUIlable: (state, action: PayloadAction<[string, string]>) => {
Expand Down