Skip to content

Commit 81133fb

Browse files
committed
support custom monaco editor versions
1 parent 1556bc5 commit 81133fb

File tree

8 files changed

+72
-104
lines changed

8 files changed

+72
-104
lines changed

README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@ Import `CodeEditorModule` into your main application module:
2222
```ts
2323
import { CodeEditorModule } from '@ngstack/code-editor';
2424

25+
@NgModule({
26+
imports: [CodeEditorModule.forRoot()]
27+
})
28+
export class AppModule {}
29+
```
30+
31+
If you want to use a specific version of the Monaco editor, use `editorVersion` parameter.
32+
If not provided, the component is always going to use the `latest` version.
33+
34+
```ts
2535
@NgModule({
2636
imports: [
27-
...,
28-
CodeEditorModule.forRoot()
29-
],
30-
...
37+
CodeEditorModule.forRoot({
38+
editorVersion: '0.44.0'
39+
})
40+
]
3141
})
3242
export class AppModule {}
3343
```
@@ -47,14 +57,14 @@ export class AppComponent {
4757
model: CodeModel = {
4858
language: 'json',
4959
uri: 'main.json',
50-
value: '{}',
60+
value: '{}'
5161
};
5262

5363
options = {
5464
contextmenu: true,
5565
minimap: {
56-
enabled: true,
57-
},
66+
enabled: true
67+
}
5868
};
5969

6070
onCodeChanged(value) {
@@ -129,7 +139,7 @@ export class MyEditorComponent {
129139
language: 'typescript',
130140
uri: 'main.ts',
131141
value: '',
132-
dependencies: ['@types/node', '@ngstack/translate', '@ngstack/code-editor'],
142+
dependencies: ['@types/node', '@ngstack/translate', '@ngstack/code-editor']
133143
};
134144
}
135145
```
@@ -174,12 +184,12 @@ export class MyEditorComponent {
174184
type: 'object',
175185
properties: {
176186
type: {
177-
enum: ['button', 'textbox'],
178-
},
179-
},
180-
},
181-
},
182-
],
187+
enum: ['button', 'textbox']
188+
}
189+
}
190+
}
191+
}
192+
]
183193
};
184194
}
185195
```

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ngstack/code-editor",
33
"description": "Code editor component for Angular applications. ",
4-
"version": "7.0.1",
4+
"version": "7.1.0",
55
"license": "MIT",
66
"author": "Denys Vuika <[email protected]>",
77
"scripts": {

projects/code-editor/README.md

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Code editor component for Angular applications.
55
Based on the [Monaco](https://www.npmjs.com/package/monaco-editor) editor
66
that powers [VS Code](https://github.com/Microsoft/vscode).
77

8-
## Live demos
9-
10-
- [Angular 6](https://stackblitz.com/edit/ngstack-code-editor-ng6)
8+
<a href="https://www.buymeacoffee.com/denys" target="_blank">
9+
<img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="51" width="217">
10+
</a>
1111

1212
## Installing
1313

@@ -22,25 +22,30 @@ Import `CodeEditorModule` into your main application module:
2222
```ts
2323
import { CodeEditorModule } from '@ngstack/code-editor';
2424

25+
@NgModule({
26+
imports: [CodeEditorModule.forRoot()]
27+
})
28+
export class AppModule {}
29+
```
30+
31+
If you want to use a specific version of the Monaco editor, use `editorVersion` parameter.
32+
If not provided, the component is always going to use the `latest` version.
33+
34+
```ts
2535
@NgModule({
2636
imports: [
27-
...,
28-
CodeEditorModule.forRoot()
29-
],
30-
...
37+
CodeEditorModule.forRoot({
38+
editorVersion: '0.44.0'
39+
})
40+
]
3141
})
3242
export class AppModule {}
3343
```
3444

3545
Update template to use the `ngs-code-editor`:
3646

3747
```html
38-
<ngs-code-editor
39-
[theme]="theme"
40-
[codeModel]="model"
41-
[options]="options"
42-
(valueChanged)="onCodeChanged($event)">
43-
</ngs-code-editor>
48+
<ngs-code-editor [theme]="theme" [codeModel]="model" [options]="options" (valueChanged)="onCodeChanged($event)"></ngs-code-editor>
4449
```
4550

4651
Update component controller class and provide corresponding properties and events:
@@ -49,7 +54,7 @@ Update component controller class and provide corresponding properties and event
4954
export class AppComponent {
5055
theme = 'vs-dark';
5156

52-
codeModel: CodeModel = {
57+
model: CodeModel = {
5358
language: 'json',
5459
uri: 'main.json',
5560
value: '{}'
@@ -95,7 +100,7 @@ export interface CodeModel {
95100

96101
### Editor Options
97102

98-
For available options see [IEditorConstructionOptions](https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.ieditorconstructionoptions.html) docs.
103+
For available options see [IEditorConstructionOptions](https://microsoft.github.io/monaco-editor/typedoc/interfaces/editor.IEditorConstructionOptions.html) docs.
99104

100105
The following options are used by default when Editor Component gets created:
101106

@@ -123,8 +128,7 @@ The editor is able to resolve typing libraries when set to the `Typescript` or `
123128
Use `dependencies` property to provide a list of libraries to resolve
124129

125130
```html
126-
<ngs-code-editor [codeModel]="model" ...>
127-
</ngs-code-editor>
131+
<ngs-code-editor [codeModel]="model" ...> </ngs-code-editor>
128132
```
129133

130134
And in the controller class:
@@ -162,8 +166,7 @@ You should have all the types resolved and auto-completion working.
162166
You can associate multiple schemas when working with JSON files.
163167

164168
```html
165-
<ngs-code-editor [codeModel]="model" ...>
166-
</ngs-code-editor>
169+
<ngs-code-editor [codeModel]="model" ...> </ngs-code-editor>
167170
```
168171

169172
Provide the required schemas like in the example below.
@@ -205,7 +208,7 @@ Install the `monaco-editor`:
205208
npm install monaco-editor
206209
```
207210

208-
Update the `.angular-cli.json` file and append the following asset rule:
211+
Update the `angular.json` file and append the following asset rule:
209212

210213
```json
211214
{
@@ -235,7 +238,7 @@ export class AppModule {}
235238

236239
### Typings Worker
237240

238-
Update the `.angular-cli.json` file and append the following asset rule:
241+
Update the `angular.json` file and append the following asset rule:
239242

240243
```ts
241244
{
@@ -268,44 +271,3 @@ use `CodeEditorModule.forRoot()` in the main application,
268271
and `CodeEditorModule.forChild()` in all lazy-loaded feature modules.
269272

270273
For more details please refer to [Lazy Loading Feature Modules](https://angular.io/guide/lazy-loading-ngmodules)
271-
272-
## Enabling error details
273-
274-
Append the following code to the `polyfills.ts` to enable error details in the tooltips:
275-
276-
```ts
277-
// workaround for https://github.com/Microsoft/monaco-editor/issues/790
278-
279-
Promise.all = function(values: any): Promise<any> {
280-
let resolve: (v: any) => void;
281-
let reject: (v: any) => void;
282-
const promise = new this((res, rej) => {
283-
resolve = res;
284-
reject = rej;
285-
});
286-
let count = 0;
287-
let index = 0;
288-
const resolvedValues: any[] = [];
289-
for (let value of values) {
290-
if (!(value && value.then)) {
291-
value = this.resolve(value);
292-
}
293-
value.then(
294-
(idx => (val: any) => {
295-
resolvedValues[idx] = val;
296-
count--;
297-
if (!count) {
298-
resolve(resolvedValues);
299-
}
300-
})(index),
301-
reject
302-
);
303-
count++;
304-
index++;
305-
}
306-
if (!count) {
307-
resolve(resolvedValues);
308-
}
309-
return promise;
310-
};
311-
```

projects/code-editor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ngstack/code-editor",
33
"description": "Code editor component for Angular applications.",
4-
"version": "7.0.1",
4+
"version": "7.1.0",
55
"license": "MIT",
66
"author": {
77
"name": "Denys Vuika",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface CodeEditorSettings {
2+
editorVersion?: string;
23
baseUrl?: string;
34
typingsWorkerUrl?: string;
45
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ export interface TypingsInfo {
1919
}
2020

2121
@Injectable({
22-
providedIn: 'root',
22+
providedIn: 'root'
2323
})
2424
export class CodeEditorService {
25-
baseUrl = 'https://cdn.jsdelivr.net/npm/monaco-editor/min';
26-
27-
typingsWorkerUrl =
28-
'https://cdn.jsdelivr.net/npm/@ngstack/code-editor/workers/typings-worker.js';
25+
readonly baseUrl: string; // = 'https://cdn.jsdelivr.net/npm/monaco-editor/min';
26+
readonly typingsWorkerUrl: string; // = 'https://cdn.jsdelivr.net/npm/@ngstack/code-editor/workers/typings-worker.js';
2927

3028
typingsLoaded = new Subject<TypingsInfo>();
3129
loaded = new BehaviorSubject<{ monaco: any } | null>(null);
@@ -39,14 +37,12 @@ export class CodeEditorService {
3937
@Inject(EDITOR_SETTINGS)
4038
settings: CodeEditorSettings
4139
) {
42-
const defaults = {
43-
baseUrl: this.baseUrl,
44-
typingsWorkerUrl: this.typingsWorkerUrl,
45-
...settings,
46-
};
40+
const editorVersion = settings?.editorVersion || 'latest';
4741

48-
this.baseUrl = defaults.baseUrl;
49-
this.typingsWorkerUrl = defaults.typingsWorkerUrl;
42+
this.baseUrl =
43+
settings?.baseUrl ||
44+
`https://cdn.jsdelivr.net/npm/monaco-editor@${editorVersion}/min`;
45+
this.typingsWorkerUrl = settings?.typingsWorkerUrl || ``;
5046
}
5147

5248
private loadTypingsWorker(): Worker {
@@ -74,7 +70,7 @@ export class CodeEditorService {
7470
if (worker) {
7571
this.loadingTypings.next(true);
7672
worker.postMessage({
77-
dependencies,
73+
dependencies
7874
});
7975
}
8076
}
@@ -84,7 +80,7 @@ export class CodeEditorService {
8480
return new Promise((resolve) => {
8581
const onGotAmdLoader = () => {
8682
(<any>window).require.config({
87-
paths: { vs: `${this.baseUrl}/vs` },
83+
paths: { vs: `${this.baseUrl}/vs` }
8884
});
8985

9086
if (this.baseUrl.startsWith('http')) {
@@ -100,7 +96,7 @@ export class CodeEditorService {
10096
window['MonacoEnvironment'] = {
10197
getWorkerUrl: function () {
10298
return proxy;
103-
},
99+
}
104100
};
105101
}
106102

src/app/app.module.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,27 @@ import { CodeEditorDemoComponent } from './code-editor-demo/code-editor-demo.com
99
const routes: Route[] = [
1010
{
1111
path: '',
12-
component: CodeEditorDemoComponent,
13-
},
12+
component: CodeEditorDemoComponent
13+
}
1414
];
1515

1616
@NgModule({
1717
imports: [
1818
BrowserModule,
1919
BrowserAnimationsModule,
2020
RouterModule.forRoot(routes, {
21-
initialNavigation: 'enabledBlocking',
21+
initialNavigation: 'enabledBlocking'
2222
}),
23-
// MaterialModule,
24-
2523
CodeEditorModule.forRoot({
24+
editorVersion: '0.44.0'
2625
// use local Monaco installation
27-
baseUrl: 'assets/monaco',
26+
// baseUrl: 'assets/monaco',
2827
// use local Typings Worker
29-
typingsWorkerUrl: 'assets/workers/typings-worker.js',
28+
// typingsWorkerUrl: 'assets/workers/typings-worker.js'
3029
}),
31-
CodeEditorDemoComponent,
30+
CodeEditorDemoComponent
3231
],
3332
declarations: [AppComponent],
34-
bootstrap: [AppComponent],
33+
bootstrap: [AppComponent]
3534
})
3635
export class AppModule {}

0 commit comments

Comments
 (0)