Skip to content

Commit cae030b

Browse files
authored
Merge pull request #243 from taichushouwang/dev
fix: 修复编辑动作时,会出现数据丢失的问题
2 parents ded34d8 + cad4088 commit cae030b

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

src/components/editor/action/EditorActionAdd.tsx

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface EditorActionAddProps {
4545
setError: UseFormSetError<CopilotDocV1.Action>,
4646
) => boolean
4747
onCancel: () => void
48-
action?: CopilotDocV1.Action
48+
editingAction?: CopilotDocV1.Action
4949
}
5050

5151
const defaultAction: DeepPartial<CopilotDocV1.Action> = {
@@ -59,11 +59,11 @@ const defaultMoveCameraAction: DeepPartial<CopilotDocV1.ActionMoveCamera> = {
5959

6060
export const EditorActionAdd = ({
6161
control: operationControl,
62-
action,
62+
editingAction,
6363
onSubmit: _onSubmit,
6464
onCancel,
6565
}: EditorActionAddProps) => {
66-
const isNew = !action
66+
const isNew = !editingAction
6767
const operatorGroups = useWatch({ control: operationControl, name: 'groups' })
6868

6969
const {
@@ -91,21 +91,73 @@ export const EditorActionAdd = ({
9191
...defaultAction,
9292
// to prevent layout jumping, we persist the action type on reset
9393
type,
94-
9594
...(type === 'MoveCamera' ? defaultMoveCameraAction : null),
9695
}
9796

9897
useEffect(() => {
99-
reset(action || resettingValues)
100-
}, [reset, action])
98+
if (editingAction) {
99+
if ('type' in editingAction) {
100+
setValue('type', editingAction.type)
101+
}
102+
// 修复切换type的时候,数据丢失的问题
103+
// 原因:因为切换type的时候会触发页面绘制,导致form和对应的item组件丢失绑定,
104+
// 当重置时没办法正常清空item组件内部的值。
105+
// 放入setTimeout中,延迟赋值,就可以避免丢失绑定的问题
106+
setTimeout(() => {
107+
// 修复先点击“部署”动作的编辑按钮,再连续点击“二倍速”动作的编辑按钮,“部署”的数据丢失
108+
// 原因:通过reset方式赋值给form,相当于将action变量跟form绑定,
109+
// 当再通过reset(undefined)后,会将action的值置为null,
110+
// 通过setValue的方式,不会将action和form绑定
111+
if ('doc' in editingAction) {
112+
setValue('doc', editingAction.doc)
113+
}
114+
if ('docColor' in editingAction) {
115+
setValue('docColor', editingAction.docColor)
116+
}
117+
if ('costs' in editingAction) {
118+
setValue('costs', editingAction.costs)
119+
}
120+
if ('costChanges' in editingAction) {
121+
setValue('costChanges', editingAction.costChanges)
122+
}
123+
if ('kills' in editingAction) {
124+
setValue('kills', editingAction.kills)
125+
}
126+
if ('cooling' in editingAction) {
127+
setValue('cooling', editingAction.cooling)
128+
}
129+
if ('preDelay' in editingAction) {
130+
setValue('preDelay', editingAction.preDelay)
131+
}
132+
if ('rearDelay' in editingAction) {
133+
setValue('rearDelay', editingAction.rearDelay)
134+
}
135+
if ('name' in editingAction) {
136+
setValue('name', editingAction.name)
137+
}
138+
if ('direction' in editingAction) {
139+
setValue('direction', editingAction.direction)
140+
}
141+
if ('location' in editingAction) {
142+
setValue('location', editingAction.location)
143+
}
144+
if ('skillUsage' in editingAction) {
145+
setValue('skillUsage', editingAction.skillUsage)
146+
}
147+
if ('distance' in editingAction) {
148+
setValue('distance', editingAction.distance)
149+
}
150+
}, 0)
151+
} else {
152+
reset(resettingValues)
153+
}
154+
}, [editingAction])
101155

102156
useEffect(() => {
103-
// fill in default values for MoveCamera
104-
if (type === 'MoveCamera' && !action) {
105-
// looks like the passed object will be mutated somehow
106-
reset({ ...defaultMoveCameraAction })
157+
if (type === 'MoveCamera') {
158+
reset(resettingValues)
107159
}
108-
}, [reset, action, type])
160+
}, [type])
109161

110162
useEffect(() => {
111163
setValue(

src/components/editor/action/EditorActionDocColor.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export const EditorActionDocColor = ({
6060
onItemSelect={(item) => {
6161
onChange(item.value)
6262
}}
63+
resetOnSelect={true}
6364
>
6465
<Button rightIcon="double-caret-vertical" onBlur={onBlur} ref={ref}>
6566
<Icon icon="full-circle" color={selected?.value} />

src/components/editor/action/EditorActions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export const EditorActions = ({ control }: EditorActionsProps) => {
9898
<div className="md:w-1/2 md:mr-8 w-full">
9999
<EditorActionAdd
100100
control={control}
101-
action={editingAction}
101+
editingAction={editingAction}
102102
onSubmit={onSubmit}
103103
onCancel={() => setEditingAction(undefined)}
104104
/>

0 commit comments

Comments
 (0)