@@ -4,32 +4,33 @@ import { ToggleGroup, ToggleGroupItem } from "@patternfly/react-core";
4
4
import YAML from "yaml" ;
5
5
import useResizeObserver from "use-resize-observer" ;
6
6
import Editor from "@monaco-editor/react" ;
7
+ import { detectContentType } from "@utils/content.utils.ts" ;
8
+ import { ContentTypes } from "@models/contentTypes.model.ts" ;
7
9
10
+ const TYPE_MAP : any = { } ;
11
+ TYPE_MAP [ ContentTypes . APPLICATION_PROTOBUF ] = "protobuf" ;
12
+ TYPE_MAP [ ContentTypes . APPLICATION_XML ] = "xml" ;
13
+ TYPE_MAP [ ContentTypes . APPLICATION_JSON ] = "json" ;
14
+ TYPE_MAP [ ContentTypes . APPLICATION_YAML ] = "yaml" ;
15
+ TYPE_MAP [ ContentTypes . APPLICATION_GRAPHQL ] = "graphqlschema" ;
8
16
9
- const getEditorMode = ( artifactType : string ) : string => {
10
- if ( artifactType === "PROTOBUF" ) {
11
- return "protobuf" ;
12
- }
13
- if ( artifactType === "WSDL" || artifactType === "XSD" || artifactType === "XML" ) {
14
- return "xml" ;
15
- }
16
- if ( artifactType === "GRAPHQL" ) {
17
- return "graphqlschema" ;
18
- }
19
- return "json" ;
20
- } ;
21
17
22
- const formatContent = ( artifactContent : string ) : string => {
23
- try {
24
- const pval : any = JSON . parse ( artifactContent ) ;
25
- if ( pval ) {
26
- return JSON . stringify ( pval , null , 2 ) ;
27
- }
28
- } catch ( e ) {
29
- // Do nothing
30
- }
31
- return artifactContent ;
18
+ const getEditorMode = ( artifactType : string , content : string ) : string => {
19
+ const ct : string = detectContentType ( artifactType , content ) ;
20
+ return TYPE_MAP [ ct ] ;
32
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
+ // };
33
34
34
35
35
36
/**
@@ -45,8 +46,8 @@ export type ContentTabContentProps = {
45
46
* Models the content of the Artifact Content tab.
46
47
*/
47
48
export const ContentTabContent : FunctionComponent < ContentTabContentProps > = ( props : ContentTabContentProps ) => {
48
- const [ content , setContent ] = useState ( formatContent ( props . versionContent ) ) ;
49
- const [ editorMode , setEditorMode ] = useState ( getEditorMode ( props . artifactType ) ) ;
49
+ const [ content , setContent ] = useState ( props . versionContent ) ;
50
+ const [ editorMode , setEditorMode ] = useState ( getEditorMode ( props . artifactType , props . versionContent ) ) ;
50
51
const [ compactButtons , setCompactButtons ] = useState ( false ) ;
51
52
52
53
const { ref, width = 0 , height = 0 } = useResizeObserver < HTMLDivElement > ( ) ;
@@ -55,25 +56,24 @@ export const ContentTabContent: FunctionComponent<ContentTabContentProps> = (pro
55
56
setCompactButtons ( width < 500 ) ;
56
57
} , [ width , height ] ) ;
57
58
58
- const switchJsonYaml = ( mode : string ) : ( ( ) => void ) => {
59
- return ( ) => {
60
- if ( mode === editorMode ) {
61
- return ;
62
- } else {
63
- let content : string = `Error formatting code to: ${ mode } ` ;
64
- try {
65
- if ( mode === "yaml" ) {
66
- content = YAML . stringify ( JSON . parse ( props . versionContent ) , null , 4 ) ;
67
- } else {
68
- content = JSON . stringify ( YAML . parse ( props . versionContent ) , null , 2 ) ;
69
- }
70
- } catch ( e ) {
71
- handleInvalidContentError ( e ) ;
59
+ const switchJsonYaml = ( mode : string ) : void => {
60
+ console . info ( "SWITCHING TO: " , mode ) ;
61
+ if ( mode === editorMode ) {
62
+ return ;
63
+ } else {
64
+ let content : string = `Error formatting code to: ${ mode } ` ;
65
+ try {
66
+ if ( mode === "yaml" ) {
67
+ content = YAML . stringify ( JSON . parse ( content ) , null , 4 ) ;
68
+ } else {
69
+ content = JSON . stringify ( YAML . parse ( content ) , null , 2 ) ;
72
70
}
73
- setEditorMode ( mode ) ;
74
- setContent ( content ) ;
71
+ } catch ( e ) {
72
+ handleInvalidContentError ( e ) ;
75
73
}
76
- } ;
74
+ setEditorMode ( mode ) ;
75
+ setContent ( content ) ;
76
+ }
77
77
} ;
78
78
79
79
const handleInvalidContentError = ( error : any ) : void => {
@@ -88,14 +88,14 @@ export const ContentTabContent: FunctionComponent<ContentTabContentProps> = (pro
88
88
text = "JSON"
89
89
buttonId = "json"
90
90
isSelected = { editorMode === "json" }
91
- onChange = { switchJsonYaml ( "json" ) }
91
+ onChange = { ( ) => switchJsonYaml ( "json" ) }
92
92
isDisabled = { editorMode === "json" }
93
93
/>
94
94
< ToggleGroupItem
95
95
text = "YAML"
96
96
buttonId = "yaml"
97
97
isSelected = { editorMode === "yaml" }
98
- onChange = { switchJsonYaml ( "yaml" ) }
98
+ onChange = { ( ) => switchJsonYaml ( "yaml" ) }
99
99
isDisabled = { editorMode === "yaml" }
100
100
/>
101
101
</ ToggleGroup >
0 commit comments