@node-projects/web-component-designer-codeview-monaco 0.2.1 → 0.3.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.
@@ -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;
@@ -64,12 +82,12 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
64
82
  _instanceServiceContainer;
65
83
  _disableSelection;
66
84
  _disableSelectionAfterUpd;
67
- static style = css `
68
- :host {
69
- display: block;
70
- height: 100%;
71
- width: 100%;
72
- }
85
+ static style = css `
86
+ :host {
87
+ display: block;
88
+ height: 100%;
89
+ width: 100%;
90
+ }
73
91
  `;
74
92
  static template = html `<div id="container" style="overflow: hidden; width: 100%; height: 100%; position: absolute;"></div>`;
75
93
  executeCommand(command) {
@@ -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,22 +242,45 @@ 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) {
216
250
  this.clearPendingSetSelection();
217
251
  this._disableSelectionAfterUpd = true;
218
- if (this._monacoEditor)
219
- this._monacoEditor.setValue(code);
252
+ const model = this._monacoEditor.getModel();
253
+ const old = model.getValue();
254
+ if (old !== code) {
255
+ let start = 0;
256
+ const max = Math.min(old.length, code.length);
257
+ while (start < max && old.charCodeAt(start) === code.charCodeAt(start))
258
+ start++;
259
+ let endOld = old.length, endNew = code.length;
260
+ while (endOld > start && endNew > start && old.charCodeAt(endOld - 1) === code.charCodeAt(endNew - 1)) {
261
+ endOld--;
262
+ endNew--;
263
+ }
264
+ const a = model.getPositionAt(start);
265
+ const b = model.getPositionAt(endOld);
266
+ //minimal edit keeps the caret position (setValue would reset it)
267
+ this._monacoEditor.executeEdits('update', [{
268
+ range: { startLineNumber: a.lineNumber, startColumn: a.column, endLineNumber: b.lineNumber, endColumn: b.column },
269
+ text: code.substring(start, endNew),
270
+ forceMoveMarkers: false
271
+ }]);
272
+ }
220
273
  this._disableSelectionAfterUpd = false;
221
274
  CodeViewMonaco.getMonacoLib().then(monaco => {
275
+ if (this._disposed)
276
+ return;
222
277
  monaco.editor.setTheme(this._theme);
223
278
  monaco.editor.setModelLanguage(this._monacoEditor.getModel(), this.language);
224
279
  });
225
280
  }
226
281
  }
227
282
  getText() {
228
- return this._monacoEditor.getValue();
283
+ return this._monacoEditor?.getValue() ?? this.#code ?? '';
229
284
  }
230
285
  setSelection(position) {
231
286
  if (this._monacoEditor && !this._disableSelection && !this._disableSelectionAfterSel && position) {
@@ -242,9 +297,12 @@ export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
242
297
  this._pendingSelectionKey = null;
243
298
  this._monacoEditor.setSelection({ startLineNumber: point1.lineNumber, startColumn: point1.column, endLineNumber: point2.lineNumber, endColumn: point2.column });
244
299
  CodeViewMonaco.getMonacoLib().then(monaco => {
300
+ if (this._disposed)
301
+ return;
245
302
  this._monacoEditor.revealRangeInCenterIfOutsideViewport(new monaco.Range(point1.lineNumber, point1.column, point2.lineNumber, point2.column), 1);
246
303
  });
247
- setTimeout(() => {
304
+ clearTimeout(this._enableSelectionTimeout);
305
+ this._enableSelectionTimeout = setTimeout(() => {
248
306
  this._disableSelectionAfterSel = false;
249
307
  }, 50);
250
308
  }, 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.1",
5
5
  "type": "module",
6
6
  "main": "./dist/widgets/codeView/code-view-monaco.js",
7
7
  "author": "jochen.kuehner@gmx.de",