ngx-monaco-editor icon indicating copy to clipboard operation
ngx-monaco-editor copied to clipboard

How can I format my json on init ? getAction(id).run is not work

Open CoderPoet opened this issue 6 years ago • 7 comments

getAction(id) the api is not work

image

CoderPoet avatar May 23 '18 09:05 CoderPoet

image

this is supported actions.There is no foramt

CoderPoet avatar May 23 '18 15:05 CoderPoet

You have two ways to do this:

  1. In init, you can pass your JSON object to JSON.stringify method and set the indentation. Ex: const jsonObj= { key: value }; const content = JSON.stringify(jsonObj, undefined, 4); Set this content on editor. You will see your content formatted.

  2. Trigger following command:

               editor.trigger('format', 'editor.action.format');
    

viccsjain avatar May 25 '18 06:05 viccsjain

@viccsjain Thank you!!

CoderPoet avatar May 25 '18 13:05 CoderPoet

@viccsjain The first way is ok~ But the second way is not work editor.trigger('format', 'editor.action.format');

CoderPoet avatar May 25 '18 13:05 CoderPoet

the editor.trigger('format', 'editor.action.format'); not work

ofirrifo avatar Jul 12 '20 12:07 ofirrifo

I'm having the same issue. Neither solution works for me

onEditorInit(editor) { editor.trigger('format', 'editor.action.format'); } Or

onEditorInit(editor) { const format = editor.getAction('editor.action.format'); format.run(); }

abyrne85 avatar Aug 13 '20 14:08 abyrne85

Changing the command to 'editor.action.formatDocument' works for me. See item 32 in the image of the supported actions.

abyrne85 avatar Aug 14 '20 11:08 abyrne85