Skip to content

Commit

Permalink
fix: //issues/6812 show code widget content if initially hidden (#7131)
Browse files Browse the repository at this point in the history
Co-authored-by: Anze Demsar <anze.demsar@p-m.si>
  • Loading branch information
hip3r and demshy committed Mar 7, 2024
1 parent 0c68d20 commit b18b51b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class EditorControl extends React.Component {
isFieldDuplicate: PropTypes.func,
isFieldHidden: PropTypes.func,
locale: PropTypes.string,
isParentListCollapsed: PropTypes.bool,
};

static defaultProps = {
Expand Down Expand Up @@ -210,6 +211,7 @@ class EditorControl extends React.Component {
isFieldDuplicate,
isFieldHidden,
locale,
isParentListCollapsed,
} = this.props;

const widgetName = field.get('widget');
Expand Down Expand Up @@ -332,6 +334,7 @@ class EditorControl extends React.Component {
isFieldHidden={isFieldHidden}
isLoadingAsset={isLoadingAsset}
locale={locale}
isParentListCollapsed={isParentListCollapsed}
/>
{fieldHint && (
<ControlHint active={isSelected || this.state.styleActive} error={hasErrors}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default class Widget extends Component {
isFieldDuplicate: PropTypes.func,
isFieldHidden: PropTypes.func,
locale: PropTypes.string,
isParentListCollapsed: PropTypes.bool,
};

shouldComponentUpdate(nextProps) {
Expand Down Expand Up @@ -298,6 +299,7 @@ export default class Widget extends Component {
isFieldDuplicate,
isFieldHidden,
locale,
isParentListCollapsed,
} = this.props;

return React.createElement(controlComponent, {
Expand Down Expand Up @@ -350,6 +352,7 @@ export default class Widget extends Component {
isFieldDuplicate,
isFieldHidden,
locale,
isParentListCollapsed,
});
}
}
25 changes: 24 additions & 1 deletion packages/decap-cms-widget-code/src/CodeControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default class CodeControl extends React.Component {
forID: PropTypes.string.isRequired,
classNameWrapper: PropTypes.string.isRequired,
widget: PropTypes.object.isRequired,
isParentListCollapsed: PropTypes.bool,
};

keys = this.getKeys(this.props.field);
Expand All @@ -83,9 +84,16 @@ export default class CodeControl extends React.Component {
lastKnownValue: this.valueIsMap() ? this.props.value?.get(this.keys.code) : this.props.value,
};

visibility = {
isInvisibleOnInit: this.props.isParentListCollapsed === true,
isRefreshedAfterInvisible: false,
};

shouldComponentUpdate(nextProps, nextState) {
return (
!isEqual(this.state, nextState) || this.props.classNameWrapper !== nextProps.classNameWrapper
!isEqual(this.state, nextState) ||
this.props.classNameWrapper !== nextProps.classNameWrapper ||
(this.visibility.isInvisibleOnInit && !this.visibility.isRefreshedAfterInvisible)
);
}

Expand All @@ -97,6 +105,14 @@ export default class CodeControl extends React.Component {

componentDidUpdate(prevProps, prevState) {
this.updateCodeMirrorProps(prevState);
// when initially hidden and then shown, codeMirror content is not visible
if (
this.visibility.isInvisibleOnInit &&
!this.visibility.isRefreshedAfterInvisible &&
!this.props.listCollapsed
) {
this.refreshCodeMirrorInstance();
}
}

updateCodeMirrorProps(prevState) {
Expand All @@ -107,6 +123,13 @@ export default class CodeControl extends React.Component {
}
}

refreshCodeMirrorInstance() {
if (this.cm?.getWrapperElement().offsetHeight) {
this.cm.refresh();
this.visibility.isRefreshedAfterInvisible = true;
}
}

getLanguageByName = name => {
return languages.find(lang => lang.name === name);
};
Expand Down
3 changes: 3 additions & 0 deletions packages/decap-cms-widget-object/src/ObjectControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default class ObjectControl extends React.Component {
hasError: PropTypes.bool,
t: PropTypes.func,
locale: PropTypes.string,
collapsed: PropTypes.bool,
};

static defaultProps = {
Expand Down Expand Up @@ -87,6 +88,7 @@ export default class ObjectControl extends React.Component {
isFieldDuplicate,
isFieldHidden,
locale,
collapsed,
} = this.props;

if (field.get('widget') === 'hidden') {
Expand Down Expand Up @@ -116,6 +118,7 @@ export default class ObjectControl extends React.Component {
isFieldDuplicate={isFieldDuplicate}
isFieldHidden={isFieldHidden}
locale={locale}
isParentListCollapsed={collapsed}
/>
);
}
Expand Down

0 comments on commit b18b51b

Please sign in to comment.