|
| 1 | +import{React,useState}from"react"; |
| 2 | + |
| 3 | +importAceEditorfrom"react-ace"; |
| 4 | +import"ace-builds/src-noconflict/theme-github"; |
| 5 | +import"ace-builds/src-noconflict/ext-language_tools"; |
| 6 | + |
| 7 | +import"ace-builds/webpack-resolver"; |
| 8 | +import"ace-builds/src-noconflict/mode-yaml"; |
| 9 | + |
| 10 | +importjsYamlfrom"js-yaml"; |
| 11 | + |
| 12 | +importAjvfrom"ajv"; |
| 13 | +constajv=newAjv({allErrors: true}); |
| 14 | + |
| 15 | +importSchemafrom"../../../static/schema/schema.v2.json"; |
| 16 | + |
| 17 | +exportdefaultfunctionYamlEditor(){ |
| 18 | +const[annotations,setAnnotations]=useState([]); |
| 19 | +const[value,setValue]=useState( |
| 20 | +"# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json\n" |
| 21 | +); |
| 22 | +constvalidate=ajv.compile(Schema.definitions.schema); |
| 23 | +functiongetRowFromPath(path){ |
| 24 | +// Convert path to row number (0-based) |
| 25 | +returnpath.split("/").length-1; |
| 26 | +} |
| 27 | +functiongetLineNumber(yaml,path){ |
| 28 | +constlines=yaml.split("\n"); |
| 29 | +constpathParts=path.split("/").filter(Boolean); |
| 30 | +letcurrentObj=jsYaml.load(yaml); |
| 31 | +letlineNumber=0; |
| 32 | + |
| 33 | +for(constpartofpathParts){ |
| 34 | +for(leti=lineNumber;i<lines.length;i++){ |
| 35 | +if(lines[i].trim().startsWith(part+":")){ |
| 36 | +lineNumber=i; |
| 37 | +break; |
| 38 | +} |
| 39 | +} |
| 40 | +currentObj=currentObj[part]; |
| 41 | +} |
| 42 | + |
| 43 | +returnlineNumber; |
| 44 | +} |
| 45 | +functiononChange(newValue){ |
| 46 | +setValue(newValue); |
| 47 | +try{ |
| 48 | +constdoc=jsYaml.load(newValue,{strict: true}); |
| 49 | +constvalid=validate(doc); |
| 50 | +console.log("Validation result:",valid); |
| 51 | +console.log("Validation errors:",validate.errors); |
| 52 | + |
| 53 | +if(!valid&&validate.errors){ |
| 54 | +setAnnotations( |
| 55 | +validate.errors.map((err)=>({ |
| 56 | +row: err.instancePath |
| 57 | + ? getLineNumber(newValue,err.instancePath) |
| 58 | + : 0, |
| 59 | +column: 0, |
| 60 | +text: `${err.keyword}: ${err.message}${ |
| 61 | +err?.params?.allowedValues |
| 62 | + ? `Allowed values: ${err.params.allowedValues.join(", ")}` |
| 63 | + : "" |
| 64 | +}`, |
| 65 | +type: "error", |
| 66 | +})) |
| 67 | +); |
| 68 | +}else{ |
| 69 | +setAnnotations([]); |
| 70 | +} |
| 71 | +}catch(err){ |
| 72 | +console.error("Error:",err); |
| 73 | +console.log({ |
| 74 | +errorMessage: err.reason, |
| 75 | +column: err.mark?.column, |
| 76 | +line: err.mark?.line, |
| 77 | +}); |
| 78 | +setAnnotations([ |
| 79 | +{ |
| 80 | +row: err.instancePath ? getLineNumber(newValue,err.instancePath) : 0, |
| 81 | +column: 0, |
| 82 | +text: |
| 83 | +`${err.keyword}: ${err.message}${ |
| 84 | +err?.params?.allowedValues |
| 85 | + ? `Allowed values: ${err.params.allowedValues.join(", ")}` |
| 86 | + : "" |
| 87 | +}`||"YAML parsing error", |
| 88 | +type: "error", |
| 89 | +}, |
| 90 | +]); |
| 91 | +} |
| 92 | +} |
| 93 | +return( |
| 94 | +<AceEditor |
| 95 | +mode="yaml" |
| 96 | +theme="github" |
| 97 | +onChange={onChange} |
| 98 | +value={value} |
| 99 | +name="UNIQUE_ID_OF_DIV" |
| 100 | +editorProps={{$blockScrolling: true}} |
| 101 | +setOptions={{ |
| 102 | +useWorker: false, |
| 103 | +showPrintMargin: false, |
| 104 | +showGutter: true, |
| 105 | +}} |
| 106 | +annotations={annotations} |
| 107 | +width="100%" |
| 108 | +height="400px" |
| 109 | +/> |
| 110 | +); |
| 111 | +} |
0 commit comments