@knotx/plugins-connection-line 0.0.0
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/LICENSE +21 -0
- package/dist/index.cjs +496 -0
- package/dist/index.d.cts +152 -0
- package/dist/index.d.mts +152 -0
- package/dist/index.d.ts +152 -0
- package/dist/index.mjs +490 -0
- package/package.json +63 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
import { jsx } from '@knotx/jsx/jsx-runtime';
|
|
2
|
+
import { Layer, bem, generateId, InteractionPriority, BasePlugin } from '@knotx/core';
|
|
3
|
+
import { inject, register, layer, edgeOperator, OnInit, OnDestroy } from '@knotx/decorators';
|
|
4
|
+
import interact from 'interactjs';
|
|
5
|
+
import { merge } from 'lodash-es';
|
|
6
|
+
import { BehaviorSubject } from 'rxjs';
|
|
7
|
+
import '@knotx/plugins-selection';
|
|
8
|
+
|
|
9
|
+
var __create = Object.create;
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
15
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
16
|
+
var __typeError = (msg) => {
|
|
17
|
+
throw TypeError(msg);
|
|
18
|
+
};
|
|
19
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
20
|
+
var __spreadValues = (a, b) => {
|
|
21
|
+
for (var prop in b || (b = {}))
|
|
22
|
+
if (__hasOwnProp.call(b, prop))
|
|
23
|
+
__defNormalProp(a, prop, b[prop]);
|
|
24
|
+
if (__getOwnPropSymbols)
|
|
25
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
26
|
+
if (__propIsEnum.call(b, prop))
|
|
27
|
+
__defNormalProp(a, prop, b[prop]);
|
|
28
|
+
}
|
|
29
|
+
return a;
|
|
30
|
+
};
|
|
31
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
32
|
+
var __decoratorStart = (base) => {
|
|
33
|
+
var _a2;
|
|
34
|
+
return [, , , __create((_a2 = base == null ? void 0 : base[__knownSymbol("metadata")]) != null ? _a2 : null)];
|
|
35
|
+
};
|
|
36
|
+
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
|
|
37
|
+
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
|
|
38
|
+
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
|
|
39
|
+
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
|
|
40
|
+
var __runInitializers = (array, flags, self, value) => {
|
|
41
|
+
for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
|
|
42
|
+
return value;
|
|
43
|
+
};
|
|
44
|
+
var __decorateElement = (array, flags, name, decorators, target, extra) => {
|
|
45
|
+
var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
|
|
46
|
+
var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
|
|
47
|
+
var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
|
|
48
|
+
var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
|
|
49
|
+
return __privateGet(this, extra);
|
|
50
|
+
}, set [name](x) {
|
|
51
|
+
return __privateSet(this, extra, x);
|
|
52
|
+
} }, name));
|
|
53
|
+
k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
|
|
54
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
55
|
+
ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
|
|
56
|
+
if (k) {
|
|
57
|
+
ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
|
|
58
|
+
if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
|
|
59
|
+
if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
|
|
60
|
+
}
|
|
61
|
+
it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
|
|
62
|
+
if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
|
|
63
|
+
else if (typeof it !== "object" || it === null) __typeError("Object expected");
|
|
64
|
+
else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
|
|
65
|
+
}
|
|
66
|
+
return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
|
|
67
|
+
};
|
|
68
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
69
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
70
|
+
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
|
|
71
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
72
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
73
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
74
|
+
var _destroy_dec, _init_dec, _createNodesAndEdges_dec, _createEdges_dec, _createEdge_dec, _createNodeAndEdge_dec, _containerRender_dec, _registerNewNodeCreator_dec, _registerValidator_dec, _options_dec, _getConnectHandleAttributes_dec, _selectedNodeIds_dec, _canInteract_dec, _endInteraction_dec, _startInteraction_dec, _dispatchEdgeOperation_dec, _dispatchNodeOperation_dec, _getNode_dec, _a, _init;
|
|
75
|
+
const DEFAULT_OPTIONS = {
|
|
76
|
+
allowCreateNodeOnBlankArea: true,
|
|
77
|
+
newNodeType: "default",
|
|
78
|
+
edgeType: "bezier",
|
|
79
|
+
connectionLineClassName: "knotx-connection-line",
|
|
80
|
+
allowMultiDrag: true
|
|
81
|
+
};
|
|
82
|
+
class ConnectionLine extends (_a = BasePlugin, _getNode_dec = [inject.getNode()], _dispatchNodeOperation_dec = [inject.dispatchNodeOperation()], _dispatchEdgeOperation_dec = [inject.dispatchEdgeOperation()], _startInteraction_dec = [inject.startInteraction()], _endInteraction_dec = [inject.endInteraction()], _canInteract_dec = [inject.canInteract()], _selectedNodeIds_dec = [inject("selection", "selectedNodeIds")], _getConnectHandleAttributes_dec = [register("getConnectHandleAttributes")], _options_dec = [register("options")], _registerValidator_dec = [register("registerValidator")], _registerNewNodeCreator_dec = [register("registerNewNodeCreator")], _containerRender_dec = [layer(Layer.Nodes, 10)], _createNodeAndEdge_dec = [edgeOperator()], _createEdge_dec = [edgeOperator()], _createEdges_dec = [edgeOperator()], _createNodesAndEdges_dec = [edgeOperator()], _init_dec = [OnInit], _destroy_dec = [OnDestroy], _a) {
|
|
83
|
+
constructor(options = {}) {
|
|
84
|
+
super();
|
|
85
|
+
__runInitializers(_init, 5, this);
|
|
86
|
+
__publicField(this, "name", "connectionLine");
|
|
87
|
+
__publicField(this, "getNode", __runInitializers(_init, 8, this)), __runInitializers(_init, 11, this);
|
|
88
|
+
__publicField(this, "dispatchNodeOperation", __runInitializers(_init, 12, this)), __runInitializers(_init, 15, this);
|
|
89
|
+
__publicField(this, "dispatchEdgeOperation", __runInitializers(_init, 16, this)), __runInitializers(_init, 19, this);
|
|
90
|
+
__publicField(this, "startInteraction", __runInitializers(_init, 20, this)), __runInitializers(_init, 23, this);
|
|
91
|
+
__publicField(this, "endInteraction", __runInitializers(_init, 24, this)), __runInitializers(_init, 27, this);
|
|
92
|
+
__publicField(this, "canInteract", __runInitializers(_init, 28, this)), __runInitializers(_init, 31, this);
|
|
93
|
+
__publicField(this, "selectedNodeIds", __runInitializers(_init, 32, this, [])), __runInitializers(_init, 35, this);
|
|
94
|
+
__publicField(this, "getConnectHandleAttributes", __runInitializers(_init, 36, this, (nodeId, type, id = 0) => {
|
|
95
|
+
return {
|
|
96
|
+
"className": `${this.classNames.connector} ${this.classNames[type]}`,
|
|
97
|
+
"data-plugin-id": this.pluginId,
|
|
98
|
+
"data-node-id": nodeId,
|
|
99
|
+
"data-connector-id": String(id),
|
|
100
|
+
"data-connector-type": type
|
|
101
|
+
};
|
|
102
|
+
})), __runInitializers(_init, 39, this);
|
|
103
|
+
__publicField(this, "classNames", {
|
|
104
|
+
connector: bem("connection-line", "connector"),
|
|
105
|
+
source: bem("connection-line", "connector", "source"),
|
|
106
|
+
target: bem("connection-line", "connector", "target"),
|
|
107
|
+
active: bem("connection-line", "connector", "active")
|
|
108
|
+
});
|
|
109
|
+
// 临时连接线的状态集合,支持多条线同时拖拽
|
|
110
|
+
__publicField(this, "connectionLines", /* @__PURE__ */ new Map());
|
|
111
|
+
// 拖拽起始缩放比例
|
|
112
|
+
__publicField(this, "startScale", 1);
|
|
113
|
+
// 用于存储当前触发拖拽事件的DOM元素对应的节点ID
|
|
114
|
+
__publicField(this, "currentDragNodeId", null);
|
|
115
|
+
// 各种订阅和交互对象
|
|
116
|
+
__publicField(this, "subscription");
|
|
117
|
+
__publicField(this, "dragInteractable");
|
|
118
|
+
__publicField(this, "dropInteractable");
|
|
119
|
+
__publicField(this, "blankAreaDropInteractable");
|
|
120
|
+
// 验证器列表
|
|
121
|
+
__publicField(this, "validators$", new BehaviorSubject([]));
|
|
122
|
+
// 新节点创建器列表
|
|
123
|
+
__publicField(this, "creators$", new BehaviorSubject([]));
|
|
124
|
+
__publicField(this, "options", __runInitializers(_init, 40, this, DEFAULT_OPTIONS)), __runInitializers(_init, 43, this);
|
|
125
|
+
__publicField(this, "registerValidator", __runInitializers(_init, 44, this, (validator) => {
|
|
126
|
+
const validators = this.validators$.value;
|
|
127
|
+
validators.push(validator);
|
|
128
|
+
this.validators$.next(validators);
|
|
129
|
+
return () => {
|
|
130
|
+
const currentValidators = this.validators$.value;
|
|
131
|
+
const index = currentValidators.indexOf(validator);
|
|
132
|
+
if (index > -1) {
|
|
133
|
+
currentValidators.splice(index, 1);
|
|
134
|
+
this.validators$.next(currentValidators);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
})), __runInitializers(_init, 47, this);
|
|
138
|
+
__publicField(this, "registerNewNodeCreator", __runInitializers(_init, 48, this, (creator) => {
|
|
139
|
+
const creators = this.creators$.value;
|
|
140
|
+
creators.push(creator);
|
|
141
|
+
this.creators$.next(creators);
|
|
142
|
+
return () => {
|
|
143
|
+
const currentCreators = this.creators$.value;
|
|
144
|
+
const index = currentCreators.indexOf(creator);
|
|
145
|
+
if (index > -1) {
|
|
146
|
+
currentCreators.splice(index, 1);
|
|
147
|
+
this.creators$.next(currentCreators);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
})), __runInitializers(_init, 51, this);
|
|
151
|
+
this.options = merge(DEFAULT_OPTIONS, options);
|
|
152
|
+
}
|
|
153
|
+
containerRender() {
|
|
154
|
+
const size = this.getContainerSize();
|
|
155
|
+
return /* @__PURE__ */ jsx(
|
|
156
|
+
"div",
|
|
157
|
+
{
|
|
158
|
+
className: bem("connection-line", "container"),
|
|
159
|
+
"data-plugin-id": this.pluginId,
|
|
160
|
+
style: {
|
|
161
|
+
position: "absolute",
|
|
162
|
+
left: 0,
|
|
163
|
+
top: 0,
|
|
164
|
+
width: size,
|
|
165
|
+
height: size,
|
|
166
|
+
overflow: "visible",
|
|
167
|
+
pointerEvents: "none"
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 获取容器大小
|
|
174
|
+
*/
|
|
175
|
+
getContainerSize() {
|
|
176
|
+
return 1;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* 应用所有验证器,检查连接是否有效
|
|
180
|
+
*/
|
|
181
|
+
validateConnection(sourceNodeId, targetNodeId) {
|
|
182
|
+
const sourceNode = this.getNode(sourceNodeId);
|
|
183
|
+
if (!sourceNode)
|
|
184
|
+
return false;
|
|
185
|
+
let targetNode;
|
|
186
|
+
if (targetNodeId) {
|
|
187
|
+
targetNode = this.getNode(targetNodeId);
|
|
188
|
+
if (!targetNode)
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
const context = {
|
|
192
|
+
sourceNodeId,
|
|
193
|
+
sourceNode,
|
|
194
|
+
targetNodeId,
|
|
195
|
+
targetNode,
|
|
196
|
+
isValid: true
|
|
197
|
+
};
|
|
198
|
+
return this.validators$.value.reduce(
|
|
199
|
+
(isValid, validator) => isValid && validator(context),
|
|
200
|
+
true
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
getContainerElement() {
|
|
204
|
+
return document.querySelector(`.${bem("connection-line", "container")}[data-plugin-id="${this.pluginId}"]`);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* 获取需要为其创建连接线的源节点ID列表
|
|
208
|
+
* 根据当前拖拽的节点和选中的节点决定
|
|
209
|
+
*/
|
|
210
|
+
getSourceNodeIdsForDrag(currentNodeId) {
|
|
211
|
+
if (!this.options.allowMultiDrag || !this.selectedNodeIds.includes(currentNodeId)) {
|
|
212
|
+
return [currentNodeId];
|
|
213
|
+
}
|
|
214
|
+
return [...this.selectedNodeIds];
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* 创建临时连接线
|
|
218
|
+
*/
|
|
219
|
+
createConnectionLine(sourceNodeId, startPosition) {
|
|
220
|
+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
221
|
+
svg.setAttribute("class", `${this.options.connectionLineClassName || "knotx-connection-line"} line-${sourceNodeId}`);
|
|
222
|
+
svg.style.position = "absolute";
|
|
223
|
+
svg.style.top = "0";
|
|
224
|
+
svg.style.left = "0";
|
|
225
|
+
svg.style.overflow = "visible";
|
|
226
|
+
svg.style.width = "100%";
|
|
227
|
+
svg.style.height = "100%";
|
|
228
|
+
svg.style.pointerEvents = "none";
|
|
229
|
+
svg.setAttribute("data-source-node-id", sourceNodeId);
|
|
230
|
+
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
231
|
+
path.setAttribute("fill", "none");
|
|
232
|
+
path.setAttribute("stroke", "currentColor");
|
|
233
|
+
path.setAttribute("stroke-width", `${2 / this.startScale}`);
|
|
234
|
+
path.setAttribute("stroke-dasharray", "5,5");
|
|
235
|
+
svg.appendChild(path);
|
|
236
|
+
const container = this.getContainerElement();
|
|
237
|
+
if (container)
|
|
238
|
+
container.appendChild(svg);
|
|
239
|
+
return {
|
|
240
|
+
sourceNodeId,
|
|
241
|
+
tempLine: svg,
|
|
242
|
+
startPosition: __spreadValues({}, startPosition),
|
|
243
|
+
currentPosition: __spreadValues({}, startPosition)
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* 更新临时连接线的路径
|
|
248
|
+
*/
|
|
249
|
+
updateConnectionLine(state) {
|
|
250
|
+
const path = state.tempLine.querySelector("path");
|
|
251
|
+
if (!path)
|
|
252
|
+
return;
|
|
253
|
+
const sourceX = state.startPosition.x;
|
|
254
|
+
const sourceY = state.startPosition.y;
|
|
255
|
+
const targetX = state.currentPosition.x;
|
|
256
|
+
const targetY = state.currentPosition.y;
|
|
257
|
+
const dx = Math.abs(targetX - sourceX) / 2;
|
|
258
|
+
const controlPoint1X = sourceX + dx;
|
|
259
|
+
const controlPoint1Y = sourceY;
|
|
260
|
+
const controlPoint2X = targetX - dx;
|
|
261
|
+
const controlPoint2Y = targetY;
|
|
262
|
+
const pathData = `M ${sourceX} ${sourceY} C ${controlPoint1X} ${controlPoint1Y}, ${controlPoint2X} ${controlPoint2Y}, ${targetX} ${targetY}`;
|
|
263
|
+
path.setAttribute("d", pathData);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* 更新所有连接线的当前位置
|
|
267
|
+
*/
|
|
268
|
+
updateAllConnectionLines(dx, dy) {
|
|
269
|
+
const currentDragConnectionLine = this.connectionLines.get(this.currentDragNodeId);
|
|
270
|
+
if (!currentDragConnectionLine) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
currentDragConnectionLine.currentPosition.x += dx / this.startScale;
|
|
274
|
+
currentDragConnectionLine.currentPosition.y += dy / this.startScale;
|
|
275
|
+
this.connectionLines.forEach((state) => {
|
|
276
|
+
state.currentPosition.x = currentDragConnectionLine.currentPosition.x;
|
|
277
|
+
state.currentPosition.y = currentDragConnectionLine.currentPosition.y;
|
|
278
|
+
this.updateConnectionLine(state);
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* 清除所有临时连接线
|
|
283
|
+
*/
|
|
284
|
+
clearAllConnectionLines() {
|
|
285
|
+
this.connectionLines.forEach((state, _nodeId) => {
|
|
286
|
+
state.tempLine.remove();
|
|
287
|
+
});
|
|
288
|
+
this.connectionLines.clear();
|
|
289
|
+
}
|
|
290
|
+
createNodeAndEdge(sourceNodeIds, position) {
|
|
291
|
+
const newNodeId = generateId();
|
|
292
|
+
const newNode = {
|
|
293
|
+
id: newNodeId,
|
|
294
|
+
type: this.options.newNodeType,
|
|
295
|
+
position,
|
|
296
|
+
data: {}
|
|
297
|
+
};
|
|
298
|
+
const newEdges = sourceNodeIds.map((sourceNodeId) => ({
|
|
299
|
+
id: generateId(),
|
|
300
|
+
source: sourceNodeId,
|
|
301
|
+
target: newNodeId,
|
|
302
|
+
type: this.options.edgeType
|
|
303
|
+
}));
|
|
304
|
+
const createResult = this.creators$.value.reduce((result, creator) => {
|
|
305
|
+
var _a2;
|
|
306
|
+
return (_a2 = creator(sourceNodeIds, newNode, newEdges)) != null ? _a2 : result;
|
|
307
|
+
}, { node: newNode, edges: newEdges });
|
|
308
|
+
if (createResult === false) {
|
|
309
|
+
return [];
|
|
310
|
+
}
|
|
311
|
+
this.dispatchNodeOperation({
|
|
312
|
+
type: "add",
|
|
313
|
+
data: createResult.node
|
|
314
|
+
});
|
|
315
|
+
return [{
|
|
316
|
+
type: "batch",
|
|
317
|
+
operations: createResult.edges.map((edge) => ({
|
|
318
|
+
type: "add",
|
|
319
|
+
data: edge
|
|
320
|
+
}))
|
|
321
|
+
}];
|
|
322
|
+
}
|
|
323
|
+
createEdge(sourceNodeId, targetNodeId) {
|
|
324
|
+
return [{
|
|
325
|
+
type: "add",
|
|
326
|
+
data: {
|
|
327
|
+
id: generateId(),
|
|
328
|
+
source: sourceNodeId,
|
|
329
|
+
target: targetNodeId,
|
|
330
|
+
type: this.options.edgeType
|
|
331
|
+
}
|
|
332
|
+
}];
|
|
333
|
+
}
|
|
334
|
+
createEdges(sourceNodeIds, targetNodeId) {
|
|
335
|
+
if (sourceNodeIds.length === 0 || !targetNodeId) {
|
|
336
|
+
return [];
|
|
337
|
+
}
|
|
338
|
+
return [{
|
|
339
|
+
type: "batch",
|
|
340
|
+
operations: sourceNodeIds.map((sourceNodeId) => ({
|
|
341
|
+
type: "add",
|
|
342
|
+
data: {
|
|
343
|
+
id: generateId(),
|
|
344
|
+
source: sourceNodeId,
|
|
345
|
+
target: targetNodeId,
|
|
346
|
+
type: this.options.edgeType
|
|
347
|
+
}
|
|
348
|
+
}))
|
|
349
|
+
}];
|
|
350
|
+
}
|
|
351
|
+
createNodesAndEdges(sourceNodeIds, position) {
|
|
352
|
+
if (sourceNodeIds.length === 0) {
|
|
353
|
+
return [];
|
|
354
|
+
}
|
|
355
|
+
return this.createNodeAndEdge(sourceNodeIds, position);
|
|
356
|
+
}
|
|
357
|
+
init() {
|
|
358
|
+
this.dragInteractable = interact(`.${this.classNames.source}[data-plugin-id="${this.pluginId}"]`).draggable({
|
|
359
|
+
inertia: false,
|
|
360
|
+
autoScroll: true,
|
|
361
|
+
listeners: {
|
|
362
|
+
start: (event) => {
|
|
363
|
+
var _a2;
|
|
364
|
+
const element = event.target;
|
|
365
|
+
const currentNodeId = element.getAttribute("data-node-id");
|
|
366
|
+
if (!currentNodeId)
|
|
367
|
+
return;
|
|
368
|
+
this.currentDragNodeId = currentNodeId;
|
|
369
|
+
const sourceNodeIds = this.getSourceNodeIdsForDrag(currentNodeId);
|
|
370
|
+
const rect = element.getBoundingClientRect();
|
|
371
|
+
const x = (rect.left + rect.right) / 2;
|
|
372
|
+
const y = (rect.top + rect.bottom) / 2;
|
|
373
|
+
const { left: containerLeft, top: containerTop, width: containerWidth } = ((_a2 = this.getContainerElement()) == null ? void 0 : _a2.getBoundingClientRect()) || { left: 0, top: 0, width: 1 };
|
|
374
|
+
this.startScale = containerWidth / this.getContainerSize();
|
|
375
|
+
const startPosition = {
|
|
376
|
+
x: (x - containerLeft) / this.startScale,
|
|
377
|
+
y: (y - containerTop) / this.startScale
|
|
378
|
+
};
|
|
379
|
+
sourceNodeIds.forEach((sourceNodeId) => {
|
|
380
|
+
const node = this.getNode(sourceNodeId);
|
|
381
|
+
if (!node)
|
|
382
|
+
return;
|
|
383
|
+
let nodeStartPosition = __spreadValues({}, startPosition);
|
|
384
|
+
if (sourceNodeId !== currentNodeId) {
|
|
385
|
+
const connectHandleType = element.getAttribute("data-connector-type");
|
|
386
|
+
const connectHandleId = element.getAttribute("data-connector-id");
|
|
387
|
+
const connector = document.querySelector(`[data-plugin-id="${this.pluginId}"][data-node-id="${sourceNodeId}"][data-connector-type="${connectHandleType}"][data-connector-id="${connectHandleId}"]`);
|
|
388
|
+
if (connector) {
|
|
389
|
+
const connectorRect = connector.getBoundingClientRect();
|
|
390
|
+
const connectorX = (connectorRect.left + connectorRect.right) / 2;
|
|
391
|
+
const connectorY = (connectorRect.top + connectorRect.bottom) / 2;
|
|
392
|
+
nodeStartPosition = {
|
|
393
|
+
x: (connectorX - containerLeft) / this.startScale,
|
|
394
|
+
y: (connectorY - containerTop) / this.startScale
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const state = this.createConnectionLine(sourceNodeId, nodeStartPosition);
|
|
399
|
+
this.connectionLines.set(sourceNodeId, state);
|
|
400
|
+
this.startInteraction(this.pluginId, "connect", InteractionPriority.EntityConnectDrag);
|
|
401
|
+
});
|
|
402
|
+
},
|
|
403
|
+
move: (event) => {
|
|
404
|
+
if (this.connectionLines.size === 0 || !this.currentDragNodeId || !this.canInteract(this.pluginId, "connect"))
|
|
405
|
+
return;
|
|
406
|
+
this.updateAllConnectionLines(event.dx, event.dy);
|
|
407
|
+
},
|
|
408
|
+
end: () => {
|
|
409
|
+
this.clearAllConnectionLines();
|
|
410
|
+
this.currentDragNodeId = null;
|
|
411
|
+
this.endInteraction(this.pluginId, "connect");
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
this.dropInteractable = interact(`.${this.classNames.target}[data-plugin-id="${this.pluginId}"]`).dropzone({
|
|
416
|
+
ondragenter: (event) => {
|
|
417
|
+
event.target.classList.add(this.classNames.active);
|
|
418
|
+
},
|
|
419
|
+
ondragleave: (event) => {
|
|
420
|
+
event.target.classList.remove(this.classNames.active);
|
|
421
|
+
},
|
|
422
|
+
ondrop: (event) => {
|
|
423
|
+
if (this.connectionLines.size === 0 || !this.currentDragNodeId)
|
|
424
|
+
return;
|
|
425
|
+
const element = event.target;
|
|
426
|
+
const nodeElement = element.closest(`[data-node-id]`);
|
|
427
|
+
if (!nodeElement)
|
|
428
|
+
return;
|
|
429
|
+
const targetNodeId = nodeElement.getAttribute("data-node-id");
|
|
430
|
+
if (!targetNodeId)
|
|
431
|
+
return;
|
|
432
|
+
const sourceNodeIds = Array.from(this.connectionLines.keys()).filter((id) => id !== targetNodeId);
|
|
433
|
+
if (sourceNodeIds.length === 0)
|
|
434
|
+
return;
|
|
435
|
+
const validSourceNodeIds = sourceNodeIds.filter(
|
|
436
|
+
(sourceNodeId) => this.validateConnection(sourceNodeId, targetNodeId)
|
|
437
|
+
);
|
|
438
|
+
if (validSourceNodeIds.length > 0) {
|
|
439
|
+
this.createEdges(validSourceNodeIds, targetNodeId);
|
|
440
|
+
}
|
|
441
|
+
event.target.classList.remove(this.classNames.active);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
if (this.options.allowCreateNodeOnBlankArea) {
|
|
445
|
+
this.blankAreaDropInteractable = interact(`.${bem("canvas", "wrapper")}`).dropzone({
|
|
446
|
+
ondrop: (event) => {
|
|
447
|
+
const element = event.target;
|
|
448
|
+
const isNode = !!element.closest(`[data-node-id]`);
|
|
449
|
+
if (!isNode && this.connectionLines.size > 0 && this.options.allowCreateNodeOnBlankArea) {
|
|
450
|
+
const sourceNodeIds = Array.from(this.connectionLines.keys());
|
|
451
|
+
const firstLine = this.connectionLines.values().next().value;
|
|
452
|
+
if (firstLine) {
|
|
453
|
+
this.createNodesAndEdges(sourceNodeIds, firstLine.currentPosition);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
destroy() {
|
|
461
|
+
var _a2, _b, _c, _d;
|
|
462
|
+
(_a2 = this.dragInteractable) == null ? void 0 : _a2.unset();
|
|
463
|
+
(_b = this.dropInteractable) == null ? void 0 : _b.unset();
|
|
464
|
+
(_c = this.blankAreaDropInteractable) == null ? void 0 : _c.unset();
|
|
465
|
+
this.clearAllConnectionLines();
|
|
466
|
+
(_d = this.subscription) == null ? void 0 : _d.unsubscribe();
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
_init = __decoratorStart(_a);
|
|
470
|
+
__decorateElement(_init, 1, "containerRender", _containerRender_dec, ConnectionLine);
|
|
471
|
+
__decorateElement(_init, 1, "createNodeAndEdge", _createNodeAndEdge_dec, ConnectionLine);
|
|
472
|
+
__decorateElement(_init, 1, "createEdge", _createEdge_dec, ConnectionLine);
|
|
473
|
+
__decorateElement(_init, 1, "createEdges", _createEdges_dec, ConnectionLine);
|
|
474
|
+
__decorateElement(_init, 1, "createNodesAndEdges", _createNodesAndEdges_dec, ConnectionLine);
|
|
475
|
+
__decorateElement(_init, 1, "init", _init_dec, ConnectionLine);
|
|
476
|
+
__decorateElement(_init, 1, "destroy", _destroy_dec, ConnectionLine);
|
|
477
|
+
__decorateElement(_init, 5, "getNode", _getNode_dec, ConnectionLine);
|
|
478
|
+
__decorateElement(_init, 5, "dispatchNodeOperation", _dispatchNodeOperation_dec, ConnectionLine);
|
|
479
|
+
__decorateElement(_init, 5, "dispatchEdgeOperation", _dispatchEdgeOperation_dec, ConnectionLine);
|
|
480
|
+
__decorateElement(_init, 5, "startInteraction", _startInteraction_dec, ConnectionLine);
|
|
481
|
+
__decorateElement(_init, 5, "endInteraction", _endInteraction_dec, ConnectionLine);
|
|
482
|
+
__decorateElement(_init, 5, "canInteract", _canInteract_dec, ConnectionLine);
|
|
483
|
+
__decorateElement(_init, 5, "selectedNodeIds", _selectedNodeIds_dec, ConnectionLine);
|
|
484
|
+
__decorateElement(_init, 5, "getConnectHandleAttributes", _getConnectHandleAttributes_dec, ConnectionLine);
|
|
485
|
+
__decorateElement(_init, 5, "options", _options_dec, ConnectionLine);
|
|
486
|
+
__decorateElement(_init, 5, "registerValidator", _registerValidator_dec, ConnectionLine);
|
|
487
|
+
__decorateElement(_init, 5, "registerNewNodeCreator", _registerNewNodeCreator_dec, ConnectionLine);
|
|
488
|
+
__decoratorMetadata(_init, ConnectionLine);
|
|
489
|
+
|
|
490
|
+
export { ConnectionLine };
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@knotx/plugins-connection-line",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"description": "Connectionline Plugin for Knotx",
|
|
6
|
+
"author": "boenfu",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/boenfu/knotx#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/boenfu/knotx.git",
|
|
12
|
+
"directory": "packages/plugins-connection-line"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.mjs",
|
|
22
|
+
"require": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"module": "./dist/index.mjs",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@interactjs/actions": "^1.10.27",
|
|
33
|
+
"@interactjs/core": "^1.10.27",
|
|
34
|
+
"@interactjs/modifiers": "^1.10.27",
|
|
35
|
+
"@interactjs/types": "^1.10.27",
|
|
36
|
+
"interactjs": "^1.10.27"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"lodash-es": "^4.17.21",
|
|
40
|
+
"rxjs": "^7.8.1",
|
|
41
|
+
"@knotx/core": "0.0.0",
|
|
42
|
+
"@knotx/decorators": "0.0.0",
|
|
43
|
+
"@knotx/plugins-selection": "0.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@interactjs/actions": "^1.10.27",
|
|
47
|
+
"@interactjs/core": "^1.10.27",
|
|
48
|
+
"@interactjs/modifiers": "^1.10.27",
|
|
49
|
+
"@interactjs/types": "^1.10.27",
|
|
50
|
+
"@types/lodash-es": "^4.17.12",
|
|
51
|
+
"interactjs": "^1.10.27",
|
|
52
|
+
"@knotx/build-config": "0.0.0",
|
|
53
|
+
"@knotx/eslint-config": "0.0.0",
|
|
54
|
+
"@knotx/typescript-config": "0.0.0",
|
|
55
|
+
"@knotx/jsx": "0.0.0"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "unbuild --failOnWarn=false",
|
|
59
|
+
"dev": "unbuild --stub",
|
|
60
|
+
"lint": "eslint .",
|
|
61
|
+
"typecheck": "tsc --noEmit"
|
|
62
|
+
}
|
|
63
|
+
}
|