Skip to content

Commit 22cb6b7

Browse files
committed
feat(editor): clearEditor.actions
1 parent e45fc16 commit 22cb6b7

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

src/plugins/topbar/actions.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export {
2424
} from './generator.actions';
2525

2626
export { importFromURL } from './importUrl.actions';
27+
export { clearEditor } from './clearEditor.actions';
2728
export { importFile as handleImportFile } from './importFile.actions';
2829

2930
// Action Types:
@@ -431,29 +432,6 @@ export const saveAsYaml = ({ overrideWarning }) => async (system) => {
431432
return { data: 'ok' };
432433
};
433434

434-
export const clearEditor = () => async (system) => {
435-
const { specActions, specSelectors } = system;
436-
// todo: using an empty string will throw an apidom parser error
437-
// we will want to detect for various specs, e.g. openapi, asyncapi
438-
// so for now, we'll use a nearly blank OAS3.0.x spec
439-
const minimalSpecOas = {
440-
openapi: '3.0.2',
441-
};
442-
const editorContent = specSelectors.specStr();
443-
const languageSubType = getDefinitionLanguage({ data: editorContent });
444-
if (!editorContent || languageSubType !== 'json') {
445-
// default to yaml
446-
// JSON String -> JS object
447-
const jsContent = YAML.safeLoad(JSON.stringify(minimalSpecOas));
448-
// JS Object -> YAML string
449-
const yamlContent = YAML.safeDump(jsContent);
450-
specActions.updateSpec(yamlContent, { lineWidth: -1 });
451-
} else {
452-
specActions.updateSpec(JSON.stringify(minimalSpecOas));
453-
}
454-
return { data: 'success' };
455-
};
456-
457435
/** menu action methods:
458436
* importFromURL
459437
* saveAsYaml
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import YAML from 'js-yaml';
2+
3+
import { getDefinitionLanguage } from '../../utils/utils-converter';
4+
5+
export const clearEditor = () => async (system) => {
6+
const { specActions, specSelectors } = system;
7+
// todo: using an empty string will throw an apidom parser error
8+
// we will want to detect for various specs, e.g. openapi, asyncapi
9+
// so for now, we'll use a nearly blank OAS3.0.x spec
10+
const minimalSpecOas = {
11+
openapi: '3.0.2',
12+
};
13+
const editorContent = specSelectors.specStr();
14+
const languageSubType = getDefinitionLanguage({ data: editorContent });
15+
if (!editorContent || languageSubType !== 'json') {
16+
// default to yaml
17+
// JSON String -> JS object
18+
const jsContent = YAML.safeLoad(JSON.stringify(minimalSpecOas));
19+
// JS Object -> YAML string
20+
const yamlContent = YAML.safeDump(jsContent);
21+
specActions.updateSpec(yamlContent, { lineWidth: -1 });
22+
} else {
23+
specActions.updateSpec(JSON.stringify(minimalSpecOas));
24+
}
25+
return { data: 'success' };
26+
};
27+
28+
export default { clearEditor };

src/plugins/topbar/components/FileMenuDropdown.test.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import ImportFileDropdownItem from './ImportFileDropdownItem';
99
import * as topbarActions from '../actions';
1010

1111
ReactModal.setAppElement('*'); // suppresses modal-related test warnings.
12+
// mock es6 re-exports
13+
jest.mock('../actions');
1214

1315
describe('renders FileMenuDropdown', () => {
1416
beforeEach(() => {

0 commit comments

Comments
 (0)