Skip to content

Commit 171abd3

Browse files
committed
support running editor actions, access editor instance
1 parent 1d4e227 commit 171abd3

File tree

6 files changed

+71
-10
lines changed

6 files changed

+71
-10
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ The following options are used by default when Editor Component gets created:
124124
| valueChanged | string | An event emitted when the text content of the model have changed. |
125125
| modelContentChanged | `IModelContentChangedEvent` | An event emitted when the contents of the underlying editor model have changed |
126126

127+
## Component API
128+
129+
| Name | Description |
130+
| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
131+
| editor | returns the instance of the underlying Monaco [ICodeEditor](https://microsoft.github.io/monaco-editor/docs.html#interfaces/editor.ICodeEditor.html) |
132+
| runAction(id, args) | runs the editor actions, for example `editor.action.formatDocument` |
133+
| formatDocument() | shortcut function to format the document |
134+
127135
## Editor Service
128136

129137
The component comes with a separate `CodeEditorService` service that provides additional APIs for the underlying `monaco` editor:
@@ -210,6 +218,39 @@ export class MyEditorComponent {
210218

211219
The schemas get automatically installed and associated with the corresponding file.
212220

221+
## Accessing Code Editor Instance
222+
223+
You can access the Code Editor component instance API from other components when using with the `@ViewChild`:
224+
225+
```ts
226+
private _codeEditor: CodeEditorComponent;
227+
228+
@ViewChild(CodeEditorComponent, { static: false })
229+
set codeEditor(value: CodeEditorComponent) {
230+
this._codeEditor = value;
231+
}
232+
233+
get codeEditor(): CodeEditorComponent {
234+
return this._codeEditor;
235+
}
236+
```
237+
238+
The code above allows you to use the code editor within the `*ngIf`, for example:
239+
240+
```html
241+
<ng-container *ngIf="selectedModel">
242+
<ngs-code-editor [codeModel]="selectedModel"></ngs-code-editor>
243+
</ng-container>
244+
```
245+
246+
Other components can now have access to the editor instance:
247+
248+
```html
249+
<button mat-icon-button title="Format code" (click)="codeEditor?.formatDocument()">
250+
<mat-icon>format_align_left</mat-icon>
251+
</button>
252+
```
253+
213254
## Offline Setup
214255

215256
### Editor

projects/code-editor/src/lib/code-editor/code-editor.component.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { editor } from 'monaco-editor';
3434
export class CodeEditorComponent
3535
implements OnChanges, OnDestroy, AfterViewInit
3636
{
37-
private _editor: editor.IEditor;
37+
private _editor: editor.ICodeEditor;
3838
private _model: editor.ITextModel;
3939
// private _value = '';
4040

@@ -49,11 +49,11 @@ export class CodeEditorComponent
4949
/**
5050
* The instance of the editor.
5151
*/
52-
get editor(): editor.IEditor {
52+
get editor(): editor.ICodeEditor {
5353
return this._editor;
5454
}
5555

56-
protected set editor(value: editor.IEditor) {
56+
protected set editor(value: editor.ICodeEditor) {
5757
this._editor = value;
5858
}
5959

@@ -204,6 +204,14 @@ export class CodeEditorComponent
204204
this.setupDependencies(this.codeModel);
205205
}
206206

207+
runEditorAction(id: string, args?: unknown) {
208+
this.editor.getAction(id)?.run(args);
209+
}
210+
211+
formatDocument() {
212+
this.runEditorAction('editor.action.formatDocument');
213+
}
214+
207215
private setupDependencies(model: CodeModel) {
208216
if (!model) {
209217
return;

projects/code-editor/src/lib/services/code-editor.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class CodeEditorService {
138138
createEditor(
139139
containerElement: HTMLElement,
140140
options?: editor.IEditorConstructionOptions
141-
): editor.IEditor {
141+
): editor.ICodeEditor {
142142
return this.monaco.editor.create(containerElement, options);
143143
}
144144

src/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ const routes: Route[] = [
2121
initialNavigation: 'enabledBlocking'
2222
}),
2323
CodeEditorModule.forRoot({
24-
editorVersion: '0.46.0',
24+
// editorVersion: '0.46.0',
2525
// use local Monaco installation
26-
// baseUrl: 'assets/monaco',
26+
baseUrl: 'assets/monaco',
2727
// use local Typings Worker
2828
typingsWorkerUrl: 'assets/workers/typings-worker.js'
2929
}),

src/app/code-editor-demo/code-editor-demo.component.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<mat-toolbar>
2-
<button mat-icon-button [matMenuTriggerFor]="mnuTheme">
2+
<button mat-icon-button [matMenuTriggerFor]="mnuTheme" title="Change theme">
33
<mat-icon>format_color_fill</mat-icon>
44
</button>
55

@@ -12,6 +12,10 @@
1212
{{ theme.name }}
1313
</button>
1414
</mat-menu>
15+
16+
<button mat-icon-button title="Format code" (click)="codeEditor?.formatDocument()">
17+
<mat-icon>format_align_left</mat-icon>
18+
</button>
1519
</mat-toolbar>
1620

1721
<div class="page-layout">

src/app/code-editor-demo/code-editor-demo.component.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { NestedTreeControl } from '@angular/cdk/tree';
22
import {
33
Component,
4-
ElementRef,
54
HostBinding,
65
OnInit,
76
ViewChild,
87
ViewEncapsulation
98
} from '@angular/core';
109
import { MatTreeModule, MatTreeNestedDataSource } from '@angular/material/tree';
1110
import {
11+
CodeEditorComponent,
1212
CodeEditorModule,
1313
CodeEditorService,
1414
CodeModel
@@ -61,8 +61,16 @@ export class CodeEditorDemoComponent implements OnInit {
6161
isLoading = false;
6262
isLoading$: Observable<boolean>;
6363

64-
@ViewChild('file')
65-
fileInput: ElementRef;
64+
private _codeEditor: CodeEditorComponent;
65+
66+
@ViewChild(CodeEditorComponent, { static: false })
67+
set codeEditor(value: CodeEditorComponent) {
68+
this._codeEditor = value;
69+
}
70+
71+
get codeEditor(): CodeEditorComponent {
72+
return this._codeEditor;
73+
}
6674

6775
@HostBinding('class')
6876
class = 'app-code-editor-demo';

0 commit comments

Comments
 (0)