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
15 changes: 12 additions & 3 deletions packages/decap-cms-widget-code/src/CodeControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default class CodeControl extends React.Component {

state = {
isActive: false,
isLangInitialized: false,
unknownLang: null,
lang: '',
keyMap: localStorage.getItem(settingsPersistKeys['keyMap']) || 'default',
Expand Down Expand Up @@ -124,7 +125,15 @@ export default class CodeControl extends React.Component {
const keys = ['lang', 'theme', 'keyMap'];
const changedProps = getChangedProps(prevState, this.state, keys);
if (changedProps) {
this.handleChangeCodeMirrorProps(changedProps);
// Check if this is the initial setting of the language prop
const shouldIgnoreLangChange =
!this.state.isLangInitialized &&
!!changedProps?.lang &&
Object.keys(changedProps).length === 1;

this.setState({ isLangInitialized: true });

this.handleChangeCodeMirrorProps(changedProps, shouldIgnoreLangChange);
Comment on lines +134 to +136
Copy link
Preview

Copilot AI Oct 7, 2025

Choose a reason for hiding this comment

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

Setting isLangInitialized to true should be done after the language change is processed, not before. If handleChangeCodeMirrorProps throws an error, the flag will be incorrectly set to true even though initialization didn't complete successfully.

Suggested change
this.setState({ isLangInitialized: true });
this.handleChangeCodeMirrorProps(changedProps, shouldIgnoreLangChange);
this.handleChangeCodeMirrorProps(changedProps, shouldIgnoreLangChange);
this.setState({ isLangInitialized: true });

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I disagree with this, the ordering here is intentional for graceful failure; if for some reason handleChangeCodeMirrorProps errors we would rather falsely report changes when there are none than potentially ignore actual changes that have been made.

}
}

Expand Down Expand Up @@ -192,7 +201,7 @@ export default class CodeControl extends React.Component {
return !field.get('output_code_only') || isEditorComponent;
}

async handleChangeCodeMirrorProps(changedProps) {
async handleChangeCodeMirrorProps(changedProps, ignoreLangChange = false) {
const { onChange } = this.props;

if (changedProps.lang) {
Expand Down Expand Up @@ -222,7 +231,7 @@ export default class CodeControl extends React.Component {

// Only persist the language change if supported - requires the value to be
// a map rather than just a code string.
if (changedProps.lang && this.valueIsMap()) {
if (changedProps.lang && !ignoreLangChange && this.valueIsMap()) {
onChange(this.toValue('lang', changedProps.lang));
}
}
Expand Down
Loading