@node-projects/web-component-designer-codeview-codemirror5 0.1.5 → 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.
@@ -4,6 +4,9 @@ export declare class CodeViewCodeMirror5 extends BaseCustomWebComponentLazyAppen
4
4
  canvasElement: HTMLElement;
5
5
  elementsToPackages: Map<string, string>;
6
6
  code: string;
7
+ private _disposed;
8
+ private _focusFrame;
9
+ private _onChange;
7
10
  onTextChanged: TypedEvent<string>;
8
11
  mode: string;
9
12
  private _codeMirrorEditor;
@@ -4,7 +4,13 @@ import CodeMirror from 'codemirror5';
4
4
  export class CodeViewCodeMirror5 extends BaseCustomWebComponentLazyAppend {
5
5
  canvasElement;
6
6
  elementsToPackages;
7
- code;
7
+ code = '';
8
+ _disposed = false;
9
+ _focusFrame;
10
+ _onChange = () => {
11
+ this.code = this._codeMirrorEditor.getValue();
12
+ this.onTextChanged.emit(this.code);
13
+ };
8
14
  onTextChanged = new TypedEvent();
9
15
  mode = 'xml';
10
16
  _codeMirrorEditor;
@@ -17,21 +23,33 @@ export class CodeViewCodeMirror5 extends BaseCustomWebComponentLazyAppend {
17
23
  }`;
18
24
  static template = html `
19
25
  <div style="width: 100%; height: 100%; overflow: auto;">
20
- <div id="textarea"></div>
26
+ <textarea id="textarea"></textarea>
21
27
  </div>`;
22
28
  constructor() {
23
29
  super();
24
30
  this._restoreCachedInititalValues();
25
31
  //@ts-ignore
26
- import("codemirror5/lib/codemirror.css", { with: { type: 'css' } }).then(x => this.shadowRoot.adoptedStyleSheets = [cssFromString(x), ...this.shadowRoot.adoptedStyleSheets]);
32
+ import("codemirror5/lib/codemirror.css", { with: { type: 'css' } }).then(x => { if (!this._disposed)
33
+ this.shadowRoot.adoptedStyleSheets = [cssFromString(x.default), ...this.shadowRoot.adoptedStyleSheets]; });
27
34
  //@ts-ignore
28
- import("codemirror5/addon/fold/foldgutter.css", { with: { type: 'css' } }).then(x => this.shadowRoot.adoptedStyleSheets = [cssFromString(x), ...this.shadowRoot.adoptedStyleSheets]);
35
+ import("codemirror5/addon/fold/foldgutter.css", { with: { type: 'css' } }).then(x => { if (!this._disposed)
36
+ this.shadowRoot.adoptedStyleSheets = [cssFromString(x.default), ...this.shadowRoot.adoptedStyleSheets]; });
29
37
  this.style.display = 'block';
30
- this._editor = this._getDomElement('textarea');
31
38
  }
32
39
  dispose() {
40
+ if (this._disposed)
41
+ return;
42
+ this._disposed = true;
43
+ cancelAnimationFrame(this._focusFrame);
44
+ if (this._codeMirrorEditor) {
45
+ this._codeMirrorEditor.off('change', this._onChange);
46
+ this._codeMirrorEditor.toTextArea();
47
+ this._codeMirrorEditor = null;
48
+ }
33
49
  }
34
50
  executeCommand(command) {
51
+ if (!this._codeMirrorEditor)
52
+ return;
35
53
  switch (command.type) {
36
54
  case CommandType.undo:
37
55
  this._codeMirrorEditor.undo();
@@ -45,7 +63,7 @@ export class CodeViewCodeMirror5 extends BaseCustomWebComponentLazyAppend {
45
63
  break;
46
64
  case CommandType.paste:
47
65
  navigator.clipboard.readText().then(text => {
48
- this._codeMirrorEditor.replaceSelection(text);
66
+ this._codeMirrorEditor?.replaceSelection(text);
49
67
  });
50
68
  break;
51
69
  case CommandType.cut:
@@ -59,6 +77,8 @@ export class CodeViewCodeMirror5 extends BaseCustomWebComponentLazyAppend {
59
77
  }
60
78
  }
61
79
  canExecuteCommand(command) {
80
+ if (!this._codeMirrorEditor)
81
+ return false;
62
82
  switch (command.type) {
63
83
  case CommandType.undo:
64
84
  case CommandType.redo:
@@ -71,12 +91,19 @@ export class CodeViewCodeMirror5 extends BaseCustomWebComponentLazyAppend {
71
91
  return false;
72
92
  }
73
93
  focusEditor() {
74
- requestAnimationFrame(() => {
94
+ cancelAnimationFrame(this._focusFrame);
95
+ this._focusFrame = requestAnimationFrame(() => {
96
+ if (this._disposed)
97
+ return;
75
98
  this.focus();
76
- this._codeMirrorEditor.focus();
99
+ this._codeMirrorEditor?.focus();
77
100
  });
78
101
  }
79
102
  ready() {
103
+ if (this._disposed)
104
+ return;
105
+ this._editor = this._getDomElement('textarea');
106
+ this._editor.value = this.code ?? '';
80
107
  const config = {
81
108
  tabSize: 3,
82
109
  lineNumbers: true,
@@ -89,17 +116,22 @@ export class CodeViewCodeMirror5 extends BaseCustomWebComponentLazyAppend {
89
116
  foldGutter: true,
90
117
  gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
91
118
  };
92
- this._codeMirrorEditor = CodeMirror(this._editor, config);
119
+ this._codeMirrorEditor = CodeMirror.fromTextArea(this._editor, config);
93
120
  this._codeMirrorEditor.setSize('100%', '100%');
94
- this._codeMirrorEditor.on('change', () => this.onTextChanged.emit(this._codeMirrorEditor.getValue()));
121
+ this._codeMirrorEditor.on('change', this._onChange);
95
122
  }
96
123
  update(code) {
97
- this._codeMirrorEditor.setValue(code);
124
+ if (this._disposed)
125
+ return;
126
+ this.code = code;
127
+ this._codeMirrorEditor?.setValue(code);
98
128
  }
99
129
  getText() {
100
- return this._codeMirrorEditor.getValue();
130
+ return this._codeMirrorEditor?.getValue() ?? this.code ?? '';
101
131
  }
102
132
  setSelection(position) {
133
+ if (!this._codeMirrorEditor || !position)
134
+ return;
103
135
  let point1 = this._codeMirrorEditor.posFromIndex(position.start);
104
136
  let point2 = this._codeMirrorEditor.posFromIndex(position.start + position.length);
105
137
  this._codeMirrorEditor.setSelection(point1, point2);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "description": "web-component-designer addon: Code Editor using Codemirror 5",
3
3
  "name": "@node-projects/web-component-designer-codeview-codemirror5",
4
- "version": "0.1.5",
4
+ "version": "0.3.0",
5
5
  "type": "module",
6
6
  "main": "./dist/widgets/codeView/code-view-codemirror5.js",
7
7
  "author": "jochen.kuehner@gmx.de",
@@ -9,12 +9,13 @@
9
9
  "scripts": {
10
10
  "tsc": "tsc",
11
11
  "build": "tsc",
12
+ "link": "npm link",
12
13
  "prepublishOnly": "npm run build"
13
14
  },
14
15
  "dependencies": {
15
16
  "codemirror5": "npm:codemirror@^5.0.0",
16
- "@node-projects/base-custom-webcomponent": ">=0.19.0",
17
- "@node-projects/web-component-designer": ">=0.1.8"
17
+ "@node-projects/base-custom-webcomponent": ">=0.27.8",
18
+ "@node-projects/web-component-designer": ">=0.1.224"
18
19
  },
19
20
  "repository": {
20
21
  "type": "git",