2022-09-04 11:31:48 +00:00
|
|
|
import { action } from '@ember/object';
|
|
|
|
|
import Component from '@glimmer/component';
|
2022-10-14 06:33:51 +00:00
|
|
|
|
2022-10-14 07:03:10 +00:00
|
|
|
import 'monaco-editor/esm/vs/editor/browser/coreCommands.js';
|
|
|
|
|
import 'monaco-editor/esm/vs/editor/contrib/find/browser/findController.js';
|
|
|
|
|
import * as monaco 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-09-04 11:31:48 +00:00
|
|
|
@action
|
|
|
|
|
initEditor(el) {
|
2022-10-14 07:03:10 +00:00
|
|
|
// let codeModel = monaco.editor.createModel(this.args.code, this.args.language)
|
|
|
|
|
// let height = codeModel.getLineCount() * 20;
|
|
|
|
|
// el.style.height = height.toString() + "px";
|
|
|
|
|
|
2022-09-04 11:31:48 +00:00
|
|
|
this.editor = monaco.editor.create(el, {
|
2022-10-14 07:03:10 +00:00
|
|
|
// model: codeModel,
|
|
|
|
|
// theme: 'vs-dark',
|
|
|
|
|
// readOnly: this.readOnly,
|
|
|
|
|
// minimap: { enabled: false },
|
|
|
|
|
// wordWrap: "on",
|
|
|
|
|
// scrollBeyondLastLine: false,
|
|
|
|
|
// wrappingIndent: 'same'
|
|
|
|
|
|
2022-09-04 11:31:48 +00:00
|
|
|
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
|
|
|
|
2022-09-10 08:34:03 +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
|
|
|
}
|