meilisearch-admin/app/components/monaco-editor.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-09-04 11:31:48 +00:00
import { action } from '@ember/object';
import Component from '@glimmer/component';
2022-09-05 15:08:46 +00:00
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js';
2022-09-04 11:31:48 +00:00
export default class MonacoEditorComponent extends Component {
@action
initEditor(el) {
this.editor = monaco.editor.create(el, {
value: this.args.value,
language: this.args.language,
2022-09-05 15:58:34 +00:00
lineNumbers: 'off',
roundedSelection: false,
scrollBeyondLastLine: false,
2022-09-04 11:31:48 +00:00
minimap: {
enabled: false,
},
});
2022-09-05 15:58:34 +00:00
// Add onChange event
2022-09-06 10:22:13 +00:00
// if (this.args.onChange) {
// this.editor.onDidChangeModelContent(() => {
// this.args.onChange(this.editor.getValue());
// });
// }
// Autoresize height of element
this.editor.onDidContentSizeChange(() => {
const contentHeight = Math.min(
2022-09-06 10:31:08 +00:00
Math.max(this.editor.getContentHeight(), 160),
420
2022-09-06 10:22:13 +00:00
);
2022-09-05 15:58:34 +00:00
el.style.height = `${contentHeight + 1}px`;
2022-09-05 15:58:34 +00:00
this.editor.layout();
});
2022-09-04 11:31:48 +00:00
}
2022-09-05 15:08:46 +00:00
2022-09-06 10:22:13 +00:00
@action
2022-09-06 10:31:08 +00:00
updateValue(el, [value]) {
2022-09-06 10:22:13 +00:00
if (value !== this.editor.getValue()) {
this.editor?.setValue(value);
}
}
@action
2022-09-06 10:31:08 +00:00
triggerSendValue() {
2022-09-06 10:22:13 +00:00
this.args.sendValue?.(this.editor.getValue());
}
2022-09-05 15:08:46 +00:00
willDestroy() {
this.editor?.dispose();
super.willDestroy(...arguments);
}
2022-09-04 11:31:48 +00:00
}