Skip to content

Commit

Permalink
fix ambiguous collapse behavior when array items have nested objects
Browse files Browse the repository at this point in the history
Signed-off-by: Atif Ali <atali@redhat.com>
  • Loading branch information
aali309 committed Dec 5, 2024
1 parent cdec7e2 commit 0d68999
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion ui/src/app/shared/components/yaml-editor/yaml-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ import {MonacoEditor} from '../monaco-editor';
const jsonMergePatch = require('json-merge-patch');
require('./yaml-editor.scss');

const formatYamlWithArrays = (input: any): string => {
// First, get the basic YAML
let yaml = jsYaml.dump(input, {
indent: 2,
lineWidth: -1,
noArrayIndent: false
});

// Add newline after dash for nested objects
yaml = yaml.replace(/^(\s*)-\s+(\w+):/gm, '$1-\n$1 $2:');

return yaml;
};

export class YamlEditor<T> extends React.Component<
{
input: T;
Expand All @@ -33,7 +47,7 @@ export class YamlEditor<T> extends React.Component<

public render() {
const props = this.props;
const yaml = props.input ? jsYaml.dump(props.input) : '';
const yaml = props.input ? formatYamlWithArrays(props.input) : '';

return (
<div className='yaml-editor'>
Expand Down

0 comments on commit 0d68999

Please # to comment.