Skip to content

Commit 684b1bd

Browse files
committed
additional api and auto-format example
1 parent 171abd3 commit 684b1bd

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ The following options are used by default when Editor Component gets created:
123123
| loaded | | Raised when editor finished loading all its components. |
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 |
126+
| codeModelChanged | `CodeModelChangedEvent` | An event emitted when the code model value is changed. |
126127

127128
## Component API
128129

@@ -251,6 +252,22 @@ Other components can now have access to the editor instance:
251252
</button>
252253
```
253254

255+
### Example: auto-formatting on load
256+
257+
```html
258+
<ngs-code-editor [codeModel]="selectedModel" [options]="options" (codeModelChanged)="onCodeModelChanged($event)"></ngs-code-editor>
259+
```
260+
261+
```ts
262+
import { CodeModelChangedEvent } from '@ngstack/code-editor';
263+
264+
onCodeModelChanged(event: CodeModelChangedEvent) {
265+
setTimeout(() => {
266+
event.sender.formatDocument();
267+
}, 100);
268+
}
269+
```
270+
254271
## Offline Setup
255272

256273
### Editor

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import { JsonDefaultsService } from '../services/json-defaults.service';
2121
import { CodeModel } from '../models/code.model';
2222
import { editor } from 'monaco-editor';
2323

24+
export interface CodeModelChangedEvent {
25+
sender: CodeEditorComponent;
26+
value: CodeModel;
27+
}
28+
2429
@Component({
2530
// eslint-disable-next-line @angular-eslint/component-selector
2631
selector: 'ngs-code-editor',
@@ -109,6 +114,12 @@ export class CodeEditorComponent
109114
@Output()
110115
valueChanged = new EventEmitter<string>();
111116

117+
/**
118+
* An event emitted when the code model value is changed.
119+
*/
120+
@Output()
121+
codeModelChanged = new EventEmitter<CodeModelChangedEvent>();
122+
112123
/**
113124
* An event emitted when the contents of the underlying editor model have changed.
114125
*/
@@ -119,7 +130,7 @@ export class CodeEditorComponent
119130
* Raised when editor finished loading all its components.
120131
*/
121132
@Output()
122-
loaded = new EventEmitter();
133+
loaded = new EventEmitter<CodeEditorComponent>();
123134

124135
protected editorService = inject(CodeEditorService);
125136
protected typescriptDefaults = inject(TypescriptDefaultsService);
@@ -165,7 +176,7 @@ export class CodeEditorComponent
165176

166177
async ngAfterViewInit() {
167178
this.setupEditor();
168-
this.loaded.emit();
179+
this.loaded.emit(this);
169180
}
170181

171182
private setupEditor() {
@@ -202,6 +213,7 @@ export class CodeEditorComponent
202213
});
203214

204215
this.setupDependencies(this.codeModel);
216+
this.codeModelChanged.emit({ sender: this, value: this.codeModel });
205217
}
206218

207219
runEditorAction(id: string, args?: unknown) {
@@ -258,6 +270,7 @@ export class CodeEditorComponent
258270
this.setEditorValue(model.value);
259271
this.editorService.setModelLanguage(this._model, model.language);
260272
this.setupDependencies(model);
273+
this.codeModelChanged.emit({ sender: this, value: model });
261274
}
262275
}
263276
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@
9898
[theme]="activeTheme"
9999
[options]="options"
100100
(valueChanged)="onCodeChanged($event)"
101-
(loaded)="onEditorLoaded()"
101+
(loaded)="onEditorLoaded($event)"
102+
(codeModelChanged)="onCodeModelChanged($event)"
102103
>
103104
</ngs-code-editor>
104105
</ng-container>

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
CodeEditorComponent,
1212
CodeEditorModule,
1313
CodeEditorService,
14-
CodeModel
14+
CodeModel,
15+
CodeModelChangedEvent
1516
} from '@ngstack/code-editor';
1617
import { Observable } from 'rxjs';
1718
import { debounceTime } from 'rxjs/operators';
@@ -129,7 +130,15 @@ export class CodeEditorDemoComponent implements OnInit {
129130
*/
130131
}
131132

132-
onEditorLoaded() {
133-
console.log('loaded');
133+
onEditorLoaded(editor: CodeEditorComponent) {
134+
console.log('loaded', editor);
135+
}
136+
137+
onCodeModelChanged(event: CodeModelChangedEvent) {
138+
console.log('code model changed', event);
139+
140+
setTimeout(() => {
141+
event.sender.formatDocument();
142+
}, 100);
134143
}
135144
}

src/app/code-editor-demo/file-database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ const FILES_DATA: FileNode[] = [
5555
uri: 'main.json',
5656
value: [
5757
'{',
58-
' "$schema": "http://custom/schema.json",',
59-
' "type": "button"',
58+
' "$schema": "http://custom/schema.json",',
59+
' "type": "button"',
6060
'}'
6161
].join('\n'),
6262
schemas: [

0 commit comments

Comments
 (0)