@node-projects/web-component-designer-codeview-monaco 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,4 +13,16 @@ This uses the Monaco Editor for the Webcomponent Designer Codeview
13
13
  ## Usage
14
14
 
15
15
  import { CodeViewCodeMonaco } from '@node-projects/web-component-designer-codeview-monaco';
16
- serviceContainer.config.codeViewWidget = CodeViewCodeMonaco;
16
+ serviceContainer.config.codeViewWidget = CodeViewCodeMonaco;
17
+
18
+ You need to load the monaco manualy and call
19
+
20
+ await CodeViewCodeMonaco.setMonacoLibrary(...);
21
+
22
+ or via
23
+
24
+ await CodeViewCodeMonaco.loadMonacoEditorViaRequire();
25
+
26
+ or
27
+
28
+ await CodeViewCodeMonaco.loadMonacoEditorViaImport();
@@ -1,7 +1,12 @@
1
1
  import { BaseCustomWebComponentLazyAppend, TypedEvent } from '@node-projects/base-custom-webcomponent';
2
2
  import { IActivateable, ICodeView, IStringPosition, IUiCommand, IUiCommandHandler } from '@node-projects/web-component-designer';
3
+ import type * as monacoType from 'monaco-editor';
3
4
  export declare class CodeViewMonaco extends BaseCustomWebComponentLazyAppend implements ICodeView, IActivateable, IUiCommandHandler {
4
5
  private static _initalized;
6
+ static monacoLib: {
7
+ editor: typeof monacoType.editor;
8
+ Range: typeof monacoType.Range;
9
+ };
5
10
  dispose(): void;
6
11
  canvasElement: HTMLElement;
7
12
  elementsToPackages: Map<string, string>;
@@ -22,7 +27,10 @@ export declare class CodeViewMonaco extends BaseCustomWebComponentLazyAppend imp
22
27
  static readonly template: HTMLTemplateElement;
23
28
  executeCommand(command: IUiCommand): void;
24
29
  canExecuteCommand(command: IUiCommand): boolean;
25
- static initMonacoEditor(): Promise<void>;
30
+ static loadMonacoEditorViaRequire(path?: string): Promise<void>;
31
+ static loadMonacoEditorViaImport(): Promise<void>;
32
+ static setMonacoLibrary(monaco: any): void;
33
+ static initMonacoEditor(monaco: any): void;
26
34
  constructor();
27
35
  ready(): Promise<void>;
28
36
  focusEditor(): void;
@@ -1,8 +1,9 @@
1
1
  import { BaseCustomWebComponentLazyAppend, css, html, TypedEvent } from '@node-projects/base-custom-webcomponent';
2
2
  import { CommandType } from '@node-projects/web-component-designer';
3
- import * as monaco from 'monaco-editor';
4
3
  export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
5
4
  static _initalized;
5
+ //@ts-ignore
6
+ static monacoLib = monaco;
6
7
  dispose() {
7
8
  this._monacoEditor?.dispose();
8
9
  }
@@ -17,7 +18,7 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
17
18
  }
18
19
  set theme(value) {
19
20
  this._theme = value;
20
- monaco.editor.setTheme(value);
21
+ CodeViewMonaco.monacoLib.editor.setTheme(value);
21
22
  }
22
23
  static properties = {
23
24
  code: String,
@@ -68,33 +69,41 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
68
69
  }
69
70
  return false;
70
71
  }
71
- static initMonacoEditor() {
72
+ static loadMonacoEditorViaRequire(path = 'node_modules/monaco-editor/min/vs') {
72
73
  return new Promise(async (resolve) => {
73
- if (!CodeViewMonaco._initalized) {
74
- CodeViewMonaco._initalized = true;
74
+ //@ts-ignore
75
+ require.config({ paths: { 'vs': path } });
76
+ //@ts-ignore
77
+ require(['vs/editor/editor.main'], () => {
75
78
  //@ts-ignore
76
- require.config({ paths: { 'vs': 'node_modules/monaco-editor/min/vs' } });
77
- //@ts-ignore
78
- require(['vs/editor/editor.main'], () => {
79
- monaco.editor.defineTheme('webComponentDesignerTheme', {
80
- base: 'vs',
81
- inherit: true,
82
- //@ts-ignore
83
- rules: [{ background: 'EDF9FA' }],
84
- colors: {
85
- 'editor.selectionBackground': '#add6ff',
86
- 'editor.inactiveSelectionBackground': '#add6ff'
87
- }
88
- });
89
- monaco.editor.setTheme('webComponentDesignerTheme');
90
- resolve();
91
- });
92
- }
93
- else {
79
+ CodeViewMonaco.monacoLib = monaco;
94
80
  resolve();
95
- }
81
+ });
96
82
  });
97
83
  }
84
+ static async loadMonacoEditorViaImport() {
85
+ let monaco = await import('monaco-editor');
86
+ CodeViewMonaco.monacoLib = monaco;
87
+ }
88
+ static setMonacoLibrary(monaco) {
89
+ CodeViewMonaco.monacoLib = monaco;
90
+ }
91
+ static initMonacoEditor(monaco) {
92
+ if (!CodeViewMonaco._initalized) {
93
+ CodeViewMonaco._initalized = true;
94
+ monaco.editor.defineTheme('webComponentDesignerTheme', {
95
+ base: 'vs',
96
+ inherit: true,
97
+ //@ts-ignore
98
+ rules: [{ background: 'EDF9FA' }],
99
+ colors: {
100
+ 'editor.selectionBackground': '#add6ff',
101
+ 'editor.inactiveSelectionBackground': '#add6ff'
102
+ }
103
+ });
104
+ }
105
+ monaco.editor.setTheme('webComponentDesignerTheme');
106
+ }
98
107
  constructor() {
99
108
  super();
100
109
  this._restoreCachedInititalValues();
@@ -105,11 +114,9 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
105
114
  let style = await import("monaco-editor/min/vs/editor/editor.main.css", { assert: { type: 'css' } });
106
115
  this.shadowRoot.adoptedStyleSheets = [style.default, this.constructor.style];
107
116
  this._editor = this._getDomElement('container');
108
- await CodeViewMonaco.initMonacoEditor();
109
117
  const resizeObserver = new ResizeObserver(() => {
110
118
  if (this._editor.offsetWidth > 0) {
111
- //@ts-ignore
112
- this._monacoEditor = monaco.editor.create(this._editor, {
119
+ this._monacoEditor = CodeViewMonaco.monacoLib.editor.create(this._editor, {
113
120
  automaticLayout: true,
114
121
  wordWrapColumn: 1000,
115
122
  //wordWrap: 'wordWrapColumn',
@@ -128,7 +135,7 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
128
135
  horizontal: 'visible'
129
136
  }
130
137
  });
131
- monaco.editor.setTheme(this.theme);
138
+ CodeViewMonaco.initMonacoEditor(CodeViewMonaco.monacoLib);
132
139
  let changeContentListener = this._monacoEditor.getModel().onDidChangeContent(e => {
133
140
  this.onTextChanged.emit(this._monacoEditor.getValue());
134
141
  });
@@ -162,8 +169,8 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
162
169
  if (this._monacoEditor) {
163
170
  if (this._monacoEditor)
164
171
  this._monacoEditor.setValue(code);
165
- monaco.editor.setModelLanguage(this._monacoEditor.getModel(), this.language);
166
- monaco.editor.setTheme(this.theme);
172
+ CodeViewMonaco.monacoLib.editor.setModelLanguage(this._monacoEditor.getModel(), this.language);
173
+ CodeViewMonaco.monacoLib.editor.setTheme(this.theme);
167
174
  }
168
175
  }
169
176
  getText() {
@@ -176,7 +183,7 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
176
183
  let point2 = model.getPositionAt(position.start + position.length);
177
184
  setTimeout(() => {
178
185
  this._monacoEditor.setSelection({ startLineNumber: point1.lineNumber, startColumn: point1.column, endLineNumber: point2.lineNumber, endColumn: point2.column });
179
- this._monacoEditor.revealRangeInCenterIfOutsideViewport(new monaco.Range(point1.lineNumber, point1.column, point2.lineNumber, point2.column), 1);
186
+ this._monacoEditor.revealRangeInCenterIfOutsideViewport(new CodeViewMonaco.monacoLib.Range(point1.lineNumber, point1.column, point2.lineNumber, point2.column), 1);
180
187
  }, 50);
181
188
  }
182
189
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "description": "web-component-designer addon: Code Editor using Monaco",
3
3
  "name": "@node-projects/web-component-designer-codeview-monaco",
4
- "version": "0.1.1",
4
+ "version": "0.1.3",
5
5
  "type": "module",
6
6
  "main": "./dist/widgets/codeView/code-view-monaco.js",
7
7
  "author": "jochen.kuehner@gmx.de",
@@ -14,7 +14,7 @@
14
14
  "dependencies": {
15
15
  "monaco-editor": "^0.43.0",
16
16
  "@node-projects/base-custom-webcomponent": "^0.17.2",
17
- "@node-projects/web-component-designer": "^0.1.0"
17
+ "@node-projects/web-component-designer": "^0.1.1"
18
18
  },
19
19
  "repository": {
20
20
  "type": "git",