Checking format to offset line number if spec string is JSON parsable. (#2467)

* Checking format to offset line number if spec string is JSON parsable.

* Cleaning up server component detection, removing unused function
This commit is contained in:
Mike Ellan 2020-08-06 14:39:37 -04:00 committed by GitHub
parent d0ac82592d
commit 55e428bbe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,6 +28,7 @@ class SpecEditorSidebar extends React.Component<Props, State> {
super(props);
this.state = {
error: '',
specContentJSON: false,
};
}
@ -45,11 +46,7 @@ class SpecEditorSidebar extends React.Component<Props, State> {
_mapPosition(itemPath: Array<any>) {
const sourceMap = new YAMLSourceMap();
const specMap = sourceMap.index(
YAML.parseDocument(this.props.apiSpec.contents, { keepCstNodes: true }),
);
const itemMappedPosition = sourceMap.lookup(itemPath, specMap);
const isServersSection = itemPath[0] === 'servers';
const { contents } = this.props.apiSpec;
const scrollPosition = {
start: {
line: 0,
@ -60,10 +57,19 @@ class SpecEditorSidebar extends React.Component<Props, State> {
col: 200,
},
};
isServersSection
? (scrollPosition.start.line = itemMappedPosition.start.line)
: (scrollPosition.start.line = itemMappedPosition.start.line - 1);
// Account for JSON (as string) line number shift
if (this.state.specContentJSON) {
scrollPosition.start.line = 1;
}
const specMap = sourceMap.index(YAML.parseDocument(contents, { keepCstNodes: true }));
const itemMappedPosition = sourceMap.lookup(itemPath, specMap);
const isServersSection = itemPath[0] === 'servers';
scrollPosition.start.line += itemMappedPosition.start.line;
if (!isServersSection) {
scrollPosition.start.line -= 1;
}
scrollPosition.end.line = scrollPosition.start.line;
this._handleScrollEditor(scrollPosition);
}
@ -71,6 +77,17 @@ class SpecEditorSidebar extends React.Component<Props, State> {
this._mapPosition(itemPath);
};
componentDidMount() {
const { contents } = this.props.apiSpec;
try {
JSON.parse(contents);
} catch (e) {
this.setState({ specContentJSON: false });
return;
}
this.setState({ specContentJSON: true });
}
render() {
const { error } = this.state;
if (error) {