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