forked from intersystems-community/vscode-objectscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowCodeEditorProvider.ts
More file actions
320 lines (311 loc) · 12.3 KB
/
LowCodeEditorProvider.ts
File metadata and controls
320 lines (311 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import * as vscode from "vscode";
import { lt } from "semver";
import { AtelierAPI } from "../api";
import { loadChanges } from "../commands/compile";
import { StudioActions } from "../commands/studio";
import { clsLangId, sendLowCodeTelemetryEvent } from "../extension";
import { currentFile, notIsfs, openLowCodeEditors, outputChannel } from "../utils";
export class LowCodeEditorProvider implements vscode.CustomTextEditorProvider {
private readonly _rule: string = "/ui/interop/rule-editor";
private readonly _dtl: string = "/ui/interop/dtl-editor";
private readonly _bpl: string = "/ui/interop/bpl-editor";
private _errorMessage(detail: string) {
return vscode.window
.showErrorMessage("Cannot open Low-Code Editor.", {
modal: true,
detail,
})
.then(() => vscode.commands.executeCommand<void>("workbench.action.reopenTextEditor"));
}
async resolveCustomTextEditor(document: vscode.TextDocument, webviewPanel: vscode.WebviewPanel): Promise<void> {
// Check that document is a clean, well-formed class
if (document.languageId != clsLangId) {
return this._errorMessage(`${document.fileName} is not a class.`);
}
if (document.isUntitled) {
return this._errorMessage(`${document.fileName} is untitled.`);
}
if (document.isDirty) {
return this._errorMessage(`${document.fileName} is dirty.`);
}
const file = currentFile(document);
if (!file) {
return this._errorMessage(`${document.fileName} is a malformed class definition.`);
}
const api = new AtelierAPI(document.uri);
if (!vscode.workspace.fs.isWritableFileSystem(document.uri.scheme) && lt(api.config.serverVersion, "2025.3.0")) {
return this._errorMessage(`File system '${document.uri.scheme}' is read-only.`);
}
const className = file.name.slice(0, -4);
if (!api.active) {
return this._errorMessage("Server connection is not active.");
}
if (lt(api.config.serverVersion, "2023.1.0")) {
return this._errorMessage(
"Opening a low-code editor in VS Code requires InterSystems IRIS version 2023.1 or above."
);
}
// Check that the class exists on the server and is a rule or DTL class
let webApp: string;
const queryData = await api.actionQuery(
"SELECT $LENGTH(rule.Name) AS Rule, $LENGTH(dtl.Name) AS DTL, $LENGTH(bpl.Name) AS BPL " +
"FROM %Dictionary.ClassDefinition AS dcd " +
"LEFT OUTER JOIN %Dictionary.ClassDefinition_SubclassOf('Ens.Rule.Definition') AS rule ON dcd.Name = rule.Name " +
"LEFT OUTER JOIN %Dictionary.ClassDefinition_SubclassOf('Ens.DataTransformDTL') AS dtl ON dcd.Name = dtl.Name " +
"LEFT OUTER JOIN %Dictionary.ClassDefinition_SubclassOf('Ens.BusinessProcessBPL') AS bpl ON dcd.Name = bpl.Name " +
"WHERE dcd.Name = ?",
[className]
);
if (queryData.result.content.length == 0) {
// Class doesn't exist on the server
return this._errorMessage(`${file.name} does not exist on the server.`);
} else if (queryData.result.content[0].Rule) {
webApp = this._rule;
} else if (queryData.result.content[0].DTL) {
if (lt(api.config.serverVersion, "2025.1.0")) {
return this._errorMessage(
"Opening the DTL editor in VS Code requires InterSystems IRIS version 2025.1 or above."
);
}
webApp = this._dtl;
} else if (queryData.result.content[0].BPL) {
if (lt(api.config.serverVersion, "2026.1.0")) {
return this._errorMessage(
"Opening the BPL editor in VS Code requires InterSystems IRIS version 2026.1 or above."
);
}
webApp = this._bpl;
} else {
// Class exists but is not a rule, BPL, or DTL class
return this._errorMessage(
`${className} is not a rule definition, DTL transformation, or BPL business process class.`
);
}
sendLowCodeTelemetryEvent(webApp == this._rule ? "rule" : webApp == this._bpl ? "bpl" : "dtl", document.uri.scheme);
// Add this document to the Set of open low-code editors
const documentUriString = document.uri.toString();
openLowCodeEditors.add(documentUriString);
const isClientSide = notIsfs(document.uri);
// Initialize the webview
const targetOrigin = `${api.config.https ? "https" : "http"}://${api.config.host}:${api.config.port}`;
webviewPanel.webview.options = {
enableScripts: true,
localResourceRoots: [],
};
webviewPanel.webview.html = `
<!DOCTYPE html>
<html lang="en">
<head>
<style type="text/css">
body, html {
margin: 0; padding: 0; height: 100%; overflow: hidden;
background-color: white;
}
#content {
position:absolute; left: 0; right: 0; bottom: 0; top: 0px;
}
</style>
</head>
<body>
<div id="content">
<iframe id="editor" title="Low-Code Editor" src="${targetOrigin}${api.config.pathPrefix}${webApp}/index.html?${[
`$NAMESPACE=${api.ns}`,
"VSCODE=1",
...(!vscode.workspace.fs.isWritableFileSystem(document.uri.scheme) ? ["READONLY=1"] : []),
`${webApp == this._rule ? "rule" : webApp == this._bpl ? "BP" : "DTL"}=${className}`,
].join("&")}" width="100%" height="100%" frameborder="0"></iframe>
</div>
<script>
(function() {
const vscode = acquireVsCodeApi();
const iframe = document.getElementById('editor');
iframe.onload = () => {
// Tell VS Code to check if the editor is compatible
vscode.postMessage({ type: "loaded" });
}
window.onmessage = (event) => {
const data = event.data;
if (data.direction == 'editor') {
iframe.contentWindow.postMessage(data, '${targetOrigin}');
}
else if (data.direction == 'vscode') {
vscode.postMessage(data);
}
}
}())
</script>
</body>
</html>
`;
// Initialize event handlers
let ignoreChanges = false;
let documentWasDirty = false;
let editorCompatible = false;
const contentDisposable = vscode.workspace.onDidChangeTextDocument((event) => {
if (documentUriString == event.document.uri.toString()) {
if (event.reason == vscode.TextDocumentChangeReason.Undo) {
if (ignoreChanges) {
ignoreChanges = false;
} else {
// User invoked undo
webviewPanel.webview.postMessage({
direction: "editor",
type: "undo",
});
ignoreChanges = true;
vscode.commands.executeCommand("redo");
}
} else if (event.reason == vscode.TextDocumentChangeReason.Redo) {
if (ignoreChanges) {
ignoreChanges = false;
} else {
// User invoked redo
webviewPanel.webview.postMessage({
direction: "editor",
type: "redo",
});
ignoreChanges = true;
vscode.commands.executeCommand("undo");
}
} else if (!ignoreChanges && !event.document.isDirty && documentWasDirty) {
// User reverted the file
webviewPanel.webview.postMessage({
direction: "editor",
type: "revert",
});
}
documentWasDirty = event.document.isDirty;
}
});
const saveDisposable = vscode.workspace.onDidSaveTextDocument((savedDocument) => {
if (documentUriString == savedDocument.uri.toString()) {
// User saved the file
webviewPanel.webview.postMessage({
direction: "editor",
type: "save",
});
}
});
webviewPanel.webview.onDidReceiveMessage((event) => {
switch (event.type) {
case "compatible":
editorCompatible = true;
return;
case "badrule":
case "baddtl":
this._errorMessage(event.reason);
return;
case "loaded":
if (!editorCompatible) {
this._errorMessage("This low-code editor does not support embedding in VS Code.");
} else {
// Editor is compatible so send the credentials
webviewPanel.webview.postMessage({
direction: "editor",
type: "auth",
username: api.config.username,
password: api.config.password,
});
}
return;
case "changed":
if (event.dirty && !document.isDirty) {
// Make a trivial edit so the document appears dirty
const edit = new vscode.WorkspaceEdit();
edit.insert(document.uri, document.lineAt(document.lineCount - 1).range.end, " ");
vscode.workspace.applyEdit(edit);
} else if (!event.dirty && document.isDirty) {
// Revert so document is clean
ignoreChanges = true;
vscode.commands.executeCommand("workbench.action.files.revert");
}
return;
case "saved":
if (document.isDirty) {
// Revert so document is clean
ignoreChanges = true;
vscode.commands.executeCommand("workbench.action.files.revert");
}
if (vscode.workspace.getConfiguration("objectscript", document.uri).get<boolean>("compileOnSave")) {
// User requests a post-save compile
webviewPanel.webview.postMessage({
direction: "editor",
type: "compile",
});
} else if (isClientSide) {
// Load changes
loadChanges([file], true);
}
return;
case "compiled":
if (document.isDirty) {
// Revert so document is clean
ignoreChanges = true;
vscode.commands.executeCommand("workbench.action.files.revert");
}
// Load changes
if (isClientSide) loadChanges([file], true);
return;
case "userAction": {
// Process the source control user action
// Should only be action 2 (show web page)
new StudioActions(document.uri).processUserAction(event.action).then((answer) => {
if (answer) {
api
.actionQuery("SELECT * FROM %Atelier_v1_Utils.Extension_AfterUserAction(?,?,?,?,?)", [
"0",
event.id,
file.name,
answer,
"",
])
.then((data) => {
if (data.result.content.length) {
const actionToProcess = data.result.content.pop();
if (actionToProcess.reload) {
// Revert so document is clean
ignoreChanges = true;
vscode.commands.executeCommand("workbench.action.files.revert");
// Tell the rule editor to reload
webviewPanel.webview.postMessage({
direction: "editor",
type: "revert",
});
}
if (actionToProcess.errorText !== "") {
outputChannel.appendLine(
`\nError executing AfterUserAction '${event.label}':\n${actionToProcess.errorText}`
);
outputChannel.show();
}
}
})
.catch((error) => {
outputChannel.appendLine(`\nError executing AfterUserAction '${event.label}':`);
if (error && error.errorText && error.errorText !== "") {
outputChannel.appendLine(error.errorText);
} else {
outputChannel.appendLine(
typeof error == "string" ? error : error instanceof Error ? error.message : JSON.stringify(error)
);
}
outputChannel.show();
});
}
});
return;
}
}
});
webviewPanel.onDidDispose(() => {
contentDisposable.dispose();
saveDisposable.dispose();
if (document.isDirty) {
// Revert so document is clean
vscode.commands.executeCommand("workbench.action.files.revert");
}
// Remove this document from the Set of open low-code editors
openLowCodeEditors.delete(documentUriString);
});
}
}