Skip to content

Commit 397f5a9

Browse files
authored
Merge pull request #12052 from remotion-dev/codex/skip-unchanged-prop-saves
`@remotion/studio`: Skip saving unchanged props on blur
2 parents 5b6bd1c + 2c9890d commit 397f5a9

6 files changed

Lines changed: 84 additions & 10 deletions

File tree

‎packages/studio/src/components/ColorPicker/ColorPickerPopup.tsx‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ const ChannelInput: React.FC<ChannelInputProps> = ({
177177

178178
const clamped = Math.max(min, Math.min(max, parsed));
179179
setDraft(String(Math.round(clamped)));
180+
if (clamped === Math.round(value)) {
181+
return;
182+
}
183+
180184
onCommit(channel, clamped);
181185
}, [channel, draft, max, min, onCommit, value]);
182186

@@ -221,7 +225,12 @@ const HexInput: React.FC<{
221225
const onBlur = useCallback(() => {
222226
try {
223227
const parsed = parseAnyColor(draft);
224-
onCommit(formatRgba(parsed));
228+
const next = formatRgba(parsed);
229+
if (next === formatRgba(parseAnyColor(value))) {
230+
return;
231+
}
232+
233+
onCommit(next);
225234
} catch {
226235
setDraft(value);
227236
}

‎packages/studio/src/components/RenderModal/SchemaEditor/ZodColorEditor.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useCallback, useMemo} from 'react';
1+
import React, {useCallback, useMemo, useRef} from 'react';
22
import {ColorPicker} from '../../ColorPicker/ColorPicker';
33
import {Row, Spacing} from '../../layout';
44
import {RemotionInput} from '../../NewComposition/RemInput';
@@ -25,6 +25,7 @@ export const ZodColorEditor: React.FC<{
2525
readonly onRemove: null | (() => void);
2626
readonly mayPad: boolean;
2727
}> = ({jsonPath, value, setValue, schema, onRemove, mayPad}) => {
28+
const valueOnFocus = useRef(value);
2829
const localValue = useMemo(
2930
() => zodSafeParse(schema, value),
3031
[schema, value],
@@ -52,8 +53,17 @@ export const ZodColorEditor: React.FC<{
5253
[setValue],
5354
);
5455

56+
const onTextFocus: React.FocusEventHandler<HTMLInputElement> =
57+
useCallback(() => {
58+
valueOnFocus.current = value;
59+
}, [value]);
60+
5561
const onTextBlur: React.FocusEventHandler<HTMLInputElement> =
5662
useCallback(() => {
63+
if (valueOnFocus.current === value) {
64+
return;
65+
}
66+
5767
setValue(() => value, {shouldSave: true});
5868
}, [setValue, value]);
5969

@@ -86,6 +96,7 @@ export const ZodColorEditor: React.FC<{
8696
status={status}
8797
placeholder={jsonPath.join('.')}
8898
onChange={onTextChange}
99+
onFocus={onTextFocus}
89100
onBlur={onTextBlur}
90101
rightAlign={false}
91102
small

‎packages/studio/src/components/RenderModal/SchemaEditor/ZodDateEditor.tsx‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useMemo} from 'react';
1+
import {useMemo, useRef} from 'react';
22
import React, {useCallback} from 'react';
33
import {WHITE_ALPHA_30} from '../../../helpers/colors';
44
import {Spacing} from '../../layout';
@@ -61,6 +61,7 @@ export const ZodDateEditor: React.FC<{
6161
readonly onRemove: null | (() => void);
6262
readonly mayPad: boolean;
6363
}> = ({jsonPath, value, setValue, schema, onRemove, mayPad}) => {
64+
const valueOnFocus = useRef(value.getTime());
6465
const onChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(
6566
(e) => {
6667
// React does not support e.target.valueAsDate :(
@@ -69,9 +70,17 @@ export const ZodDateEditor: React.FC<{
6970
[setValue],
7071
);
7172

73+
const onFocus: React.FocusEventHandler<HTMLInputElement> = useCallback(() => {
74+
valueOnFocus.current = value.getTime();
75+
}, [value]);
76+
7277
const onBlur: React.FocusEventHandler<HTMLInputElement> = useCallback(() => {
78+
if (Object.is(valueOnFocus.current, value.getTime())) {
79+
return;
80+
}
81+
7382
setValue((v) => v, {shouldSave: true});
74-
}, [setValue]);
83+
}, [setValue, value]);
7584

7685
const zodValidation = useMemo(
7786
() => zodSafeParse(schema, value),
@@ -95,6 +104,7 @@ export const ZodDateEditor: React.FC<{
95104
status={zodValidation.success ? 'ok' : 'error'}
96105
placeholder={jsonPath.join('.')}
97106
onChange={onChange}
107+
onFocus={onFocus}
98108
onBlur={onBlur}
99109
style={inputStyle}
100110
rightAlign={false}

‎packages/studio/src/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useMemo} from 'react';
1+
import {useMemo, useRef} from 'react';
22
import React, {useCallback} from 'react';
33
import {InputDragger} from '../../NewComposition/InputDragger';
44
import {Fieldset} from './Fieldset';
@@ -29,23 +29,45 @@ export const ZodNumberEditor: React.FC<{
2929
readonly onRemove: null | (() => void);
3030
readonly mayPad: boolean;
3131
}> = ({jsonPath, value, schema, setValue, onRemove, mayPad}) => {
32+
const valueOnFocus = useRef(value);
33+
const hasUnsavedChanges = useRef(false);
34+
const onFocus: React.FocusEventHandler<HTMLInputElement> = useCallback(() => {
35+
if (!hasUnsavedChanges.current) {
36+
valueOnFocus.current = value;
37+
}
38+
}, [value]);
39+
3240
const onNumberChange = useCallback(
33-
(newValue: number) => {
41+
(newValue: number, source: 'input' | 'drag') => {
42+
if (source === 'drag' && !hasUnsavedChanges.current) {
43+
valueOnFocus.current = value;
44+
}
45+
46+
hasUnsavedChanges.current = valueOnFocus.current !== newValue;
3447
setValue(() => newValue, {shouldSave: false});
3548
},
36-
[setValue],
49+
[setValue, value],
3750
);
3851

3952
const onNumberChangeEnd = useCallback(
4053
(newValue: number) => {
54+
hasUnsavedChanges.current = false;
55+
if (valueOnFocus.current === newValue) {
56+
return;
57+
}
58+
59+
valueOnFocus.current = newValue;
4160
setValue(() => newValue, {shouldSave: true});
4261
},
4362
[setValue],
4463
);
4564

4665
const onTextChange = useCallback(
4766
(newValue: string) => {
48-
setValue(() => Number(newValue), {shouldSave: true});
67+
const number = Number(newValue);
68+
hasUnsavedChanges.current = false;
69+
valueOnFocus.current = number;
70+
setValue(() => number, {shouldSave: true});
4971
},
5072
[setValue],
5173
);
@@ -75,6 +97,7 @@ export const ZodNumberEditor: React.FC<{
7597
onTextChange={onTextChange}
7698
onValueChange={onNumberChange}
7799
onValueChangeEnd={onNumberChangeEnd}
100+
onFocus={onFocus}
78101
min={getZodNumberMinimum(schema)}
79102
max={getZodNumberMaximum(schema)}
80103
step={getZodNumberStep(schema)}

‎packages/studio/src/components/RenderModal/SchemaEditor/ZodStringEditor.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useMemo} from 'react';
1+
import {useMemo, useRef} from 'react';
22
import React, {useCallback} from 'react';
33
import {useZodIfPossible} from '../../get-zod-if-possible';
44
import {RemotionInput} from '../../NewComposition/RemInput';
@@ -25,6 +25,7 @@ export const ZodStringEditor: React.FC<{
2525
readonly onRemove: null | (() => void);
2626
readonly mayPad: boolean;
2727
}> = ({jsonPath, value, setValue, schema, onRemove, mayPad}) => {
28+
const valueOnFocus = useRef(value);
2829
const z = useZodIfPossible();
2930
if (!z) {
3031
throw new Error('expected zod');
@@ -42,7 +43,15 @@ export const ZodStringEditor: React.FC<{
4243
[setValue],
4344
);
4445

46+
const onFocus: React.FocusEventHandler<HTMLInputElement> = useCallback(() => {
47+
valueOnFocus.current = value;
48+
}, [value]);
49+
4550
const onBlur: React.FocusEventHandler<HTMLInputElement> = useCallback(() => {
51+
if (valueOnFocus.current === value) {
52+
return;
53+
}
54+
4655
setValue(() => value, {shouldSave: true});
4756
}, [setValue, value]);
4857

@@ -62,6 +71,7 @@ export const ZodStringEditor: React.FC<{
6271
status={zodValidation.success ? 'ok' : 'error'}
6372
placeholder={jsonPath.join('.')}
6473
onChange={onChange}
74+
onFocus={onFocus}
6575
onBlur={onBlur}
6676
rightAlign={false}
6777
name={jsonPath.join('.')}

‎packages/studio/src/components/RenderModal/SchemaEditor/ZodTextareaEditor.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useCallback} from 'react';
1+
import React, {useCallback, useRef} from 'react';
22
import {
33
useZodIfPossible,
44
useZodTypesIfPossible,
@@ -29,6 +29,7 @@ export const ZodTextareaEditor: React.FC<{
2929
readonly onRemove: null | (() => void);
3030
readonly mayPad: boolean;
3131
}> = ({jsonPath, value, setValue, schema, onRemove, mayPad}) => {
32+
const valueOnFocus = useRef(value);
3233
const z = useZodIfPossible();
3334
if (!z) {
3435
throw new Error('expected zod');
@@ -46,8 +47,17 @@ export const ZodTextareaEditor: React.FC<{
4647
[setValue],
4748
);
4849

50+
const onFocus: React.FocusEventHandler<HTMLTextAreaElement> =
51+
useCallback(() => {
52+
valueOnFocus.current = value;
53+
}, [value]);
54+
4955
const onBlur: React.FocusEventHandler<HTMLTextAreaElement> =
5056
useCallback(() => {
57+
if (valueOnFocus.current === value) {
58+
return;
59+
}
60+
5161
setValue(() => value, {shouldSave: true});
5262
}, [setValue, value]);
5363

@@ -69,6 +79,7 @@ export const ZodTextareaEditor: React.FC<{
6979
<div style={fullWidth}>
7080
<RemTextarea
7181
onChange={onChange}
82+
onFocus={onFocus}
7283
onBlur={onBlur}
7384
value={value}
7485
status={zodValidation ? 'ok' : 'error'}

0 commit comments

Comments
 (0)