@node-projects/web-component-designer-codeview-monaco 0.2.1 → 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.
@@ -12,6 +12,10 @@ export declare class CodeViewMonaco extends BaseCustomWebComponentLazyAppend imp
12
12
  private _disableSelectionAfterSel;
13
13
  private _setSelectionTimeout;
14
14
  private _pendingSelectionKey;
15
+ private _disposed;
16
+ private _cleanup;
17
+ private _enableSelectionTimeout;
18
+ private _focusFrame;
15
19
  dispose(): void;
16
20
  canvasElement: HTMLElement;
17
21
  elementsToPackages: Map<string, string>;
@@ -16,8 +16,25 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
16
16
  _disableSelectionAfterSel;
17
17
  _setSelectionTimeout;
18
18
  _pendingSelectionKey;
19
+ _disposed = false;
20
+ _cleanup = [];
21
+ _enableSelectionTimeout;
22
+ _focusFrame;
19
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();
20
34
  this._monacoEditor?.dispose();
35
+ model?.dispose();
36
+ this._monacoEditor = null;
37
+ this._instanceServiceContainer = null;
21
38
  }
22
39
  canvasElement;
23
40
  elementsToPackages;
@@ -30,13 +47,14 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
30
47
  }
31
48
  set theme(value) {
32
49
  this._theme = value;
33
- CodeViewMonaco.getMonacoLib().then(monaco => monaco.editor.setTheme(value));
50
+ CodeViewMonaco.getMonacoLib().then(monaco => { if (!this._disposed)
51
+ monaco.editor.setTheme(value); });
34
52
  }
35
53
  #code = null;
36
54
  get code() {
37
55
  if (this._monacoEditor)
38
56
  return this._monacoEditor.getModel().getValue();
39
- return null;
57
+ return this.#code ?? '';
40
58
  }
41
59
  set code(v) {
42
60
  this.#code = v;
@@ -112,10 +130,12 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
112
130
  async ready() {
113
131
  this._parseAttributesToProperties();
114
132
  const monaco = await CodeViewMonaco.getMonacoLib();
133
+ if (this._disposed)
134
+ return;
115
135
  this.shadowRoot.adoptedStyleSheets = [CodeViewMonaco._monacoStyle, this.constructor.style];
116
136
  this._editor = this._getDomElement('container');
117
137
  const resizeObserver = new ResizeObserver(() => {
118
- if (this._editor.offsetWidth > 0) {
138
+ if (!this._disposed && this._editor.offsetWidth > 0) {
119
139
  let options = {
120
140
  automaticLayout: true,
121
141
  theme: this._theme,
@@ -145,6 +165,7 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
145
165
  }
146
166
  this._monacoEditor = monaco.editor.create(this._editor, options);
147
167
  let selectionTimeout;
168
+ let cursorChangeTimeout;
148
169
  let disableCursorChange;
149
170
  let changeContentListener = this._monacoEditor.getModel().onDidChangeContent(e => {
150
171
  if (selectionTimeout) {
@@ -153,12 +174,18 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
153
174
  this._disableSelection = false;
154
175
  }
155
176
  disableCursorChange = true;
156
- setTimeout(() => {
177
+ clearTimeout(cursorChangeTimeout);
178
+ cursorChangeTimeout = setTimeout(() => {
157
179
  disableCursorChange = false;
158
180
  }, 50);
159
181
  this.onTextChanged.emit(this._monacoEditor.getValue());
160
182
  this.dispatchEvent(new CustomEvent('code-changed'));
161
183
  });
184
+ this._cleanup.push(() => {
185
+ changeContentListener.dispose();
186
+ clearTimeout(selectionTimeout);
187
+ clearTimeout(cursorChangeTimeout);
188
+ });
162
189
  this._monacoEditor.onDidChangeModel(e => {
163
190
  changeContentListener.dispose();
164
191
  changeContentListener = this._monacoEditor.getModel().onDidChangeContent(e => {
@@ -168,7 +195,8 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
168
195
  this._disableSelection = false;
169
196
  }
170
197
  disableCursorChange = true;
171
- setTimeout(() => {
198
+ clearTimeout(cursorChangeTimeout);
199
+ cursorChangeTimeout = setTimeout(() => {
172
200
  disableCursorChange = false;
173
201
  }, 50);
174
202
  this.onTextChanged.emit(this._monacoEditor.getValue());
@@ -195,10 +223,14 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
195
223
  }
196
224
  ;
197
225
  });
226
+ this._cleanup.push(() => resizeObserver.disconnect());
198
227
  resizeObserver.observe(this._editor);
199
228
  }
200
229
  focusEditor() {
201
- requestAnimationFrame(() => {
230
+ cancelAnimationFrame(this._focusFrame);
231
+ this._focusFrame = requestAnimationFrame(() => {
232
+ if (this._disposed)
233
+ return;
202
234
  this.focus();
203
235
  if (this._monacoEditor)
204
236
  this._monacoEditor.focus();
@@ -210,6 +242,8 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
210
242
  this._monacoEditor.layout();
211
243
  }
212
244
  update(code, instanceServiceContainer) {
245
+ if (this._disposed)
246
+ return;
213
247
  this.#code = code;
214
248
  this._instanceServiceContainer = instanceServiceContainer;
215
249
  if (this._monacoEditor) {
@@ -219,13 +253,15 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
219
253
  this._monacoEditor.setValue(code);
220
254
  this._disableSelectionAfterUpd = false;
221
255
  CodeViewMonaco.getMonacoLib().then(monaco => {
256
+ if (this._disposed)
257
+ return;
222
258
  monaco.editor.setTheme(this._theme);
223
259
  monaco.editor.setModelLanguage(this._monacoEditor.getModel(), this.language);
224
260
  });
225
261
  }
226
262
  }
227
263
  getText() {
228
- return this._monacoEditor.getValue();
264
+ return this._monacoEditor?.getValue() ?? this.#code ?? '';
229
265
  }
230
266
  setSelection(position) {
231
267
  if (this._monacoEditor && !this._disableSelection && !this._disableSelectionAfterSel && position) {
@@ -242,9 +278,12 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
242
278
  this._pendingSelectionKey = null;
243
279
  this._monacoEditor.setSelection({ startLineNumber: point1.lineNumber, startColumn: point1.column, endLineNumber: point2.lineNumber, endColumn: point2.column });
244
280
  CodeViewMonaco.getMonacoLib().then(monaco => {
281
+ if (this._disposed)
282
+ return;
245
283
  this._monacoEditor.revealRangeInCenterIfOutsideViewport(new monaco.Range(point1.lineNumber, point1.column, point2.lineNumber, point2.column), 1);
246
284
  });
247
- setTimeout(() => {
285
+ clearTimeout(this._enableSelectionTimeout);
286
+ this._enableSelectionTimeout = setTimeout(() => {
248
287
  this._disableSelectionAfterSel = false;
249
288
  }, 50);
250
289
  }, 50);
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.1",
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",