문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 다음 판 | 이전 판 | ||
| monaco-editor [2023/05/05 01:24] – 만듦 taekgu | monaco-editor [2025/04/15 10:05] (현재) – 바깥 편집 127.0.0.1 | ||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| + | ====== Microsoft Monaco Editor ====== | ||
| + | * [[study: | ||
| + | |||
| + | ===== Angular 자료교환 ===== | ||
| + | |||
| + | 이것을 별도참조 atularen/ | ||
| + | |||
| + | ===== Angular App ===== | ||
| + | |||
| + | <code bash> | ||
| + | npm install monaco-editor | ||
| + | </ | ||
| + | |||
| + | <code json angular.json> | ||
| + | " | ||
| + | ... | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | ], | ||
| + | </ | ||
| + | |||
| + | <code javascript monaco-editor-service.ts> | ||
| + | import { Injectable } from ' | ||
| + | import { Subject } from ' | ||
| + | |||
| + | @Injectable({ | ||
| + | providedIn: ' | ||
| + | }) | ||
| + | export class MonacoEditorService { | ||
| + | loaded: boolean = false; | ||
| + | |||
| + | public loadingFinished: | ||
| + | |||
| + | constructor() {} | ||
| + | |||
| + | private finishLoading() { | ||
| + | this.loaded = true; | ||
| + | this.loadingFinished.next(); | ||
| + | } | ||
| + | |||
| + | public load() { | ||
| + | // load the assets | ||
| + | |||
| + | const baseUrl = ' | ||
| + | |||
| + | if (typeof (< | ||
| + | this.finishLoading(); | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | const onGotAmdLoader: | ||
| + | // load Monaco | ||
| + | (< | ||
| + | (< | ||
| + | this.finishLoading(); | ||
| + | }); | ||
| + | }; | ||
| + | |||
| + | // load AMD loader, if necessary | ||
| + | if (!(< | ||
| + | const loaderScript: | ||
| + | loaderScript.type = ' | ||
| + | loaderScript.src = `${baseUrl}/ | ||
| + | loaderScript.addEventListener(' | ||
| + | document.body.appendChild(loaderScript); | ||
| + | } else { | ||
| + | onGotAmdLoader(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Now call monacoEditorService.load(), | ||
| + | |||
| + | Now, you can create editors as you please, but make sure to not create them, before Monaco is loaded yet. Like this: | ||
| + | |||
| + | <code javascript monaco-editor.component.ts> | ||
| + | import ... | ||
| + | |||
| + | declare var monaco: any; | ||
| + | |||
| + | @Component({ | ||
| + | selector: ' | ||
| + | templateUrl: | ||
| + | styleUrls: [' | ||
| + | }) | ||
| + | export class MonacoEditorComponent implements OnInit, OnDestroy, AfterViewInit { | ||
| + | public _editor: any; | ||
| + | @ViewChild(' | ||
| + | |||
| + | private initMonaco(): | ||
| + | if(!this.monacoEditorService.loaded) { | ||
| + | this.monacoEditorService.loadingFinished.pipe(first()).subscribe(() => { | ||
| + | this.initMonaco(); | ||
| + | }); | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | this._editor = monaco.editor.create( | ||
| + | this._editorContainer.nativeElement, | ||
| + | options | ||
| + | ); | ||
| + | } | ||
| + | |||
| + | ngAfterViewInit(): | ||
| + | this.initMonaco(); | ||
| + | } | ||
| + | </ | ||
| + | There are most probably more elegant solutions than a boolean flag and this subject. | ||
| + | |||
| + | Make sure, there is a div in the component, like this: | ||
| + | |||
| + | <code html monaco-editor.component.html> | ||
| + | <div class=" | ||
| + | </ | ||