@node-projects/web-component-designer-codeview-monaco 0.1.33 → 0.2.1

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.
@@ -3,11 +3,15 @@ import { IActivateable, ICodeView, InstanceServiceContainer, IStringPosition, IU
3
3
  import type * as monacoType from 'monaco-editor';
4
4
  export declare class CodeViewMonaco extends BaseCustomWebComponentLazyAppend implements ICodeView, IActivateable, IUiCommandHandler {
5
5
  #private;
6
- static monacoLib: {
6
+ private static _monaco;
7
+ private static _monacoStyle;
8
+ static getMonacoLib(): Promise<{
7
9
  editor: typeof monacoType.editor;
8
10
  Range: typeof monacoType.Range;
9
- };
11
+ }>;
10
12
  private _disableSelectionAfterSel;
13
+ private _setSelectionTimeout;
14
+ private _pendingSelectionKey;
11
15
  dispose(): void;
12
16
  canvasElement: HTMLElement;
13
17
  elementsToPackages: Map<string, string>;
@@ -37,9 +41,6 @@ export declare class CodeViewMonaco extends BaseCustomWebComponentLazyAppend imp
37
41
  static readonly template: HTMLTemplateElement;
38
42
  executeCommand(command: IUiCommand): void;
39
43
  canExecuteCommand(command: IUiCommand): boolean;
40
- static loadMonacoEditorViaRequire(path?: string): Promise<void>;
41
- static loadMonacoEditorViaImport(): Promise<void>;
42
- static setMonacoLibrary(monaco: any): void;
43
44
  constructor();
44
45
  ready(): Promise<void>;
45
46
  focusEditor(): void;
@@ -47,4 +48,5 @@ export declare class CodeViewMonaco extends BaseCustomWebComponentLazyAppend imp
47
48
  update(code: string, instanceServiceContainer?: InstanceServiceContainer): void;
48
49
  getText(): string;
49
50
  setSelection(position: IStringPosition): void;
51
+ private clearPendingSetSelection;
50
52
  }
@@ -1,8 +1,21 @@
1
- import { BaseCustomWebComponentLazyAppend, css, cssFromString, html, TypedEvent } from '@node-projects/base-custom-webcomponent';
1
+ import { BaseCustomWebComponentLazyAppend, css, html, TypedEvent } from '@node-projects/base-custom-webcomponent';
2
2
  import { CommandType } from '@node-projects/web-component-designer';
3
3
  export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
4
- static monacoLib;
4
+ static _monaco;
5
+ static _monacoStyle;
6
+ static async getMonacoLib() {
7
+ if (CodeViewMonaco._monaco) {
8
+ return CodeViewMonaco._monaco;
9
+ }
10
+ const monaco = await import('monaco-editor');
11
+ CodeViewMonaco._monaco = monaco;
12
+ //@ts-ignore
13
+ CodeViewMonaco._monacoStyle = (await import("monaco-editor/min/vs/editor/editor.main.css", { with: { type: 'css' } })).default;
14
+ return monaco;
15
+ }
5
16
  _disableSelectionAfterSel;
17
+ _setSelectionTimeout;
18
+ _pendingSelectionKey;
6
19
  dispose() {
7
20
  this._monacoEditor?.dispose();
8
21
  }
@@ -17,9 +30,7 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
17
30
  }
18
31
  set theme(value) {
19
32
  this._theme = value;
20
- //@ts-ignore
21
- CodeViewMonaco.monacoLib ??= window.monaco;
22
- CodeViewMonaco.monacoLib.editor.setTheme(value);
33
+ CodeViewMonaco.getMonacoLib().then(monaco => monaco.editor.setTheme(value));
23
34
  }
24
35
  #code = null;
25
36
  get code() {
@@ -60,9 +71,7 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
60
71
  width: 100%;
61
72
  }
62
73
  `;
63
- static template = html `
64
- <div id="container" style="overflow: hidden; width: 100%; height: 100%; position: absolute;"></div>
65
- `;
74
+ static template = html `<div id="container" style="overflow: hidden; width: 100%; height: 100%; position: absolute;"></div>`;
66
75
  executeCommand(command) {
67
76
  switch (command.type) {
68
77
  case CommandType.undo:
@@ -95,25 +104,6 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
95
104
  }
96
105
  return false;
97
106
  }
98
- static loadMonacoEditorViaRequire(path = 'node_modules/monaco-editor/min/vs') {
99
- return new Promise(async (resolve) => {
100
- //@ts-ignore
101
- require.config({ paths: { 'vs': path } });
102
- //@ts-ignore
103
- require(['vs/editor/editor.main'], () => {
104
- //@ts-ignore
105
- CodeViewMonaco.monacoLib = window.monaco;
106
- resolve();
107
- });
108
- });
109
- }
110
- static async loadMonacoEditorViaImport() {
111
- let monaco = await import('monaco-editor');
112
- CodeViewMonaco.monacoLib = monaco;
113
- }
114
- static setMonacoLibrary(monaco) {
115
- CodeViewMonaco.monacoLib = monaco;
116
- }
117
107
  constructor() {
118
108
  super();
119
109
  this._restoreCachedInititalValues();
@@ -121,12 +111,9 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
121
111
  }
122
112
  async ready() {
123
113
  this._parseAttributesToProperties();
124
- //@ts-ignore
125
- let style = await import("monaco-editor/min/vs/editor/editor.main.css", { with: { type: 'css' } });
126
- this.shadowRoot.adoptedStyleSheets = [cssFromString(style), this.constructor.style];
114
+ const monaco = await CodeViewMonaco.getMonacoLib();
115
+ this.shadowRoot.adoptedStyleSheets = [CodeViewMonaco._monacoStyle, this.constructor.style];
127
116
  this._editor = this._getDomElement('container');
128
- //@ts-ignore
129
- CodeViewMonaco.monacoLib ??= window.monaco;
130
117
  const resizeObserver = new ResizeObserver(() => {
131
118
  if (this._editor.offsetWidth > 0) {
132
119
  let options = {
@@ -156,7 +143,7 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
156
143
  options.scrollbar.vertical = 'visible';
157
144
  options.scrollbar.horizontal = 'visible';
158
145
  }
159
- this._monacoEditor = CodeViewMonaco.monacoLib.editor.create(this._editor, options);
146
+ this._monacoEditor = monaco.editor.create(this._editor, options);
160
147
  let selectionTimeout;
161
148
  let disableCursorChange;
162
149
  let changeContentListener = this._monacoEditor.getModel().onDidChangeContent(e => {
@@ -226,12 +213,15 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
226
213
  this.#code = code;
227
214
  this._instanceServiceContainer = instanceServiceContainer;
228
215
  if (this._monacoEditor) {
216
+ this.clearPendingSetSelection();
229
217
  this._disableSelectionAfterUpd = true;
230
218
  if (this._monacoEditor)
231
219
  this._monacoEditor.setValue(code);
232
- CodeViewMonaco.monacoLib.editor.setTheme(this._theme);
233
- CodeViewMonaco.monacoLib.editor.setModelLanguage(this._monacoEditor.getModel(), this.language);
234
220
  this._disableSelectionAfterUpd = false;
221
+ CodeViewMonaco.getMonacoLib().then(monaco => {
222
+ monaco.editor.setTheme(this._theme);
223
+ monaco.editor.setModelLanguage(this._monacoEditor.getModel(), this.language);
224
+ });
235
225
  }
236
226
  }
237
227
  getText() {
@@ -239,18 +229,34 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
239
229
  }
240
230
  setSelection(position) {
241
231
  if (this._monacoEditor && !this._disableSelection && !this._disableSelectionAfterSel && position) {
232
+ const selectionKey = `${position.start}:${position.length}`;
233
+ if (this._pendingSelectionKey === selectionKey)
234
+ return;
242
235
  this._disableSelectionAfterSel = true;
236
+ this._pendingSelectionKey = selectionKey;
243
237
  let model = this._monacoEditor.getModel();
244
238
  let point1 = model.getPositionAt(position.start);
245
239
  let point2 = model.getPositionAt(position.start + position.length);
246
- setTimeout(() => {
240
+ this._setSelectionTimeout = setTimeout(async () => {
241
+ this._setSelectionTimeout = null;
242
+ this._pendingSelectionKey = null;
247
243
  this._monacoEditor.setSelection({ startLineNumber: point1.lineNumber, startColumn: point1.column, endLineNumber: point2.lineNumber, endColumn: point2.column });
248
- this._monacoEditor.revealRangeInCenterIfOutsideViewport(new CodeViewMonaco.monacoLib.Range(point1.lineNumber, point1.column, point2.lineNumber, point2.column), 1);
244
+ CodeViewMonaco.getMonacoLib().then(monaco => {
245
+ this._monacoEditor.revealRangeInCenterIfOutsideViewport(new monaco.Range(point1.lineNumber, point1.column, point2.lineNumber, point2.column), 1);
246
+ });
249
247
  setTimeout(() => {
250
248
  this._disableSelectionAfterSel = false;
251
249
  }, 50);
252
250
  }, 50);
253
251
  }
254
252
  }
253
+ clearPendingSetSelection() {
254
+ if (this._setSelectionTimeout) {
255
+ clearTimeout(this._setSelectionTimeout);
256
+ this._setSelectionTimeout = null;
257
+ }
258
+ this._pendingSelectionKey = null;
259
+ this._disableSelectionAfterSel = false;
260
+ }
255
261
  }
256
262
  customElements.define('node-projects-code-view-monaco', CodeViewMonaco);
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.33",
4
+ "version": "0.2.1",
5
5
  "type": "module",
6
6
  "main": "./dist/widgets/codeView/code-view-monaco.js",
7
7
  "author": "jochen.kuehner@gmx.de",