Skip to content

Commit

Permalink
standalone editor component (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysVuika authored Nov 6, 2024
1 parent aa78e1f commit 8034043
Show file tree
Hide file tree
Showing 25 changed files with 163 additions and 201 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"singleQuote": true,
"trailingComma": "none"
"trailingComma": "none",
"printWidth": 150
}
51 changes: 39 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,38 @@ that powers [VS Code](https://github.com/Microsoft/vscode).
npm install @ngstack/code-editor
```

## Integrating with Angular CLI project
## Integrating with Standalone Angular Project

Update the `app.config.ts` file to provide the code editor configuration:

```ts
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideAnimationsAsync(),

// Configure Code Editor
provideCodeEditor({
// editorVersion: '0.46.0',
// use local Monaco installation
baseUrl: 'assets/monaco',
// use local Typings Worker
typingsWorkerUrl: 'assets/workers/typings-worker.js'
})
]
};
```

## Integrating with Modules-based Angular Project

Import `CodeEditorModule` into your main application module:

```ts
import { CodeEditorModule } from '@ngstack/code-editor';
import { provideCodeEditor } from '@ngstack/code-editor';

@NgModule({
imports: [CodeEditorModule.forRoot()]
providers: [provideCodeEditor()]
})
export class AppModule {}
```
Expand All @@ -34,9 +57,11 @@ If not provided, the component is always going to use the `latest` version.
> For a full list of Monaco versions and changes, please refer to the official [CHANGELOG.md](https://github.com/microsoft/monaco-editor/blob/main/CHANGELOG.md) file
```ts
import { provideCodeEditor } from '@ngstack/code-editor';

@NgModule({
imports: [
CodeEditorModule.forRoot({
providers: [
provideCodeEditor({
editorVersion: '0.44.0'
})
]
Expand Down Expand Up @@ -78,7 +103,7 @@ export class AppComponent {
## Input Properties

| Name | Type | Default Value | Description |
| --------- | --------- | ------------- | -------------------------------------------------------------- |
|-----------|-----------|---------------|----------------------------------------------------------------|
| theme | string | vs | Editor theme. Supported values: `vs`, `vs-dark` or `hc-black`. |
| options | Object | {...} | Editor options. |
| readOnly | boolean | false | Toggles readonly state of the editor. |
Expand Down Expand Up @@ -297,11 +322,11 @@ Update the `angular.json` file and append the following asset rule:
Update the main application module and setup the service to use the custom `baseUrl` when application starts:

```ts
import { CodeEditorModule } from '@ngstack/code-editor';
import { provideCodeEditor } from '@ngstack/code-editor';

@NgModule({
imports: [
CodeEditorModule.forRoot({
providers: [
provideCodeEditor({
baseUrl: 'assets/monaco'
})
]
Expand All @@ -324,9 +349,11 @@ Update the `angular.json` file and append the following asset rule:
Then update the `CodeEditorService` configuration at the application startup:

```ts
import { provideCodeEditor } from '@ngstack/code-editor';

@NgModule({
imports: [
CodeEditorModule.forRoot({
providers: [
provideCodeEditor({
typingsWorkerUrl: 'assets/workers/typings-worker.js'
})
]
Expand All @@ -338,6 +365,6 @@ export class AppModule {}

To enable Lazy Loading
use `CodeEditorModule.forRoot()` in the main application,
and `CodeEditorModule.forChild()` in all lazy-loaded feature modules.
and `CodeEditorModule` in all lazy-loaded feature modules.

For more details please refer to [Lazy Loading Feature Modules](https://angular.io/guide/lazy-loading-ngmodules)
4 changes: 2 additions & 2 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
"budgets": [
{
"type": "initial",
"maximumWarning": "2.41MB",
"maximumError": "2.5MB"
"maximumWarning": "2.8MB",
"maximumError": "3MB"
},
{
"type": "anyComponentStyle",
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "^2.1.0",
"ng-packagr": "^18.1.0",
"prettier": "^3.3.3",
"typescript": "5.5.4"
}
}
7 changes: 0 additions & 7 deletions projects/code-editor/src/lib/code-editor.module.spec.ts

This file was deleted.

41 changes: 22 additions & 19 deletions projects/code-editor/src/lib/code-editor.module.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import { NgModule, ModuleWithProviders, APP_INITIALIZER } from '@angular/core';
import { APP_INITIALIZER, ModuleWithProviders, NgModule, Provider } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CodeEditorComponent } from './code-editor/code-editor.component';
import {
CodeEditorService,
EDITOR_SETTINGS,
} from './services/code-editor.service';
import { CodeEditorService, EDITOR_SETTINGS } from './services/code-editor.service';
import { CodeEditorSettings } from './editor-settings';
import { TypescriptDefaultsService } from './services/typescript-defaults.service';
import { JavascriptDefaultsService } from './services/javascript-defaults.service';
import { CodeEditorSettings } from './editor-settings';
import { JsonDefaultsService } from './services/json-defaults.service';

export function setupEditorService(service: CodeEditorService) {
const result = () => service.loadEditor();
return result;
return () => service.loadEditor();
}

export function provideCodeEditor(settings?: CodeEditorSettings): Provider[] {
return [
{ provide: EDITOR_SETTINGS, useValue: settings },
CodeEditorService,
TypescriptDefaultsService,
JavascriptDefaultsService,
JsonDefaultsService,
{
provide: APP_INITIALIZER,
useFactory: setupEditorService,
deps: [CodeEditorService],
multi: true,
},
];
}

/** @deprecated use `provideCodeEditor(settings)` instead */
@NgModule({
imports: [CommonModule],
declarations: [CodeEditorComponent],
imports: [CommonModule, CodeEditorComponent],
exports: [CodeEditorComponent],
})
export class CodeEditorModule {
Expand All @@ -29,9 +41,6 @@ export class CodeEditorModule {
providers: [
{ provide: EDITOR_SETTINGS, useValue: settings },
CodeEditorService,
TypescriptDefaultsService,
JavascriptDefaultsService,
JsonDefaultsService,
{
provide: APP_INITIALIZER,
useFactory: setupEditorService,
Expand All @@ -41,10 +50,4 @@ export class CodeEditorModule {
],
};
}

static forChild(): ModuleWithProviders<CodeEditorModule> {
return {
ngModule: CodeEditorModule,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { ComponentFixture, TestBed, fakeAsync } from '@angular/core/testing';

import { CodeEditorComponent } from './code-editor.component';
import { CodeEditorService } from '../services/code-editor.service';
import { TypescriptDefaultsService } from '../services/typescript-defaults.service';
import { JavascriptDefaultsService } from '../services/javascript-defaults.service';
import { CodeEditorModule } from '../code-editor.module';
import { provideCodeEditor } from '../code-editor.module';

describe('CodeEditorComponent', () => {
let component: CodeEditorComponent;
Expand All @@ -13,12 +11,8 @@ describe('CodeEditorComponent', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [CodeEditorModule.forRoot({ baseUrl: 'assets/monaco' })],
providers: [
CodeEditorService,
TypescriptDefaultsService,
JavascriptDefaultsService
]
imports: [CodeEditorComponent],
providers: [provideCodeEditor({ baseUrl: 'assets/monaco' })]
});

codeEditorService = TestBed.inject(CodeEditorService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface CodeModelChangedEvent {
@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'ngs-code-editor',
standalone: true,
templateUrl: './code-editor.component.html',
styleUrls: ['./code-editor.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
Expand Down
11 changes: 7 additions & 4 deletions projects/multiple-editors/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { Component } from '@angular/core';
import { CodeModel } from '@ngstack/code-editor';
import { CodeEditorComponent, CodeModel } from '@ngstack/code-editor';
import { RouterOutlet } from '@angular/router';

@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, CodeEditorComponent],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
styleUrls: ['./app.component.css']
})
export class AppComponent {
file1: CodeModel = {
language: 'text',
value: 'left editor',
uri: 'left.txt',
uri: 'left.txt'
};

file2: CodeModel = {
language: 'text',
value: 'right editor',
uri: 'right.txt',
uri: 'right.txt'
};
}
18 changes: 18 additions & 0 deletions projects/multiple-editors/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideCodeEditor } from '@ngstack/code-editor';
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';

export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
// provideRouter(routes),
provideAnimationsAsync(),
provideCodeEditor({
// editorVersion: '0.46.0',
// use local Monaco installation
baseUrl: 'assets/monaco',
// use local Typings Worker
typingsWorkerUrl: 'assets/workers/typings-worker.js'
})
]
};
21 changes: 0 additions & 21 deletions projects/multiple-editors/src/app/app.module.ts

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions projects/multiple-editors/src/environments/environment.ts

This file was deleted.

15 changes: 4 additions & 11 deletions projects/multiple-editors/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));
3 changes: 1 addition & 2 deletions projects/multiple-editors/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"src/main.ts"
],
"include": [
"src/**/*.d.ts",
"src/environments/*.ts"
"src/**/*.d.ts"
]
}
3 changes: 3 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';

@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
Expand Down
Loading

0 comments on commit 8034043

Please sign in to comment.