diff --git a/src/plugins/editor-content-type/actions.js b/src/plugins/editor-content-type/actions.js index a58c0338123..ae9ac460676 100644 --- a/src/plugins/editor-content-type/actions.js +++ b/src/plugins/editor-content-type/actions.js @@ -100,6 +100,28 @@ export const detectContentType = (content) => { return editorActions.detectContentTypeSuccess({ contentType, content, requestId }); } + const asyncApi3JSONMatch = content.match( + /"asyncapi"\s*:\s*"(?3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))"/ + ); + if (asyncApi3JSONMatch !== null && fn.isValidJSONObject(content)) { + const { groups } = asyncApi3JSONMatch; + const version = groups?.version_json; + const contentType = `application/vnd.aai.asyncapi+json;version=${version}`; + + return editorActions.detectContentTypeSuccess({ contentType, content, requestId }); + } + + const asyncApi3YAMLMatch = content.match( + /(?^(["']?)asyncapi\2\s*:\s*(["']?)(?3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))\3(?:\s+|$))|(?"asyncapi"\s*:\s*"(?3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))")/m + ); + if (asyncApi3YAMLMatch !== null && fn.isValidYAMLObject(content)) { + const { groups } = asyncApi3YAMLMatch; + const version = groups?.version_json ?? groups?.version_yaml; + const contentType = `application/vnd.aai.asyncapi+yaml;version=${version}`; + + return editorActions.detectContentTypeSuccess({ contentType, content, requestId }); + } + const openApi30xJSONMatch = content.match(detectionRegExpOpenAPIJSON30x); if (openApi30xJSONMatch !== null && fn.isValidJSONObject(content)) { const { groups } = openApi30xJSONMatch; diff --git a/src/plugins/editor-preview-asyncapi/monaco.contribution.js b/src/plugins/editor-preview-asyncapi/monaco.contribution.js index 619e0f12967..64318dcdda1 100644 --- a/src/plugins/editor-preview-asyncapi/monaco.contribution.js +++ b/src/plugins/editor-preview-asyncapi/monaco.contribution.js @@ -16,7 +16,7 @@ const lazyMonacoContribution = ({ system }) => { const modelValue = model.getValue(); const modelVersionId = model.getVersionId(); const conditionFn = () => - editorSelectors.selectIsContentTypeAsyncAPI2() && + editorSelectors.selectIsContentTypeAsyncAPI() && modelValue === editorSelectors.selectContent() && !editorPreviewAsyncAPISelectors.selectIsParseInProgress(); diff --git a/src/plugins/editor-preview-asyncapi/wrap-actions.js b/src/plugins/editor-preview-asyncapi/wrap-actions.js index 63feba206b6..467cd5ae2fe 100644 --- a/src/plugins/editor-preview-asyncapi/wrap-actions.js +++ b/src/plugins/editor-preview-asyncapi/wrap-actions.js @@ -6,7 +6,7 @@ export const detectContentTypeSuccess = createSafeActionWrapper( ({ content }) => { const { editorSelectors, editorPreviewAsyncAPIActions } = system; - if (editorSelectors.selectIsContentTypeAsyncAPI2()) { + if (editorSelectors.selectIsContentTypeAsyncAPI()) { editorPreviewAsyncAPIActions.parse(content); } } diff --git a/src/plugins/editor-preview-asyncapi/wrap-components/EditorPreviewWrapper.jsx b/src/plugins/editor-preview-asyncapi/wrap-components/EditorPreviewWrapper.jsx index 25e2e48ac1d..9af02a0c9dc 100644 --- a/src/plugins/editor-preview-asyncapi/wrap-components/EditorPreviewWrapper.jsx +++ b/src/plugins/editor-preview-asyncapi/wrap-components/EditorPreviewWrapper.jsx @@ -4,18 +4,19 @@ import PropTypes from 'prop-types'; const EditorPreviewWrapper = (Original, system) => { const EditorPreview = ({ getComponent, editorSelectors }) => { const EditorPreviewAsyncAPI = getComponent('EditorPreviewAsyncAPI', true); + const isAsyncAPI = editorSelectors.selectIsContentTypeAsyncAPI(); - return editorSelectors.selectIsContentTypeAsyncAPI2() ? ( - - ) : ( - // eslint-disable-line react/jsx-props-no-spreading - ); + if (isAsyncAPI) { + return ; + } + + return ; // eslint-disable-line react/jsx-props-no-spreading }; EditorPreview.propTypes = { getComponent: PropTypes.func.isRequired, editorSelectors: PropTypes.shape({ - selectIsContentTypeAsyncAPI2: PropTypes.func.isRequired, + selectIsContentTypeAsyncAPI: PropTypes.func.isRequired, }).isRequired, };