jsoneditor icon indicating copy to clipboard operation
jsoneditor copied to clipboard

Autocompletion based on dynamic schema doesn't work

Open FINCoding opened this issue 1 year ago • 4 comments

Hello! I would like define suggestions depends on user input. In onChange function i edit schema and call JSONEditor.setSchema to set new schema. Validation works correct, but autocompletion doesn't work.

I use Chrome 122.0.6261.95.

FINCoding avatar Mar 04 '24 10:03 FINCoding

Auto-completion based on a JSON schema is not built-in.

Have you tried https://github.com/josdejong/jsoneditor/blob/develop/examples/26_autocomplete_by_schema.html ?

josdejong avatar Mar 06 '24 09:03 josdejong

Yes, i tried it. But my case is a little different.

  1. I define a schema and create an editor with schema.
  2. onChange event I change the schema and pass it to the editor. I expect that the new scheme will be applied, but only validation works correctly (according new schema), autocompletion does not work.

Example:

<!DOCTYPE HTML>
<html lang="en">
<head>
 <meta charset="utf-8">

 <title>JSONEditor | Auto-completion by schema</title>

<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
   <script src="../dist/jsoneditor.js"></script>

 <style type="text/css">
   body {
     width: 600px;
     font: 11pt sans-serif;
   }
   #jsoneditor {
     width: 100%;
     height: 500px;
   }

   /* custom bold styling for non-default JSON schema values */
   .jsoneditor-is-not-default {
     font-weight: bold;
   }
 </style>
</head>
<body>

<div id="jsoneditor"></div>

<script>
 var schema = {"type": "object",
			  "properties": {	
                            "step_param": { 
			        "type": "array",
                               "items": {
				  "type": "object",
				  "properties": {
				    runnable_type":{
			              "type": "string",
				       "enum": ["func", "clas"]}
				       },
						}}}};
const json = {
     
 "step_param": [
   {
     "runnable_type": "",
	   "runnable_object": ""}
 ]

};

 const options = {
   schema: schema,
   allowSchemaSuggestions: true,
	onChange: handleChange,	
   mode: 'code',
   modes: ['code']
 }

 // create the editor
 const container = document.getElementById('jsoneditor')
 const editor = new JSONEditor(container, options, json);
 
 function handleChange() {
		let json = editor.getText();
		let selection = editor.getTextSelection();
		let linesOfJson = json.split(/\r?\n/);
		if (selection.start.row == selection.end.row 
		    && selection.start.column == selection.end.column
			&& json.length != 0
			&& selection.start.row != 0			) {
			
			let line = linesOfJson[selection.start.row - 1];
			if (line.toLowerCase().includes('runnable_object')) {
                      
                      //Set new schema
			schema = {"type": "object",
			                  "properties": {	
                                           "step_param": { 
			                    "type": "array",
                                            "items": {
				                "type": "object",
				               "properties": {
				                  "runnable_type":{
					             "type": "string",
						     "enum": ["func", "clas"]},
					           "runnable_object":{
					              "type": "string",
						      "enum": ["test1", "test2"]} 
						},
						}}}
					};
				editor.setSchema(schema, true);
			}
		}
    }
</script>
</body>
</html>

FINCoding avatar Mar 06 '24 12:03 FINCoding

Auto-completion based on a JSON schema is not built-in.

Auto-completion is built in sorry for the confusion, I was mistaken.

I see what you mean. You're right, when updating the schema later, validation is correctly using the the update schema, but the auto completion isn't.

@meirotstein do you have an idea where this issue can originate? I validated that the method textmode._onSchemaChange is invoked, and that this.aceEditor.setOption('enableBasicAutocompletion' ...) is called with the new schema.

josdejong avatar Mar 06 '24 13:03 josdejong

@josdejong I will try to look into it soon

meirotstein avatar Mar 13 '24 14:03 meirotstein