浏览代码

primeNG module and ngx-monaco-editor-v2 module

Mamadou BAH 1年前
父节点
当前提交
5b59931fd3
共有 39 个文件被更改,包括 23324 次插入1 次删除
  1. 16
    0
      .editorconfig
  2. 46
    0
      .gitignore
  3. 413
    1
      README.md
  4. 151
    0
      angular.json
  5. 4
    0
      license
  6. 10
    0
      npm-publish
  7. 21140
    0
      package-lock.json
  8. 71
    0
      package.json
  9. 17
    0
      projects/editor-tester/.browserslistrc
  10. 44
    0
      projects/editor-tester/karma.conf.js
  11. 138
    0
      projects/editor-tester/src/app/app.component.ts
  12. 73
    0
      projects/editor-tester/src/app/app.module.ts
  13. 0
    0
      projects/editor-tester/src/assets/.gitkeep
  14. 3
    0
      projects/editor-tester/src/environments/environment.prod.ts
  15. 16
    0
      projects/editor-tester/src/environments/environment.ts
  16. 二进制
      projects/editor-tester/src/favicon.ico
  17. 13
    0
      projects/editor-tester/src/index.html
  18. 12
    0
      projects/editor-tester/src/main.ts
  19. 53
    0
      projects/editor-tester/src/polyfills.ts
  20. 7
    0
      projects/editor-tester/src/styles.css
  21. 27
    0
      projects/editor-tester/src/test.ts
  22. 15
    0
      projects/editor-tester/tsconfig.app.json
  23. 18
    0
      projects/editor-tester/tsconfig.spec.json
  24. 414
    0
      projects/editor/README.md
  25. 44
    0
      projects/editor/karma.conf.js
  26. 7
    0
      projects/editor/ng-package.json
  27. 48
    0
      projects/editor/package.json
  28. 126
    0
      projects/editor/src/lib/base-editor.ts
  29. 11
    0
      projects/editor/src/lib/config.ts
  30. 102
    0
      projects/editor/src/lib/diff-editor.component.ts
  31. 129
    0
      projects/editor/src/lib/editor.component.ts
  32. 30
    0
      projects/editor/src/lib/editor.module.ts
  33. 9
    0
      projects/editor/src/lib/types.ts
  34. 5
    0
      projects/editor/src/public-api.ts
  35. 28
    0
      projects/editor/src/test.ts
  36. 20
    0
      projects/editor/tsconfig.lib.json
  37. 10
    0
      projects/editor/tsconfig.lib.prod.json
  38. 17
    0
      projects/editor/tsconfig.spec.json
  39. 37
    0
      tsconfig.json

+ 16
- 0
.editorconfig 查看文件

@@ -0,0 +1,16 @@
1
+# Editor configuration, see https://editorconfig.org
2
+root = true
3
+
4
+[*]
5
+charset = utf-8
6
+indent_style = space
7
+indent_size = 2
8
+insert_final_newline = true
9
+trim_trailing_whitespace = true
10
+
11
+[*.ts]
12
+quote_type = single
13
+
14
+[*.md]
15
+max_line_length = off
16
+trim_trailing_whitespace = false

+ 46
- 0
.gitignore 查看文件

@@ -0,0 +1,46 @@
1
+# See http://help.github.com/ignore-files/ for more about ignoring files.
2
+
3
+# compiled output
4
+/dist
5
+/tmp
6
+/out-tsc
7
+# Only exists if Bazel was run
8
+/bazel-out
9
+
10
+# dependencies
11
+/node_modules
12
+
13
+# profiling files
14
+chrome-profiler-events*.json
15
+
16
+# IDEs and editors
17
+/.idea
18
+.project
19
+.classpath
20
+.c9/
21
+*.launch
22
+.settings/
23
+*.sublime-workspace
24
+
25
+# IDE - VSCode
26
+.vscode/*
27
+!.vscode/settings.json
28
+!.vscode/tasks.json
29
+!.vscode/launch.json
30
+!.vscode/extensions.json
31
+.history/*
32
+
33
+# misc
34
+/.angular/cache
35
+/.sass-cache
36
+/connect.lock
37
+/coverage
38
+/libpeerconnection.log
39
+npm-debug.log
40
+yarn-error.log
41
+testem.log
42
+/typings
43
+
44
+# System Files
45
+.DS_Store
46
+Thumbs.db

+ 413
- 1
README.md 查看文件

@@ -1,2 +1,414 @@
1
-# ngx-monaco-editor-v2
1
+# Monaco Editor Component for Angular 2 and above.
2 2
 
3
+ - Angular <= 4: v3.x.x
4
+ - Angular 5: v5.x.x
5
+ - Angular 6: v6.x.x
6
+ - Angular 7: v7.x.x
7
+ - Angular 8: v8.x.x
8
+ - Angular 9: v9.x.x
9
+ - Angular 10: v10.x.x 
10
+ - Angular 12: v12.x.x
11
+ - Angular 13: not supported ( best to use 14, otherwise contact author)
12
+ - Angular 14: v14.x.x
13
+ - Angular 15: v15.x.x
14
+ - Angular 16: v16.x.x
15
+
16
+Using this Module you can utilize the Monaco Editor as an Angular Component. Feel free to contribute, raise feature requests and make it better.
17
+
18
+Supports all the options available in monaco-editor [Monaco Editor Options](https://microsoft.github.io/monaco-editor/api/index.html)
19
+
20
+## Demo
21
+
22
+https://ngx-monaco-editor-v2.surge.sh/
23
+
24
+![img.png](img.png)
25
+
26
+## Setup
27
+
28
+### Installation
29
+
30
+Install from npm repository:
31
+```
32
+npm install monaco-editor ngx-monaco-editor-v2 --save
33
+ ```
34
+
35
+Breaking change from v10, is to use monaco-editor next to ngx-monaco-editor-v2 in your package.json file.
36
+ 
37
+For angular version 6 use v6.x.x
38
+```
39
+npm install ngx-monaco-editor-v2@6.0.0 --save
40
+ ```
41
+
42
+Add the glob to assets in `angular.json`
43
+```typescript
44
+{
45
+  "apps": [
46
+    {
47
+      "assets": [
48
+      { "glob": "**/*", "input": "node_modules/monaco-editor", "output": "/assets/monaco/" }
49
+      ],
50
+      ...
51
+    }
52
+    ...
53
+  ],
54
+  ...
55
+}
56
+ ```
57
+
58
+
59
+For Angular 6 and below, add the glob to assets in `.angular-cli.json` schema - `projects.[project-name].architect.build` (to make monaco-editor lib available to the app):
60
+```typescript
61
+{
62
+  "options":{
63
+        {"assets": [
64
+          { "glob": "**/*", "input": "node_modules/ngx-monaco-editor/assets/monaco", "output": "./assets/monaco/" }
65
+
66
+        ],
67
+        ...
68
+        }
69
+    ...
70
+    },
71
+  ...
72
+}
73
+ ```
74
+
75
+### Sample
76
+Include MonacoEditorModule in Main Module and Feature Modules where you want to use the editor component.(eg: app.module.ts): 
77
+```typescript
78
+import { BrowserModule } from '@angular/platform-browser';
79
+import { NgModule } from '@angular/core';
80
+import { FormsModule } from '@angular/forms';
81
+
82
+import { AppComponent } from './app.component';
83
+import { MonacoEditorModule } from 'ngx-monaco-editor-v2';
84
+
85
+@NgModule({
86
+  declarations: [
87
+    AppComponent
88
+  ],
89
+  imports: [
90
+    BrowserModule,
91
+    FormsModule,
92
+    MonacoEditorModule.forRoot() // use forRoot() in main app module only.
93
+  ],
94
+  providers: [],
95
+  bootstrap: [AppComponent]
96
+})
97
+export class AppModule {
98
+}
99
+```
100
+
101
+Create Editor options in component.(eg: app.component.ts)
102
+```typescript
103
+import { Component } from '@angular/core';
104
+
105
+@Component({
106
+  selector: 'app-root',
107
+  templateUrl: './app.component.html'
108
+})
109
+export class AppComponent {
110
+  editorOptions = {theme: 'vs-dark', language: 'javascript'};
111
+  code: string= 'function x() {\nconsole.log("Hello world!");\n}';
112
+}
113
+```
114
+Include editor in html with options and ngModel bindings.(eg: app.component.html)
115
+```html
116
+<ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
117
+```
118
+
119
+Include diff-editor in html with options.(eg: app.component.html)
120
+```html
121
+<ngx-monaco-diff-editor [options]="options" [originalModel]="originalModel" [modifiedModel]="modifiedModel"></ngx-monaco-diff-editor>
122
+```
123
+```typescript
124
+import { Component } from '@angular/core';
125
+import { DiffEditorModel } from 'ngx-monaco-editor';
126
+
127
+@Component({
128
+  selector: 'app-root',
129
+  templateUrl: './app.component.html'
130
+})
131
+export class AppComponent {
132
+  options = {
133
+    theme: 'vs-dark'
134
+  };
135
+  originalModel: DiffEditorModel = {
136
+    code: 'heLLo world!',
137
+    language: 'text/plain'
138
+  };
139
+
140
+  modifiedModel: DiffEditorModel = {
141
+    code: 'hello orlando!',
142
+    language: 'text/plain'
143
+  };
144
+}
145
+```
146
+
147
+### Styling
148
+To match height of container element add height: 100% and wrap in container
149
+```html
150
+<div style="height: 500px">
151
+    <ngx-monaco-editor style="height: 100%" [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
152
+</div>
153
+```
154
+Add class to editor tag. (eg. class="my-code-editor")
155
+```html
156
+<ngx-monaco-editor class="my-code-editor" [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
157
+```
158
+Add styling in css/scss file:
159
+```scss
160
+.my-code-editor {
161
+  .editor-container {
162
+    height: calc(100vh - 100px);
163
+  }
164
+}
165
+```
166
+Set automaticLayout option to adjust editor size dynamically. Recommended when using in modal dialog or tabs where editor is not visible initially.
167
+
168
+### Events
169
+Output event (onInit) expose editor instance that can be used for performing custom operations on the editor. 
170
+```html
171
+<ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code" (onInit)="onInit($event)"></ngx-monaco-editor>
172
+```
173
+
174
+```typescript
175
+export class AppComponent {
176
+  editorOptions = {theme: 'vs-dark', language: 'javascript'};
177
+  code: string= 'function x() {\nconsole.log("Hello world!");\n}';
178
+  onInit(editor) {
179
+      let line = editor.getPosition();
180
+      console.log(line);
181
+    }
182
+}
183
+```
184
+
185
+## Configurations
186
+`forRoot()` method of MonacoEditorModule accepts config of type `NgxMonacoEditorConfig`.
187
+```typescript
188
+import { NgModule } from '@angular/core';
189
+import { FormsModule } from '@angular/forms';
190
+import { BrowserModule } from '@angular/platform-browser';
191
+
192
+import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor';
193
+import { AppComponent } from './app.component';
194
+
195
+const monacoConfig: NgxMonacoEditorConfig = {
196
+  baseUrl: 'app-name/assets', // configure base path for monaco editor. Starting with version 8.0.0 it defaults to './assets'. Previous releases default to '/assets'
197
+  defaultOptions: { scrollBeyondLastLine: false }, // pass default options to be used
198
+  onMonacoLoad: () => { console.log((<any>window).monaco); } // here monaco object will be available as window.monaco use this function to extend monaco editor functionalities.
199
+  requireConfig: { preferScriptTags: true } // allows to oweride configuration passed to monacos loader
200
+  monacoRequire: (<any>window).monacoRequire // pass here monacos require function if you loaded monacos loader (loader.js) yourself 
201
+};
202
+
203
+@NgModule({
204
+  declarations: [
205
+    AppComponent
206
+  ],
207
+  imports: [
208
+    BrowserModule,
209
+    FormsModule,
210
+    MonacoEditorModule.forRoot(monacoConfig)
211
+  ],
212
+  providers: [],
213
+  bootstrap: [AppComponent]
214
+})
215
+export class AppModule {
216
+}
217
+```
218
+
219
+### Configure JSON Defaults
220
+`onMonacoLoad` property of `NgxMonacoEditorConfig` can be used to configure JSON default.
221
+```typescript
222
+import { NgModule } from '@angular/core';
223
+import { FormsModule } from '@angular/forms';
224
+import { BrowserModule } from '@angular/platform-browser';
225
+
226
+import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor';
227
+import { AppComponent } from './app.component';
228
+
229
+export function onMonacoLoad() {
230
+
231
+  console.log((window as any).monaco);
232
+
233
+  const uri = monaco.Uri.parse('a://b/foo.json');
234
+  monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
235
+    validate: true,
236
+    schemas: [{
237
+      uri: 'http://myserver/foo-schema.json',
238
+      fileMatch: [uri.toString()],
239
+      schema: {
240
+        type: 'object',
241
+        properties: {
242
+          p1: {
243
+            enum: ['v1', 'v2']
244
+          },
245
+          p2: {
246
+            $ref: 'http://myserver/bar-schema.json'
247
+          }
248
+        }
249
+      }
250
+    }, {
251
+      uri: 'http://myserver/bar-schema.json',
252
+      fileMatch: [uri.toString()],
253
+      schema: {
254
+        type: 'object',
255
+        properties: {
256
+          q1: {
257
+            enum: ['x1', 'x2']
258
+          }
259
+        }
260
+      }
261
+    }]
262
+  });
263
+
264
+}
265
+
266
+const monacoConfig: NgxMonacoEditorConfig = {
267
+  baseUrl: 'assets',
268
+  defaultOptions: { scrollBeyondLastLine: false },
269
+  onMonacoLoad
270
+};
271
+
272
+@NgModule({
273
+  declarations: [
274
+    AppComponent
275
+  ],
276
+  imports: [
277
+    BrowserModule,
278
+    FormsModule,
279
+    MonacoEditorModule.forRoot(monacoConfig)
280
+  ],
281
+  providers: [],
282
+  bootstrap: [AppComponent]
283
+})
284
+export class AppModule {
285
+}
286
+```
287
+
288
+Now pass model config of type `NgxEditorModel` to Editor Component
289
+```typescript
290
+@Component({
291
+  selector: 'app-root',
292
+  template: `<ngx-monaco-editor [options]="options" [model]="model"></ngx-monaco-editor>`,
293
+  styles: []
294
+})
295
+export class AppComponent {
296
+  options = {
297
+    theme: 'vs-dark'
298
+  };
299
+  
300
+  jsonCode = [
301
+    '{',
302
+    '    "p1": "v3",',
303
+    '    "p2": false',
304
+    '}'
305
+  ].join('\n');
306
+
307
+  model: NgxEditorModel = {
308
+    value: this.jsonCode,
309
+    language: 'json',
310
+    uri: monaco.Uri.parse('a://b/foo.json')
311
+  };
312
+}
313
+```
314
+
315
+### Configuration for Electron
316
+If you expose node's `require` in your render process, monaco will try to use its `NodeScriptLoader` and fail to load its files. To presuade it to use its `BrowserScriptLoader` instead it is necessery to set `preferScriptTags` to true.
317
+```typescript
318
+import { NgModule } from '@angular/core';
319
+import { FormsModule } from '@angular/forms';
320
+import { BrowserModule } from '@angular/platform-browser';
321
+
322
+import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor';
323
+import { AppComponent } from './app.component';
324
+
325
+const monacoConfig: NgxMonacoEditorConfig = {
326
+  baseUrl: 'assets', 
327
+  requireConfig: { preferScriptTags: true }
328
+};
329
+
330
+@NgModule({
331
+  declarations: [
332
+    AppComponent
333
+  ],
334
+  imports: [
335
+    BrowserModule,
336
+    FormsModule,
337
+    MonacoEditorModule.forRoot(monacoConfig)
338
+  ],
339
+  providers: [],
340
+  bootstrap: [AppComponent]
341
+})
342
+export class AppModule {
343
+}
344
+```
345
+If for some reason you want to load monaco yourself. 
346
+```html
347
+<!doctype html>
348
+<html>
349
+
350
+<head>
351
+  <meta charset="utf-8">
352
+  <title>Angular Electron</title>
353
+  <base href="./">
354
+
355
+  <meta name="viewport" content="width=device-width, initial-scale=1">
356
+  <link rel="icon" type="image/x-icon" href="assets/icons/favicon.ico">
357
+</head>
358
+
359
+<body>
360
+  <app-root></app-root>
361
+  <script>
362
+    // Monaco uses a custom amd loader that over-rides node's require.
363
+    // Keep a reference to node's require so we can restore it after executing the amd loader file.
364
+    var nodeRequire = require;
365
+  </script>
366
+  <script src="assets/monaco/min/vs/loader.js"></script>
367
+  <script type="text/javascript">
368
+    // Save Monaco's amd require and restore Node's require
369
+    var monacoRequire = require;
370
+    require = nodeRequire;
371
+    require.nodeRequire = require;
372
+  </script>
373
+</body>
374
+
375
+</html>
376
+```
377
+You just need to save monaco `require` function defined in `loader.js` somewhere and pass it to `monacoRequire` in configuration.
378
+```typescript
379
+import { NgModule } from '@angular/core';
380
+import { FormsModule } from '@angular/forms';
381
+import { BrowserModule } from '@angular/platform-browser';
382
+
383
+import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor';
384
+import { AppComponent } from './app.component';
385
+
386
+const monacoConfig: NgxMonacoEditorConfig = {
387
+  baseUrl: 'assets', 
388
+  requireConfig: { preferScriptTags: true },
389
+  monacoRequire: (window as any).monacoRequire
390
+};
391
+
392
+@NgModule({
393
+  declarations: [
394
+    AppComponent
395
+  ],
396
+  imports: [
397
+    BrowserModule,
398
+    FormsModule,
399
+    MonacoEditorModule.forRoot(monacoConfig)
400
+  ],
401
+  providers: [],
402
+  bootstrap: [AppComponent]
403
+})
404
+export class AppModule {
405
+}
406
+```
407
+
408
+## Links
409
+[Monaco Editor](https://github.com/Microsoft/monaco-editor/)<br/>
410
+[Monaco Editor Options](https://microsoft.github.io/monaco-editor/api/index.html)
411
+
412
+## License
413
+
414
+Natan © [Mamadou BAH](https://github.com/mamadou4bah or https://gitea.natan.fr/mamadoubah/)

+ 151
- 0
angular.json 查看文件

@@ -0,0 +1,151 @@
1
+{
2
+  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
3
+  "version": 1,
4
+  "newProjectRoot": "projects",
5
+  "projects": {
6
+    "editor-tester": {
7
+      "projectType": "application",
8
+      "schematics": {
9
+        "@schematics/angular:application": {
10
+          "strict": true
11
+        }
12
+      },
13
+      "root": "projects/editor-tester",
14
+      "sourceRoot": "projects/editor-tester/src",
15
+      "prefix": "app",
16
+      "architect": {
17
+        "build": {
18
+          "builder": "@angular-devkit/build-angular:browser",
19
+          "options": {
20
+            "outputPath": "dist/editor-tester",
21
+            "index": "projects/editor-tester/src/index.html",
22
+            "main": "projects/editor-tester/src/main.ts",
23
+            "polyfills": "projects/editor-tester/src/polyfills.ts",
24
+            "tsConfig": "projects/editor-tester/tsconfig.app.json",
25
+            "assets": [
26
+              "projects/editor-tester/src/favicon.ico",
27
+              "projects/editor-tester/src/assets",
28
+              {
29
+                "glob": "**/*",
30
+                "input": "node_modules/monaco-editor/min",
31
+                "output": "./assets/monaco/min"
32
+              },
33
+              {
34
+                "glob": "**/*",
35
+                "input": "node_modules/monaco-editor/min-maps",
36
+                "output": "./assets/monaco/min-maps"
37
+              }
38
+            ],
39
+            "styles": [
40
+              "projects/editor-tester/src/styles.css",
41
+              "node_modules/primeng/resources/themes/lara-light-blue/theme.css",
42
+              "node_modules/primeng/resources/primeng.min.css"
43
+            ],
44
+            "scripts": []
45
+          },
46
+          "configurations": {
47
+            "production": {
48
+              "budgets": [
49
+                {
50
+                  "type": "initial",
51
+                  "maximumWarning": "500kb",
52
+                  "maximumError": "1mb"
53
+                },
54
+                {
55
+                  "type": "anyComponentStyle",
56
+                  "maximumWarning": "2kb",
57
+                  "maximumError": "4kb"
58
+                }
59
+              ],
60
+              "fileReplacements": [
61
+                {
62
+                  "replace": "projects/editor-tester/src/environments/environment.ts",
63
+                  "with": "projects/editor-tester/src/environments/environment.prod.ts"
64
+                }
65
+              ],
66
+              "outputHashing": "all"
67
+            },
68
+            "development": {
69
+              "buildOptimizer": false,
70
+              "optimization": false,
71
+              "vendorChunk": true,
72
+              "extractLicenses": false,
73
+              "sourceMap": true,
74
+              "namedChunks": true
75
+            }
76
+          },
77
+          "defaultConfiguration": "production"
78
+        },
79
+        "serve": {
80
+          "builder": "@angular-devkit/build-angular:dev-server",
81
+          "configurations": {
82
+            "production": {
83
+              "browserTarget": "editor-tester:build:production"
84
+            },
85
+            "development": {
86
+              "browserTarget": "editor-tester:build:development"
87
+            }
88
+          },
89
+          "defaultConfiguration": "development"
90
+        },
91
+        "extract-i18n": {
92
+          "builder": "@angular-devkit/build-angular:extract-i18n",
93
+          "options": {
94
+            "browserTarget": "editor-tester:build"
95
+          }
96
+        },
97
+        "test": {
98
+          "builder": "@angular-devkit/build-angular:karma",
99
+          "options": {
100
+            "main": "projects/editor-tester/src/test.ts",
101
+            "polyfills": "projects/editor-tester/src/polyfills.ts",
102
+            "tsConfig": "projects/editor-tester/tsconfig.spec.json",
103
+            "karmaConfig": "projects/editor-tester/karma.conf.js",
104
+            "assets": [
105
+              "projects/editor-tester/src/favicon.ico",
106
+              "projects/editor-tester/src/assets"
107
+            ],
108
+            "styles": [
109
+              "projects/editor-tester/src/styles.css"
110
+            ],
111
+            "scripts": []
112
+          }
113
+        }
114
+      }
115
+    },
116
+    "editor": {
117
+      "projectType": "library",
118
+      "root": "projects/editor",
119
+      "sourceRoot": "projects/editor/src",
120
+      "prefix": "ngx-monaco",
121
+      "architect": {
122
+        "build": {
123
+          "builder": "@angular-devkit/build-angular:ng-packagr",
124
+          "options": {
125
+            "project": "projects/editor/ng-package.json"
126
+          },
127
+          "configurations": {
128
+            "production": {
129
+              "tsConfig": "projects/editor/tsconfig.lib.prod.json"
130
+            },
131
+            "development": {
132
+              "tsConfig": "projects/editor/tsconfig.lib.json"
133
+            }
134
+          },
135
+          "defaultConfiguration": "production"
136
+        },
137
+        "test": {
138
+          "builder": "@angular-devkit/build-angular:karma",
139
+          "options": {
140
+            "main": "projects/editor/src/test.ts",
141
+            "tsConfig": "projects/editor/tsconfig.spec.json",
142
+            "karmaConfig": "projects/editor/karma.conf.js"
143
+          }
144
+        }
145
+      }
146
+    }
147
+  },
148
+  "cli": {
149
+    "analytics": "72066354-96f9-4e53-bc61-34a393f09d37"
150
+  }
151
+}

+ 4
- 0
license 查看文件

@@ -0,0 +1,4 @@
1
+Natan License
2
+
3
+Copyright (c) Mamadou BAH <mamadou.bah@natan.fr> (https://github.com/mamadou4bah or https://gitea.natan.fr/mamadoubah)
4
+

+ 10
- 0
npm-publish 查看文件

@@ -0,0 +1,10 @@
1
+#!/bin/bash
2
+
3
+#login into private registry
4
+npm login
5
+
6
+npm publish ./dist/editor/ --access=public
7
+
8
+#logout when finished
9
+npm logout
10
+echo "Published successfully. Use 'npm install [package-name]' in the project you want to use it."

+ 21140
- 0
package-lock.json
文件差异内容过多而无法显示
查看文件


+ 71
- 0
package.json 查看文件

@@ -0,0 +1,71 @@
1
+{
2
+  "name": "ngx-monaco-editor-v2",
3
+  "version": "16.0.1",
4
+  "description": "PrimeNG and Monaco Code Editor For Angular",
5
+  "keywords": [
6
+    "angular",
7
+    "monaco",
8
+    "monaco-editor-v2",
9
+    "code",
10
+    "editor",
11
+    "reusable"
12
+  ],
13
+  "license": "NATAN",
14
+  "scripts": {
15
+    "ng": "ng",
16
+    "start": "run-s build-editor serve",
17
+    "serve": "ng serve",
18
+    "build": "ng build --project=editor-tester --base-href=editor-tester --configuration=development",
19
+    "build:prod": "ng build --project=editor-tester --base-href=editor-tester",
20
+    "lint": "ng lint",
21
+    "e2e": "ng e2e",
22
+    "build-and-deploy-demo": "npm run build:prod && cp dist/editor-tester/index.html dist/editor-tester/200.html && cd dist/editor-tester && surge --domain ngx-monaco-editor-v2.surge.sh",
23
+    "build-editor": "npm run copy:readme && ng build editor",
24
+    "npm-publish": "npm run build-editor && bash npm-publish",
25
+    "copy:readme": "cp -rf README.md projects/editor/"
26
+  },
27
+  "private": false,
28
+  "repository": {
29
+    "type": "git",
30
+    "url": "https://github.com/miki995/ngx-monaco-editor-v2.git"
31
+  },
32
+  "bugs": {
33
+    "url": "https://github.com/miki995/ngx-monaco-editor-v2/issues"
34
+  },
35
+  "author": "Mamadou BAH",
36
+  "contributors": [
37
+    "Mamadou BAH <mamadou.bah@natan.fr>"
38
+  ],
39
+  "dependencies": {
40
+    "@angular/animations": "^16.0.4",
41
+    "@angular/common": "^16.0.4",
42
+    "@angular/compiler": "^16.0.4",
43
+    "@angular/core": "^16.0.4",
44
+    "@angular/forms": "^16.0.4",
45
+    "@angular/platform-browser": "^16.0.4",
46
+    "@angular/platform-browser-dynamic": "^16.0.4",
47
+    "@angular/router": "^16.0.4",
48
+    "monaco-editor": "^0.41.0",
49
+    "primeng": "^15.2.0",
50
+    "rxjs": "^7.5.6",
51
+    "tslib": "^2.4.0",
52
+    "zone.js": "^0.13.0"
53
+  },
54
+  "devDependencies": {
55
+    "@angular-devkit/build-angular": "16.0.4",
56
+    "@angular/cli": "16.0.4",
57
+    "@angular/compiler-cli": "16.0.4",
58
+    "@angular/language-service": "16.0.4",
59
+    "@types/jasmine": "^3.6.0",
60
+    "@types/node": "^12.11.1",
61
+    "jasmine-core": "^3.7.0",
62
+    "karma": "^6.3.0",
63
+    "karma-chrome-launcher": "^3.1.0",
64
+    "karma-coverage": "^2.0.3",
65
+    "karma-jasmine": "^4.0.0",
66
+    "karma-jasmine-html-reporter": "^1.5.0",
67
+    "ng-packagr": "^16.0.1",
68
+    "npm-run-all": "^4.1.5",
69
+    "typescript": "5.0.4"
70
+  }
71
+}

+ 17
- 0
projects/editor-tester/.browserslistrc 查看文件

@@ -0,0 +1,17 @@
1
+# This file is used by the build system to adjust CSS and JS output to support the specified browsers below.
2
+# For additional information regarding the format and rule options, please see:
3
+# https://github.com/browserslist/browserslist#queries
4
+
5
+# For the full list of supported browsers by the Angular framework, please see:
6
+# https://angular.io/guide/browser-support
7
+
8
+# You can see what browsers were selected by your queries by running:
9
+#   npx browserslist
10
+
11
+last 1 Chrome version
12
+last 1 Firefox version
13
+last 2 Edge major versions
14
+last 2 Safari major versions
15
+last 2 iOS major versions
16
+Firefox ESR
17
+not IE 11 # Angular supports IE 11 only as an opt-in. To opt-in, remove the 'not' prefix on this line.

+ 44
- 0
projects/editor-tester/karma.conf.js 查看文件

@@ -0,0 +1,44 @@
1
+// Karma configuration file, see link for more information
2
+// https://karma-runner.github.io/1.0/config/configuration-file.html
3
+
4
+module.exports = function (config) {
5
+  config.set({
6
+    basePath: '',
7
+    frameworks: ['jasmine', '@angular-devkit/build-angular'],
8
+    plugins: [
9
+      require('karma-jasmine'),
10
+      require('karma-chrome-launcher'),
11
+      require('karma-jasmine-html-reporter'),
12
+      require('karma-coverage'),
13
+      require('@angular-devkit/build-angular/plugins/karma')
14
+    ],
15
+    client: {
16
+      jasmine: {
17
+        // you can add configuration options for Jasmine here
18
+        // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html
19
+        // for example, you can disable the random execution with `random: false`
20
+        // or set a specific seed with `seed: 4321`
21
+      },
22
+      clearContext: false // leave Jasmine Spec Runner output visible in browser
23
+    },
24
+    jasmineHtmlReporter: {
25
+      suppressAll: true // removes the duplicated traces
26
+    },
27
+    coverageReporter: {
28
+      dir: require('path').join(__dirname, '../../coverage/editor-tester'),
29
+      subdir: '.',
30
+      reporters: [
31
+        { type: 'html' },
32
+        { type: 'text-summary' }
33
+      ]
34
+    },
35
+    reporters: ['progress', 'kjhtml'],
36
+    port: 9876,
37
+    colors: true,
38
+    logLevel: config.LOG_INFO,
39
+    autoWatch: true,
40
+    browsers: ['Chrome'],
41
+    singleRun: false,
42
+    restartOnFileChange: true
43
+  });
44
+};

+ 138
- 0
projects/editor-tester/src/app/app.component.ts 查看文件

@@ -0,0 +1,138 @@
1
+import { Component, OnInit } from '@angular/core';
2
+import { DiffEditorModel, NgxEditorModel } from '../../../editor/src/lib/types';
3
+
4
+declare var monaco: any;
5
+
6
+@Component({
7
+  selector: 'app-root',
8
+  template: `
9
+    <h1>Editor</h1>
10
+    <p-button (click)="javaLanguage()" label="Java"></p-button>&nbsp;&nbsp;&nbsp;
11
+    <p-button (click)="jsLanguage()" label="JavaScript"></p-button>&nbsp;&nbsp;&nbsp;
12
+    <p-button (click)="cSharpLanguage()" label="C#"></p-button>&nbsp;&nbsp;&nbsp;
13
+    <p-button (click)="pythonLanguage()" label="Python"></p-button>&nbsp;&nbsp;&nbsp;
14
+    <p-button (click)="typeScriptLanguage()" label="TypeScript"></p-button>&nbsp;&nbsp;&nbsp;
15
+    <p-button (click)="code = ''; codeInput=''" label="Set Value To Empty String"></p-button>&nbsp;&nbsp;&nbsp;
16
+    <p-button (click)="showMultiple = !showMultiple" label=" Multiple Editor">{{showMultiple ? 'Hide ' : 'Show '}}</p-button>
17
+
18
+    <div style="height: 100px">
19
+        <ngx-monaco-editor style="height: 100%" [options]="options" [(ngModel)]="code" (onInit)="onInit($event)"></ngx-monaco-editor>
20
+    </div>
21
+
22
+    <ngx-monaco-editor *ngIf="showMultiple" [options]="options" [(ngModel)]="code"></ngx-monaco-editor>
23
+
24
+    <pre>{{code | json}}</pre>
25
+
26
+    <h1>Diff Editor</h1>
27
+    <p-button (click)="updateDiffModel()" label="Update Models"></p-button>
28
+    <ngx-monaco-diff-editor [options]="options" [originalModel]="originalModel" [modifiedModel]="modifiedModel"
29
+                            (onInit)="onInitDiffEditor($event)"></ngx-monaco-diff-editor>
30
+
31
+    <ngx-monaco-editor [options]="options" [model]="model"></ngx-monaco-editor>
32
+  `,
33
+  styles: []
34
+})
35
+export class AppComponent implements OnInit {
36
+  codeInput = 'Sample Code';
37
+  editor: any;
38
+  diffEditor: any;
39
+  showMultiple = false;
40
+  toggleLanguage = true;
41
+  options = {
42
+    theme: 'vs-dark'
43
+  };
44
+
45
+  code: string;
46
+
47
+  javaCode = `public void hello() {
48
+   System.out.println ("Hello world!");
49
+}`;
50
+
51
+  jsCode = `function hello() {
52
+	 alert('Hello world!');
53
+}`;
54
+
55
+typeScriptCode = `function hello() {
56
+  console.log('Hello world!');
57
+}`;
58
+
59
+  cSharpCode = `static void Main() {
60
+  Console.WriteLine("Hello World!");
61
+}`;
62
+
63
+pythonCode = `print("Hello, World!")`;
64
+
65
+  originalModel: DiffEditorModel = {
66
+    code: 'Hello world!',
67
+    language: 'text/plain'
68
+  };
69
+
70
+  modifiedModel: DiffEditorModel = {
71
+    code: 'hello orlando!',
72
+    language: 'text/plain'
73
+  };
74
+
75
+  jsonCode = [
76
+    '{',
77
+    '    "p1": "v3",',
78
+    '    "p2": false',
79
+    '}'
80
+  ].join('\n');
81
+
82
+  model: NgxEditorModel = {
83
+    value: this.jsonCode,
84
+    language: 'json'
85
+  };
86
+
87
+  constructor() {
88
+  }
89
+
90
+  ngOnInit() {
91
+    this.javaLanguage();
92
+  }
93
+
94
+  javaLanguage() {
95
+      this.code = this.javaCode;
96
+      this.options = Object.assign({}, this.options, { language: 'java' });
97
+  }
98
+
99
+  jsLanguage() {
100
+    this.code = this.jsCode;
101
+    this.options = Object.assign({}, this.options, { language: 'javascript' });
102
+  }
103
+
104
+  cSharpLanguage() {
105
+      this.code = this.cSharpCode;
106
+      this.options = Object.assign({}, this.options, { language: 'cSharp' });
107
+  }
108
+
109
+  typeScriptLanguage() {
110
+    this.code = this.typeScriptCode;
111
+    this.options = Object.assign({}, this.options, { language: 'TypeScript' });
112
+  }
113
+
114
+  pythonLanguage() {
115
+    this.code = this.pythonCode;
116
+    this.options = Object.assign({}, this.options, { language: 'Python' });
117
+  }
118
+
119
+  updateDiffModel() {
120
+    this.originalModel = Object.assign({}, this.originalModel, { code: 'abcd' });
121
+    this.modifiedModel = Object.assign({}, this.originalModel, { code: 'ABCD ef' });
122
+  }
123
+
124
+  onInit(editor) {
125
+    this.editor = editor;
126
+    console.log(editor);
127
+    this.model = {
128
+      value: this.jsonCode,
129
+      language: 'json',
130
+      uri: monaco.Uri.parse('a://b/foo.json')
131
+    };
132
+  }
133
+
134
+  onInitDiffEditor(editor) {
135
+    this.diffEditor = editor;
136
+    console.log(editor);
137
+  }
138
+}

+ 73
- 0
projects/editor-tester/src/app/app.module.ts 查看文件

@@ -0,0 +1,73 @@
1
+import { NgModule } from '@angular/core';
2
+import { FormsModule } from '@angular/forms';
3
+import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
4
+import { BrowserModule } from '@angular/platform-browser';
5
+import { ButtonModule } from 'primeng/button';
6
+import {DropdownModule} from 'primeng/dropdown';
7
+
8
+import { AppComponent } from './app.component';
9
+import { NgxMonacoEditorConfig } from '../../../editor/src/lib/config';
10
+import { MonacoEditorModule } from '../../../editor/src/lib/editor.module';
11
+
12
+declare var monaco: any;
13
+
14
+export function onMonacoLoad() {
15
+
16
+  console.log((window as any).monaco);
17
+
18
+  const uri = monaco.Uri.parse('a://b/foo.json');
19
+  monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
20
+    validate: true,
21
+    schemas: [{
22
+      uri: 'http://myserver/foo-schema.json',
23
+      fileMatch: [uri.toString()],
24
+      schema: {
25
+        type: 'object',
26
+        properties: {
27
+          p1: {
28
+            enum: ['v1', 'v2']
29
+          },
30
+          p2: {
31
+            $ref: 'http://myserver/bar-schema.json'
32
+          }
33
+        }
34
+      }
35
+    }, {
36
+      uri: 'http://myserver/bar-schema.json',
37
+      fileMatch: [uri.toString()],
38
+      schema: {
39
+        type: 'object',
40
+        properties: {
41
+          q1: {
42
+            enum: ['x1', 'x2']
43
+          }
44
+        }
45
+      }
46
+    }]
47
+  });
48
+
49
+}
50
+
51
+const monacoConfig: NgxMonacoEditorConfig = {
52
+  baseUrl: 'assets',
53
+  defaultOptions: { scrollBeyondLastLine: false },
54
+  onMonacoLoad
55
+};
56
+
57
+@NgModule({
58
+  declarations: [
59
+    AppComponent
60
+  ],
61
+  imports: [
62
+    BrowserModule,
63
+    FormsModule,
64
+    ButtonModule,
65
+    DropdownModule,
66
+    BrowserAnimationsModule,
67
+    MonacoEditorModule.forRoot(monacoConfig)
68
+  ],
69
+  providers: [],
70
+  bootstrap: [AppComponent]
71
+})
72
+export class AppModule {
73
+}

+ 0
- 0
projects/editor-tester/src/assets/.gitkeep 查看文件


+ 3
- 0
projects/editor-tester/src/environments/environment.prod.ts 查看文件

@@ -0,0 +1,3 @@
1
+export const environment = {
2
+  production: true
3
+};

+ 16
- 0
projects/editor-tester/src/environments/environment.ts 查看文件

@@ -0,0 +1,16 @@
1
+// This file can be replaced during build by using the `fileReplacements` array.
2
+// `ng build` replaces `environment.ts` with `environment.prod.ts`.
3
+// The list of file replacements can be found in `angular.json`.
4
+
5
+export const environment = {
6
+  production: false
7
+};
8
+
9
+/*
10
+ * For easier debugging in development mode, you can import the following file
11
+ * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`.
12
+ *
13
+ * This import should be commented out in production mode because it will have a negative impact
14
+ * on performance if an error is thrown.
15
+ */
16
+// import 'zone.js/plugins/zone-error';  // Included with Angular CLI.

二进制
projects/editor-tester/src/favicon.ico 查看文件


+ 13
- 0
projects/editor-tester/src/index.html 查看文件

@@ -0,0 +1,13 @@
1
+<!doctype html>
2
+<html lang="en">
3
+<head>
4
+  <meta charset="utf-8">
5
+  <title>NgxMonacoEditor</title>
6
+  <base href="/">
7
+  <meta name="viewport" content="width=device-width, initial-scale=1">
8
+  <link rel="icon" type="image/x-icon" href="favicon.ico">
9
+</head>
10
+<body>
11
+  <app-root></app-root>
12
+</body>
13
+</html>

+ 12
- 0
projects/editor-tester/src/main.ts 查看文件

@@ -0,0 +1,12 @@
1
+import { enableProdMode } from '@angular/core';
2
+import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
3
+
4
+import { AppModule } from './app/app.module';
5
+import { environment } from './environments/environment';
6
+
7
+if (environment.production) {
8
+  enableProdMode();
9
+}
10
+
11
+platformBrowserDynamic().bootstrapModule(AppModule)
12
+  .catch(err => console.error(err));

+ 53
- 0
projects/editor-tester/src/polyfills.ts 查看文件

@@ -0,0 +1,53 @@
1
+/**
2
+ * This file includes polyfills needed by Angular and is loaded before the app.
3
+ * You can add your own extra polyfills to this file.
4
+ *
5
+ * This file is divided into 2 sections:
6
+ *   1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers.
7
+ *   2. Application imports. Files imported after ZoneJS that should be loaded before your main
8
+ *      file.
9
+ *
10
+ * The current setup is for so-called "evergreen" browsers; the last versions of browsers that
11
+ * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera),
12
+ * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile.
13
+ *
14
+ * Learn more in https://angular.io/guide/browser-support
15
+ */
16
+
17
+/***************************************************************************************************
18
+ * BROWSER POLYFILLS
19
+ */
20
+
21
+/**
22
+ * By default, zone.js will patch all possible macroTask and DomEvents
23
+ * user can disable parts of macroTask/DomEvents patch by setting following flags
24
+ * because those flags need to be set before `zone.js` being loaded, and webpack
25
+ * will put import in the top of bundle, so user need to create a separate file
26
+ * in this directory (for example: zone-flags.ts), and put the following flags
27
+ * into that file, and then add the following code before importing zone.js.
28
+ * import './zone-flags';
29
+ *
30
+ * The flags allowed in zone-flags.ts are listed here.
31
+ *
32
+ * The following flags will work for all browsers.
33
+ *
34
+ * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame
35
+ * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick
36
+ * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames
37
+ *
38
+ *  in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js
39
+ *  with the following flag, it will bypass `zone.js` patch for IE/Edge
40
+ *
41
+ *  (window as any).__Zone_enable_cross_context_check = true;
42
+ *
43
+ */
44
+
45
+/***************************************************************************************************
46
+ * Zone JS is required by default for Angular itself.
47
+ */
48
+import 'zone.js';  // Included with Angular CLI.
49
+
50
+
51
+/***************************************************************************************************
52
+ * APPLICATION IMPORTS
53
+ */

+ 7
- 0
projects/editor-tester/src/styles.css 查看文件

@@ -0,0 +1,7 @@
1
+/* You can add global styles to this file, and also import other style files */
2
+@import "primeng/resources/themes/lara-light-blue/theme.css";
3
+@import "primeng/resources/primeng.css";
4
+
5
+body {
6
+    font-family: var(--font-family);
7
+}

+ 27
- 0
projects/editor-tester/src/test.ts 查看文件

@@ -0,0 +1,27 @@
1
+// This file is required by karma.conf.js and loads recursively all the .spec and framework files
2
+
3
+import 'zone.js/testing';
4
+import { getTestBed } from '@angular/core/testing';
5
+import {
6
+  BrowserDynamicTestingModule,
7
+  platformBrowserDynamicTesting
8
+} from '@angular/platform-browser-dynamic/testing';
9
+
10
+declare const require: {
11
+  context(path: string, deep?: boolean, filter?: RegExp): {
12
+    keys(): string[];
13
+    <T>(id: string): T;
14
+  };
15
+};
16
+
17
+// First, initialize the Angular testing environment.
18
+getTestBed().initTestEnvironment(
19
+  BrowserDynamicTestingModule,
20
+  platformBrowserDynamicTesting(), {
21
+    teardown: { destroyAfterEach: false }
22
+}
23
+);
24
+// Then we find all the tests.
25
+const context = require.context('./', true, /\.spec\.ts$/);
26
+// And load the modules.
27
+context.keys().map(context);

+ 15
- 0
projects/editor-tester/tsconfig.app.json 查看文件

@@ -0,0 +1,15 @@
1
+/* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+{
3
+  "extends": "../../tsconfig.json",
4
+  "compilerOptions": {
5
+    "outDir": "../../out-tsc/app",
6
+    "types": []
7
+  },
8
+  "files": [
9
+    "src/main.ts",
10
+    "src/polyfills.ts"
11
+  ],
12
+  "include": [
13
+    "src/**/*.d.ts"
14
+  ]
15
+}

+ 18
- 0
projects/editor-tester/tsconfig.spec.json 查看文件

@@ -0,0 +1,18 @@
1
+/* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+{
3
+  "extends": "../../tsconfig.json",
4
+  "compilerOptions": {
5
+    "outDir": "../../out-tsc/spec",
6
+    "types": [
7
+      "jasmine"
8
+    ]
9
+  },
10
+  "files": [
11
+    "src/test.ts",
12
+    "src/polyfills.ts"
13
+  ],
14
+  "include": [
15
+    "src/**/*.spec.ts",
16
+    "src/**/*.d.ts"
17
+  ]
18
+}

+ 414
- 0
projects/editor/README.md 查看文件

@@ -0,0 +1,414 @@
1
+# Monaco Editor Component for Angular 2 and above.
2
+
3
+ - Angular <= 4: v3.x.x
4
+ - Angular 5: v5.x.x
5
+ - Angular 6: v6.x.x
6
+ - Angular 7: v7.x.x
7
+ - Angular 8: v8.x.x
8
+ - Angular 9: v9.x.x
9
+ - Angular 10: v10.x.x 
10
+ - Angular 12: v12.x.x
11
+ - Angular 13: not supported ( best to use 14, otherwise contact author)
12
+ - Angular 14: v14.x.x
13
+ - Angular 15: v15.x.x
14
+ - Angular 16: v16.x.x
15
+
16
+Using this Module you can utilize the Monaco Editor as an Angular Component. Feel free to contribute, raise feature requests and make it better.
17
+
18
+Supports all the options available in monaco-editor [Monaco Editor Options](https://microsoft.github.io/monaco-editor/api/index.html)
19
+
20
+## Demo
21
+
22
+https://ngx-monaco-editor-v2.surge.sh/
23
+
24
+![img.png](img.png)
25
+
26
+## Setup
27
+
28
+### Installation
29
+
30
+Install from npm repository:
31
+```
32
+npm install monaco-editor ngx-monaco-editor-v2 --save
33
+ ```
34
+
35
+Breaking change from v10, is to use monaco-editor next to ngx-monaco-editor-v2 in your package.json file.
36
+ 
37
+For angular version 6 use v6.x.x
38
+```
39
+npm install ngx-monaco-editor-v2@6.0.0 --save
40
+ ```
41
+
42
+Add the glob to assets in `angular.json`
43
+```typescript
44
+{
45
+  "apps": [
46
+    {
47
+      "assets": [
48
+      { "glob": "**/*", "input": "node_modules/monaco-editor", "output": "/assets/monaco/" }
49
+      ],
50
+      ...
51
+    }
52
+    ...
53
+  ],
54
+  ...
55
+}
56
+ ```
57
+
58
+
59
+For Angular 6 and below, add the glob to assets in `.angular-cli.json` schema - `projects.[project-name].architect.build` (to make monaco-editor lib available to the app):
60
+```typescript
61
+{
62
+  "options":{
63
+        {"assets": [
64
+          { "glob": "**/*", "input": "node_modules/ngx-monaco-editor/assets/monaco", "output": "./assets/monaco/" }
65
+
66
+        ],
67
+        ...
68
+        }
69
+    ...
70
+    },
71
+  ...
72
+}
73
+ ```
74
+
75
+### Sample
76
+Include MonacoEditorModule in Main Module and Feature Modules where you want to use the editor component.(eg: app.module.ts): 
77
+```typescript
78
+import { BrowserModule } from '@angular/platform-browser';
79
+import { NgModule } from '@angular/core';
80
+import { FormsModule } from '@angular/forms';
81
+
82
+import { AppComponent } from './app.component';
83
+import { MonacoEditorModule } from 'ngx-monaco-editor-v2';
84
+
85
+@NgModule({
86
+  declarations: [
87
+    AppComponent
88
+  ],
89
+  imports: [
90
+    BrowserModule,
91
+    FormsModule,
92
+    MonacoEditorModule.forRoot() // use forRoot() in main app module only.
93
+  ],
94
+  providers: [],
95
+  bootstrap: [AppComponent]
96
+})
97
+export class AppModule {
98
+}
99
+```
100
+
101
+Create Editor options in component.(eg: app.component.ts)
102
+```typescript
103
+import { Component } from '@angular/core';
104
+
105
+@Component({
106
+  selector: 'app-root',
107
+  templateUrl: './app.component.html'
108
+})
109
+export class AppComponent {
110
+  editorOptions = {theme: 'vs-dark', language: 'javascript'};
111
+  code: string= 'function x() {\nconsole.log("Hello world!");\n}';
112
+}
113
+```
114
+Include editor in html with options and ngModel bindings.(eg: app.component.html)
115
+```html
116
+<ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
117
+```
118
+
119
+Include diff-editor in html with options.(eg: app.component.html)
120
+```html
121
+<ngx-monaco-diff-editor [options]="options" [originalModel]="originalModel" [modifiedModel]="modifiedModel"></ngx-monaco-diff-editor>
122
+```
123
+```typescript
124
+import { Component } from '@angular/core';
125
+import { DiffEditorModel } from 'ngx-monaco-editor';
126
+
127
+@Component({
128
+  selector: 'app-root',
129
+  templateUrl: './app.component.html'
130
+})
131
+export class AppComponent {
132
+  options = {
133
+    theme: 'vs-dark'
134
+  };
135
+  originalModel: DiffEditorModel = {
136
+    code: 'heLLo world!',
137
+    language: 'text/plain'
138
+  };
139
+
140
+  modifiedModel: DiffEditorModel = {
141
+    code: 'hello orlando!',
142
+    language: 'text/plain'
143
+  };
144
+}
145
+```
146
+
147
+### Styling
148
+To match height of container element add height: 100% and wrap in container
149
+```html
150
+<div style="height: 500px">
151
+    <ngx-monaco-editor style="height: 100%" [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
152
+</div>
153
+```
154
+Add class to editor tag. (eg. class="my-code-editor")
155
+```html
156
+<ngx-monaco-editor class="my-code-editor" [options]="editorOptions" [(ngModel)]="code"></ngx-monaco-editor>
157
+```
158
+Add styling in css/scss file:
159
+```scss
160
+.my-code-editor {
161
+  .editor-container {
162
+    height: calc(100vh - 100px);
163
+  }
164
+}
165
+```
166
+Set automaticLayout option to adjust editor size dynamically. Recommended when using in modal dialog or tabs where editor is not visible initially.
167
+
168
+### Events
169
+Output event (onInit) expose editor instance that can be used for performing custom operations on the editor. 
170
+```html
171
+<ngx-monaco-editor [options]="editorOptions" [(ngModel)]="code" (onInit)="onInit($event)"></ngx-monaco-editor>
172
+```
173
+
174
+```typescript
175
+export class AppComponent {
176
+  editorOptions = {theme: 'vs-dark', language: 'javascript'};
177
+  code: string= 'function x() {\nconsole.log("Hello world!");\n}';
178
+  onInit(editor) {
179
+      let line = editor.getPosition();
180
+      console.log(line);
181
+    }
182
+}
183
+```
184
+
185
+## Configurations
186
+`forRoot()` method of MonacoEditorModule accepts config of type `NgxMonacoEditorConfig`.
187
+```typescript
188
+import { NgModule } from '@angular/core';
189
+import { FormsModule } from '@angular/forms';
190
+import { BrowserModule } from '@angular/platform-browser';
191
+
192
+import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor';
193
+import { AppComponent } from './app.component';
194
+
195
+const monacoConfig: NgxMonacoEditorConfig = {
196
+  baseUrl: 'app-name/assets', // configure base path for monaco editor. Starting with version 8.0.0 it defaults to './assets'. Previous releases default to '/assets'
197
+  defaultOptions: { scrollBeyondLastLine: false }, // pass default options to be used
198
+  onMonacoLoad: () => { console.log((<any>window).monaco); } // here monaco object will be available as window.monaco use this function to extend monaco editor functionalities.
199
+  requireConfig: { preferScriptTags: true } // allows to oweride configuration passed to monacos loader
200
+  monacoRequire: (<any>window).monacoRequire // pass here monacos require function if you loaded monacos loader (loader.js) yourself 
201
+};
202
+
203
+@NgModule({
204
+  declarations: [
205
+    AppComponent
206
+  ],
207
+  imports: [
208
+    BrowserModule,
209
+    FormsModule,
210
+    MonacoEditorModule.forRoot(monacoConfig)
211
+  ],
212
+  providers: [],
213
+  bootstrap: [AppComponent]
214
+})
215
+export class AppModule {
216
+}
217
+```
218
+
219
+### Configure JSON Defaults
220
+`onMonacoLoad` property of `NgxMonacoEditorConfig` can be used to configure JSON default.
221
+```typescript
222
+import { NgModule } from '@angular/core';
223
+import { FormsModule } from '@angular/forms';
224
+import { BrowserModule } from '@angular/platform-browser';
225
+
226
+import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor';
227
+import { AppComponent } from './app.component';
228
+
229
+export function onMonacoLoad() {
230
+
231
+  console.log((window as any).monaco);
232
+
233
+  const uri = monaco.Uri.parse('a://b/foo.json');
234
+  monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
235
+    validate: true,
236
+    schemas: [{
237
+      uri: 'http://myserver/foo-schema.json',
238
+      fileMatch: [uri.toString()],
239
+      schema: {
240
+        type: 'object',
241
+        properties: {
242
+          p1: {
243
+            enum: ['v1', 'v2']
244
+          },
245
+          p2: {
246
+            $ref: 'http://myserver/bar-schema.json'
247
+          }
248
+        }
249
+      }
250
+    }, {
251
+      uri: 'http://myserver/bar-schema.json',
252
+      fileMatch: [uri.toString()],
253
+      schema: {
254
+        type: 'object',
255
+        properties: {
256
+          q1: {
257
+            enum: ['x1', 'x2']
258
+          }
259
+        }
260
+      }
261
+    }]
262
+  });
263
+
264
+}
265
+
266
+const monacoConfig: NgxMonacoEditorConfig = {
267
+  baseUrl: 'assets',
268
+  defaultOptions: { scrollBeyondLastLine: false },
269
+  onMonacoLoad
270
+};
271
+
272
+@NgModule({
273
+  declarations: [
274
+    AppComponent
275
+  ],
276
+  imports: [
277
+    BrowserModule,
278
+    FormsModule,
279
+    MonacoEditorModule.forRoot(monacoConfig)
280
+  ],
281
+  providers: [],
282
+  bootstrap: [AppComponent]
283
+})
284
+export class AppModule {
285
+}
286
+```
287
+
288
+Now pass model config of type `NgxEditorModel` to Editor Component
289
+```typescript
290
+@Component({
291
+  selector: 'app-root',
292
+  template: `<ngx-monaco-editor [options]="options" [model]="model"></ngx-monaco-editor>`,
293
+  styles: []
294
+})
295
+export class AppComponent {
296
+  options = {
297
+    theme: 'vs-dark'
298
+  };
299
+  
300
+  jsonCode = [
301
+    '{',
302
+    '    "p1": "v3",',
303
+    '    "p2": false',
304
+    '}'
305
+  ].join('\n');
306
+
307
+  model: NgxEditorModel = {
308
+    value: this.jsonCode,
309
+    language: 'json',
310
+    uri: monaco.Uri.parse('a://b/foo.json')
311
+  };
312
+}
313
+```
314
+
315
+### Configuration for Electron
316
+If you expose node's `require` in your render process, monaco will try to use its `NodeScriptLoader` and fail to load its files. To presuade it to use its `BrowserScriptLoader` instead it is necessery to set `preferScriptTags` to true.
317
+```typescript
318
+import { NgModule } from '@angular/core';
319
+import { FormsModule } from '@angular/forms';
320
+import { BrowserModule } from '@angular/platform-browser';
321
+
322
+import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor';
323
+import { AppComponent } from './app.component';
324
+
325
+const monacoConfig: NgxMonacoEditorConfig = {
326
+  baseUrl: 'assets', 
327
+  requireConfig: { preferScriptTags: true }
328
+};
329
+
330
+@NgModule({
331
+  declarations: [
332
+    AppComponent
333
+  ],
334
+  imports: [
335
+    BrowserModule,
336
+    FormsModule,
337
+    MonacoEditorModule.forRoot(monacoConfig)
338
+  ],
339
+  providers: [],
340
+  bootstrap: [AppComponent]
341
+})
342
+export class AppModule {
343
+}
344
+```
345
+If for some reason you want to load monaco yourself. 
346
+```html
347
+<!doctype html>
348
+<html>
349
+
350
+<head>
351
+  <meta charset="utf-8">
352
+  <title>Angular Electron</title>
353
+  <base href="./">
354
+
355
+  <meta name="viewport" content="width=device-width, initial-scale=1">
356
+  <link rel="icon" type="image/x-icon" href="assets/icons/favicon.ico">
357
+</head>
358
+
359
+<body>
360
+  <app-root></app-root>
361
+  <script>
362
+    // Monaco uses a custom amd loader that over-rides node's require.
363
+    // Keep a reference to node's require so we can restore it after executing the amd loader file.
364
+    var nodeRequire = require;
365
+  </script>
366
+  <script src="assets/monaco/min/vs/loader.js"></script>
367
+  <script type="text/javascript">
368
+    // Save Monaco's amd require and restore Node's require
369
+    var monacoRequire = require;
370
+    require = nodeRequire;
371
+    require.nodeRequire = require;
372
+  </script>
373
+</body>
374
+
375
+</html>
376
+```
377
+You just need to save monaco `require` function defined in `loader.js` somewhere and pass it to `monacoRequire` in configuration.
378
+```typescript
379
+import { NgModule } from '@angular/core';
380
+import { FormsModule } from '@angular/forms';
381
+import { BrowserModule } from '@angular/platform-browser';
382
+
383
+import { MonacoEditorModule, NgxMonacoEditorConfig } from 'ngx-monaco-editor';
384
+import { AppComponent } from './app.component';
385
+
386
+const monacoConfig: NgxMonacoEditorConfig = {
387
+  baseUrl: 'assets', 
388
+  requireConfig: { preferScriptTags: true },
389
+  monacoRequire: (window as any).monacoRequire
390
+};
391
+
392
+@NgModule({
393
+  declarations: [
394
+    AppComponent
395
+  ],
396
+  imports: [
397
+    BrowserModule,
398
+    FormsModule,
399
+    MonacoEditorModule.forRoot(monacoConfig)
400
+  ],
401
+  providers: [],
402
+  bootstrap: [AppComponent]
403
+})
404
+export class AppModule {
405
+}
406
+```
407
+
408
+## Links
409
+[Monaco Editor](https://github.com/Microsoft/monaco-editor/)<br/>
410
+[Monaco Editor Options](https://microsoft.github.io/monaco-editor/api/index.html)
411
+
412
+## License
413
+
414
+Natan © [Mamadou BAH ](https://github.com/mamadou4bah or https://gitea.natan.fr/mamadoubah)

+ 44
- 0
projects/editor/karma.conf.js 查看文件

@@ -0,0 +1,44 @@
1
+// Karma configuration file, see link for more information
2
+// https://karma-runner.github.io/1.0/config/configuration-file.html
3
+
4
+module.exports = function (config) {
5
+  config.set({
6
+    basePath: '',
7
+    frameworks: ['jasmine', '@angular-devkit/build-angular'],
8
+    plugins: [
9
+      require('karma-jasmine'),
10
+      require('karma-chrome-launcher'),
11
+      require('karma-jasmine-html-reporter'),
12
+      require('karma-coverage'),
13
+      require('@angular-devkit/build-angular/plugins/karma')
14
+    ],
15
+    client: {
16
+      jasmine: {
17
+        // you can add configuration options for Jasmine here
18
+        // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html
19
+        // for example, you can disable the random execution with `random: false`
20
+        // or set a specific seed with `seed: 4321`
21
+      },
22
+      clearContext: false // leave Jasmine Spec Runner output visible in browser
23
+    },
24
+    jasmineHtmlReporter: {
25
+      suppressAll: true // removes the duplicated traces
26
+    },
27
+    coverageReporter: {
28
+      dir: require('path').join(__dirname, '../../coverage/editor'),
29
+      subdir: '.',
30
+      reporters: [
31
+        { type: 'html' },
32
+        { type: 'text-summary' }
33
+      ]
34
+    },
35
+    reporters: ['progress', 'kjhtml'],
36
+    port: 9876,
37
+    colors: true,
38
+    logLevel: config.LOG_INFO,
39
+    autoWatch: true,
40
+    browsers: ['Chrome'],
41
+    singleRun: false,
42
+    restartOnFileChange: true
43
+  });
44
+};

+ 7
- 0
projects/editor/ng-package.json 查看文件

@@ -0,0 +1,7 @@
1
+{
2
+  "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
3
+  "dest": "../../dist/editor",
4
+  "lib": {
5
+    "entryFile": "src/public-api.ts"
6
+  }
7
+}

+ 48
- 0
projects/editor/package.json 查看文件

@@ -0,0 +1,48 @@
1
+{
2
+  "name": "ngx-monaco-editor-v2",
3
+  "version": "16.0.1",
4
+  "private": false,
5
+  "description": "Monaco Code Editor for Angular",
6
+  "keywords": [
7
+    "angular",
8
+    "angular 2+",
9
+    "angular 2",
10
+    "angular 4",
11
+    "angular 5",
12
+    "angular 6",
13
+    "angular 7",
14
+    "angular 8",
15
+    "angular 9",
16
+    "angular 10",
17
+    "angular 12",
18
+    "angular 14",
19
+    "angular 15",
20
+    "angular 16",
21
+    "monaco",
22
+    "monaco-editor",
23
+    "code",
24
+    "editor",
25
+    "reusable",
26
+    "component"
27
+  ],
28
+  "repository": {
29
+    "type": "git",
30
+    "url": "https://github.com/miki995/ngx-monaco-editor-v2.git"
31
+  },
32
+  "bugs": {
33
+    "url": "https://github.com/miki995/ngx-monaco-editor-v2/issues"
34
+  },
35
+  "license": "NATAN",
36
+  "author": "Mamadou BAH",
37
+  "contributors": [
38
+    "Mamadou BAH <mamadou.bah@natan.fr>"
39
+  ],
40
+  "peerDependencies": {
41
+    "@angular/common": "^16.0.4",
42
+    "@angular/core": "^16.0.4",
43
+    "monaco-editor": "^0.41.0"
44
+  },
45
+  "dependencies": {
46
+    "tslib": "^2.1.0"
47
+  }
48
+}

+ 126
- 0
projects/editor/src/lib/base-editor.ts 查看文件

@@ -0,0 +1,126 @@
1
+import {
2
+  AfterViewInit,
3
+  Component,
4
+  ElementRef,
5
+  EventEmitter,
6
+  Inject,
7
+  Input,
8
+  OnDestroy,
9
+  Output,
10
+  ViewChild
11
+} from '@angular/core';
12
+import { Subscription } from 'rxjs';
13
+import { NGX_MONACO_EDITOR_CONFIG, NgxMonacoEditorConfig } from './config';
14
+
15
+let loadedMonaco = false;
16
+let loadPromise: Promise<void>;
17
+
18
+@Component({
19
+  template: ''
20
+})
21
+export abstract class BaseEditor implements AfterViewInit, OnDestroy {
22
+
23
+  @Input('insideNg')
24
+  set insideNg(insideNg: boolean) {
25
+    this._insideNg = insideNg;
26
+    if (this._editor) {
27
+      this._editor.dispose();
28
+      this.initMonaco(this._options, this.insideNg);
29
+    }
30
+  }
31
+
32
+  get insideNg(): boolean {
33
+    return this._insideNg;
34
+  }
35
+
36
+  @ViewChild('editorContainer', { static: true }) _editorContainer: ElementRef;
37
+  @Output() onInit = new EventEmitter<any>();
38
+  protected _editor: any;
39
+  protected _options: any;
40
+  protected _windowResizeSubscription: Subscription;
41
+  private _insideNg: boolean = false;
42
+
43
+  constructor(@Inject(NGX_MONACO_EDITOR_CONFIG) protected config: NgxMonacoEditorConfig) {}
44
+
45
+  ngAfterViewInit(): void {
46
+    if (loadedMonaco) {
47
+      // Wait until monaco editor is available
48
+      loadPromise.then(() => {
49
+        this.initMonaco(this._options, this.insideNg);
50
+      });
51
+    } else {
52
+      loadedMonaco = true;
53
+      loadPromise = new Promise<void>((resolve: any) => {
54
+        const baseUrl = this.config.baseUrl || "./assets";
55
+        if (typeof ((<any>window).monaco) === 'object') {
56
+          this.initMonaco(this._options, this.insideNg);
57
+          resolve();
58
+          return;
59
+        }
60
+        const onGotAmdLoader: any = (require?: any) => {
61
+          let usedRequire = require || (<any>window).require;
62
+          let requireConfig = { paths: { vs: `${baseUrl}/monaco/min/vs` } };
63
+          Object.assign(requireConfig, this.config.requireConfig || {});
64
+
65
+          // Load monaco
66
+          usedRequire.config(requireConfig);
67
+          usedRequire([`vs/editor/editor.main`], () => {
68
+            if (typeof this.config.onMonacoLoad === 'function') {
69
+              this.config.onMonacoLoad();
70
+            }
71
+            this.initMonaco(this._options, this.insideNg);
72
+            resolve();
73
+          });
74
+        };
75
+
76
+        if (this.config.monacoRequire) {
77
+          onGotAmdLoader(this.config.monacoRequire);
78
+        // Load AMD loader if necessary
79
+        } else if (!(<any>window).require) {
80
+          const loaderScript: HTMLScriptElement = document.createElement('script');
81
+          loaderScript.type = 'text/javascript';
82
+          loaderScript.src = `${baseUrl}/monaco/min/vs/loader.js`;
83
+          loaderScript.addEventListener('load', () => { onGotAmdLoader(); });
84
+          document.body.appendChild(loaderScript);
85
+        // Load AMD loader without over-riding node's require
86
+        } else if (!(<any>window).require.config) {
87
+            var src = `${baseUrl}/monaco/min/vs/loader.js`;
88
+            
89
+            var loaderRequest = new XMLHttpRequest();
90
+            loaderRequest.addEventListener("load", () => {
91
+                let scriptElem = document.createElement('script'); 
92
+                scriptElem.type = 'text/javascript';
93
+                scriptElem.text = [
94
+                    // Monaco uses a custom amd loader that over-rides node's require.
95
+                    // Keep a reference to node's require so we can restore it after executing the amd loader file.
96
+                    'var nodeRequire = require;',
97
+                    loaderRequest.responseText.replace('"use strict";', ''),
98
+                    // Save Monaco's amd require and restore Node's require
99
+                    'var monacoAmdRequire = require;',
100
+                    'require = nodeRequire;',
101
+                    'require.nodeRequire = require;'
102
+                ].join('\n');
103
+                document.body.appendChild(scriptElem);
104
+                onGotAmdLoader((<any>window).monacoAmdRequire);
105
+            });
106
+            loaderRequest.open("GET", src);
107
+            loaderRequest.send();
108
+        } else {
109
+          onGotAmdLoader();
110
+        }
111
+      });
112
+    }
113
+  }
114
+
115
+  protected abstract initMonaco(options: any, insideNg: boolean): void;
116
+
117
+  ngOnDestroy() {
118
+    if (this._windowResizeSubscription) {
119
+      this._windowResizeSubscription.unsubscribe();
120
+    }
121
+    if (this._editor) {
122
+      this._editor.dispose();
123
+      this._editor = undefined;
124
+    }
125
+  }
126
+}

+ 11
- 0
projects/editor/src/lib/config.ts 查看文件

@@ -0,0 +1,11 @@
1
+import { InjectionToken } from '@angular/core';
2
+
3
+export const NGX_MONACO_EDITOR_CONFIG = new InjectionToken('NGX_MONACO_EDITOR_CONFIG');
4
+
5
+export interface NgxMonacoEditorConfig {
6
+  baseUrl?: string;
7
+  requireConfig?: { [key: string]: any; };
8
+  defaultOptions?: { [key: string]: any; };
9
+  monacoRequire?: Function;
10
+  onMonacoLoad?: Function;
11
+}

+ 102
- 0
projects/editor/src/lib/diff-editor.component.ts 查看文件

@@ -0,0 +1,102 @@
1
+import { Component, Inject, Input, NgZone } from '@angular/core';
2
+import { fromEvent } from 'rxjs';
3
+
4
+import { BaseEditor } from './base-editor';
5
+import { NGX_MONACO_EDITOR_CONFIG, NgxMonacoEditorConfig } from './config';
6
+import { DiffEditorModel } from './types';
7
+
8
+declare var monaco: any;
9
+
10
+@Component({
11
+  selector: 'ngx-monaco-diff-editor',
12
+  template: '<div class="editor-container" #editorContainer></div>',
13
+  styles: [`
14
+    :host {
15
+      display: block;
16
+      height: 200px;
17
+    }
18
+
19
+    .editor-container {
20
+      width: 100%;
21
+      height: 98%;
22
+    }
23
+  `]
24
+})
25
+export class DiffEditorComponent extends BaseEditor {
26
+
27
+  _originalModel: DiffEditorModel;
28
+  _modifiedModel: DiffEditorModel;
29
+
30
+  @Input('options')
31
+  set options(options: any) {
32
+    this._options = Object.assign({}, this.config.defaultOptions, options);
33
+    if (this._editor) {
34
+      this._editor.dispose();
35
+      this.initMonaco(options, this.insideNg);
36
+    }
37
+  }
38
+
39
+  get options(): any {
40
+    return this._options;
41
+  }
42
+
43
+  @Input('originalModel')
44
+  set originalModel(model: DiffEditorModel) {
45
+    this._originalModel = model;
46
+    if (this._editor) {
47
+      this._editor.dispose();
48
+      this.initMonaco(this.options, this.insideNg);
49
+    }
50
+  }
51
+
52
+  @Input('modifiedModel')
53
+  set modifiedModel(model: DiffEditorModel) {
54
+    this._modifiedModel = model;
55
+    if (this._editor) {
56
+      this._editor.dispose();
57
+      this.initMonaco(this.options, this.insideNg);
58
+    }
59
+  }
60
+
61
+  constructor(private zone: NgZone, @Inject(NGX_MONACO_EDITOR_CONFIG) private editorConfig: NgxMonacoEditorConfig) {
62
+    super(editorConfig);
63
+  }
64
+
65
+  protected initMonaco(options: any, insideNg: boolean): void {
66
+
67
+    if (!this._originalModel || !this._modifiedModel) {
68
+      throw new Error('originalModel or modifiedModel not found for ngx-monaco-diff-editor');
69
+    }
70
+
71
+    this._originalModel.language = this._originalModel.language || options.language;
72
+    this._modifiedModel.language = this._modifiedModel.language || options.language;
73
+
74
+    let originalModel = monaco.editor.createModel(this._originalModel.code, this._originalModel.language);
75
+    let modifiedModel = monaco.editor.createModel(this._modifiedModel.code, this._modifiedModel.language);
76
+
77
+    this._editorContainer.nativeElement.innerHTML = '';
78
+    const theme = options.theme;
79
+
80
+    if (insideNg) {
81
+      this._editor = monaco.editor.createDiffEditor(this._editorContainer.nativeElement, options);
82
+    } else {
83
+      this.zone.runOutsideAngular(() => {
84
+        this._editor = monaco.editor.createDiffEditor(this._editorContainer.nativeElement, options);
85
+      })
86
+    }
87
+
88
+    options.theme = theme;
89
+    this._editor.setModel({
90
+      original: originalModel,
91
+      modified: modifiedModel
92
+    });
93
+
94
+    // refresh layout on resize event.
95
+    if (this._windowResizeSubscription) {
96
+      this._windowResizeSubscription.unsubscribe();
97
+    }
98
+    this._windowResizeSubscription = fromEvent(window, 'resize').subscribe(() => this._editor.layout());
99
+    this.onInit.emit(this._editor);
100
+  }
101
+
102
+}

+ 129
- 0
projects/editor/src/lib/editor.component.ts 查看文件

@@ -0,0 +1,129 @@
1
+import { Component, forwardRef, Inject, Input, NgZone } from '@angular/core';
2
+import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
3
+import { fromEvent } from 'rxjs';
4
+
5
+import { BaseEditor } from './base-editor';
6
+import { NGX_MONACO_EDITOR_CONFIG, NgxMonacoEditorConfig } from './config';
7
+import { NgxEditorModel } from './types';
8
+
9
+declare var monaco: any;
10
+
11
+@Component({
12
+  selector: 'ngx-monaco-editor',
13
+  template: '<div class="editor-container" #editorContainer></div>',
14
+  styles: [`
15
+      :host {
16
+          display: block;
17
+          height: 200px;
18
+      }
19
+
20
+      .editor-container {
21
+          width: 100%;
22
+          height: 98%;
23
+      }
24
+  `],
25
+  providers: [{
26
+    provide: NG_VALUE_ACCESSOR,
27
+    useExisting: forwardRef(() => EditorComponent),
28
+    multi: true
29
+  }]
30
+})
31
+export class EditorComponent extends BaseEditor implements ControlValueAccessor {
32
+  private _value: string = '';
33
+
34
+  propagateChange = (_: any) => {};
35
+  onTouched = () => {};
36
+
37
+  @Input('options')
38
+  set options(options: any) {
39
+    this._options = Object.assign({}, this.config.defaultOptions, options);
40
+    if (this._editor) {
41
+      this._editor.dispose();
42
+      this.initMonaco(options, this.insideNg);
43
+    }
44
+  }
45
+
46
+  get options(): any {
47
+    return this._options;
48
+  }
49
+
50
+  @Input('model')
51
+  set model(model: NgxEditorModel) {
52
+    this.options.model = model;
53
+    if (this._editor) {
54
+      this._editor.dispose();
55
+      this.initMonaco(this.options, this.insideNg);
56
+    }
57
+  }
58
+
59
+  constructor(private zone: NgZone, @Inject(NGX_MONACO_EDITOR_CONFIG) private editorConfig: NgxMonacoEditorConfig) {
60
+    super(editorConfig);
61
+  }
62
+
63
+  writeValue(value: any): void {
64
+    this._value = value || '';
65
+    // Fix for value change while dispose in process.
66
+    setTimeout(() => {
67
+      if (this._editor && !this.options.model) {
68
+        this._editor.setValue(this._value);
69
+      }
70
+    });
71
+  }
72
+
73
+  registerOnChange(fn: any): void {
74
+    this.propagateChange = fn;
75
+  }
76
+
77
+  registerOnTouched(fn: any): void {
78
+    this.onTouched = fn;
79
+  }
80
+
81
+  protected initMonaco(options: any, insideNg: boolean): void {
82
+
83
+    const hasModel = !!options.model;
84
+
85
+    if (hasModel) {
86
+      const model = monaco.editor.getModel(options.model.uri || '');
87
+      if (model) {
88
+        options.model = model;
89
+        options.model.setValue(this._value);
90
+      } else {
91
+        options.model = monaco.editor.createModel(options.model.value, options.model.language, options.model.uri);
92
+      }
93
+    }
94
+
95
+    if (insideNg) {
96
+      this._editor = monaco.editor.create(this._editorContainer.nativeElement, options);
97
+    } else {
98
+      this.zone.runOutsideAngular(() => {
99
+        this._editor = monaco.editor.create(this._editorContainer.nativeElement, options);
100
+      })
101
+    }
102
+
103
+    if (!hasModel) {
104
+      this._editor.setValue(this._value);
105
+    }
106
+
107
+    this._editor.onDidChangeModelContent((e: any) => {
108
+      const value = this._editor.getValue();
109
+
110
+      // value is not propagated to parent when executing outside zone.
111
+      this.zone.run(() => {
112
+        this.propagateChange(value);
113
+        this._value = value;
114
+      });
115
+    });
116
+
117
+    this._editor.onDidBlurEditorWidget(() => {
118
+      this.onTouched();
119
+    });
120
+
121
+    // refresh layout on resize event.
122
+    if (this._windowResizeSubscription) {
123
+      this._windowResizeSubscription.unsubscribe();
124
+    }
125
+    this._windowResizeSubscription = fromEvent(window, 'resize').subscribe(() => this._editor.layout());
126
+    this.onInit.emit(this._editor);
127
+  }
128
+
129
+}

+ 30
- 0
projects/editor/src/lib/editor.module.ts 查看文件

@@ -0,0 +1,30 @@
1
+import { CommonModule } from '@angular/common';
2
+import { ModuleWithProviders, NgModule } from '@angular/core';
3
+
4
+import { NGX_MONACO_EDITOR_CONFIG, NgxMonacoEditorConfig } from './config';
5
+import { DiffEditorComponent } from './diff-editor.component';
6
+import { EditorComponent } from './editor.component';
7
+
8
+@NgModule({
9
+  imports: [
10
+    CommonModule
11
+  ],
12
+  declarations: [
13
+    EditorComponent,
14
+    DiffEditorComponent
15
+  ],
16
+  exports: [
17
+    EditorComponent,
18
+    DiffEditorComponent
19
+  ]
20
+})
21
+export class MonacoEditorModule {
22
+  public static forRoot(config: NgxMonacoEditorConfig = {}): ModuleWithProviders<MonacoEditorModule> {
23
+    return {
24
+      ngModule: MonacoEditorModule,
25
+      providers: [
26
+        { provide: NGX_MONACO_EDITOR_CONFIG, useValue: config }
27
+      ]
28
+    };
29
+  }
30
+}

+ 9
- 0
projects/editor/src/lib/types.ts 查看文件

@@ -0,0 +1,9 @@
1
+export interface DiffEditorModel {
2
+    code: string;
3
+    language: string;
4
+}
5
+export interface NgxEditorModel {
6
+    value: string;
7
+    language?: string;
8
+    uri?: any;
9
+}

+ 5
- 0
projects/editor/src/public-api.ts 查看文件

@@ -0,0 +1,5 @@
1
+export * from './lib/editor.component';
2
+export * from './lib/diff-editor.component';
3
+export * from './lib/editor.module';
4
+export * from './lib/config';
5
+export * from './lib/types';

+ 28
- 0
projects/editor/src/test.ts 查看文件

@@ -0,0 +1,28 @@
1
+// This file is required by karma.conf.js and loads recursively all the .spec and framework files
2
+
3
+import 'zone.js';
4
+import 'zone.js/testing';
5
+import { getTestBed } from '@angular/core/testing';
6
+import {
7
+  BrowserDynamicTestingModule,
8
+  platformBrowserDynamicTesting
9
+} from '@angular/platform-browser-dynamic/testing';
10
+
11
+declare const require: {
12
+  context(path: string, deep?: boolean, filter?: RegExp): {
13
+    keys(): string[];
14
+    <T>(id: string): T;
15
+  };
16
+};
17
+
18
+// First, initialize the Angular testing environment.
19
+getTestBed().initTestEnvironment(
20
+  BrowserDynamicTestingModule,
21
+  platformBrowserDynamicTesting(), {
22
+    teardown: { destroyAfterEach: false }
23
+}
24
+);
25
+// Then we find all the tests.
26
+const context = require.context('./', true, /\.spec\.ts$/);
27
+// And load the modules.
28
+context.keys().map(context);

+ 20
- 0
projects/editor/tsconfig.lib.json 查看文件

@@ -0,0 +1,20 @@
1
+/* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+{
3
+  "extends": "../../tsconfig.json",
4
+  "compilerOptions": {
5
+    "outDir": "../../out-tsc/lib",
6
+    "target": "es2020",
7
+    "declaration": true,
8
+    "declarationMap": true,
9
+    "inlineSources": true,
10
+    "types": [],
11
+    "lib": [
12
+      "dom",
13
+      "es2018"
14
+    ]
15
+  },
16
+  "exclude": [
17
+    "src/test.ts",
18
+    "**/*.spec.ts"
19
+  ]
20
+}

+ 10
- 0
projects/editor/tsconfig.lib.prod.json 查看文件

@@ -0,0 +1,10 @@
1
+/* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+{
3
+  "extends": "./tsconfig.lib.json",
4
+  "compilerOptions": {
5
+    "declarationMap": false
6
+  },
7
+  "angularCompilerOptions": {
8
+    "compilationMode": "partial"
9
+  }
10
+}

+ 17
- 0
projects/editor/tsconfig.spec.json 查看文件

@@ -0,0 +1,17 @@
1
+/* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+{
3
+  "extends": "../../tsconfig.json",
4
+  "compilerOptions": {
5
+    "outDir": "../../out-tsc/spec",
6
+    "types": [
7
+      "jasmine"
8
+    ]
9
+  },
10
+  "files": [
11
+    "src/test.ts"
12
+  ],
13
+  "include": [
14
+    "**/*.spec.ts",
15
+    "**/*.d.ts"
16
+  ]
17
+}

+ 37
- 0
tsconfig.json 查看文件

@@ -0,0 +1,37 @@
1
+/* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+{
3
+  "compileOnSave": false,
4
+  "compilerOptions": {
5
+    "baseUrl": "./",
6
+    "outDir": "./dist/out-tsc",
7
+    "forceConsistentCasingInFileNames": true,
8
+    "strict": false,
9
+    "noImplicitReturns": true,
10
+    "noFallthroughCasesInSwitch": true,
11
+    "sourceMap": true,
12
+    "declaration": false,
13
+    "downlevelIteration": true,
14
+    "paths": {
15
+      "editor": [
16
+        "dist/editor/editor",
17
+        "dist/editor"
18
+      ]
19
+    },
20
+    "experimentalDecorators": true,
21
+    "moduleResolution": "node",
22
+    "importHelpers": true,
23
+    "useDefineForClassFields": false,
24
+    "target": "es2022",
25
+    "module": "es2020",
26
+    "lib": [
27
+      "es2018",
28
+      "dom"
29
+    ]
30
+  },
31
+  "angularCompilerOptions": {
32
+    "enableI18nLegacyMessageIdFormat": false,
33
+    "strictInjectionParameters": true,
34
+    "strictInputAccessModifiers": true,
35
+    "strictTemplates": true
36
+  }
37
+}

Powered by TurnKey Linux.