YamlEditor.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. import AceEditor from "react-ace";
  4. import "shared/ace-porter-theme";
  5. import "ace-builds/src-noconflict/mode-yaml";
  6. type PropsType = {
  7. value: string;
  8. onChange?: (e: any) => void; // Might be read-only
  9. height?: string;
  10. border?: boolean;
  11. readOnly?: boolean;
  12. };
  13. type StateType = {};
  14. class YamlEditor extends Component<PropsType, StateType> {
  15. // Uses the yaml-lint library to determine if a given string is valid yaml.
  16. // If the code is invalid, it returns an error message detailing what went wrong.
  17. checkYaml = () => {
  18. /*
  19. yamlLint.lint(y).then(() => {
  20. alert('Valid YAML file.');
  21. }).catch((error) => {
  22. alert(error.message);
  23. });
  24. */
  25. };
  26. // Calls checkYaml and passes in the value from the textarea
  27. handleChange = (e: any) => {
  28. this.setState({ yaml: e });
  29. };
  30. handleSubmit = (e: any) => {
  31. this.checkYaml();
  32. e.preventDefault();
  33. };
  34. render() {
  35. return (
  36. <Holder>
  37. <Editor onSubmit={this.handleSubmit} border={this.props.border}>
  38. <AceEditor
  39. mode="yaml"
  40. value={this.props.value}
  41. theme="porter"
  42. onChange={this.props.onChange}
  43. name="codeEditor"
  44. readOnly={this.props.readOnly}
  45. editorProps={{ $blockScrolling: true }}
  46. height={this.props.height}
  47. width="100%"
  48. style={{ borderRadius: "10px" }}
  49. showPrintMargin={false}
  50. showGutter={true}
  51. highlightActiveLine={true}
  52. fontSize={14}
  53. />
  54. </Editor>
  55. </Holder>
  56. );
  57. }
  58. }
  59. export default YamlEditor;
  60. const Editor = styled.form`
  61. border-radius: ${(props: { border: boolean }) =>
  62. props.border ? "10px" : ""};
  63. border: ${(props: { border: boolean }) =>
  64. props.border ? "1px solid #ffffff33" : ""};
  65. `;
  66. const Holder = styled.div`
  67. .ace_scrollbar {
  68. display: none;
  69. }
  70. .ace_editor,
  71. .ace_editor * {
  72. font-family: "Monaco", "Menlo", "Ubuntu Mono", "Droid Sans Mono", "Consolas",
  73. monospace !important;
  74. font-size: 12px !important;
  75. font-weight: 400 !important;
  76. letter-spacing: 0 !important;
  77. }
  78. `;