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

89 lines
1.9 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-10-14 13:49:26 +00:00
import { Uri, editor } from 'monaco-editor/esm/vs/editor/editor.api.js';
2022-10-14 06:33:51 +00:00
2022-10-14 07:03:10 +00:00
// This is needed because the SimpleWorker.js in monaco-editor has the following code:
// loaderConfiguration = self.requirejs.s.contexts._.config;
window.requirejs.s = {
contexts: {
_: {
config: '',
},
},
};
2022-09-04 11:31:48 +00:00
export default class MonacoEditorComponent extends Component {
2022-09-15 10:30:58 +00:00
constructor(owner, args) {
super(owner, args);
this.args.invoker?.subscribe(this);
}
2022-10-14 14:38:32 +00:00
get modelUri() {
return Uri.parse(this.args.uri);
}
2022-09-04 11:31:48 +00:00
@action
initEditor(el) {
2022-10-14 14:38:32 +00:00
let model = editor.getModel(this.modelUri);
if (model === null) {
model = editor.createModel(
this.args.value,
this.args.language,
this.modelUri
);
}
2022-10-14 07:03:10 +00:00
2022-10-14 09:30:14 +00:00
this.editor = editor.create(el, {
2022-10-14 08:59:34 +00:00
model,
// theme: 'vs-dark', //TODO: add checking global theme
readOnly: this.args.readOnly ?? false,
// wordWrap: 'on',
// wrappingIndent: 'same',
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-15 07:36:45 +00:00
invokeSendValue() {
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
}