@node-projects/web-component-designer-codeview-codemirror 0.1.0 → 0.1.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.
|
File without changes
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
import { BaseCustomWebComponentLazyAppend, css, html, TypedEvent } from '@node-projects/base-custom-webcomponent';
|
|
4
|
+
import { ICodeView, IDisposable, IUiCommand, CommandType, IStringPosition } from '@node-projects/web-component-designer';
|
|
5
|
+
import CodeMirror from 'codemirror';
|
|
6
|
+
|
|
7
|
+
export class CodeViewCodeMirror extends BaseCustomWebComponentLazyAppend implements ICodeView, IDisposable {
|
|
8
|
+
canvasElement: HTMLElement;
|
|
9
|
+
elementsToPackages: Map<string, string>;
|
|
10
|
+
|
|
11
|
+
public code: string;
|
|
12
|
+
public onTextChanged = new TypedEvent<string>();
|
|
13
|
+
public mode: string = 'xml';
|
|
14
|
+
|
|
15
|
+
private _codeMirrorEditor: CodeMirror.Editor;
|
|
16
|
+
private _editor: HTMLTextAreaElement;
|
|
17
|
+
|
|
18
|
+
static override readonly style = css`
|
|
19
|
+
:host {
|
|
20
|
+
display: block;
|
|
21
|
+
height: 100%;
|
|
22
|
+
width: 100%;
|
|
23
|
+
}`;
|
|
24
|
+
|
|
25
|
+
static override readonly template = html`
|
|
26
|
+
<div style="width: 100%; height: 100%; overflow: auto;">
|
|
27
|
+
<div id="textarea"></div>
|
|
28
|
+
</div>`;
|
|
29
|
+
|
|
30
|
+
constructor() {
|
|
31
|
+
super();
|
|
32
|
+
this._restoreCachedInititalValues();
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
this.style.display = 'block';
|
|
36
|
+
this._editor = this._getDomElement<HTMLTextAreaElement>('textarea');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
dispose(): void {
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
executeCommand(command: IUiCommand) {
|
|
43
|
+
switch (command.type) {
|
|
44
|
+
case CommandType.undo:
|
|
45
|
+
this._codeMirrorEditor.undo();
|
|
46
|
+
break;
|
|
47
|
+
case CommandType.redo:
|
|
48
|
+
this._codeMirrorEditor.redo();
|
|
49
|
+
break;
|
|
50
|
+
case CommandType.copy:
|
|
51
|
+
const text = this._codeMirrorEditor.getSelection();
|
|
52
|
+
navigator.clipboard.writeText(text);
|
|
53
|
+
break;
|
|
54
|
+
case CommandType.paste:
|
|
55
|
+
navigator.clipboard.readText().then(text => {
|
|
56
|
+
this._codeMirrorEditor.replaceSelection(text);
|
|
57
|
+
});
|
|
58
|
+
break;
|
|
59
|
+
case CommandType.cut:
|
|
60
|
+
const textc = this._codeMirrorEditor.getSelection();
|
|
61
|
+
navigator.clipboard.writeText(textc);
|
|
62
|
+
this._codeMirrorEditor.replaceSelection('');
|
|
63
|
+
break;
|
|
64
|
+
case CommandType.delete:
|
|
65
|
+
this._codeMirrorEditor.replaceSelection('');
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
canExecuteCommand(command: IUiCommand) {
|
|
71
|
+
switch (command.type) {
|
|
72
|
+
case CommandType.undo:
|
|
73
|
+
case CommandType.redo:
|
|
74
|
+
case CommandType.copy:
|
|
75
|
+
case CommandType.paste:
|
|
76
|
+
case CommandType.cut:
|
|
77
|
+
case CommandType.delete:
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
focusEditor() {
|
|
84
|
+
requestAnimationFrame(() => {
|
|
85
|
+
this.focus();
|
|
86
|
+
this._codeMirrorEditor.focus();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
ready() {
|
|
91
|
+
const config: CodeMirror.EditorConfiguration = {
|
|
92
|
+
tabSize: 3,
|
|
93
|
+
lineNumbers: true,
|
|
94
|
+
mode: this.mode,
|
|
95
|
+
//@ts-ignore
|
|
96
|
+
htmlMode: true,
|
|
97
|
+
lineWrapping: true,
|
|
98
|
+
//@ts-ignore
|
|
99
|
+
extraKeys: { "Ctrl-Q": function (cm) { cm.foldCode(cm.getCursor()); } },
|
|
100
|
+
foldGutter: true,
|
|
101
|
+
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
this._codeMirrorEditor = CodeMirror(this._editor, config);
|
|
105
|
+
this._codeMirrorEditor.setSize('100%', '100%');
|
|
106
|
+
this._codeMirrorEditor.on('change', () => this.onTextChanged.emit(this._codeMirrorEditor.getValue()))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
update(code) {
|
|
110
|
+
this._codeMirrorEditor.setValue(code);
|
|
111
|
+
}
|
|
112
|
+
getText() {
|
|
113
|
+
return this._codeMirrorEditor.getValue();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
setSelection(position: IStringPosition) {
|
|
117
|
+
|
|
118
|
+
let point1 = this._codeMirrorEditor.posFromIndex(position.start);
|
|
119
|
+
let point2 = this._codeMirrorEditor.posFromIndex(position.start + position.length);
|
|
120
|
+
this._codeMirrorEditor.setSelection(point1, point2);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
customElements.define('node-projects-code-view-codemirror', CodeViewCodeMirror);
|
|
125
|
+
*/
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"description": "web-component-designer addon: Code Editor using Codemirror 6 and newer",
|
|
3
3
|
"name": "@node-projects/web-component-designer-codeview-codemirror",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/widgets/codeView/code-view-codemirror.js",
|
|
7
7
|
"author": "jochen.kuehner@gmx.de",
|