Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocompletion based on dynamic schema doesn't work #1579

Open
FINCoding opened this issue Mar 4, 2024 · 4 comments
Open

Autocompletion based on dynamic schema doesn't work #1579

FINCoding opened this issue Mar 4, 2024 · 4 comments

Comments

@FINCoding
Copy link

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.

@josdejong
Copy link
Owner

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 ?

@FINCoding
Copy link
Author

FINCoding commented Mar 6, 2024

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>

@josdejong
Copy link
Owner

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.

@meirotstein
Copy link
Contributor

@josdejong I will try to look into it soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants