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

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;
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 = window.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,11 @@ 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();
117
+ //@ts-ignore
118
+ CodeViewMonaco.monacoLib ??= window.monaco;
109
119
  const resizeObserver = new ResizeObserver(() => {
110
120
  if (this._editor.offsetWidth > 0) {
111
- //@ts-ignore
112
- this._monacoEditor = monaco.editor.create(this._editor, {
121
+ this._monacoEditor = CodeViewMonaco.monacoLib.editor.create(this._editor, {
113
122
  automaticLayout: true,
114
123
  wordWrapColumn: 1000,
115
124
  //wordWrap: 'wordWrapColumn',
@@ -128,7 +137,7 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
128
137
  horizontal: 'visible'
129
138
  }
130
139
  });
131
- monaco.editor.setTheme(this.theme);
140
+ CodeViewMonaco.initMonacoEditor(CodeViewMonaco.monacoLib);
132
141
  let changeContentListener = this._monacoEditor.getModel().onDidChangeContent(e => {
133
142
  this.onTextChanged.emit(this._monacoEditor.getValue());
134
143
  });
@@ -162,8 +171,8 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
162
171
  if (this._monacoEditor) {
163
172
  if (this._monacoEditor)
164
173
  this._monacoEditor.setValue(code);
165
- monaco.editor.setModelLanguage(this._monacoEditor.getModel(), this.language);
166
- monaco.editor.setTheme(this.theme);
174
+ CodeViewMonaco.monacoLib.editor.setModelLanguage(this._monacoEditor.getModel(), this.language);
175
+ CodeViewMonaco.monacoLib.editor.setTheme(this.theme);
167
176
  }
168
177
  }
169
178
  getText() {
@@ -176,7 +185,7 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
176
185
  let point2 = model.getPositionAt(position.start + position.length);
177
186
  setTimeout(() => {
178
187
  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);
188
+ this._monacoEditor.revealRangeInCenterIfOutsideViewport(new CodeViewMonaco.monacoLib.Range(point1.lineNumber, point1.column, point2.lineNumber, point2.column), 1);
180
189
  }, 50);
181
190
  }
182
191
  }
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.4",
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",