YamlEditor.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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: "5px" }}
  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 }) => (props.border ? "5px" : "")};
  62. border: ${(props: { border: boolean }) =>
  63. props.border ? "1px solid #ffffff22" : ""};
  64. `;
  65. const Holder = styled.div`
  66. .ace_scrollbar {
  67. display: none;
  68. }
  69. .ace_editor,
  70. .ace_editor * {
  71. font-family: "Monaco", "Menlo", "Ubuntu Mono", "Droid Sans Mono", "Consolas",
  72. monospace !important;
  73. font-size: 12px !important;
  74. font-weight: 400 !important;
  75. letter-spacing: 0 !important;
  76. }
  77. `;