@node-projects/web-component-designer-codeview-monaco 0.2.0 → 0.3.0

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.
@@ -10,6 +10,12 @@ export declare class CodeViewMonaco extends BaseCustomWebComponentLazyAppend imp
10
10
  Range: typeof monacoType.Range;
11
11
  }>;
12
12
  private _disableSelectionAfterSel;
13
+ private _setSelectionTimeout;
14
+ private _pendingSelectionKey;
15
+ private _disposed;
16
+ private _cleanup;
17
+ private _enableSelectionTimeout;
18
+ private _focusFrame;
13
19
  dispose(): void;
14
20
  canvasElement: HTMLElement;
15
21
  elementsToPackages: Map<string, string>;
@@ -46,4 +52,5 @@ export declare class CodeViewMonaco extends BaseCustomWebComponentLazyAppend imp
46
52
  update(code: string, instanceServiceContainer?: InstanceServiceContainer): void;
47
53
  getText(): string;
48
54
  setSelection(position: IStringPosition): void;
55
+ private clearPendingSetSelection;
49
56
  }
@@ -14,8 +14,27 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
14
14
  return monaco;
15
15
  }
16
16
  _disableSelectionAfterSel;
17
+ _setSelectionTimeout;
18
+ _pendingSelectionKey;
19
+ _disposed = false;
20
+ _cleanup = [];
21
+ _enableSelectionTimeout;
22
+ _focusFrame;
17
23
  dispose() {
24
+ if (this._disposed)
25
+ return;
26
+ this._disposed = true;
27
+ this.clearPendingSetSelection();
28
+ clearTimeout(this._enableSelectionTimeout);
29
+ cancelAnimationFrame(this._focusFrame);
30
+ for (const cleanup of this._cleanup)
31
+ cleanup();
32
+ this._cleanup = [];
33
+ const model = this._monacoEditor?.getModel();
18
34
  this._monacoEditor?.dispose();
35
+ model?.dispose();
36
+ this._monacoEditor = null;
37
+ this._instanceServiceContainer = null;
19
38
  }
20
39
  canvasElement;
21
40
  elementsToPackages;
@@ -28,13 +47,14 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
28
47
  }
29
48
  set theme(value) {
30
49
  this._theme = value;
31
- CodeViewMonaco.getMonacoLib().then(monaco => monaco.editor.setTheme(value));
50
+ CodeViewMonaco.getMonacoLib().then(monaco => { if (!this._disposed)
51
+ monaco.editor.setTheme(value); });
32
52
  }
33
53
  #code = null;
34
54
  get code() {
35
55
  if (this._monacoEditor)
36
56
  return this._monacoEditor.getModel().getValue();
37
- return null;
57
+ return this.#code ?? '';
38
58
  }
39
59
  set code(v) {
40
60
  this.#code = v;
@@ -110,10 +130,12 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
110
130
  async ready() {
111
131
  this._parseAttributesToProperties();
112
132
  const monaco = await CodeViewMonaco.getMonacoLib();
133
+ if (this._disposed)
134
+ return;
113
135
  this.shadowRoot.adoptedStyleSheets = [CodeViewMonaco._monacoStyle, this.constructor.style];
114
136
  this._editor = this._getDomElement('container');
115
137
  const resizeObserver = new ResizeObserver(() => {
116
- if (this._editor.offsetWidth > 0) {
138
+ if (!this._disposed && this._editor.offsetWidth > 0) {
117
139
  let options = {
118
140
  automaticLayout: true,
119
141
  theme: this._theme,
@@ -143,6 +165,7 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
143
165
  }
144
166
  this._monacoEditor = monaco.editor.create(this._editor, options);
145
167
  let selectionTimeout;
168
+ let cursorChangeTimeout;
146
169
  let disableCursorChange;
147
170
  let changeContentListener = this._monacoEditor.getModel().onDidChangeContent(e => {
148
171
  if (selectionTimeout) {
@@ -151,12 +174,18 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
151
174
  this._disableSelection = false;
152
175
  }
153
176
  disableCursorChange = true;
154
- setTimeout(() => {
177
+ clearTimeout(cursorChangeTimeout);
178
+ cursorChangeTimeout = setTimeout(() => {
155
179
  disableCursorChange = false;
156
180
  }, 50);
157
181
  this.onTextChanged.emit(this._monacoEditor.getValue());
158
182
  this.dispatchEvent(new CustomEvent('code-changed'));
159
183
  });
184
+ this._cleanup.push(() => {
185
+ changeContentListener.dispose();
186
+ clearTimeout(selectionTimeout);
187
+ clearTimeout(cursorChangeTimeout);
188
+ });
160
189
  this._monacoEditor.onDidChangeModel(e => {
161
190
  changeContentListener.dispose();
162
191
  changeContentListener = this._monacoEditor.getModel().onDidChangeContent(e => {
@@ -166,7 +195,8 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
166
195
  this._disableSelection = false;
167
196
  }
168
197
  disableCursorChange = true;
169
- setTimeout(() => {
198
+ clearTimeout(cursorChangeTimeout);
199
+ cursorChangeTimeout = setTimeout(() => {
170
200
  disableCursorChange = false;
171
201
  }, 50);
172
202
  this.onTextChanged.emit(this._monacoEditor.getValue());
@@ -193,10 +223,14 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
193
223
  }
194
224
  ;
195
225
  });
226
+ this._cleanup.push(() => resizeObserver.disconnect());
196
227
  resizeObserver.observe(this._editor);
197
228
  }
198
229
  focusEditor() {
199
- requestAnimationFrame(() => {
230
+ cancelAnimationFrame(this._focusFrame);
231
+ this._focusFrame = requestAnimationFrame(() => {
232
+ if (this._disposed)
233
+ return;
200
234
  this.focus();
201
235
  if (this._monacoEditor)
202
236
  this._monacoEditor.focus();
@@ -208,38 +242,60 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
208
242
  this._monacoEditor.layout();
209
243
  }
210
244
  update(code, instanceServiceContainer) {
245
+ if (this._disposed)
246
+ return;
211
247
  this.#code = code;
212
248
  this._instanceServiceContainer = instanceServiceContainer;
213
249
  if (this._monacoEditor) {
250
+ this.clearPendingSetSelection();
214
251
  this._disableSelectionAfterUpd = true;
215
252
  if (this._monacoEditor)
216
253
  this._monacoEditor.setValue(code);
217
254
  this._disableSelectionAfterUpd = false;
218
255
  CodeViewMonaco.getMonacoLib().then(monaco => {
256
+ if (this._disposed)
257
+ return;
219
258
  monaco.editor.setTheme(this._theme);
220
259
  monaco.editor.setModelLanguage(this._monacoEditor.getModel(), this.language);
221
260
  });
222
261
  }
223
262
  }
224
263
  getText() {
225
- return this._monacoEditor.getValue();
264
+ return this._monacoEditor?.getValue() ?? this.#code ?? '';
226
265
  }
227
266
  setSelection(position) {
228
267
  if (this._monacoEditor && !this._disableSelection && !this._disableSelectionAfterSel && position) {
268
+ const selectionKey = `${position.start}:${position.length}`;
269
+ if (this._pendingSelectionKey === selectionKey)
270
+ return;
229
271
  this._disableSelectionAfterSel = true;
272
+ this._pendingSelectionKey = selectionKey;
230
273
  let model = this._monacoEditor.getModel();
231
274
  let point1 = model.getPositionAt(position.start);
232
275
  let point2 = model.getPositionAt(position.start + position.length);
233
- setTimeout(() => {
276
+ this._setSelectionTimeout = setTimeout(async () => {
277
+ this._setSelectionTimeout = null;
278
+ this._pendingSelectionKey = null;
234
279
  this._monacoEditor.setSelection({ startLineNumber: point1.lineNumber, startColumn: point1.column, endLineNumber: point2.lineNumber, endColumn: point2.column });
235
280
  CodeViewMonaco.getMonacoLib().then(monaco => {
281
+ if (this._disposed)
282
+ return;
236
283
  this._monacoEditor.revealRangeInCenterIfOutsideViewport(new monaco.Range(point1.lineNumber, point1.column, point2.lineNumber, point2.column), 1);
237
284
  });
238
- setTimeout(() => {
285
+ clearTimeout(this._enableSelectionTimeout);
286
+ this._enableSelectionTimeout = setTimeout(() => {
239
287
  this._disableSelectionAfterSel = false;
240
288
  }, 50);
241
289
  }, 50);
242
290
  }
243
291
  }
292
+ clearPendingSetSelection() {
293
+ if (this._setSelectionTimeout) {
294
+ clearTimeout(this._setSelectionTimeout);
295
+ this._setSelectionTimeout = null;
296
+ }
297
+ this._pendingSelectionKey = null;
298
+ this._disableSelectionAfterSel = false;
299
+ }
244
300
  }
245
301
  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.2.0",
4
+ "version": "0.3.0",
5
5
  "type": "module",
6
6
  "main": "./dist/widgets/codeView/code-view-monaco.js",
7
7
  "author": "jochen.kuehner@gmx.de",