@leafer-in/linker 2.3.2 → 2.3.4
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/dist/linker.cjs +458 -49
- package/dist/linker.esm.js +459 -50
- package/dist/linker.esm.min.js +1 -1
- package/dist/linker.esm.min.js.map +1 -1
- package/dist/linker.js +430 -49
- package/dist/linker.min.cjs +1 -1
- package/dist/linker.min.cjs.map +1 -1
- package/dist/linker.min.js +1 -1
- package/dist/linker.min.js.map +1 -1
- package/package.json +1 -1
- package/src/Linker.ts +72 -57
- package/src/helper/drawPolyline.ts +456 -0
- package/types/index.d.ts +2 -1
package/dist/linker.js
CHANGED
|
@@ -16,7 +16,7 @@ this.LeaferIN.linker = function(exports, draw, core) {
|
|
|
16
16
|
return this.path || this.__pathInputed;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
const {directionData: directionData} = draw.AroundHelper;
|
|
19
|
+
const {directionData: directionData$1} = draw.AroundHelper;
|
|
20
20
|
const directionNames = [ "top-left", "top", "top-right", "right", "bottom-right", "bottom", "bottom-left", "left", "center" ];
|
|
21
21
|
const LinkerHelper = {
|
|
22
22
|
getCtrlPoint(linker, node, point, direction, ctrl, offsetAngle) {
|
|
@@ -129,8 +129,8 @@ this.LeaferIN.linker = function(exports, draw, core) {
|
|
|
129
129
|
const {width: width, height: height} = bounds;
|
|
130
130
|
const hitRadiusX = width / 4;
|
|
131
131
|
const hitRadiusY = height / 4;
|
|
132
|
-
for (let i = 0; i < directionData.length; i++) {
|
|
133
|
-
const unit = directionData[i];
|
|
132
|
+
for (let i = 0; i < directionData$1.length; i++) {
|
|
133
|
+
const unit = directionData$1[i];
|
|
134
134
|
const px = unit.x * width;
|
|
135
135
|
const py = unit.y * height;
|
|
136
136
|
const dx = boxPoint.x - px;
|
|
@@ -172,7 +172,373 @@ this.LeaferIN.linker = function(exports, draw, core) {
|
|
|
172
172
|
}
|
|
173
173
|
}));
|
|
174
174
|
}
|
|
175
|
-
const {
|
|
175
|
+
const {directionData: directionData} = draw.AroundHelper;
|
|
176
|
+
const {moveTo: moveTo$1, lineTo: lineTo$1} = draw.PathCommandDataHelper;
|
|
177
|
+
const POLYLINE_X = 0, POLYLINE_Y = 1;
|
|
178
|
+
const POLYLINE_EPSILON = .001;
|
|
179
|
+
const POLYLINE_MIN_SEGMENT = 4;
|
|
180
|
+
const POLYLINE_TURN_WEIGHT = 1.5;
|
|
181
|
+
function getPolylineDirectionIndex(direction) {
|
|
182
|
+
switch (direction) {
|
|
183
|
+
case "top-left":
|
|
184
|
+
return 0;
|
|
185
|
+
|
|
186
|
+
case "top":
|
|
187
|
+
return 1;
|
|
188
|
+
|
|
189
|
+
case "top-right":
|
|
190
|
+
return 2;
|
|
191
|
+
|
|
192
|
+
case "right":
|
|
193
|
+
return 3;
|
|
194
|
+
|
|
195
|
+
case "bottom-right":
|
|
196
|
+
return 4;
|
|
197
|
+
|
|
198
|
+
case "bottom":
|
|
199
|
+
return 5;
|
|
200
|
+
|
|
201
|
+
case "bottom-left":
|
|
202
|
+
return 6;
|
|
203
|
+
|
|
204
|
+
case "left":
|
|
205
|
+
return 7;
|
|
206
|
+
|
|
207
|
+
default:
|
|
208
|
+
return 8;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function getPolylinePorts(direction, point, target, bounds, targetBounds) {
|
|
212
|
+
const data = directionData[getPolylineDirectionIndex(direction)];
|
|
213
|
+
const dx = Math.sign(data.x - .5), dy = Math.sign(data.y - .5);
|
|
214
|
+
const cx = bounds ? bounds.x + bounds.width / 2 : point.x;
|
|
215
|
+
const cy = bounds ? bounds.y + bounds.height / 2 : point.y;
|
|
216
|
+
const tx = targetBounds ? targetBounds.x + targetBounds.width / 2 : target.x;
|
|
217
|
+
const ty = targetBounds ? targetBounds.y + targetBounds.height / 2 : target.y;
|
|
218
|
+
const deltaX = tx - cx, deltaY = ty - cy;
|
|
219
|
+
const xPort = (dx || (deltaX >= 0 ? 1 : -1)) > 0 ? 1 : 0;
|
|
220
|
+
const yPort = (dy || (deltaY >= 0 ? 1 : -1)) > 0 ? 3 : 2;
|
|
221
|
+
if (dx && !dy) return [ xPort ];
|
|
222
|
+
if (dy && !dx) return [ yPort ];
|
|
223
|
+
return Math.abs(deltaX) >= Math.abs(deltaY) ? [ xPort, yPort ] : [ yPort, xPort ];
|
|
224
|
+
}
|
|
225
|
+
function getPolylineExit(point, bounds, port, clearance) {
|
|
226
|
+
const axis = port >> 1, sign = port & 1 ? 1 : -1;
|
|
227
|
+
if (axis === POLYLINE_X) {
|
|
228
|
+
const x = bounds ? bounds.x + (sign > 0 ? bounds.width : 0) : point.x;
|
|
229
|
+
return {
|
|
230
|
+
x: x + sign * clearance,
|
|
231
|
+
y: point.y
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
const y = bounds ? bounds.y + (sign > 0 ? bounds.height : 0) : point.y;
|
|
235
|
+
return {
|
|
236
|
+
x: point.x,
|
|
237
|
+
y: y + sign * clearance
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function isPolylinePointBlocked(x, y, bounds, clearance) {
|
|
241
|
+
if (!bounds) return false;
|
|
242
|
+
const left = bounds.x - clearance, right = bounds.x + bounds.width + clearance;
|
|
243
|
+
const top = bounds.y - clearance, bottom = bounds.y + bounds.height + clearance;
|
|
244
|
+
return x > left + POLYLINE_EPSILON && x < right - POLYLINE_EPSILON && y > top + POLYLINE_EPSILON && y < bottom - POLYLINE_EPSILON;
|
|
245
|
+
}
|
|
246
|
+
function isPolylineSegmentBlocked(x1, y1, x2, y2, bounds, clearance) {
|
|
247
|
+
if (!bounds) return false;
|
|
248
|
+
const left = bounds.x - clearance, right = bounds.x + bounds.width + clearance;
|
|
249
|
+
const top = bounds.y - clearance, bottom = bounds.y + bounds.height + clearance;
|
|
250
|
+
if (Math.abs(y1 - y2) <= POLYLINE_EPSILON) {
|
|
251
|
+
return y1 > top + POLYLINE_EPSILON && y1 < bottom - POLYLINE_EPSILON && Math.max(Math.min(x1, x2), left + POLYLINE_EPSILON) < Math.min(Math.max(x1, x2), right - POLYLINE_EPSILON);
|
|
252
|
+
}
|
|
253
|
+
if (Math.abs(x1 - x2) <= POLYLINE_EPSILON) {
|
|
254
|
+
return x1 > left + POLYLINE_EPSILON && x1 < right - POLYLINE_EPSILON && Math.max(Math.min(y1, y2), top + POLYLINE_EPSILON) < Math.min(Math.max(y1, y2), bottom - POLYLINE_EPSILON);
|
|
255
|
+
}
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
function addPolylineBoundsCoordinates(xs, ys, bounds, clearance, margin) {
|
|
259
|
+
if (!bounds) return;
|
|
260
|
+
const left = bounds.x - clearance, right = bounds.x + bounds.width + clearance;
|
|
261
|
+
const top = bounds.y - clearance, bottom = bounds.y + bounds.height + clearance;
|
|
262
|
+
xs.push(left, right, left - margin, right + margin);
|
|
263
|
+
ys.push(top, bottom, top - margin, bottom + margin);
|
|
264
|
+
}
|
|
265
|
+
function sortPolylineCoordinates(values) {
|
|
266
|
+
values.sort((a, b) => a - b);
|
|
267
|
+
let write = 0;
|
|
268
|
+
for (let i = 0; i < values.length; i++) {
|
|
269
|
+
if (!write || Math.abs(values[i] - values[write - 1]) > POLYLINE_EPSILON) values[write++] = values[i];
|
|
270
|
+
}
|
|
271
|
+
values.length = write;
|
|
272
|
+
}
|
|
273
|
+
function getPolylineCoordinateIndex(values, value) {
|
|
274
|
+
for (let i = 0; i < values.length; i++) {
|
|
275
|
+
if (Math.abs(values[i] - value) <= POLYLINE_EPSILON) return i;
|
|
276
|
+
}
|
|
277
|
+
return -1;
|
|
278
|
+
}
|
|
279
|
+
function getPolylineMidpointAxis(startPort, endPort, sx, sy, ex, ey) {
|
|
280
|
+
const startAxis = startPort >> 1, endAxis = endPort >> 1;
|
|
281
|
+
const startSign = startPort & 1 ? 1 : -1, endSign = endPort & 1 ? 1 : -1;
|
|
282
|
+
if (startAxis === endAxis && startSign === -endSign) return startAxis;
|
|
283
|
+
return Math.abs(ex - sx) >= Math.abs(ey - sy) ? POLYLINE_X : POLYLINE_Y;
|
|
284
|
+
}
|
|
285
|
+
function isPolylinePortAligned(port, stub, dx, dy) {
|
|
286
|
+
const offset = port >> 1 === POLYLINE_X ? dx : dy;
|
|
287
|
+
const sign = port & 1 ? 1 : -1;
|
|
288
|
+
return Math.abs(offset) <= POLYLINE_EPSILON ? stub > POLYLINE_EPSILON : offset * sign > 0;
|
|
289
|
+
}
|
|
290
|
+
function isPolylineMidpointAligned(port, stub, dx, dy, axis) {
|
|
291
|
+
if (port === undefined) return true;
|
|
292
|
+
if (axis === POLYLINE_X && Math.abs(dx / 2) > POLYLINE_EPSILON) return isPolylinePortAligned(port, stub, dx, 0);
|
|
293
|
+
if (axis === POLYLINE_Y && Math.abs(dy / 2) > POLYLINE_EPSILON) return isPolylinePortAligned(port, stub, 0, dy);
|
|
294
|
+
if (Math.abs(dx) <= POLYLINE_EPSILON && Math.abs(dy) <= POLYLINE_EPSILON) return true;
|
|
295
|
+
return isPolylinePortAligned(port, stub, dx, dy);
|
|
296
|
+
}
|
|
297
|
+
function isPolylineApproachAligned(points, port) {
|
|
298
|
+
if (port === undefined) return true;
|
|
299
|
+
const last = points.length - 2, x = points[last], y = points[last + 1];
|
|
300
|
+
for (let i = last - 2; i >= 0; i -= 2) {
|
|
301
|
+
const dx = points[i] - x, dy = points[i + 1] - y;
|
|
302
|
+
if (Math.abs(dx) > POLYLINE_EPSILON || Math.abs(dy) > POLYLINE_EPSILON) return isPolylinePortAligned(port, 1, dx, dy);
|
|
303
|
+
}
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
function getPolylineRoute(result, startPoint, endPoint, startPort, endPort, startBounds, endBounds, clearance, strictPorts, approachPort) {
|
|
307
|
+
const startExit = getPolylineExit(startPoint, startBounds, startPort, clearance);
|
|
308
|
+
const endExit = endPort === undefined ? endPoint : getPolylineExit(endPoint, endBounds, endPort, clearance);
|
|
309
|
+
const {x: sx, y: sy} = startExit, {x: ex, y: ey} = endExit;
|
|
310
|
+
const startStub = Math.abs(sx - startPoint.x) + Math.abs(sy - startPoint.y);
|
|
311
|
+
const endStub = Math.abs(ex - endPoint.x) + Math.abs(ey - endPoint.y);
|
|
312
|
+
if (isPolylineSegmentBlocked(startPoint.x, startPoint.y, sx, sy, endBounds, clearance) || isPolylineSegmentBlocked(endPoint.x, endPoint.y, ex, ey, startBounds, clearance)) return Infinity;
|
|
313
|
+
if (endPort === undefined && (isPolylinePointBlocked(ex, ey, startBounds, clearance) || isPolylinePointBlocked(ex, ey, endBounds, clearance))) return Infinity;
|
|
314
|
+
const axis = endPort === undefined ? startPort >> 1 : getPolylineMidpointAxis(startPort, endPort, sx, sy, ex, ey);
|
|
315
|
+
const dx = ex - sx, dy = ey - sy;
|
|
316
|
+
result.length = 0;
|
|
317
|
+
result.push(startPoint.x, startPoint.y, sx, sy);
|
|
318
|
+
if ((!strictPorts || isPolylineMidpointAligned(startPort, startStub, dx, dy, axis) && isPolylineMidpointAligned(endPort, endStub, -dx, -dy, axis)) && isPolylineMidpointAligned(approachPort, 1, -dx, -dy, axis)) {
|
|
319
|
+
const mx = (sx + ex) / 2, my = (sy + ey) / 2;
|
|
320
|
+
if (axis === POLYLINE_X) result.push(mx, sy, mx, ey); else result.push(sx, my, ex, my);
|
|
321
|
+
result.push(ex, ey, endPoint.x, endPoint.y);
|
|
322
|
+
for (let i = 4; i < result.length - 2; i += 2) {
|
|
323
|
+
if (isPolylineSegmentBlocked(result[i - 2], result[i - 1], result[i], result[i + 1], startBounds, clearance) || isPolylineSegmentBlocked(result[i - 2], result[i - 1], result[i], result[i + 1], endBounds, clearance)) {
|
|
324
|
+
result.length = 4;
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
if (result.length > 4 && isPolylineApproachAligned(result, approachPort)) return getPolylineScore(result, clearance);
|
|
329
|
+
result.length = 4;
|
|
330
|
+
}
|
|
331
|
+
const score = findPolylineGridRoute(result, startExit, endExit, startPort, endPort, startBounds, endBounds, clearance, axis, startStub, endStub, strictPorts, approachPort);
|
|
332
|
+
if (score === Infinity) return score;
|
|
333
|
+
result.push(endPoint.x, endPoint.y);
|
|
334
|
+
return isPolylineApproachAligned(result, approachPort) ? score + startStub + endStub : Infinity;
|
|
335
|
+
}
|
|
336
|
+
function findPolylineGridRoute(result, startExit, endExit, startPort, endPort, startBounds, endBounds, clearance, midpointAxis, startStub, endStub, strictPorts, approachPort) {
|
|
337
|
+
const {x: sx, y: sy} = startExit, {x: ex, y: ey} = endExit;
|
|
338
|
+
const margin = Math.max(clearance, POLYLINE_MIN_SEGMENT) * 2;
|
|
339
|
+
const xs = [ sx, ex, (sx + ex) / 2 ], ys = [ sy, ey, (sy + ey) / 2 ];
|
|
340
|
+
addPolylineBoundsCoordinates(xs, ys, startBounds, clearance, margin);
|
|
341
|
+
addPolylineBoundsCoordinates(xs, ys, endBounds, clearance, margin);
|
|
342
|
+
xs.push(Math.min(...xs) - margin * 2, Math.max(...xs) + margin * 2);
|
|
343
|
+
ys.push(Math.min(...ys) - margin * 2, Math.max(...ys) + margin * 2);
|
|
344
|
+
sortPolylineCoordinates(xs);
|
|
345
|
+
sortPolylineCoordinates(ys);
|
|
346
|
+
const nx = xs.length, ny = ys.length;
|
|
347
|
+
const startXIndex = getPolylineCoordinateIndex(xs, sx), startYIndex = getPolylineCoordinateIndex(ys, sy);
|
|
348
|
+
const endXIndex = getPolylineCoordinateIndex(xs, ex), endYIndex = getPolylineCoordinateIndex(ys, ey);
|
|
349
|
+
if (startXIndex < 0 || startYIndex < 0 || endXIndex < 0 || endYIndex < 0) return Infinity;
|
|
350
|
+
const startNode = startYIndex * nx + startXIndex, endNode = endYIndex * nx + endXIndex;
|
|
351
|
+
const total = nx * ny * 3;
|
|
352
|
+
const distance = new Float64Array(total), previous = new Int32Array(total), visited = new Uint8Array(total);
|
|
353
|
+
distance.fill(Infinity);
|
|
354
|
+
previous.fill(-1);
|
|
355
|
+
const startState = startNode * 3;
|
|
356
|
+
distance[startState] = 0;
|
|
357
|
+
const pending = [ startState ];
|
|
358
|
+
const turnCost = Math.max(POLYLINE_MIN_SEGMENT, clearance * POLYLINE_TURN_WEIGHT);
|
|
359
|
+
const preferredCoordinate = midpointAxis === POLYLINE_X ? (sx + ex) / 2 : (sy + ey) / 2;
|
|
360
|
+
let currentState = -1, currentNode = -1, currentAxis = 0;
|
|
361
|
+
let currentX = 0, currentY = 0, currentDistance = 0;
|
|
362
|
+
const relax = (nextXIndex, nextYIndex, axis) => {
|
|
363
|
+
const nextX = xs[nextXIndex], nextY = ys[nextYIndex];
|
|
364
|
+
const nextNode = nextYIndex * nx + nextXIndex;
|
|
365
|
+
const dx = nextX - currentX, dy = nextY - currentY;
|
|
366
|
+
if (nextNode === startNode) return;
|
|
367
|
+
if (approachPort !== undefined && nextNode === endNode && !isPolylinePortAligned(approachPort, 1, -dx, -dy)) return;
|
|
368
|
+
if (strictPorts && (currentState === startState && !isPolylinePortAligned(startPort, startStub, dx, dy) || endPort !== undefined && nextNode === endNode && !isPolylinePortAligned(endPort, endStub, -dx, -dy))) return;
|
|
369
|
+
if (isPolylinePointBlocked(nextX, nextY, startBounds, clearance) || isPolylinePointBlocked(nextX, nextY, endBounds, clearance) || isPolylineSegmentBlocked(currentX, currentY, nextX, nextY, startBounds, clearance) || isPolylineSegmentBlocked(currentX, currentY, nextX, nextY, endBounds, clearance)) return;
|
|
370
|
+
const nextState = nextNode * 3 + axis;
|
|
371
|
+
if (visited[nextState]) return;
|
|
372
|
+
let cost = currentDistance + (Math.abs(dx) + Math.abs(dy));
|
|
373
|
+
if (currentAxis && currentAxis !== axis) {
|
|
374
|
+
cost += turnCost;
|
|
375
|
+
const turnCoordinate = currentAxis === POLYLINE_X + 1 ? currentX : currentY;
|
|
376
|
+
if (Math.abs(turnCoordinate - preferredCoordinate) > POLYLINE_EPSILON) cost += turnCost * 2;
|
|
377
|
+
}
|
|
378
|
+
if (cost < distance[nextState]) {
|
|
379
|
+
if (distance[nextState] === Infinity) pending.push(nextState);
|
|
380
|
+
distance[nextState] = cost;
|
|
381
|
+
previous[nextState] = currentState;
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
let bestState = -1;
|
|
385
|
+
while (pending.length) {
|
|
386
|
+
let index = 0, state = pending[0];
|
|
387
|
+
for (let i = 1; i < pending.length; i++) {
|
|
388
|
+
const next = pending[i];
|
|
389
|
+
if (distance[next] < distance[state] || distance[next] === distance[state] && next < state) {
|
|
390
|
+
index = i;
|
|
391
|
+
state = next;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
pending[index] = pending[pending.length - 1];
|
|
395
|
+
pending.pop();
|
|
396
|
+
currentState = state;
|
|
397
|
+
currentNode = Math.floor(state / 3);
|
|
398
|
+
currentAxis = state % 3;
|
|
399
|
+
currentDistance = distance[state];
|
|
400
|
+
visited[state] = 1;
|
|
401
|
+
if (currentNode === endNode) {
|
|
402
|
+
bestState = state;
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
const xIndex = currentNode % nx, yIndex = Math.floor(currentNode / nx);
|
|
406
|
+
currentX = xs[xIndex];
|
|
407
|
+
currentY = ys[yIndex];
|
|
408
|
+
if (xIndex > 0) relax(xIndex - 1, yIndex, POLYLINE_X + 1);
|
|
409
|
+
if (xIndex < nx - 1) relax(xIndex + 1, yIndex, POLYLINE_X + 1);
|
|
410
|
+
if (yIndex > 0) relax(xIndex, yIndex - 1, POLYLINE_Y + 1);
|
|
411
|
+
if (yIndex < ny - 1) relax(xIndex, yIndex + 1, POLYLINE_Y + 1);
|
|
412
|
+
}
|
|
413
|
+
if (bestState < 0) return Infinity;
|
|
414
|
+
const reverse = [];
|
|
415
|
+
let state = bestState;
|
|
416
|
+
while (state >= 0) {
|
|
417
|
+
const node = Math.floor(state / 3);
|
|
418
|
+
reverse.push(xs[node % nx], ys[Math.floor(node / nx)]);
|
|
419
|
+
if (state === startState) break;
|
|
420
|
+
state = previous[state];
|
|
421
|
+
}
|
|
422
|
+
if (state !== startState) return Infinity;
|
|
423
|
+
for (let i = reverse.length - 4; i >= 0; i -= 2) result.push(reverse[i], reverse[i + 1]);
|
|
424
|
+
return distance[bestState];
|
|
425
|
+
}
|
|
426
|
+
function getPolylineFallback(start, end, port, startBounds, endBounds) {
|
|
427
|
+
const {x: sx, y: sy} = start, {x: ex, y: ey} = end;
|
|
428
|
+
if (sx === ex || sy === ey) return [ sx, sy, ex, ey ];
|
|
429
|
+
const horizontal = port >> 1 === POLYLINE_X;
|
|
430
|
+
const crosses = (x, y) => {
|
|
431
|
+
let count = 0;
|
|
432
|
+
if (isPolylineSegmentBlocked(sx, sy, x, y, startBounds, 0)) count++;
|
|
433
|
+
if (isPolylineSegmentBlocked(x, y, ex, ey, startBounds, 0)) count++;
|
|
434
|
+
if (isPolylineSegmentBlocked(sx, sy, x, y, endBounds, 0)) count++;
|
|
435
|
+
if (isPolylineSegmentBlocked(x, y, ex, ey, endBounds, 0)) count++;
|
|
436
|
+
return count;
|
|
437
|
+
};
|
|
438
|
+
const xy = crosses(ex, sy), yx = crosses(sx, ey);
|
|
439
|
+
return xy < yx || xy === yx && horizontal ? [ sx, sy, ex, sy, ex, ey ] : [ sx, sy, sx, ey, ex, ey ];
|
|
440
|
+
}
|
|
441
|
+
function getBestPolyline(startPoint, endPoint, startDirection, endDirection, startBounds, endBounds, clearance, approachPort) {
|
|
442
|
+
const startPorts = getPolylinePorts(startDirection, startPoint, endPoint, startBounds, endDirection === undefined ? undefined : endBounds);
|
|
443
|
+
const endPorts = endDirection === undefined ? [ undefined ] : getPolylinePorts(endDirection, endPoint, startPoint, endBounds, startBounds);
|
|
444
|
+
let candidate = [], best = [], bestScore = Infinity;
|
|
445
|
+
for (let pass = clearance ? 0 : 1; pass < 3 && !best.length; pass++) {
|
|
446
|
+
const gap = pass ? 0 : clearance;
|
|
447
|
+
for (let i = 0; i < startPorts.length; i++) {
|
|
448
|
+
for (let j = 0; j < endPorts.length; j++) {
|
|
449
|
+
const score = getPolylineRoute(candidate, startPoint, endPoint, startPorts[i], endPorts[j], startBounds, endBounds, gap, pass < 2, approachPort);
|
|
450
|
+
if (score < bestScore) {
|
|
451
|
+
bestScore = score;
|
|
452
|
+
const previous = best;
|
|
453
|
+
best = candidate;
|
|
454
|
+
candidate = previous;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (!best.length) return getPolylineFallback(startPoint, endPoint, startPorts[0], startBounds, endBounds);
|
|
460
|
+
simplifyPolyline(best);
|
|
461
|
+
const last = best.length - 2;
|
|
462
|
+
if (best[last] !== endPoint.x || best[last + 1] !== endPoint.y) best.push(endPoint.x, endPoint.y);
|
|
463
|
+
return best;
|
|
464
|
+
}
|
|
465
|
+
function getPolylineEditSide(point, waypoint, direction, bounds, otherBounds, clearance, points, index, step) {
|
|
466
|
+
if (point.x === waypoint.x && point.y === waypoint.y) return [];
|
|
467
|
+
let approachPort;
|
|
468
|
+
for (let i = index + step; i >= 0 && i < points.length; i += step) {
|
|
469
|
+
const dx = points[i] - waypoint.x, dy = points[i + 1] - waypoint.y;
|
|
470
|
+
if (Math.abs(dx) <= POLYLINE_EPSILON && Math.abs(dy) <= POLYLINE_EPSILON) continue;
|
|
471
|
+
if (Math.abs(dy) <= POLYLINE_EPSILON) approachPort = dx > 0 ? 0 : 1; else if (Math.abs(dx) <= POLYLINE_EPSILON) approachPort = dy > 0 ? 2 : 3;
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
const side = getBestPolyline(point, waypoint, direction, undefined, bounds, otherBounds, clearance, approachPort);
|
|
475
|
+
side.length -= 2;
|
|
476
|
+
return side;
|
|
477
|
+
}
|
|
478
|
+
function drawPolylineEditPath(path, startPoint, endPoint, startDirection, endDirection, startBounds, endBounds, clearance, points) {
|
|
479
|
+
const length = points.length, last = length - 2;
|
|
480
|
+
if (length % 2) return false;
|
|
481
|
+
for (let i = 0; i < length; i++) if (!Number.isFinite(points[i])) return false;
|
|
482
|
+
const start = getPolylineEditSide(startPoint, {
|
|
483
|
+
x: points[0],
|
|
484
|
+
y: points[1]
|
|
485
|
+
}, startDirection, startBounds, endBounds, clearance, points, 0, 2);
|
|
486
|
+
const end = getPolylineEditSide(endPoint, {
|
|
487
|
+
x: points[last],
|
|
488
|
+
y: points[last + 1]
|
|
489
|
+
}, endDirection, endBounds, startBounds, clearance, points, last, -2);
|
|
490
|
+
const first = start.length ? start : points;
|
|
491
|
+
moveTo$1(path, first[0], first[1]);
|
|
492
|
+
for (let i = 2; i < start.length; i += 2) lineTo$1(path, start[i], start[i + 1]);
|
|
493
|
+
for (let i = start.length ? 0 : 2; i < length; i += 2) lineTo$1(path, points[i], points[i + 1]);
|
|
494
|
+
for (let i = end.length - 2; i >= 0; i -= 2) lineTo$1(path, end[i], end[i + 1]);
|
|
495
|
+
return true;
|
|
496
|
+
}
|
|
497
|
+
function getPolylineScore(points, clearance) {
|
|
498
|
+
let length = 0, turns = 0, previousAxis = -1;
|
|
499
|
+
for (let i = 2; i < points.length; i += 2) {
|
|
500
|
+
const dx = points[i] - points[i - 2], dy = points[i + 1] - points[i - 1];
|
|
501
|
+
if (Math.abs(dx) <= POLYLINE_EPSILON && Math.abs(dy) <= POLYLINE_EPSILON) continue;
|
|
502
|
+
const axis = Math.abs(dx) > POLYLINE_EPSILON ? POLYLINE_X : POLYLINE_Y;
|
|
503
|
+
length += Math.abs(dx) + Math.abs(dy);
|
|
504
|
+
if (previousAxis !== -1 && previousAxis !== axis) turns++;
|
|
505
|
+
previousAxis = axis;
|
|
506
|
+
}
|
|
507
|
+
return length + turns * Math.max(clearance, POLYLINE_MIN_SEGMENT) * POLYLINE_TURN_WEIGHT;
|
|
508
|
+
}
|
|
509
|
+
function simplifyPolyline(points) {
|
|
510
|
+
let write = 2;
|
|
511
|
+
for (let read = 2; read < points.length; read += 2) {
|
|
512
|
+
const x = points[read], y = points[read + 1];
|
|
513
|
+
const px = points[write - 2], py = points[write - 1];
|
|
514
|
+
if (Math.abs(x - px) <= POLYLINE_EPSILON && Math.abs(y - py) <= POLYLINE_EPSILON) continue;
|
|
515
|
+
if (write >= 4) {
|
|
516
|
+
const ax = points[write - 4], ay = points[write - 3];
|
|
517
|
+
const horizontal = Math.abs(ay - py) <= POLYLINE_EPSILON && Math.abs(py - y) <= POLYLINE_EPSILON && (px - ax) * (x - px) >= 0;
|
|
518
|
+
const vertical = Math.abs(ax - px) <= POLYLINE_EPSILON && Math.abs(px - x) <= POLYLINE_EPSILON && (py - ay) * (y - py) >= 0;
|
|
519
|
+
if (horizontal || vertical) {
|
|
520
|
+
points[write - 2] = x;
|
|
521
|
+
points[write - 1] = y;
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
points[write++] = x;
|
|
526
|
+
points[write++] = y;
|
|
527
|
+
}
|
|
528
|
+
points.length = write;
|
|
529
|
+
}
|
|
530
|
+
function drawPolyline(path, startPoint, endPoint, startDirection, endDirection, startBounds, endBounds, clearance = 8, points) {
|
|
531
|
+
if (!Number.isFinite(startPoint.x) || !Number.isFinite(startPoint.y) || !Number.isFinite(endPoint.x) || !Number.isFinite(endPoint.y)) return false;
|
|
532
|
+
if (startBounds && (!Number.isFinite(startBounds.x) || !Number.isFinite(startBounds.y) || !Number.isFinite(startBounds.width) || !Number.isFinite(startBounds.height) || startBounds.width < 0 || startBounds.height < 0)) return false;
|
|
533
|
+
if (endBounds && (!Number.isFinite(endBounds.x) || !Number.isFinite(endBounds.y) || !Number.isFinite(endBounds.width) || !Number.isFinite(endBounds.height) || endBounds.width < 0 || endBounds.height < 0)) return false;
|
|
534
|
+
clearance = Number.isFinite(clearance) ? Math.max(0, clearance) : 8;
|
|
535
|
+
if (points === null || points === void 0 ? void 0 : points.length) return drawPolylineEditPath(path, startPoint, endPoint, startDirection, endDirection, startBounds, endBounds, clearance, points);
|
|
536
|
+
const best = getBestPolyline(startPoint, endPoint, startDirection, endDirection, startBounds, endBounds, clearance);
|
|
537
|
+
moveTo$1(path, best[0], best[1]);
|
|
538
|
+
for (let i = 2; i < best.length; i += 2) lineTo$1(path, best[i], best[i + 1]);
|
|
539
|
+
return true;
|
|
540
|
+
}
|
|
541
|
+
const {moveTo: moveTo, lineTo: lineTo, bezierCurveTo: bezierCurveTo, drawPoints: drawPoints$1} = draw.PathCommandDataHelper;
|
|
176
542
|
const {isEmpty: isEmpty, getPoint: getPoint} = draw.BoundsHelper;
|
|
177
543
|
const {getDistance: getDistance} = draw.PointHelper;
|
|
178
544
|
const {getCtrlPoint: getCtrlPoint, getDirectionForFromPoint: getDirectionForFromPoint, isOppositeDirection: isOppositeDirection} = LinkerHelper;
|
|
@@ -260,7 +626,7 @@ this.LeaferIN.linker = function(exports, draw, core) {
|
|
|
260
626
|
}
|
|
261
627
|
}
|
|
262
628
|
updateLinkerPoint(type) {
|
|
263
|
-
const {startPoint: startPoint, endPoint: endPoint} = this;
|
|
629
|
+
const {startPoint: startPoint, endPoint: endPoint, lineType: lineType} = this;
|
|
264
630
|
const nowPoint = type === "end" ? endPoint : startPoint;
|
|
265
631
|
if (nowPoint) {
|
|
266
632
|
const data = {};
|
|
@@ -285,6 +651,7 @@ this.LeaferIN.linker = function(exports, draw, core) {
|
|
|
285
651
|
if (isEmpty(boxBounds)) draw.LeafHelper.updateBounds(ui);
|
|
286
652
|
p = getPoint(boxBounds, point, true);
|
|
287
653
|
ui.getWorldPointByBox(p, this, false, true);
|
|
654
|
+
if (lineType === "polyline") data.bounds = ui.getBounds("box", this.parent);
|
|
288
655
|
}
|
|
289
656
|
} else {
|
|
290
657
|
if (draw.isObject(point)) p = point;
|
|
@@ -310,56 +677,69 @@ this.LeaferIN.linker = function(exports, draw, core) {
|
|
|
310
677
|
}
|
|
311
678
|
const startPoint = startData.point, endPoint = endData.point;
|
|
312
679
|
const startDirection = startData.direction, endDirection = endData.direction;
|
|
313
|
-
const half = {
|
|
314
|
-
x: abs((startPoint.x - endPoint.x) / 2),
|
|
315
|
-
y: abs((startPoint.y - endPoint.y) / 2)
|
|
316
|
-
};
|
|
317
680
|
const arrowWidth = this.__.strokeWidth * 8;
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
681
|
+
switch (data.lineType) {
|
|
682
|
+
case "straight":
|
|
683
|
+
moveTo(path, startPoint.x, startPoint.y);
|
|
684
|
+
lineTo(path, endPoint.x, endPoint.y);
|
|
685
|
+
break;
|
|
686
|
+
|
|
687
|
+
case "polyline":
|
|
688
|
+
drawPolyline(path, startPoint, endPoint, startDirection, endDirection, startData.bounds, endData.bounds, arrowWidth, hasPoints && draw.PointHelper.toNumberPoints(data.points));
|
|
689
|
+
break;
|
|
690
|
+
|
|
691
|
+
case "curve":
|
|
692
|
+
default:
|
|
693
|
+
const half = {
|
|
694
|
+
x: abs((startPoint.x - endPoint.x) / 2),
|
|
695
|
+
y: abs((startPoint.y - endPoint.y) / 2)
|
|
696
|
+
};
|
|
697
|
+
let minCtrl;
|
|
698
|
+
if (this.__.__useArrow) {
|
|
699
|
+
const ratio = min(getDistance(startPoint, endPoint) / (arrowWidth * 25), 1);
|
|
700
|
+
minCtrl = max(arrowWidth * ratio * 12, arrowWidth * 3);
|
|
701
|
+
} else {
|
|
327
702
|
minCtrl = arrowWidth * 2;
|
|
328
703
|
}
|
|
329
|
-
}
|
|
330
|
-
half.x = max(minCtrl, half.x);
|
|
331
|
-
half.y = max(minCtrl, half.y);
|
|
332
|
-
if (hasPoints) {
|
|
333
|
-
minCtrl *= .6;
|
|
334
|
-
const points = draw.PointHelper.toNumberPoints(data.points);
|
|
335
|
-
drawPoints$1(path, [ startPoint.x, startPoint.y, ...points, endPoint.x, endPoint.y ], data.curve, false, data);
|
|
336
|
-
if (startDirection) {
|
|
337
|
-
const handleX = path[4];
|
|
338
|
-
const handleY = path[5];
|
|
339
|
-
half.x = max(minCtrl, abs(handleX - startPoint.x));
|
|
340
|
-
half.y = max(minCtrl, abs(handleY - startPoint.y));
|
|
341
|
-
const startCtrlPoint = getCtrlPoint(this, startNode, startPoint, startDirection, half);
|
|
342
|
-
path[4] = startCtrlPoint.x;
|
|
343
|
-
path[5] = startCtrlPoint.y;
|
|
344
|
-
}
|
|
345
|
-
const len = path.length;
|
|
346
704
|
if (endDirection) {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
705
|
+
if (isOppositeDirection(startDirection, endDirection) && startDirection === getDirectionForFromPoint(startPoint, endPoint)) {
|
|
706
|
+
minCtrl = arrowWidth * 2;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
half.x = max(minCtrl, half.x);
|
|
710
|
+
half.y = max(minCtrl, half.y);
|
|
711
|
+
if (hasPoints) {
|
|
712
|
+
minCtrl *= .6;
|
|
713
|
+
const points = draw.PointHelper.toNumberPoints(data.points);
|
|
714
|
+
drawPoints$1(path, [ startPoint.x, startPoint.y, ...points, endPoint.x, endPoint.y ], data.curve, false, data);
|
|
715
|
+
if (startDirection) {
|
|
716
|
+
const handleX = path[4];
|
|
717
|
+
const handleY = path[5];
|
|
718
|
+
half.x = max(minCtrl, abs(handleX - startPoint.x));
|
|
719
|
+
half.y = max(minCtrl, abs(handleY - startPoint.y));
|
|
720
|
+
const startCtrlPoint = getCtrlPoint(this, startNode, startPoint, startDirection, half);
|
|
721
|
+
path[4] = startCtrlPoint.x;
|
|
722
|
+
path[5] = startCtrlPoint.y;
|
|
723
|
+
}
|
|
724
|
+
const len = path.length;
|
|
725
|
+
if (endDirection) {
|
|
726
|
+
const handleX = path[len - 4];
|
|
727
|
+
const handleY = path[len - 3];
|
|
728
|
+
half.x = max(minCtrl, abs(handleX - endPoint.x));
|
|
729
|
+
half.y = max(minCtrl, abs(handleY - endPoint.y));
|
|
730
|
+
const endCtrlPoint = getCtrlPoint(this, endNode, endPoint, endDirection, half);
|
|
731
|
+
path[len - 4] = endCtrlPoint.x;
|
|
732
|
+
path[len - 3] = endCtrlPoint.y;
|
|
733
|
+
} else {
|
|
734
|
+
path[len - 4] = endPoint.x;
|
|
735
|
+
path[len - 3] = endPoint.y;
|
|
736
|
+
}
|
|
354
737
|
} else {
|
|
355
|
-
|
|
356
|
-
|
|
738
|
+
const startCtrlPoint = startDirection ? getCtrlPoint(this, startNode, startPoint, startDirection, half) : startPoint;
|
|
739
|
+
const endCtrlPoint = endDirection ? getCtrlPoint(this, endNode, endPoint, endDirection, half) : endPoint;
|
|
740
|
+
moveTo(path, startPoint.x, startPoint.y);
|
|
741
|
+
bezierCurveTo(path, startCtrlPoint.x, startCtrlPoint.y, endCtrlPoint.x, endCtrlPoint.y, endPoint.x, endPoint.y);
|
|
357
742
|
}
|
|
358
|
-
} else {
|
|
359
|
-
const startCtrlPoint = startDirection ? getCtrlPoint(this, startNode, startPoint, startDirection, half) : startPoint;
|
|
360
|
-
const endCtrlPoint = endDirection ? getCtrlPoint(this, endNode, endPoint, endDirection, half) : endPoint;
|
|
361
|
-
moveTo(path, startPoint.x, startPoint.y);
|
|
362
|
-
bezierCurveTo(path, startCtrlPoint.x, startCtrlPoint.y, endCtrlPoint.x, endCtrlPoint.y, endPoint.x, endPoint.y);
|
|
363
743
|
}
|
|
364
744
|
}
|
|
365
745
|
createStartPoint(worldPoint, node, options) {
|
|
@@ -427,6 +807,7 @@ this.LeaferIN.linker = function(exports, draw, core) {
|
|
|
427
807
|
}
|
|
428
808
|
};
|
|
429
809
|
__decorate([ draw.dataProcessor(LinkerData) ], exports.Linker.prototype, "__", void 0);
|
|
810
|
+
__decorate([ draw.boundsType("curve") ], exports.Linker.prototype, "lineType", void 0);
|
|
430
811
|
__decorate([ linkerPointType() ], exports.Linker.prototype, "startPoint", void 0);
|
|
431
812
|
__decorate([ linkerPointType() ], exports.Linker.prototype, "endPoint", void 0);
|
|
432
813
|
__decorate([ draw.pathType({
|
package/dist/linker.min.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var t=require("@leafer-ui/draw"),e=require("@leafer-ui/core");function n(t,e,n,o){var i,r=arguments.length,s=r<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,n):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,n,o);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(s=(r<3?i(s):r>3?i(e,n,s):i(e,n))||s);return r>3&&s&&Object.defineProperty(e,n,s),s}"function"==typeof SuppressedError&&SuppressedError;class o extends t.LineData{get __usePathBox(){return this.path||this.__pathInputed}}const{directionData:i}=t.AroundHelper,r=["top-left","top","top-right","right","bottom-right","bottom","bottom-left","left","center"],s={getCtrlPoint(t,e,n,o,i,r){let s;switch(e&&(n=e.getBoxPoint(n,t)),o){case"left":case"top-left":case"bottom-left":s={x:n.x-i.x,y:n.y};break;case"top":s={x:n.x,y:n.y-i.y};break;case"right":case"top-right":case"bottom-right":s={x:n.x+i.x,y:n.y};break;case"bottom":s={x:n.x,y:n.y+i.y};break;default:s={x:n.x,y:n.y}}return r&&(r=r/180*Math.PI,s={x:(s.x-n.x)*Math.cos(r)-(s.y-n.y)*Math.sin(r)+n.x,y:(s.x-n.x)*Math.sin(r)+(s.y-n.y)*Math.cos(r)+n.y}),e&&e.getWorldPointByBox(s,t,void 0,!0),s},isOppositeDirection(t,e){switch(t){case"left":return"right"==e;case"top":return"bottom"==e;case"right":return"left"==e;case"bottom":return"top"==e;case"center":return"center"==e;default:return!1}},getDirectionForFromPoint(t,e){const n=t.x-e.x,o=t.y-e.y;let i,r=180*Math.atan2(o,n)/Math.PI;return r<0&&(r=360+r),i=r>315||r<45?"left":r>45&&r<135?"top":r>135&&r<225?"right":"bottom",i},getRotateDirection(e,n,o=8){let i=t.Direction9[e];return i=(i+Math.round(n/(360/o)))%o,i<0&&(i+=o),r[i]},getDirection:function(e,n,o){const{width:i,height:r}=n,s=t.BoundsHelper.getPoint(n,"center",!0),a=e.x-s.x,d=e.y-s.y;if(!o&&Math.abs(a)<i/4&&Math.abs(d)<r/4)return"center";const h=Math.abs(a)*r,c=Math.abs(d)*i;if(h>c)return a>0?"right":"left";return d>0?"bottom":"top"},getPointDirection:function(t,e,n){const{width:o,height:s}=e,a=o/4,d=s/4;for(let e=0;e<i.length;e++){const h=i[e],c=h.x*o,p=h.y*s,u=t.x-c,l=t.y-p;if(Math.abs(u)<=a&&Math.abs(l)<=d){if(!n)return r[e];if("center"!==r[e])return r[e]}}return},autoRotatePoint(e,n,o){const{rotation:i,scaleX:r,scaleY:s}=o,a=r*s<0;(i||a)&&t.PointHelper.rotate(e,a?-i:i,n)}};function a(e){return t.decorateLeafAttr(e,e=>t.attr({set(n){if(this.__setAttr(e,n)){const e=this.__layout;e.linkerChanged||(t.doBoundsType(this),t.doStrokeType(this),e.linkerChanged=!0)}}}))}const{moveTo:d,bezierCurveTo:h,drawPoints:c}=t.PathCommandDataHelper,{isEmpty:p,getPoint:u}=t.BoundsHelper,{getDistance:l}=t.PointHelper,{getCtrlPoint:f,getDirectionForFromPoint:x,isOppositeDirection:y}=s,{max:g,min:P,abs:k}=Math;exports.Linker=class extends t.Line{get __tag(){return"Linker"}constructor(t){super(t),this.onStartNodeChange=this.onStartNodeChange.bind(this),this.onEndNodeChange=this.onEndNodeChange.bind(this)}__updatePath(){const t=this.__layout,{startNode:e,endNode:n}=this;t.linkerChanged&&(this.updateLinkerNode("start"),this.updateLinkerNode("end"),this.updateLinkerPoint("start"),this.updateLinkerPoint("end"),e!==this.startNode&&this.listenLinkerNode(this.startNode,e,"start"),n!==this.endNode&&this.listenLinkerNode(this.endNode,n,"end"),t.linkerChanged=!1),this.updateLinkerPath()}listenLinkerNode(e,n,o){if(n){n.off(t.BoundsEvent.WORLD,"end"===o?this.onEndNodeChange:this.onStartNodeChange);const e="end"===o?n.endLinker:n.startLinker;if(e){const t=e.indexOf(this);t>-1&&e.splice(t,1)}}if(e){e.on(t.BoundsEvent.WORLD,"end"===o?this.onEndNodeChange:this.onStartNodeChange);("end"===o?e.endLinker||(e.endLinker=[]):e.startLinker||(e.startLinker=[])).push(this)}}onStartNodeChange(){this.onLinkerNodeChange("start")}onEndNodeChange(){this.onLinkerNodeChange("end")}onLinkerNodeChange(e){if(this.leaferIsCreated){const{layouter:t,zoomLayer:e}=this.leafer;if(t.__updatedList.has(e))return;t.addExtra(this,!0)}const{startData:n,endData:o}=this;"start"==e?(this.updateLinkerPoint("start"),o&&o.auto&&this.updateLinkerPoint("end")):(n&&n.auto&&this.updateLinkerPoint("start"),this.updateLinkerPoint("end")),this.__layout.boxChange(),t.LeafHelper.updateBounds(this)}updateLinkerNode(e){const{startPoint:n,endPoint:o}=this,i="end"===e?o:n;if(i){const{id:n}=i,o=n&&(t.isString(n)?this.app.findId(n):n);"end"===e?this.endNode=o:this.startNode=o}}getLinkerNodeCenter(e){const{startPoint:n,endPoint:o,startNode:i,endNode:r,startData:s,endData:a}=this,d="end"===e?r:i,h="end"===e?a||o:s||n;if(d){const{worldRenderBounds:e}=d;return p(e)&&t.LeafHelper.updateBounds(d),u(d.worldRenderBounds,"center")}return h&&t.isObject(h.point)&&this.getWorldPointByLocal(h.point)}updateLinkerPoint(e){const{startPoint:n,endPoint:o}=this,i="end"===e?o:n;if(i){const n={},{id:o,offset:r}=i;let{direction:s,point:a}=i;if("center"===s||!s&&o&&!a){const t=this.getLinkerNodeCenter("start"),o=this.getLinkerNodeCenter("end"),i="end"===e?o:t,r="end"===e?t:o;i&&r&&(s=x(i,r)),n.auto=!0}!s&&t.isString(a)&&(s=a),n.direction=s;const d="end"===e?this.endNode:this.startNode;let h;if(d){if(a||(a=s),a){const{boxBounds:e}=d;p(e)&&t.LeafHelper.updateBounds(d),h=u(e,a,!0),d.getWorldPointByBox(h,this,!1,!0)}}else t.isObject(a)&&(h=a);if(h&&r){const e=Object.assign({},r);r.x||(e.x=0),r.y||(e.y=0),d&&d.getWorldPoint(e,this,!0,!0),t.PointHelper.move(h,e)}n.point=h,"end"===e?this.endData=h&&n:this.startData=h&&n}}updateLinkerPath(){const e=this.__,n=e.path=[],o=e.points&&e.points.length,{startData:i,endData:r,startNode:s,endNode:a}=this;if(!i||!r)return;const p=i.point,u=r.point,L=i.direction,b=r.direction,N={x:k((p.x-u.x)/2),y:k((p.y-u.y)/2)},C=8*this.__.strokeWidth;let M;if(this.__.__useArrow){const t=P(l(p,u)/(25*C),1);M=g(C*t*12,3*C)}else M=2*C;if(b&&y(L,b)&&L===x(p,u)&&(M=2*C),N.x=g(M,N.x),N.y=g(M,N.y),o){M*=.6;const o=t.PointHelper.toNumberPoints(e.points);if(c(n,[p.x,p.y,...o,u.x,u.y],e.curve,!1,e),L){const t=n[4],e=n[5];N.x=g(M,k(t-p.x)),N.y=g(M,k(e-p.y));const o=f(this,s,p,L,N);n[4]=o.x,n[5]=o.y}const i=n.length;if(b){const t=n[i-4],e=n[i-3];N.x=g(M,k(t-u.x)),N.y=g(M,k(e-u.y));const o=f(this,a,u,b,N);n[i-4]=o.x,n[i-3]=o.y}else n[i-4]=u.x,n[i-3]=u.y}else{const t=L?f(this,s,p,L,N):p,e=b?f(this,a,u,b,N):u;d(n,p.x,p.y),h(n,t.x,t.y,e.x,e.y,u.x,u.y)}}createStartPoint(t,e,n){const o=this.createLinkerPoint(t,e,n);return this.startPoint=o,o}createEndPoint(t,e,n){const o=this.createLinkerPoint(t,e,n);return this.endPoint=o,o}createLinkerPoint(e,n,o){const i={};if(n&&n.id){const r=n.getBoxPoint(e),{boxBounds:a}=n,d=o&&o.mode,h="edge"===d||!d,c="four"===d,p="free"===d;if(!("auto"===d))if(c)i.point=s.getDirection(r,a,c);else if(i.direction=s.getDirection(r,a,h),i.point=s.getPointDirection(r,a,h)||i.direction,i.point===i.direction&&delete i.direction,p||h){const e=t.BoundsHelper.getPoint(a,i.point),n=r.x-e.x,s=r.y-e.y,d=o&&o.snapRadius||10;if(Math.abs(n)>d||Math.abs(s)>d){const t=i.offset={x:Math.abs(n)>d?n:0,y:Math.abs(s)>d?s:0};if(h&&(t.x||t.y))switch(i.direction||i.point){case"top":case"bottom":t.y=0;break;case"right":case"left":t.x=0;break;default:t.x>t.y?t.y=0:t.x=0}}}i.id=n.id}else i.point=this.getLocalPoint(e);return i}},n([t.dataProcessor(o)],exports.Linker.prototype,"__",void 0),n([a()],exports.Linker.prototype,"startPoint",void 0),n([a()],exports.Linker.prototype,"endPoint",void 0),n([t.pathType({type:"C",value:.5})],exports.Linker.prototype,"curve",void 0),exports.Linker=n([t.registerUI()],exports.Linker);const{toNumberPoints:L}=e.PointHelper,{M:b,L:N,C:C,Z:M}=e.PathCommandMap;t.Plugin.add("linker"),t.registerPointsCurve("C",function(t,e,n,o){const i=L(e),r=i.length>>1;if(r<2)return;if(t.push(b,i[0],i[1]),!n||r<3){for(let e=1;e<r;e++)t.push(N,i[2*e],i[2*e+1]);return void(o&&t.push(M))}let s=!0===n?.5:n;s<0?s=0:s>1&&(s=1);const a=o?r:r-1;let d=i[0],h=i[1];{const t=i[2]-i[0],e=i[3]-i[1],n=Math.sqrt(t*t+e*e);n&&(d=i[0]+t/n*n*s,h=i[1]+e/n*n*s)}for(let e=0;e<a;e++){const n=e+1===r?0:e+1,o=i[2*n],a=i[2*n+1],c=0===n?r-1:n-1,p=n+1===r?0:n+1;let u=i[2*c]-o,l=i[2*c+1]-a,f=i[2*p]-o,x=i[2*p+1]-a;const y=Math.sqrt(u*u+l*l),g=Math.sqrt(f*f+x*x);let P=o,k=a,L=o,b=a;if(y||g){let t=0,e=0;y&&(t+=u/y,e+=l/y),g&&(t+=f/g,e+=x/g);let n=Math.sqrt(t*t+e*e);n||(g?(t=f/g,e=x/g):(t=u/y,e=l/y),n=1),t/=n,e/=n;let i=-e,r=t;g&&i*f+r*x<0&&(i=-i,r=-r);let d=0;y&&g&&(d=(u*f+l*x)/(y*g),d>1?d=1:d<-1&&(d=-1));let h=Math.sqrt(Math.sqrt(Math.max(0,1-d*d)));h<.15&&(h=.15);let c=y*s*h,p=g*s*h;const N=.5*Math.min(y||g,g||y);c>N&&(c=N),p>N&&(p=N),P=o-i*c,k=a-r*c,L=o+i*p,b=a+r*p}t.push(C,d,h,P,k,o,a),d=L,h=b}o&&t.push(M)}),exports.LinkerData=o;
|
|
1
|
+
"use strict";var t=require("@leafer-ui/draw"),e=require("@leafer-ui/core");function n(t,e,n,i){var o,r=arguments.length,s=r<3?e:null===i?i=Object.getOwnPropertyDescriptor(e,n):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,n,i);else for(var a=t.length-1;a>=0;a--)(o=t[a])&&(s=(r<3?o(s):r>3?o(e,n,s):o(e,n))||s);return r>3&&s&&Object.defineProperty(e,n,s),s}"function"==typeof SuppressedError&&SuppressedError;class i extends t.LineData{get __usePathBox(){return this.path||this.__pathInputed}}const{directionData:o}=t.AroundHelper,r=["top-left","top","top-right","right","bottom-right","bottom","bottom-left","left","center"],s={getCtrlPoint(t,e,n,i,o,r){let s;switch(e&&(n=e.getBoxPoint(n,t)),i){case"left":case"top-left":case"bottom-left":s={x:n.x-o.x,y:n.y};break;case"top":s={x:n.x,y:n.y-o.y};break;case"right":case"top-right":case"bottom-right":s={x:n.x+o.x,y:n.y};break;case"bottom":s={x:n.x,y:n.y+o.y};break;default:s={x:n.x,y:n.y}}return r&&(r=r/180*Math.PI,s={x:(s.x-n.x)*Math.cos(r)-(s.y-n.y)*Math.sin(r)+n.x,y:(s.x-n.x)*Math.sin(r)+(s.y-n.y)*Math.cos(r)+n.y}),e&&e.getWorldPointByBox(s,t,void 0,!0),s},isOppositeDirection(t,e){switch(t){case"left":return"right"==e;case"top":return"bottom"==e;case"right":return"left"==e;case"bottom":return"top"==e;case"center":return"center"==e;default:return!1}},getDirectionForFromPoint(t,e){const n=t.x-e.x,i=t.y-e.y;let o,r=180*Math.atan2(i,n)/Math.PI;return r<0&&(r=360+r),o=r>315||r<45?"left":r>45&&r<135?"top":r>135&&r<225?"right":"bottom",o},getRotateDirection(e,n,i=8){let o=t.Direction9[e];return o=(o+Math.round(n/(360/i)))%i,o<0&&(o+=i),r[o]},getDirection:function(e,n,i){const{width:o,height:r}=n,s=t.BoundsHelper.getPoint(n,"center",!0),a=e.x-s.x,h=e.y-s.y;if(!i&&Math.abs(a)<o/4&&Math.abs(h)<r/4)return"center";const u=Math.abs(a)*r,c=Math.abs(h)*o;if(u>c)return a>0?"right":"left";return h>0?"bottom":"top"},getPointDirection:function(t,e,n){const{width:i,height:s}=e,a=i/4,h=s/4;for(let e=0;e<o.length;e++){const u=o[e],c=u.x*i,d=u.y*s,l=t.x-c,f=t.y-d;if(Math.abs(l)<=a&&Math.abs(f)<=h){if(!n)return r[e];if("center"!==r[e])return r[e]}}return},autoRotatePoint(e,n,i){const{rotation:o,scaleX:r,scaleY:s}=i,a=r*s<0;(o||a)&&t.PointHelper.rotate(e,a?-o:o,n)}};function a(e){return t.decorateLeafAttr(e,e=>t.attr({set(n){if(this.__setAttr(e,n)){const e=this.__layout;e.linkerChanged||(t.doBoundsType(this),t.doStrokeType(this),e.linkerChanged=!0)}}}))}const{directionData:h}=t.AroundHelper,{moveTo:u,lineTo:c}=t.PathCommandDataHelper,d=.001,l=1.5;function f(t,e,n,i,o){const r=h[function(t){switch(t){case"top-left":return 0;case"top":return 1;case"top-right":return 2;case"right":return 3;case"bottom-right":return 4;case"bottom":return 5;case"bottom-left":return 6;case"left":return 7;default:return 8}}(t)],s=Math.sign(r.x-.5),a=Math.sign(r.y-.5),u=i?i.x+i.width/2:e.x,c=i?i.y+i.height/2:e.y,d=(o?o.x+o.width/2:n.x)-u,l=(o?o.y+o.height/2:n.y)-c,f=(s||(d>=0?1:-1))>0?1:0,p=(a||(l>=0?1:-1))>0?3:2;return s&&!a?[f]:a&&!s?[p]:Math.abs(d)>=Math.abs(l)?[f,p]:[p,f]}function p(t,e,n,i){const o=1&n?1:-1;if(0===n>>1){return{x:(e?e.x+(o>0?e.width:0):t.x)+o*i,y:t.y}}const r=e?e.y+(o>0?e.height:0):t.y;return{x:t.x,y:r+o*i}}function x(t,e,n,i){if(!n)return!1;const o=n.x-i,r=n.x+n.width+i,s=n.y-i,a=n.y+n.height+i;return t>o+d&&t<r-d&&e>s+d&&e<a-d}function y(t,e,n,i,o,r){if(!o)return!1;const s=o.x-r,a=o.x+o.width+r,h=o.y-r,u=o.y+o.height+r;return Math.abs(e-i)<=d?e>h+d&&e<u-d&&Math.max(Math.min(t,n),s+d)<Math.min(Math.max(t,n),a-d):!(Math.abs(t-n)<=d)||t>s+d&&t<a-d&&Math.max(Math.min(e,i),h+d)<Math.min(Math.max(e,i),u-d)}function g(t,e,n,i,o){if(!n)return;const r=n.x-i,s=n.x+n.width+i,a=n.y-i,h=n.y+n.height+i;t.push(r,s,r-o,s+o),e.push(a,h,a-o,h+o)}function b(t){t.sort((t,e)=>t-e);let e=0;for(let n=0;n<t.length;n++)(!e||Math.abs(t[n]-t[e-1])>d)&&(t[e++]=t[n]);t.length=e}function M(t,e){for(let n=0;n<t.length;n++)if(Math.abs(t[n]-e)<=d)return n;return-1}function m(t,e,n,i){const o=t>>1?i:n,r=1&t?1:-1;return Math.abs(o)<=d?e>d:o*r>0}function P(t,e,n,i,o){return void 0===t||(0===o&&Math.abs(n/2)>d?m(t,e,n,0):1===o&&Math.abs(i/2)>d?m(t,e,0,i):Math.abs(n)<=d&&Math.abs(i)<=d||m(t,e,n,i))}function k(t,e){if(void 0===e)return!0;const n=t.length-2,i=t[n],o=t[n+1];for(let r=n-2;r>=0;r-=2){const n=t[r]-i,s=t[r+1]-o;if(Math.abs(n)>d||Math.abs(s)>d)return m(e,1,n,s)}return!0}function L(t,e,n,i,o,r,s,a,h,u){const c=p(e,r,i,a),f=void 0===o?n:p(n,s,o,a),{x:L,y:N}=c,{x:v,y:C}=f,D=Math.abs(L-e.x)+Math.abs(N-e.y),_=Math.abs(v-n.x)+Math.abs(C-n.y);if(y(e.x,e.y,L,N,s,a)||y(n.x,n.y,v,C,r,a))return 1/0;if(void 0===o&&(x(v,C,r,a)||x(v,C,s,a)))return 1/0;const w=void 0===o?i>>1:function(t,e,n,i,o,r){const s=t>>1;return s===e>>1&&(1&t?1:-1)==-(1&e?1:-1)?s:Math.abs(o-n)>=Math.abs(r-i)?0:1}(i,o,L,N,v,C),B=v-L,F=C-N;if(t.length=0,t.push(e.x,e.y,L,N),(!h||P(i,D,B,F,w)&&P(o,_,-B,-F,w))&&P(u,1,-B,-F,w)){const e=(L+v)/2,i=(N+C)/2;0===w?t.push(e,N,e,C):t.push(L,i,v,i),t.push(v,C,n.x,n.y);for(let e=4;e<t.length-2;e+=2)if(y(t[e-2],t[e-1],t[e],t[e+1],r,a)||y(t[e-2],t[e-1],t[e],t[e+1],s,a)){t.length=4;break}if(t.length>4&&k(t,u))return function(t,e){let n=0,i=0,o=-1;for(let e=2;e<t.length;e+=2){const r=t[e]-t[e-2],s=t[e+1]-t[e-1];if(Math.abs(r)<=d&&Math.abs(s)<=d)continue;const a=Math.abs(r)>d?0:1;n+=Math.abs(r)+Math.abs(s),-1!==o&&o!==a&&i++,o=a}return n+i*Math.max(e,4)*1.5}(t,a);t.length=4}const H=function(t,e,n,i,o,r,s,a,h,u,c,f,p){const{x:P,y:k}=e,{x:L,y:N}=n,v=2*Math.max(a,4),C=[P,L,(P+L)/2],D=[k,N,(k+N)/2];g(C,D,r,a,v),g(C,D,s,a,v),C.push(Math.min(...C)-2*v,Math.max(...C)+2*v),D.push(Math.min(...D)-2*v,Math.max(...D)+2*v),b(C),b(D);const _=C.length,w=D.length,B=M(C,P),F=M(D,k),H=M(C,L),E=M(D,N);if(B<0||F<0||H<0||E<0)return 1/0;const T=F*_+B,O=E*_+H,S=_*w*3,R=new Float64Array(S),q=new Int32Array(S),A=new Uint8Array(S);R.fill(1/0),q.fill(-1);const I=3*T;R[I]=0;const W=[I],j=Math.max(4,a*l),z=0===h?(P+L)/2:(k+N)/2;let U=-1,X=-1,Y=0,Z=0,G=0,J=0;const K=(t,e,n)=>{const h=C[t],l=D[e],g=e*_+t,b=h-Z,M=l-G;if(g===T)return;if(void 0!==p&&g===O&&!m(p,1,-b,-M))return;if(f&&(U===I&&!m(i,u,b,M)||void 0!==o&&g===O&&!m(o,c,-b,-M)))return;if(x(h,l,r,a)||x(h,l,s,a)||y(Z,G,h,l,r,a)||y(Z,G,h,l,s,a))return;const P=3*g+n;if(A[P])return;let k=J+(Math.abs(b)+Math.abs(M));if(Y&&Y!==n){k+=j;const t=1===Y?Z:G;Math.abs(t-z)>d&&(k+=2*j)}k<R[P]&&(R[P]===1/0&&W.push(P),R[P]=k,q[P]=U)};let Q=-1;for(;W.length;){let t=0,e=W[0];for(let n=1;n<W.length;n++){const i=W[n];(R[i]<R[e]||R[i]===R[e]&&i<e)&&(t=n,e=i)}if(W[t]=W[W.length-1],W.pop(),U=e,X=Math.floor(e/3),Y=e%3,J=R[e],A[e]=1,X===O){Q=e;break}const n=X%_,i=Math.floor(X/_);Z=C[n],G=D[i],n>0&&K(n-1,i,1),n<_-1&&K(n+1,i,1),i>0&&K(n,i-1,2),i<w-1&&K(n,i+1,2)}if(Q<0)return 1/0;const V=[];let $=Q;for(;$>=0;){const t=Math.floor($/3);if(V.push(C[t%_],D[Math.floor(t/_)]),$===I)break;$=q[$]}if($!==I)return 1/0;for(let e=V.length-4;e>=0;e-=2)t.push(V[e],V[e+1]);return R[Q]}(t,c,f,i,o,r,s,a,w,D,_,h,u);return H===1/0?H:(t.push(n.x,n.y),k(t,u)?H+D+_:1/0)}function N(t,e,n,i,o,r,s,a){const h=f(n,t,e,o,void 0===i?void 0:r),u=void 0===i?[void 0]:f(i,e,t,r,o);let c=[],l=[],p=1/0;for(let n=s?0:1;n<3&&!l.length;n++){const i=n?0:s;for(let s=0;s<h.length;s++)for(let d=0;d<u.length;d++){const f=L(c,t,e,h[s],u[d],o,r,i,n<2,a);if(f<p){p=f;const t=l;l=c,c=t}}}if(!l.length)return function(t,e,n,i,o){const{x:r,y:s}=t,{x:a,y:h}=e;if(r===a||s===h)return[r,s,a,h];const u=!(n>>1),c=(t,e)=>{let n=0;return y(r,s,t,e,i,0)&&n++,y(t,e,a,h,i,0)&&n++,y(r,s,t,e,o,0)&&n++,y(t,e,a,h,o,0)&&n++,n},d=c(a,s),l=c(r,h);return d<l||d===l&&u?[r,s,a,s,a,h]:[r,s,r,h,a,h]}(t,e,h[0],o,r);!function(t){let e=2;for(let n=2;n<t.length;n+=2){const i=t[n],o=t[n+1],r=t[e-2],s=t[e-1];if(!(Math.abs(i-r)<=d&&Math.abs(o-s)<=d)){if(e>=4){const n=t[e-4],a=t[e-3],h=Math.abs(a-s)<=d&&Math.abs(s-o)<=d&&(r-n)*(i-r)>=0,u=Math.abs(n-r)<=d&&Math.abs(r-i)<=d&&(s-a)*(o-s)>=0;if(h||u){t[e-2]=i,t[e-1]=o;continue}}t[e++]=i,t[e++]=o}}t.length=e}(l);const x=l.length-2;return l[x]===e.x&&l[x+1]===e.y||l.push(e.x,e.y),l}function v(t,e,n,i,o,r,s,a,h){if(t.x===e.x&&t.y===e.y)return[];let u;for(let t=a+h;t>=0&&t<s.length;t+=h){const n=s[t]-e.x,i=s[t+1]-e.y;if(!(Math.abs(n)<=d&&Math.abs(i)<=d)){Math.abs(i)<=d?u=n>0?0:1:Math.abs(n)<=d&&(u=i>0?2:3);break}}const c=N(t,e,n,void 0,i,o,r,u);return c.length-=2,c}function C(t,e,n,i,o,r,s,a=8,h){if(!(Number.isFinite(e.x)&&Number.isFinite(e.y)&&Number.isFinite(n.x)&&Number.isFinite(n.y)))return!1;if(r&&(!Number.isFinite(r.x)||!Number.isFinite(r.y)||!Number.isFinite(r.width)||!Number.isFinite(r.height)||r.width<0||r.height<0))return!1;if(s&&(!Number.isFinite(s.x)||!Number.isFinite(s.y)||!Number.isFinite(s.width)||!Number.isFinite(s.height)||s.width<0||s.height<0))return!1;if(a=Number.isFinite(a)?Math.max(0,a):8,null==h?void 0:h.length)return function(t,e,n,i,o,r,s,a,h){const d=h.length,l=d-2;if(d%2)return!1;for(let t=0;t<d;t++)if(!Number.isFinite(h[t]))return!1;const f=v(e,{x:h[0],y:h[1]},i,r,s,a,h,0,2),p=v(n,{x:h[l],y:h[l+1]},o,s,r,a,h,l,-2),x=f.length?f:h;u(t,x[0],x[1]);for(let e=2;e<f.length;e+=2)c(t,f[e],f[e+1]);for(let e=f.length?0:2;e<d;e+=2)c(t,h[e],h[e+1]);for(let e=p.length-2;e>=0;e-=2)c(t,p[e],p[e+1]);return!0}(t,e,n,i,o,r,s,a,h);const d=N(e,n,i,o,r,s,a);u(t,d[0],d[1]);for(let e=2;e<d.length;e+=2)c(t,d[e],d[e+1]);return!0}const{moveTo:D,lineTo:_,bezierCurveTo:w,drawPoints:B}=t.PathCommandDataHelper,{isEmpty:F,getPoint:H}=t.BoundsHelper,{getDistance:E}=t.PointHelper,{getCtrlPoint:T,getDirectionForFromPoint:O,isOppositeDirection:S}=s,{max:R,min:q,abs:A}=Math;exports.Linker=class extends t.Line{get __tag(){return"Linker"}constructor(t){super(t),this.onStartNodeChange=this.onStartNodeChange.bind(this),this.onEndNodeChange=this.onEndNodeChange.bind(this)}__updatePath(){const t=this.__layout,{startNode:e,endNode:n}=this;t.linkerChanged&&(this.updateLinkerNode("start"),this.updateLinkerNode("end"),this.updateLinkerPoint("start"),this.updateLinkerPoint("end"),e!==this.startNode&&this.listenLinkerNode(this.startNode,e,"start"),n!==this.endNode&&this.listenLinkerNode(this.endNode,n,"end"),t.linkerChanged=!1),this.updateLinkerPath()}listenLinkerNode(e,n,i){if(n){n.off(t.BoundsEvent.WORLD,"end"===i?this.onEndNodeChange:this.onStartNodeChange);const e="end"===i?n.endLinker:n.startLinker;if(e){const t=e.indexOf(this);t>-1&&e.splice(t,1)}}if(e){e.on(t.BoundsEvent.WORLD,"end"===i?this.onEndNodeChange:this.onStartNodeChange);("end"===i?e.endLinker||(e.endLinker=[]):e.startLinker||(e.startLinker=[])).push(this)}}onStartNodeChange(){this.onLinkerNodeChange("start")}onEndNodeChange(){this.onLinkerNodeChange("end")}onLinkerNodeChange(e){if(this.leaferIsCreated){const{layouter:t,zoomLayer:e}=this.leafer;if(t.__updatedList.has(e))return;t.addExtra(this,!0)}const{startData:n,endData:i}=this;"start"==e?(this.updateLinkerPoint("start"),i&&i.auto&&this.updateLinkerPoint("end")):(n&&n.auto&&this.updateLinkerPoint("start"),this.updateLinkerPoint("end")),this.__layout.boxChange(),t.LeafHelper.updateBounds(this)}updateLinkerNode(e){const{startPoint:n,endPoint:i}=this,o="end"===e?i:n;if(o){const{id:n}=o,i=n&&(t.isString(n)?this.app.findId(n):n);"end"===e?this.endNode=i:this.startNode=i}}getLinkerNodeCenter(e){const{startPoint:n,endPoint:i,startNode:o,endNode:r,startData:s,endData:a}=this,h="end"===e?r:o,u="end"===e?a||i:s||n;if(h){const{worldRenderBounds:e}=h;return F(e)&&t.LeafHelper.updateBounds(h),H(h.worldRenderBounds,"center")}return u&&t.isObject(u.point)&&this.getWorldPointByLocal(u.point)}updateLinkerPoint(e){const{startPoint:n,endPoint:i,lineType:o}=this,r="end"===e?i:n;if(r){const n={},{id:i,offset:s}=r;let{direction:a,point:h}=r;if("center"===a||!a&&i&&!h){const t=this.getLinkerNodeCenter("start"),i=this.getLinkerNodeCenter("end"),o="end"===e?i:t,r="end"===e?t:i;o&&r&&(a=O(o,r)),n.auto=!0}!a&&t.isString(h)&&(a=h),n.direction=a;const u="end"===e?this.endNode:this.startNode;let c;if(u){if(h||(h=a),h){const{boxBounds:e}=u;F(e)&&t.LeafHelper.updateBounds(u),c=H(e,h,!0),u.getWorldPointByBox(c,this,!1,!0),"polyline"===o&&(n.bounds=u.getBounds("box",this.parent))}}else t.isObject(h)&&(c=h);if(c&&s){const e=Object.assign({},s);s.x||(e.x=0),s.y||(e.y=0),u&&u.getWorldPoint(e,this,!0,!0),t.PointHelper.move(c,e)}n.point=c,"end"===e?this.endData=c&&n:this.startData=c&&n}}updateLinkerPath(){const e=this.__,n=e.path=[],i=e.points&&e.points.length,{startData:o,endData:r,startNode:s,endNode:a}=this;if(!o||!r)return;const h=o.point,u=r.point,c=o.direction,d=r.direction,l=8*this.__.strokeWidth;switch(e.lineType){case"straight":D(n,h.x,h.y),_(n,u.x,u.y);break;case"polyline":C(n,h,u,c,d,o.bounds,r.bounds,l,i&&t.PointHelper.toNumberPoints(e.points));break;default:const f={x:A((h.x-u.x)/2),y:A((h.y-u.y)/2)};let p;if(this.__.__useArrow){const t=q(E(h,u)/(25*l),1);p=R(l*t*12,3*l)}else p=2*l;if(d&&S(c,d)&&c===O(h,u)&&(p=2*l),f.x=R(p,f.x),f.y=R(p,f.y),i){p*=.6;const i=t.PointHelper.toNumberPoints(e.points);if(B(n,[h.x,h.y,...i,u.x,u.y],e.curve,!1,e),c){const t=n[4],e=n[5];f.x=R(p,A(t-h.x)),f.y=R(p,A(e-h.y));const i=T(this,s,h,c,f);n[4]=i.x,n[5]=i.y}const o=n.length;if(d){const t=n[o-4],e=n[o-3];f.x=R(p,A(t-u.x)),f.y=R(p,A(e-u.y));const i=T(this,a,u,d,f);n[o-4]=i.x,n[o-3]=i.y}else n[o-4]=u.x,n[o-3]=u.y}else{const t=c?T(this,s,h,c,f):h,e=d?T(this,a,u,d,f):u;D(n,h.x,h.y),w(n,t.x,t.y,e.x,e.y,u.x,u.y)}}}createStartPoint(t,e,n){const i=this.createLinkerPoint(t,e,n);return this.startPoint=i,i}createEndPoint(t,e,n){const i=this.createLinkerPoint(t,e,n);return this.endPoint=i,i}createLinkerPoint(e,n,i){const o={};if(n&&n.id){const r=n.getBoxPoint(e),{boxBounds:a}=n,h=i&&i.mode,u="edge"===h||!h,c="four"===h,d="free"===h;if(!("auto"===h))if(c)o.point=s.getDirection(r,a,c);else if(o.direction=s.getDirection(r,a,u),o.point=s.getPointDirection(r,a,u)||o.direction,o.point===o.direction&&delete o.direction,d||u){const e=t.BoundsHelper.getPoint(a,o.point),n=r.x-e.x,s=r.y-e.y,h=i&&i.snapRadius||10;if(Math.abs(n)>h||Math.abs(s)>h){const t=o.offset={x:Math.abs(n)>h?n:0,y:Math.abs(s)>h?s:0};if(u&&(t.x||t.y))switch(o.direction||o.point){case"top":case"bottom":t.y=0;break;case"right":case"left":t.x=0;break;default:t.x>t.y?t.y=0:t.x=0}}}o.id=n.id}else o.point=this.getLocalPoint(e);return o}},n([t.dataProcessor(i)],exports.Linker.prototype,"__",void 0),n([t.boundsType("curve")],exports.Linker.prototype,"lineType",void 0),n([a()],exports.Linker.prototype,"startPoint",void 0),n([a()],exports.Linker.prototype,"endPoint",void 0),n([t.pathType({type:"C",value:.5})],exports.Linker.prototype,"curve",void 0),exports.Linker=n([t.registerUI()],exports.Linker);const{toNumberPoints:I}=e.PointHelper,{M:W,L:j,C:z,Z:U}=e.PathCommandMap;t.Plugin.add("linker"),t.registerPointsCurve("C",function(t,e,n,i){const o=I(e),r=o.length>>1;if(r<2)return;if(t.push(W,o[0],o[1]),!n||r<3){for(let e=1;e<r;e++)t.push(j,o[2*e],o[2*e+1]);return void(i&&t.push(U))}let s=!0===n?.5:n;s<0?s=0:s>1&&(s=1);const a=i?r:r-1;let h=o[0],u=o[1];{const t=o[2]-o[0],e=o[3]-o[1],n=Math.sqrt(t*t+e*e);n&&(h=o[0]+t/n*n*s,u=o[1]+e/n*n*s)}for(let e=0;e<a;e++){const n=e+1===r?0:e+1,i=o[2*n],a=o[2*n+1],c=0===n?r-1:n-1,d=n+1===r?0:n+1;let l=o[2*c]-i,f=o[2*c+1]-a,p=o[2*d]-i,x=o[2*d+1]-a;const y=Math.sqrt(l*l+f*f),g=Math.sqrt(p*p+x*x);let b=i,M=a,m=i,P=a;if(y||g){let t=0,e=0;y&&(t+=l/y,e+=f/y),g&&(t+=p/g,e+=x/g);let n=Math.sqrt(t*t+e*e);n||(g?(t=p/g,e=x/g):(t=l/y,e=f/y),n=1),t/=n,e/=n;let o=-e,r=t;g&&o*p+r*x<0&&(o=-o,r=-r);let h=0;y&&g&&(h=(l*p+f*x)/(y*g),h>1?h=1:h<-1&&(h=-1));let u=Math.sqrt(Math.sqrt(Math.max(0,1-h*h)));u<.15&&(u=.15);let c=y*s*u,d=g*s*u;const k=.5*Math.min(y||g,g||y);c>k&&(c=k),d>k&&(d=k),b=i-o*c,M=a-r*c,m=i+o*d,P=a+r*d}t.push(z,h,u,b,M,i,a),h=m,u=P}i&&t.push(U)}),exports.LinkerData=i;
|
|
2
2
|
//# sourceMappingURL=linker.min.cjs.map
|