@node-projects/web-component-designer-codeview-monaco 0.1.0 → 0.1.3
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.
package/README.md
CHANGED
|
@@ -13,4 +13,16 @@ This uses the Monaco Editor for the Webcomponent Designer Codeview
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
15
|
import { CodeViewCodeMonaco } from '@node-projects/web-component-designer-codeview-monaco';
|
|
16
|
-
serviceContainer.config.codeViewWidget = CodeViewCodeMonaco;
|
|
16
|
+
serviceContainer.config.codeViewWidget = CodeViewCodeMonaco;
|
|
17
|
+
|
|
18
|
+
You need to load the monaco manualy and call
|
|
19
|
+
|
|
20
|
+
await CodeViewCodeMonaco.setMonacoLibrary(...);
|
|
21
|
+
|
|
22
|
+
or via
|
|
23
|
+
|
|
24
|
+
await CodeViewCodeMonaco.loadMonacoEditorViaRequire();
|
|
25
|
+
|
|
26
|
+
or
|
|
27
|
+
|
|
28
|
+
await CodeViewCodeMonaco.loadMonacoEditorViaImport();
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { BaseCustomWebComponentLazyAppend, TypedEvent } from '@node-projects/base-custom-webcomponent';
|
|
2
|
+
import { IActivateable, ICodeView, IStringPosition, IUiCommand, IUiCommandHandler } from '@node-projects/web-component-designer';
|
|
3
|
+
import type * as monacoType from 'monaco-editor';
|
|
4
|
+
export declare class CodeViewMonaco extends BaseCustomWebComponentLazyAppend implements ICodeView, IActivateable, IUiCommandHandler {
|
|
5
|
+
private static _initalized;
|
|
6
|
+
static monacoLib: {
|
|
7
|
+
editor: typeof monacoType.editor;
|
|
8
|
+
Range: typeof monacoType.Range;
|
|
9
|
+
};
|
|
10
|
+
dispose(): void;
|
|
11
|
+
canvasElement: HTMLElement;
|
|
12
|
+
elementsToPackages: Map<string, string>;
|
|
13
|
+
code: string;
|
|
14
|
+
onTextChanged: TypedEvent<string>;
|
|
15
|
+
language: string;
|
|
16
|
+
private _theme;
|
|
17
|
+
get theme(): string;
|
|
18
|
+
set theme(value: string);
|
|
19
|
+
static readonly properties: {
|
|
20
|
+
code: StringConstructor;
|
|
21
|
+
language: StringConstructor;
|
|
22
|
+
theme: StringConstructor;
|
|
23
|
+
};
|
|
24
|
+
private _monacoEditor;
|
|
25
|
+
private _editor;
|
|
26
|
+
static readonly style: CSSStyleSheet;
|
|
27
|
+
static readonly template: HTMLTemplateElement;
|
|
28
|
+
executeCommand(command: IUiCommand): void;
|
|
29
|
+
canExecuteCommand(command: IUiCommand): boolean;
|
|
30
|
+
static loadMonacoEditorViaRequire(path?: string): Promise<void>;
|
|
31
|
+
static loadMonacoEditorViaImport(): Promise<void>;
|
|
32
|
+
static setMonacoLibrary(monaco: any): void;
|
|
33
|
+
static initMonacoEditor(monaco: any): void;
|
|
34
|
+
constructor();
|
|
35
|
+
ready(): Promise<void>;
|
|
36
|
+
focusEditor(): void;
|
|
37
|
+
activated(): void;
|
|
38
|
+
update(code: any): void;
|
|
39
|
+
getText(): string;
|
|
40
|
+
setSelection(position: IStringPosition): void;
|
|
41
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { BaseCustomWebComponentLazyAppend, css, html, TypedEvent } from '@node-projects/base-custom-webcomponent';
|
|
2
|
+
import { CommandType } from '@node-projects/web-component-designer';
|
|
3
|
+
export class CodeViewMonaco extends BaseCustomWebComponentLazyAppend {
|
|
4
|
+
static _initalized;
|
|
5
|
+
//@ts-ignore
|
|
6
|
+
static monacoLib = monaco;
|
|
7
|
+
dispose() {
|
|
8
|
+
this._monacoEditor?.dispose();
|
|
9
|
+
}
|
|
10
|
+
canvasElement;
|
|
11
|
+
elementsToPackages;
|
|
12
|
+
code;
|
|
13
|
+
onTextChanged = new TypedEvent();
|
|
14
|
+
language = 'html';
|
|
15
|
+
_theme = 'webComponentDesignerTheme';
|
|
16
|
+
get theme() {
|
|
17
|
+
return this._theme;
|
|
18
|
+
}
|
|
19
|
+
set theme(value) {
|
|
20
|
+
this._theme = value;
|
|
21
|
+
CodeViewMonaco.monacoLib.editor.setTheme(value);
|
|
22
|
+
}
|
|
23
|
+
static properties = {
|
|
24
|
+
code: String,
|
|
25
|
+
language: String,
|
|
26
|
+
theme: String
|
|
27
|
+
};
|
|
28
|
+
_monacoEditor;
|
|
29
|
+
_editor;
|
|
30
|
+
static style = css `
|
|
31
|
+
:host {
|
|
32
|
+
display: block;
|
|
33
|
+
height: 100%;
|
|
34
|
+
width: 100%;
|
|
35
|
+
}
|
|
36
|
+
`;
|
|
37
|
+
static template = html `
|
|
38
|
+
<div id="container" style="overflow: hidden; width: 100%; height: 100%; position: absolute;"></div>
|
|
39
|
+
`;
|
|
40
|
+
executeCommand(command) {
|
|
41
|
+
switch (command.type) {
|
|
42
|
+
case CommandType.undo:
|
|
43
|
+
this._monacoEditor.trigger('source', 'undo', null);
|
|
44
|
+
break;
|
|
45
|
+
case CommandType.redo:
|
|
46
|
+
this._monacoEditor.trigger('source', 'redo', null);
|
|
47
|
+
break;
|
|
48
|
+
case CommandType.copy:
|
|
49
|
+
this._monacoEditor.trigger('source', 'editor.action.clipboardCopyAction', null);
|
|
50
|
+
break;
|
|
51
|
+
case CommandType.paste:
|
|
52
|
+
this._monacoEditor.trigger('source', 'editor.action.clipboardPasteAction', null);
|
|
53
|
+
break;
|
|
54
|
+
case CommandType.cut:
|
|
55
|
+
break;
|
|
56
|
+
case CommandType.delete:
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
canExecuteCommand(command) {
|
|
61
|
+
switch (command.type) {
|
|
62
|
+
case CommandType.undo:
|
|
63
|
+
case CommandType.redo:
|
|
64
|
+
case CommandType.copy:
|
|
65
|
+
case CommandType.paste:
|
|
66
|
+
case CommandType.cut:
|
|
67
|
+
case CommandType.delete:
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
static loadMonacoEditorViaRequire(path = 'node_modules/monaco-editor/min/vs') {
|
|
73
|
+
return new Promise(async (resolve) => {
|
|
74
|
+
//@ts-ignore
|
|
75
|
+
require.config({ paths: { 'vs': path } });
|
|
76
|
+
//@ts-ignore
|
|
77
|
+
require(['vs/editor/editor.main'], () => {
|
|
78
|
+
//@ts-ignore
|
|
79
|
+
CodeViewMonaco.monacoLib = monaco;
|
|
80
|
+
resolve();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
static async loadMonacoEditorViaImport() {
|
|
85
|
+
let monaco = await import('monaco-editor');
|
|
86
|
+
CodeViewMonaco.monacoLib = monaco;
|
|
87
|
+
}
|
|
88
|
+
static setMonacoLibrary(monaco) {
|
|
89
|
+
CodeViewMonaco.monacoLib = monaco;
|
|
90
|
+
}
|
|
91
|
+
static initMonacoEditor(monaco) {
|
|
92
|
+
if (!CodeViewMonaco._initalized) {
|
|
93
|
+
CodeViewMonaco._initalized = true;
|
|
94
|
+
monaco.editor.defineTheme('webComponentDesignerTheme', {
|
|
95
|
+
base: 'vs',
|
|
96
|
+
inherit: true,
|
|
97
|
+
//@ts-ignore
|
|
98
|
+
rules: [{ background: 'EDF9FA' }],
|
|
99
|
+
colors: {
|
|
100
|
+
'editor.selectionBackground': '#add6ff',
|
|
101
|
+
'editor.inactiveSelectionBackground': '#add6ff'
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
monaco.editor.setTheme('webComponentDesignerTheme');
|
|
106
|
+
}
|
|
107
|
+
constructor() {
|
|
108
|
+
super();
|
|
109
|
+
this._restoreCachedInititalValues();
|
|
110
|
+
}
|
|
111
|
+
async ready() {
|
|
112
|
+
this._parseAttributesToProperties();
|
|
113
|
+
//@ts-ignore
|
|
114
|
+
let style = await import("monaco-editor/min/vs/editor/editor.main.css", { assert: { type: 'css' } });
|
|
115
|
+
this.shadowRoot.adoptedStyleSheets = [style.default, this.constructor.style];
|
|
116
|
+
this._editor = this._getDomElement('container');
|
|
117
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
118
|
+
if (this._editor.offsetWidth > 0) {
|
|
119
|
+
this._monacoEditor = CodeViewMonaco.monacoLib.editor.create(this._editor, {
|
|
120
|
+
automaticLayout: true,
|
|
121
|
+
wordWrapColumn: 1000,
|
|
122
|
+
//wordWrap: 'wordWrapColumn',
|
|
123
|
+
fontLigatures: '',
|
|
124
|
+
value: this.code,
|
|
125
|
+
language: this.language,
|
|
126
|
+
minimap: {
|
|
127
|
+
size: 'fill'
|
|
128
|
+
},
|
|
129
|
+
fixedOverflowWidgets: true,
|
|
130
|
+
scrollbar: {
|
|
131
|
+
useShadows: false,
|
|
132
|
+
verticalHasArrows: true,
|
|
133
|
+
horizontalHasArrows: true,
|
|
134
|
+
vertical: 'visible',
|
|
135
|
+
horizontal: 'visible'
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
CodeViewMonaco.initMonacoEditor(CodeViewMonaco.monacoLib);
|
|
139
|
+
let changeContentListener = this._monacoEditor.getModel().onDidChangeContent(e => {
|
|
140
|
+
this.onTextChanged.emit(this._monacoEditor.getValue());
|
|
141
|
+
});
|
|
142
|
+
this._monacoEditor.onDidChangeModel(e => {
|
|
143
|
+
changeContentListener.dispose();
|
|
144
|
+
changeContentListener = this._monacoEditor.getModel().onDidChangeContent(e => {
|
|
145
|
+
this.onTextChanged.emit(this._monacoEditor.getValue());
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
this._monacoEditor.focus();
|
|
149
|
+
resizeObserver.disconnect();
|
|
150
|
+
}
|
|
151
|
+
;
|
|
152
|
+
});
|
|
153
|
+
resizeObserver.observe(this._editor);
|
|
154
|
+
}
|
|
155
|
+
focusEditor() {
|
|
156
|
+
requestAnimationFrame(() => {
|
|
157
|
+
this.focus();
|
|
158
|
+
if (this._monacoEditor)
|
|
159
|
+
this._monacoEditor.focus();
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
activated() {
|
|
163
|
+
if (this._monacoEditor)
|
|
164
|
+
if (this._monacoEditor)
|
|
165
|
+
this._monacoEditor.layout();
|
|
166
|
+
}
|
|
167
|
+
update(code) {
|
|
168
|
+
this.code = code;
|
|
169
|
+
if (this._monacoEditor) {
|
|
170
|
+
if (this._monacoEditor)
|
|
171
|
+
this._monacoEditor.setValue(code);
|
|
172
|
+
CodeViewMonaco.monacoLib.editor.setModelLanguage(this._monacoEditor.getModel(), this.language);
|
|
173
|
+
CodeViewMonaco.monacoLib.editor.setTheme(this.theme);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
getText() {
|
|
177
|
+
return this._monacoEditor.getValue();
|
|
178
|
+
}
|
|
179
|
+
setSelection(position) {
|
|
180
|
+
if (this._monacoEditor) {
|
|
181
|
+
let model = this._monacoEditor.getModel();
|
|
182
|
+
let point1 = model.getPositionAt(position.start);
|
|
183
|
+
let point2 = model.getPositionAt(position.start + position.length);
|
|
184
|
+
setTimeout(() => {
|
|
185
|
+
this._monacoEditor.setSelection({ startLineNumber: point1.lineNumber, startColumn: point1.column, endLineNumber: point2.lineNumber, endColumn: point2.column });
|
|
186
|
+
this._monacoEditor.revealRangeInCenterIfOutsideViewport(new CodeViewMonaco.monacoLib.Range(point1.lineNumber, point1.column, point2.lineNumber, point2.column), 1);
|
|
187
|
+
}, 50);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
customElements.define('node-projects-code-view-monaco', CodeViewMonaco);
|
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.1.
|
|
4
|
+
"version": "0.1.3",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/widgets/codeView/code-view-monaco.js",
|
|
7
7
|
"author": "jochen.kuehner@gmx.de",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"monaco-editor": "^0.43.0",
|
|
16
16
|
"@node-projects/base-custom-webcomponent": "^0.17.2",
|
|
17
|
-
"@node-projects/web-component-designer": "^0.1.
|
|
17
|
+
"@node-projects/web-component-designer": "^0.1.1"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|