YamlEditor.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import React, { Component } from 'react';
  2. import styled from 'styled-components';
  3. import AceEditor from 'react-ace';
  4. import 'ace-builds/src-noconflict/mode-yaml';
  5. import 'ace-builds/src-noconflict/theme-terminal';
  6. type PropsType = {
  7. value: string,
  8. onChange: (e: any) => void,
  9. }
  10. type StateType = {
  11. }
  12. class YamlEditor extends Component<PropsType, StateType> {
  13. // Uses the yaml-lint library to determine if a given string is valid yaml.
  14. // If the code is invalid, it returns an error message detailing what went wrong.
  15. checkYaml = () => {
  16. /*
  17. yamlLint.lint(y).then(() => {
  18. alert('Valid YAML file.');
  19. }).catch((error) => {
  20. alert(error.message);
  21. });
  22. */
  23. }
  24. // Calls checkYaml and passes in the value from the textarea
  25. handleChange = (e: any) => {
  26. this.setState({ yaml: e });
  27. }
  28. handleSubmit = (e: any) => {
  29. this.checkYaml();
  30. e.preventDefault();
  31. }
  32. render() {
  33. return (
  34. <Holder>
  35. <Editor onSubmit={this.handleSubmit}>
  36. <AceEditor
  37. mode='yaml'
  38. value={this.props.value}
  39. theme='terminal'
  40. onChange={this.props.onChange}
  41. name='codeEditor'
  42. editorProps={{ $blockScrolling: true }}
  43. width='100%'
  44. style={{ borderRadius: '5px' }}
  45. />
  46. </Editor>
  47. </Holder>
  48. );
  49. }
  50. }
  51. export default YamlEditor;
  52. const Editor = styled.form`
  53. margin-top: 0px;
  54. margin-bottom: 12px;
  55. width: calc(100% - 0px);
  56. border-radius: 5px;
  57. border: 1px solid #ffffff22;
  58. height: 295px;
  59. overflow: auto;
  60. `;
  61. const Holder = styled.div`
  62. `;