@@ -19,18 +19,18 @@ const getEditorMode = (artifactType: string, content: string): string => {
19
19
const ct : string = detectContentType ( artifactType , content ) ;
20
20
return TYPE_MAP [ ct ] ;
21
21
} ;
22
- //
23
- // const formatContent = (artifactContent: string): string => {
24
- // try {
25
- // const pval: any = JSON.parse(artifactContent);
26
- // if (pval) {
27
- // return JSON.stringify(pval, null, 2);
28
- // }
29
- // } catch (e) {
30
- // // Do nothing
31
- // }
32
- // return artifactContent;
33
- // };
22
+
23
+ const formatJsonContent = ( artifactContent : string ) : string => {
24
+ try {
25
+ const pval : any = JSON . parse ( artifactContent ) ;
26
+ if ( pval ) {
27
+ return JSON . stringify ( pval , null , 2 ) ;
28
+ }
29
+ } catch ( e ) {
30
+ // Do nothing
31
+ }
32
+ return artifactContent ;
33
+ } ;
34
34
35
35
36
36
/**
@@ -46,8 +46,11 @@ export type ContentTabContentProps = {
46
46
* Models the content of the Artifact Content tab.
47
47
*/
48
48
export const ContentTabContent : FunctionComponent < ContentTabContentProps > = ( props : ContentTabContentProps ) => {
49
- const [ content , setContent ] = useState ( props . versionContent ) ;
50
- const [ editorMode , setEditorMode ] = useState ( getEditorMode ( props . artifactType , props . versionContent ) ) ;
49
+ const em : string = getEditorMode ( props . artifactType , props . versionContent ) ;
50
+ const fc : string = em === "json" ? formatJsonContent ( props . versionContent ) : props . versionContent ;
51
+
52
+ const [ content , setContent ] = useState ( fc ) ;
53
+ const [ editorMode , setEditorMode ] = useState ( em ) ;
51
54
const [ compactButtons , setCompactButtons ] = useState ( false ) ;
52
55
53
56
const { ref, width = 0 , height = 0 } = useResizeObserver < HTMLDivElement > ( ) ;
@@ -61,18 +64,20 @@ export const ContentTabContent: FunctionComponent<ContentTabContentProps> = (pro
61
64
if ( mode === editorMode ) {
62
65
return ;
63
66
} else {
64
- let content : string = `Error formatting code to: ${ mode } ` ;
67
+ let newContent : string = `Error formatting code to: ${ mode } ` ;
65
68
try {
66
69
if ( mode === "yaml" ) {
67
- content = YAML . stringify ( JSON . parse ( content ) , null , 4 ) ;
70
+ newContent = YAML . stringify ( JSON . parse ( content ) , null , 4 ) ;
71
+ console . info ( "NEW CONTENT (yaml): " , newContent ) ;
68
72
} else {
69
- content = JSON . stringify ( YAML . parse ( content ) , null , 2 ) ;
73
+ newContent = JSON . stringify ( YAML . parse ( content ) , null , 2 ) ;
74
+ console . info ( "NEW CONTENT (json): " , newContent ) ;
70
75
}
71
76
} catch ( e ) {
72
77
handleInvalidContentError ( e ) ;
73
78
}
74
79
setEditorMode ( mode ) ;
75
- setContent ( content ) ;
80
+ setContent ( newContent ) ;
76
81
}
77
82
} ;
78
83
0 commit comments