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