@plait/common 0.92.3 → 0.93.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/README.md
CHANGED
|
@@ -5,7 +5,7 @@ Common logic for drawing plugins. When writing drawing business code, if you fin
|
|
|
5
5
|
|
|
6
6
|
`core` core logic, currently mainly places the base class of plugin components
|
|
7
7
|
|
|
8
|
-
`generators` Generator is an abstract concept, mainly used for plug-in component rendering and management of drawing elements, 👉 [View more](https://plait-docs.
|
|
8
|
+
`generators` Generator is an abstract concept, mainly used for plug-in component rendering and management of drawing elements, 👉 [View more](https://plait-docs.pages.dev/guides/concepts/generator)
|
|
9
9
|
|
|
10
10
|
`plugins` places reusable plugin logic
|
|
11
11
|
|
|
@@ -444,8 +444,8 @@ function getRatioByPoint(points, point) {
|
|
|
444
444
|
}
|
|
445
445
|
const removeDuplicatePoints = (points) => {
|
|
446
446
|
const newArray = [];
|
|
447
|
-
points.forEach(point => {
|
|
448
|
-
const index = newArray.findIndex(otherPoint => {
|
|
447
|
+
points.forEach((point) => {
|
|
448
|
+
const index = newArray.findIndex((otherPoint) => {
|
|
449
449
|
return Point.isEquals(point, otherPoint);
|
|
450
450
|
});
|
|
451
451
|
if (index === -1)
|
|
@@ -712,7 +712,7 @@ const getFirstTextManage = (element) => {
|
|
|
712
712
|
return textManage;
|
|
713
713
|
};
|
|
714
714
|
const getTextEditorsByElement = (element) => {
|
|
715
|
-
return getTextManages(element).map(manage => {
|
|
715
|
+
return getTextManages(element).map((manage) => {
|
|
716
716
|
return manage.editor;
|
|
717
717
|
});
|
|
718
718
|
};
|
|
@@ -726,7 +726,7 @@ const getFirstTextEditor = (element) => {
|
|
|
726
726
|
const findFirstTextEditor = (board) => {
|
|
727
727
|
const selectedElements = getSelectedElements(board);
|
|
728
728
|
let firstEditor = null;
|
|
729
|
-
selectedElements.forEach(element => {
|
|
729
|
+
selectedElements.forEach((element) => {
|
|
730
730
|
const editors = getTextEditorsByElement(element);
|
|
731
731
|
if (!firstEditor && editors && editors.length > 0) {
|
|
732
732
|
firstEditor = editors[0];
|
|
@@ -736,12 +736,12 @@ const findFirstTextEditor = (board) => {
|
|
|
736
736
|
};
|
|
737
737
|
const getElementsText = (elements) => {
|
|
738
738
|
return elements
|
|
739
|
-
.map(item => {
|
|
739
|
+
.map((item) => {
|
|
740
740
|
try {
|
|
741
741
|
const editors = getTextEditorsByElement(item);
|
|
742
742
|
if (editors.length) {
|
|
743
743
|
return editors
|
|
744
|
-
.map(editor => {
|
|
744
|
+
.map((editor) => {
|
|
745
745
|
const textsEntry = Node.texts(editor);
|
|
746
746
|
return Array.from(textsEntry).reduce((total, text) => (total += text[0].text), '');
|
|
747
747
|
})
|
|
@@ -753,21 +753,21 @@ const getElementsText = (elements) => {
|
|
|
753
753
|
return '';
|
|
754
754
|
}
|
|
755
755
|
})
|
|
756
|
-
.filter(item => item)
|
|
756
|
+
.filter((item) => item)
|
|
757
757
|
.join(' ');
|
|
758
758
|
};
|
|
759
759
|
const getTextEditors = (board, elements) => {
|
|
760
760
|
const selectedElements = elements || getSelectedElements(board);
|
|
761
761
|
if (selectedElements.length) {
|
|
762
762
|
const textManages = [];
|
|
763
|
-
selectedElements.forEach(item => {
|
|
763
|
+
selectedElements.forEach((item) => {
|
|
764
764
|
textManages.push(...getTextManages(item));
|
|
765
765
|
});
|
|
766
|
-
const editingTextManage = textManages.find(textManage => textManage.isEditing);
|
|
766
|
+
const editingTextManage = textManages.find((textManage) => textManage.isEditing);
|
|
767
767
|
if (editingTextManage) {
|
|
768
768
|
return [editingTextManage.editor];
|
|
769
769
|
}
|
|
770
|
-
return textManages.map(item => {
|
|
770
|
+
return textManages.map((item) => {
|
|
771
771
|
return item.editor;
|
|
772
772
|
});
|
|
773
773
|
}
|
|
@@ -776,10 +776,10 @@ const getTextEditors = (board, elements) => {
|
|
|
776
776
|
const getEditingTextEditor = (board, elements) => {
|
|
777
777
|
const selectedElements = elements || getSelectedElements(board);
|
|
778
778
|
const textManages = [];
|
|
779
|
-
selectedElements.forEach(item => {
|
|
779
|
+
selectedElements.forEach((item) => {
|
|
780
780
|
textManages.push(...getTextManages(item));
|
|
781
781
|
});
|
|
782
|
-
const editingTextManage = textManages.find(textManage => textManage.isEditing);
|
|
782
|
+
const editingTextManage = textManages.find((textManage) => textManage.isEditing);
|
|
783
783
|
if (editingTextManage) {
|
|
784
784
|
return editingTextManage.editor;
|
|
785
785
|
}
|
|
@@ -886,7 +886,7 @@ class AStar {
|
|
|
886
886
|
if (currentPoint[0] === end[0] && currentPoint[1] === end[1]) {
|
|
887
887
|
break;
|
|
888
888
|
}
|
|
889
|
-
current.node.adjacentNodes.forEach(next => {
|
|
889
|
+
current.node.adjacentNodes.forEach((next) => {
|
|
890
890
|
let newCost = costSoFar.get(current.node) + this.heuristic(next.data, current.node.data);
|
|
891
891
|
const previousNode = this.cameFrom.get(current.node);
|
|
892
892
|
// Inflection point weight, if an inflection point occurs, cost + 1 to avoid the inflection point path
|
|
@@ -1014,7 +1014,7 @@ const routeAdjust = (path, options, board) => {
|
|
|
1014
1014
|
const adjust = (route, options, board) => {
|
|
1015
1015
|
const { parallelPaths, pointOfHit, sourceRectangle, targetRectangle } = options;
|
|
1016
1016
|
let result = null;
|
|
1017
|
-
parallelPaths.forEach(parallelPath => {
|
|
1017
|
+
parallelPaths.forEach((parallelPath) => {
|
|
1018
1018
|
// Construct a rectangle
|
|
1019
1019
|
const tempRectPoints = [pointOfHit, parallelPath[0], parallelPath[1]];
|
|
1020
1020
|
// directly use getCornerPoints will bring the precision issue (eg: 263.6923375175286 - 57.130859375)
|
|
@@ -1027,7 +1027,7 @@ const adjust = (route, options, board) => {
|
|
|
1027
1027
|
const indexRangeInPath = [];
|
|
1028
1028
|
const indexRangeInCorner = [];
|
|
1029
1029
|
route.forEach((point, index) => {
|
|
1030
|
-
const cornerResult = tempCorners.findIndex(corner => Point.isEquals(point, corner));
|
|
1030
|
+
const cornerResult = tempCorners.findIndex((corner) => Point.isEquals(point, corner));
|
|
1031
1031
|
if (cornerResult !== -1) {
|
|
1032
1032
|
indexRangeInPath.push(index);
|
|
1033
1033
|
indexRangeInCorner.push(cornerResult);
|
|
@@ -1082,7 +1082,7 @@ const getGraphPoints = (options) => {
|
|
|
1082
1082
|
const x = [];
|
|
1083
1083
|
const y = [];
|
|
1084
1084
|
let result = [];
|
|
1085
|
-
[sourceOuterRectangle, targetOuterRectangle].forEach(rectangle => {
|
|
1085
|
+
[sourceOuterRectangle, targetOuterRectangle].forEach((rectangle) => {
|
|
1086
1086
|
x.push(rectangle.x, rectangle.x + rectangle.width / 2, rectangle.x + rectangle.width);
|
|
1087
1087
|
y.push(rectangle.y, rectangle.y + rectangle.height / 2, rectangle.y + rectangle.height);
|
|
1088
1088
|
});
|
|
@@ -1110,7 +1110,7 @@ const getGraphPoints = (options) => {
|
|
|
1110
1110
|
}
|
|
1111
1111
|
}
|
|
1112
1112
|
}
|
|
1113
|
-
result = removeDuplicatePoints(result).filter(point => {
|
|
1113
|
+
result = removeDuplicatePoints(result).filter((point) => {
|
|
1114
1114
|
const isInSource = RectangleClient.isPointInRectangle(sourceOuterRectangle, point);
|
|
1115
1115
|
const isInTarget = RectangleClient.isPointInRectangle(targetOuterRectangle, point);
|
|
1116
1116
|
return !isInSource && !isInTarget;
|
|
@@ -1121,7 +1121,7 @@ const createGraph = (points) => {
|
|
|
1121
1121
|
const graph = new PointGraph();
|
|
1122
1122
|
const Xs = [];
|
|
1123
1123
|
const Ys = [];
|
|
1124
|
-
points.forEach(p => {
|
|
1124
|
+
points.forEach((p) => {
|
|
1125
1125
|
const x = p[0], y = p[1];
|
|
1126
1126
|
if (Xs.indexOf(x) < 0)
|
|
1127
1127
|
Xs.push(x);
|
|
@@ -1311,13 +1311,13 @@ const getStrokeLineDash = (strokeStyle, strokeWidth) => {
|
|
|
1311
1311
|
};
|
|
1312
1312
|
|
|
1313
1313
|
const buildClipboardData = (board, elements, startPoint, elementBuilder) => {
|
|
1314
|
-
return elements.map(element => {
|
|
1314
|
+
return elements.map((element) => {
|
|
1315
1315
|
const newElement = elementBuilder && elementBuilder(element);
|
|
1316
1316
|
if (newElement) {
|
|
1317
1317
|
return newElement;
|
|
1318
1318
|
}
|
|
1319
1319
|
if (element.points) {
|
|
1320
|
-
const points = element.points.map(point => [point[0] - startPoint[0], point[1] - startPoint[1]]);
|
|
1320
|
+
const points = element.points.map((point) => [point[0] - startPoint[0], point[1] - startPoint[1]]);
|
|
1321
1321
|
return { ...element, points };
|
|
1322
1322
|
}
|
|
1323
1323
|
return element;
|
|
@@ -1325,14 +1325,14 @@ const buildClipboardData = (board, elements, startPoint, elementBuilder) => {
|
|
|
1325
1325
|
};
|
|
1326
1326
|
const insertClipboardData = (board, elements, startPoint, elementHandler) => {
|
|
1327
1327
|
const idsMap = {};
|
|
1328
|
-
elements.forEach(element => {
|
|
1328
|
+
elements.forEach((element) => {
|
|
1329
1329
|
idsMap[element.id] = idCreator();
|
|
1330
1330
|
});
|
|
1331
|
-
elements.forEach(element => {
|
|
1331
|
+
elements.forEach((element) => {
|
|
1332
1332
|
element.id = idsMap[element.id];
|
|
1333
1333
|
elementHandler && elementHandler(element, idsMap);
|
|
1334
1334
|
if (element.points) {
|
|
1335
|
-
element.points = element.points.map(point => [startPoint[0] + point[0], startPoint[1] + point[1]]);
|
|
1335
|
+
element.points = element.points.map((point) => [startPoint[0] + point[0], startPoint[1] + point[1]]);
|
|
1336
1336
|
}
|
|
1337
1337
|
Transforms.insertNode(board, element, [board.children.length]);
|
|
1338
1338
|
});
|
|
@@ -1490,7 +1490,7 @@ class ImageGenerator extends Generator {
|
|
|
1490
1490
|
|
|
1491
1491
|
const setProperty = (board, properties, options) => {
|
|
1492
1492
|
const selectedElements = getSelectedElements(board);
|
|
1493
|
-
selectedElements.forEach(element => {
|
|
1493
|
+
selectedElements.forEach((element) => {
|
|
1494
1494
|
if (options?.match && !options?.match(element))
|
|
1495
1495
|
return;
|
|
1496
1496
|
const path = PlaitBoard.findPath(board, element);
|
|
@@ -1569,7 +1569,7 @@ const alignRight = (board) => {
|
|
|
1569
1569
|
function setOffset(board, getOffset) {
|
|
1570
1570
|
const elements = getHighestSelectedElements(board);
|
|
1571
1571
|
const outerRectangle = getRectangleByElements(board, elements, false);
|
|
1572
|
-
elements.forEach(element => {
|
|
1572
|
+
elements.forEach((element) => {
|
|
1573
1573
|
if (!element.points && !PlaitGroupElement.isGroup(element))
|
|
1574
1574
|
return;
|
|
1575
1575
|
const rectangle = board.getRectangle(element);
|
|
@@ -1581,8 +1581,8 @@ function setOffset(board, getOffset) {
|
|
|
1581
1581
|
else if (element.points) {
|
|
1582
1582
|
updateElements = [element];
|
|
1583
1583
|
}
|
|
1584
|
-
updateElements.forEach(item => {
|
|
1585
|
-
const newPoints = item.points.map(p => [p[0] + offset[0], p[1] + offset[1]]);
|
|
1584
|
+
updateElements.forEach((item) => {
|
|
1585
|
+
const newPoints = item.points.map((p) => [p[0] + offset[0], p[1] + offset[1]]);
|
|
1586
1586
|
const path = PlaitBoard.findPath(board, item);
|
|
1587
1587
|
Transforms.setNode(board, {
|
|
1588
1588
|
points: newPoints
|
|
@@ -1602,14 +1602,14 @@ const distribute = (board, isHorizontal) => {
|
|
|
1602
1602
|
const axis = isHorizontal ? 'x' : 'y';
|
|
1603
1603
|
const side = isHorizontal ? 'width' : 'height';
|
|
1604
1604
|
const highestSelectedElements = getHighestSelectedElements(board);
|
|
1605
|
-
const refs = highestSelectedElements.map(element => {
|
|
1605
|
+
const refs = highestSelectedElements.map((element) => {
|
|
1606
1606
|
return { element, rectangle: board.getRectangle(element) };
|
|
1607
1607
|
});
|
|
1608
1608
|
const outerRectangle = getRectangleByElements(board, highestSelectedElements, false);
|
|
1609
1609
|
const minRectangleRef = refs.sort((a, b) => a.rectangle[axis] - b.rectangle[axis])[0];
|
|
1610
1610
|
const maxRectangleRef = refs.sort((a, b) => b.rectangle[axis] + b.rectangle[side] - (a.rectangle[axis] + a.rectangle[side]))[0];
|
|
1611
|
-
const minIndex = refs.findIndex(ref => ref === minRectangleRef);
|
|
1612
|
-
const maxIndex = refs.findIndex(ref => ref === maxRectangleRef);
|
|
1611
|
+
const minIndex = refs.findIndex((ref) => ref === minRectangleRef);
|
|
1612
|
+
const maxIndex = refs.findIndex((ref) => ref === maxRectangleRef);
|
|
1613
1613
|
let distributeRefs = refs.filter((element, index) => index !== minIndex && index !== maxIndex);
|
|
1614
1614
|
const sum = distributeRefs.reduce((accumulator, current) => current.rectangle[side] + accumulator, 0);
|
|
1615
1615
|
const offset = (outerRectangle[side] - minRectangleRef.rectangle[side] - maxRectangleRef.rectangle[side] - sum) / (distributeRefs.length + 1);
|
|
@@ -1621,7 +1621,7 @@ const distribute = (board, isHorizontal) => {
|
|
|
1621
1621
|
const moveAxis = isHorizontal ? 0 : 1;
|
|
1622
1622
|
moveOffset[moveAxis] = position - rectangle[axis];
|
|
1623
1623
|
const path = PlaitBoard.findPath(board, distributeRefs[i].element);
|
|
1624
|
-
const newPoints = distributeRefs[i].element.points.map(p => [p[0] + moveOffset[0], p[1] + moveOffset[1]]);
|
|
1624
|
+
const newPoints = distributeRefs[i].element.points.map((p) => [p[0] + moveOffset[0], p[1] + moveOffset[1]]);
|
|
1625
1625
|
Transforms.setNode(board, {
|
|
1626
1626
|
points: newPoints
|
|
1627
1627
|
}, path);
|
|
@@ -1684,7 +1684,7 @@ const withResize = (board, options) => {
|
|
|
1684
1684
|
}
|
|
1685
1685
|
startPoint = [event.x, event.y];
|
|
1686
1686
|
const path = Array.isArray(resizeHitTestRef.element)
|
|
1687
|
-
? resizeHitTestRef.element.map(el => PlaitBoard.findPath(board, el))
|
|
1687
|
+
? resizeHitTestRef.element.map((el) => PlaitBoard.findPath(board, el))
|
|
1688
1688
|
: PlaitBoard.findPath(board, resizeHitTestRef.element);
|
|
1689
1689
|
resizeRef = {
|
|
1690
1690
|
path,
|