Add saving json from monaco-editor

This commit is contained in:
Stanislav Romanov 2022-09-05 18:58:34 +03:00
parent 5d4dc81fdf
commit 0df2b6b1bf
5 changed files with 32 additions and 13 deletions

View file

@ -1 +1 @@
<div {{did-insert this.initEditor}} class="h-80" ...attributes></div>
<div {{did-insert this.initEditor}} class="mb-4" ...attributes></div>

View file

@ -8,10 +8,28 @@ export default class MonacoEditorComponent extends Component {
this.editor = monaco.editor.create(el, {
value: this.args.value,
language: this.args.language,
lineNumbers: 'off',
roundedSelection: false,
scrollBeyondLastLine: false,
minimap: {
enabled: false,
},
});
// Add onChange event
if (this.args.onChange) {
this.editor.onDidChangeModelContent(() => {
this.args.onChange(this.editor.getValue());
});
}
// autoresize height of element
const disposable = this.editor.onDidContentSizeChange(() => {
const contentHeight = Math.min(320, this.editor.getContentHeight());
el.style.height = `${contentHeight}px`;
this.editor.layout();
disposable.dispose();
});
}
willDestroy() {

View file

@ -3,12 +3,7 @@ import { action } from '@ember/object';
export default class AdminIndexesItemDocumentsDocumentEditController extends Controller {
@action
save(event) {
event.preventDefault();
const formData = new FormData(event.currentTarget);
const data = formData.get('document');
this.model.index.saveDocument(data);
save() {
this.model.index.saveDocument(this.model.document);
}
}

View file

@ -48,9 +48,13 @@ export default class Index {
}
saveDocument(document) {
if (typeof document === 'string') {
document = JSON.parse(document);
}
return query(`${this.path}/documents`, {
method: 'POST',
body: JSON.stringify([JSON.parse(document)]),
body: JSON.stringify([document]),
});
}

View file

@ -1,5 +1,7 @@
<form {{on 'submit' this.save}}>
<MonacoEditor @value={{stringify @model.document}} @language='json'/>
<MonacoEditor
@value={{stringify @model.document}}
@language='json'
@onChange={{set this.model 'document'}}
/>
<Button type='submit' @text='Save' @style='primary' />
</form>
<Button type='submit' @text='Save' @style='primary' @onClick={{this.save}} />