-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rete.jsx
269 lines (257 loc) · 9.64 KB
/
rete.jsx
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
// eslint-disable-next-line no-unused-vars
import React from "react";
import Rete from "rete";
import ReactRenderPlugin from "rete-react-render-plugin";
import ConnectionPlugin from "rete-connection-plugin";
import AreaPlugin from "rete-area-plugin";
import ContextMenuPlugin from "rete-context-menu-plugin";
import MinimapPlugin from "rete-minimap-plugin";
import ConnectionPathPlugin from "rete-connection-path-plugin";
import ConnectionReroutePlugin from "rete-connection-reroute-plugin";
import AutoArrangePlugin from "rete-auto-arrange-plugin";
import HistoryPlugin from "rete-history-plugin";
import { JsonComponent } from "./rete/components/input/JsonComponent";
import { DummyJsonComponent } from "./rete/components/input/DummyJsonComponent";
import { UrlComponent } from "./rete/components/input/UrlComponent";
import { UrlWithPathParamComponent } from "./rete/components/input/UrlWithPathParamComponent";
import { HtmlComponent } from "./rete/components/input/HtmlComponent";
import { SqlComponent } from "./rete/components/input/SqlComponent";
import { SqlWithPlaceHolderComponent } from "./rete/components/input/SqlWithPlaceHolderComponent";
import { TemplateComponent } from "./rete/components/template/TemplateComponent";
import { HandlebarsComponent } from "./rete/components/template/HandlebarsComponent";
import { PugComponent } from "./rete/components/template/PugComponent";
import { EndpointComponent } from "./rete/components/EndpointComponent";
import { EndpointWithErrorComponent } from "./rete/components/EndpointWithErrorComponent";
import { JsonManagerComponent } from "./rete/components/JsonManagerComponent";
import { ApiComponent } from "./rete/components/ApiComponent";
import { MySQLComponent } from "./rete/components/MySQLComponent";
import { PostgreSQLComponent } from "./rete/components/PostgreSQLComponent";
import { SQLiteComponent } from "./rete/components/SQLiteComponent";
import { RequestComponent } from "./rete/components/RequestComponent";
import { RedirectEndpointComponent } from "./rete/components/RedirectEndpointComponent";
export async function createEditor(container) {
// 各種Socket定義
// primitive
const stringSocket = new Rete.Socket("String value");
const errorSocket = new Rete.Socket("Error value");
// json
const jsonSocket = new Rete.Socket("Json value");
jsonSocket.combineWith(stringSocket);
const rawJsonSocket = new Rete.Socket("raw Json value");
rawJsonSocket.combineWith(errorSocket);
rawJsonSocket.combineWith(jsonSocket);
rawJsonSocket.combineWith(stringSocket);
// dummyJson
const dummyJsonSocket = new Rete.Socket("DummyJson value");
// template
const templateSocket = new Rete.Socket("Template value");
templateSocket.combineWith(stringSocket);
const handlebarsSocket = new Rete.Socket("Handlebars value");
handlebarsSocket.combineWith(templateSocket);
const pugSocket = new Rete.Socket("Pug value");
pugSocket.combineWith(templateSocket);
// html
const htmlSocket = new Rete.Socket("HTML value");
htmlSocket.combineWith(stringSocket);
const rawHtmlSocket = new Rete.Socket("HTML Json value");
rawHtmlSocket.combineWith(errorSocket);
rawHtmlSocket.combineWith(htmlSocket);
rawHtmlSocket.combineWith(stringSocket);
// sql
const sqlSocket = new Rete.Socket("SQL value");
// url
const urlSocket = new Rete.Socket("URL value");
// 利用可能なコンポーネント一覧
const components = [
new EndpointComponent(stringSocket),
new EndpointWithErrorComponent(stringSocket, errorSocket),
new JsonManagerComponent(jsonSocket),
new JsonComponent(rawJsonSocket),
new DummyJsonComponent(dummyJsonSocket),
new UrlComponent(urlSocket),
new UrlWithPathParamComponent(urlSocket, jsonSocket),
new HtmlComponent(rawHtmlSocket),
new TemplateComponent(jsonSocket, templateSocket, htmlSocket),
new HandlebarsComponent(handlebarsSocket),
new PugComponent(pugSocket),
new SqlComponent(sqlSocket),
new SqlWithPlaceHolderComponent(sqlSocket, jsonSocket),
new ApiComponent(jsonSocket, dummyJsonSocket, urlSocket),
new MySQLComponent(jsonSocket, dummyJsonSocket, sqlSocket),
new PostgreSQLComponent(jsonSocket, dummyJsonSocket, sqlSocket),
new SQLiteComponent(jsonSocket, dummyJsonSocket, sqlSocket),
new RequestComponent(jsonSocket, dummyJsonSocket),
new RedirectEndpointComponent(),
];
const ua = window.navigator.userAgent.toLowerCase();
const isMacOS = ua.indexOf("mac os x") !== -1;
const isInputFocused = () => {
const { nodeName } = document.activeElement;
return nodeName === "TEXTAREA" || nodeName === "INPUT";
};
const editor = new Rete.NodeEditor("tuna-mayonnaise@0.0.1", container);
editor.use(ConnectionPlugin);
editor.use(ReactRenderPlugin);
// Nodeのサブメニュー(削除・複製機能)を追加
// 及びNode外を右クリックでコンテキストメニューを表示
const errorHandlerOnSave = () => {
import("react-hot-toast").then((_) => _.toast.error("NOT CONNECTED :("));
};
editor.use(ContextMenuPlugin, {
searchBar: false,
delay: 100,
allocate(component) {
return component.path;
},
rename(component) {
return component.name;
},
items: {
Arrange() {
editor.nodes.forEach((node) => {
editor.trigger("arrange", node);
});
},
Undo() {
editor.trigger("undo");
},
Redo() {
editor.trigger("redo");
},
Save() {
fetch("/regist", {
method: "POST",
body: JSON.stringify(editor.toJSON()),
headers: {
"Content-Type": "application/json",
},
})
.then((res) => {
res.ok
? import("react-hot-toast").then((_) =>
_.toast.success("SAVED :)")
)
: errorHandlerOnSave();
})
.catch(() => errorHandlerOnSave());
},
Download() {
const blob = new Blob([JSON.stringify(editor.toJSON())], {
type: "application/json; charset=utf-8",
});
import("file-saver").then(({ saveAs }) =>
saveAs(blob, "tuna-mayonnaise.json")
);
},
Debug() {
// eslint-disable-next-line no-console
console.log(JSON.stringify(editor.toJSON()));
},
},
});
// ダブルクリックおよび入力エリアでのズームを無効化
editor.on(
"zoom",
({ source }) =>
source !== "dblclick" && source === "wheel" && !isInputFocused()
);
// 履歴機能を追加
editor.use(HistoryPlugin, { keyboard: false });
// ショートカットキー設定
editor.on("keydown", (e) => {
// テキスト入力時はキー無効
if (isInputFocused()) return;
switch (e.code) {
// DeleteキーでNodeを削除
case "Delete":
case "Backspace":
editor.selected.each((n) => {
editor.removeNode(n);
});
editor.selected.clear();
return;
// Spaceキーでメニュー表示
case "Space": {
const rect = editor.view.container.getBoundingClientRect();
const event = new PointerEvent("contextmenu", {
clientX: rect.left + rect.width / 2,
clientY: rect.top + rect.height / 2,
});
editor.trigger("contextmenu", { e: event, view: editor.view });
return;
}
default:
break;
}
// MacOSの場合は、Ctrlに加えCommandキーでの入力に対応
if (e.ctrlKey || (isMacOS && e.metaKey)) {
switch (e.code) {
case "KeyY":
editor.trigger("redo");
return;
case "KeyZ":
if (e.shiftKey) {
editor.trigger("redo");
} else {
editor.trigger("undo");
}
break;
default:
break;
}
}
});
// 右下にミニマップを表示
editor.use(MinimapPlugin);
// コネクションパスの見た目を変更(矢印有表記へ)
editor.use(ConnectionPathPlugin, {
type: ConnectionPathPlugin.DEFAULT, // DEFAULT or LINEAR transformer
transformer:
() =>
([x1, y1, x2, y2]) =>
[
[x1, y1],
[x2, y2],
], // optional, custom transformer
curve: ConnectionPathPlugin.curveBundle, // curve id
options: { vertical: false, curvature: 0.4 }, // optional
arrow: { color: "steelblue", marker: "M-5,-10 L-5,10 L20,0 z" },
});
// コネクションパスの見た目を変更(直線ではなく曲線で各Node間を結ぶ)
editor.use(ConnectionReroutePlugin);
editor.use(AutoArrangePlugin, { margin: { x: 200, y: 50 }, depth: 0 });
const engine = new Rete.Engine("tuna-mayonnaise@0.0.1");
components.forEach((c) => {
editor.register(c);
engine.register(c);
});
const errorHandlerOnGetConfig = () => {
document.getElementsByClassName("rightClick")[0].style.display = "block";
return null;
};
const data = await fetch("/tuna-configuration")
.then((res) => (res.ok ? res.json() : errorHandlerOnGetConfig()))
.catch(() => errorHandlerOnGetConfig());
if (data !== null) {
await editor.fromJSON(data);
import("react-hot-toast").then((_) => _.toast.success("RESUMED :)"));
} else {
const endpoint = await components[0].createNode();
endpoint.position = [1000, 200];
editor.addNode(endpoint);
}
editor.on("showcontextmenu", () => {
document.getElementsByClassName("rightClick")[0].style.display = "none";
return true;
});
editor.on(
"process nodecreated noderemoved connectioncreated connectionremoved",
async () => {
await engine.abort();
await engine.process(editor.toJSON());
}
);
editor.view.resize();
editor.trigger("process");
AreaPlugin.zoomAt(editor, editor.nodes);
}