@plait/core 0.24.0-next.11 → 0.24.0-next.12

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.
@@ -2954,17 +2954,27 @@ class ReactionManager {
2954
2954
  }
2955
2955
  }
2956
2956
  const alignDeltaX = deltaX;
2957
+ const alignDeltaY = deltaY;
2957
2958
  this.activeRectangle.x += deltaX;
2958
- const distributeHorizontalResult = this.alignDistributeHorizontal(alignRectangles);
2959
- const distributeLines = distributeHorizontalResult.distributeLines;
2960
- if (distributeHorizontalResult.deltaX) {
2961
- deltaX = distributeHorizontalResult.deltaX;
2959
+ const distributeHorizontalResult = this.alignDistribute(alignRectangles, true);
2960
+ const distributeVerticalResult = this.alignDistribute(alignRectangles, false);
2961
+ const distributeLines = [...distributeHorizontalResult.distributeLines, ...distributeVerticalResult.distributeLines];
2962
+ if (distributeHorizontalResult.delta) {
2963
+ deltaX = distributeHorizontalResult.delta;
2962
2964
  if (alignDeltaX !== deltaX) {
2963
2965
  alignLines[0] = [];
2964
2966
  alignLines[1] = [];
2965
2967
  alignLines[2] = [];
2966
2968
  }
2967
2969
  }
2970
+ if (distributeVerticalResult.delta) {
2971
+ deltaY = distributeVerticalResult.delta;
2972
+ if (alignDeltaY !== deltaY) {
2973
+ alignLines[3] = [];
2974
+ alignLines[4] = [];
2975
+ alignLines[5] = [];
2976
+ }
2977
+ }
2968
2978
  if (alignLines.length) {
2969
2979
  this.drawAlignLines(alignLines, g);
2970
2980
  }
@@ -3001,82 +3011,85 @@ class ReactionManager {
3001
3011
  indexY
3002
3012
  };
3003
3013
  }
3004
- alignDistributeHorizontal(alignRectangles) {
3014
+ alignDistribute(alignRectangles, isHorizontal) {
3005
3015
  let distributeLines = [];
3006
- let deltaX = 0;
3007
- const activeRectangleCenterX = this.activeRectangle.x + this.activeRectangle.width / 2;
3016
+ let delta = 0;
3008
3017
  let rectangles = [];
3018
+ const axis = isHorizontal ? 'x' : 'y';
3019
+ const side = isHorizontal ? 'width' : 'height';
3020
+ const activeRectangleCenter = this.activeRectangle[axis] + this.activeRectangle[side] / 2;
3009
3021
  alignRectangles.forEach(rec => {
3010
- if (isHorizontalCross(rec, this.activeRectangle) && !RectangleClient.isHit(rec, this.activeRectangle)) {
3022
+ const isCross = isHorizontal ? isHorizontalCross(rec, this.activeRectangle) : isVerticalCross(rec, this.activeRectangle);
3023
+ if (isCross && !RectangleClient.isHit(rec, this.activeRectangle)) {
3011
3024
  rectangles.push(rec);
3012
3025
  }
3013
3026
  });
3014
- rectangles = [...rectangles, this.activeRectangle].sort((a, b) => a.x - b.x);
3027
+ rectangles = [...rectangles, this.activeRectangle].sort((a, b) => a[axis] - b[axis]);
3015
3028
  const refArray = [];
3016
3029
  let distributeDistance = 0;
3017
- let leftIndex = undefined;
3018
- let rightIndex = undefined;
3030
+ let beforeIndex = undefined;
3031
+ let afterIndex = undefined;
3019
3032
  for (let i = 0; i < rectangles.length; i++) {
3020
3033
  for (let j = i + 1; j < rectangles.length; j++) {
3021
- const left = rectangles[i];
3022
- const right = rectangles[j];
3023
- const distance = right.x - (left.x + left.width);
3034
+ const before = rectangles[i];
3035
+ const after = rectangles[j];
3036
+ const distance = after[axis] - (before[axis] + before[side]);
3024
3037
  let dif = Infinity;
3025
- if (refArray[i]?.right) {
3026
- refArray[i].right.push({ distance, index: j });
3038
+ if (refArray[i]?.after) {
3039
+ refArray[i].after.push({ distance, index: j });
3027
3040
  }
3028
3041
  else {
3029
- refArray[i] = { ...refArray[i], right: [{ distance, index: j }] };
3042
+ refArray[i] = { ...refArray[i], after: [{ distance, index: j }] };
3030
3043
  }
3031
- if (refArray[j]?.left) {
3032
- refArray[j].left.push({ distance, index: i });
3044
+ if (refArray[j]?.before) {
3045
+ refArray[j].before.push({ distance, index: i });
3033
3046
  }
3034
3047
  else {
3035
- refArray[j] = { ...refArray[j], left: [{ distance, index: i }] };
3048
+ refArray[j] = { ...refArray[j], before: [{ distance, index: i }] };
3036
3049
  }
3037
3050
  //middle
3038
- let _centerX = (left.x + left.width + right.x) / 2;
3039
- dif = Math.abs(activeRectangleCenterX - _centerX);
3051
+ let _center = (before[axis] + before[side] + after[axis]) / 2;
3052
+ dif = Math.abs(activeRectangleCenter - _center);
3040
3053
  if (dif < ALIGN_TOLERANCE) {
3041
- distributeDistance = (right.x - (left.x + left.width) - this.activeRectangle.width) / 2;
3042
- deltaX = activeRectangleCenterX - _centerX;
3043
- leftIndex = i;
3044
- rightIndex = j;
3054
+ distributeDistance = (after[axis] - (before[axis] + before[side]) - this.activeRectangle[side]) / 2;
3055
+ delta = activeRectangleCenter - _center;
3056
+ beforeIndex = i;
3057
+ afterIndex = j;
3045
3058
  }
3046
- //right
3047
- const distanceRight = right.x - (left.x + left.width);
3048
- _centerX = right.x + right.width + distanceRight + this.activeRectangle.width / 2;
3049
- dif = Math.abs(activeRectangleCenterX - _centerX);
3059
+ //after
3060
+ const distanceRight = after[axis] - (before[axis] + before[side]);
3061
+ _center = after[axis] + after[side] + distanceRight + this.activeRectangle[side] / 2;
3062
+ dif = Math.abs(activeRectangleCenter - _center);
3050
3063
  if (!distributeDistance && dif < ALIGN_TOLERANCE) {
3051
3064
  distributeDistance = distanceRight;
3052
- leftIndex = j;
3053
- deltaX = activeRectangleCenterX - _centerX;
3065
+ beforeIndex = j;
3066
+ delta = activeRectangleCenter - _center;
3054
3067
  }
3055
- //left
3056
- const distanceLeft = right.x - (left.x + left.width);
3057
- _centerX = left.x - distanceLeft - this.activeRectangle.width / 2;
3058
- dif = Math.abs(activeRectangleCenterX - _centerX);
3068
+ //before
3069
+ const distanceBefore = after[axis] - (before[axis] + before[side]);
3070
+ _center = before[axis] - distanceBefore - this.activeRectangle[side] / 2;
3071
+ dif = Math.abs(activeRectangleCenter - _center);
3059
3072
  if (!distributeDistance && dif < ALIGN_TOLERANCE) {
3060
- distributeDistance = distanceLeft;
3061
- rightIndex = i;
3062
- deltaX = activeRectangleCenterX - _centerX;
3073
+ distributeDistance = distanceBefore;
3074
+ afterIndex = i;
3075
+ delta = activeRectangleCenter - _center;
3063
3076
  }
3064
3077
  }
3065
3078
  }
3066
3079
  const activeIndex = rectangles.indexOf(this.activeRectangle);
3067
- let leftIndexes = [];
3068
- let rightIndexes = [];
3069
- if (leftIndex !== undefined) {
3070
- leftIndexes.push(leftIndex);
3071
- findRectangle(distributeDistance, refArray[leftIndex], Direction.left, leftIndexes);
3072
- }
3073
- if (rightIndex !== undefined) {
3074
- rightIndexes.push(rightIndex);
3075
- findRectangle(distributeDistance, refArray[rightIndex], Direction.right, rightIndexes);
3076
- }
3077
- if (leftIndexes.length || rightIndexes.length) {
3078
- const indexArr = [...leftIndexes.reverse(), activeIndex, ...rightIndexes];
3079
- this.activeRectangle.x -= deltaX;
3080
+ let beforeIndexes = [];
3081
+ let afterIndexes = [];
3082
+ if (beforeIndex !== undefined) {
3083
+ beforeIndexes.push(beforeIndex);
3084
+ findRectangle(distributeDistance, refArray[beforeIndex], 'before', beforeIndexes);
3085
+ }
3086
+ if (afterIndex !== undefined) {
3087
+ afterIndexes.push(afterIndex);
3088
+ findRectangle(distributeDistance, refArray[afterIndex], 'after', afterIndexes);
3089
+ }
3090
+ if (beforeIndexes.length || afterIndexes.length) {
3091
+ const indexArr = [...beforeIndexes.reverse(), activeIndex, ...afterIndexes];
3092
+ this.activeRectangle[axis] -= delta;
3080
3093
  for (let i = 1; i < indexArr.length; i++) {
3081
3094
  distributeLines.push(getLinePoints(rectangles[indexArr[i - 1]], rectangles[indexArr[i]]));
3082
3095
  }
@@ -3084,7 +3097,7 @@ class ReactionManager {
3084
3097
  function findRectangle(distance, ref, direction, rectangleIndexes) {
3085
3098
  const arr = ref[direction];
3086
3099
  const index = refArray.indexOf(ref);
3087
- if ((index === 0 && direction === Direction.left) || (index === refArray.length - 1 && direction === Direction.right))
3100
+ if ((index === 0 && direction === 'before') || (index === refArray.length - 1 && direction === 'after'))
3088
3101
  return;
3089
3102
  for (let i = 0; i < arr.length; i++) {
3090
3103
  if (Math.abs(arr[i].distance - distance) < 0.1) {
@@ -3094,22 +3107,29 @@ class ReactionManager {
3094
3107
  }
3095
3108
  }
3096
3109
  }
3097
- function getLinePoints(leftRectangle, rightRectangle) {
3098
- const verticalY = [
3099
- leftRectangle.y,
3100
- leftRectangle.y + leftRectangle.height,
3101
- rightRectangle.y,
3102
- rightRectangle.y + rightRectangle.height
3110
+ function getLinePoints(beforeRectangle, afterRectangle) {
3111
+ const oppositeAxis = axis === 'x' ? 'y' : 'x';
3112
+ const oppositeSide = side === 'width' ? 'height' : 'width';
3113
+ const align = [
3114
+ beforeRectangle[oppositeAxis],
3115
+ beforeRectangle[oppositeAxis] + beforeRectangle[oppositeSide],
3116
+ afterRectangle[oppositeAxis],
3117
+ afterRectangle[oppositeAxis] + afterRectangle[oppositeSide]
3103
3118
  ];
3104
- const sortArr = verticalY.sort((a, b) => a - b);
3105
- const y = (sortArr[1] + sortArr[2]) / 2;
3106
- const line = [
3107
- [leftRectangle.x + leftRectangle.width + 2, y],
3108
- [rightRectangle.x - 2, y]
3109
- ];
3110
- return line;
3119
+ const sortArr = align.sort((a, b) => a - b);
3120
+ const average = (sortArr[1] + sortArr[2]) / 2;
3121
+ const offset = 3;
3122
+ return isHorizontal
3123
+ ? [
3124
+ [beforeRectangle.x + beforeRectangle.width + offset, average],
3125
+ [afterRectangle.x - offset, average]
3126
+ ]
3127
+ : [
3128
+ [average, beforeRectangle.y + beforeRectangle.height + offset],
3129
+ [average, afterRectangle.y - offset]
3130
+ ];
3111
3131
  }
3112
- return { deltaX, distributeLines };
3132
+ return { delta, distributeLines };
3113
3133
  }
3114
3134
  drawAlignLines(lines, g) {
3115
3135
  lines.forEach(points => {
@@ -3124,32 +3144,43 @@ class ReactionManager {
3124
3144
  });
3125
3145
  }
3126
3146
  drawDistributeLines(lines, g) {
3127
- lines.forEach(points => {
3128
- if (!points.length)
3147
+ lines.forEach(line => {
3148
+ if (!line.length)
3129
3149
  return;
3130
- if (points[0][1] === points[1][1]) {
3131
- const yAlign = PlaitBoard.getRoughSVG(this.board).line(points[0][0], points[0][1], points[1][0], points[1][1], {
3132
- stroke: SELECTION_BORDER_COLOR,
3133
- strokeWidth: 1
3134
- });
3135
- const bar1 = PlaitBoard.getRoughSVG(this.board).line(points[0][0], points[0][1] - 4, points[0][0], points[1][1] + 4, {
3136
- stroke: SELECTION_BORDER_COLOR,
3137
- strokeWidth: 1
3138
- });
3139
- const bar2 = PlaitBoard.getRoughSVG(this.board).line(points[1][0], points[0][1] - 4, points[1][0], points[1][1] + 4, {
3150
+ let isHorizontal = line[0][1] === line[1][1];
3151
+ const yAlign = PlaitBoard.getRoughSVG(this.board).line(line[0][0], line[0][1], line[1][0], line[1][1], {
3152
+ stroke: SELECTION_BORDER_COLOR,
3153
+ strokeWidth: 1
3154
+ });
3155
+ g.appendChild(yAlign);
3156
+ line.forEach(point => {
3157
+ const barPoint = getBarPoint(point, isHorizontal);
3158
+ const bar = PlaitBoard.getRoughSVG(this.board).line(barPoint[0][0], barPoint[0][1], barPoint[1][0], barPoint[1][1], {
3140
3159
  stroke: SELECTION_BORDER_COLOR,
3141
3160
  strokeWidth: 1
3142
3161
  });
3143
- g.appendChild(yAlign);
3144
- g.appendChild(bar1);
3145
- g.appendChild(bar2);
3146
- }
3162
+ g.appendChild(bar);
3163
+ });
3147
3164
  });
3148
3165
  }
3149
3166
  }
3150
3167
  function isHorizontalCross(rectangle, other) {
3151
3168
  return !(rectangle.y + rectangle.height < other.y || rectangle.y > other.y + other.height);
3152
3169
  }
3170
+ function isVerticalCross(rectangle, other) {
3171
+ return !(rectangle.x + rectangle.width < other.x || rectangle.x > other.x + other.width);
3172
+ }
3173
+ function getBarPoint(point, isHorizontal) {
3174
+ return isHorizontal
3175
+ ? [
3176
+ [point[0], point[1] - 4],
3177
+ [point[0], point[1] + 4]
3178
+ ]
3179
+ : [
3180
+ [point[0] - 4, point[1]],
3181
+ [point[0] + 4, point[1]]
3182
+ ];
3183
+ }
3153
3184
 
3154
3185
  function withMoving(board) {
3155
3186
  const { pointerDown, pointerMove, globalPointerUp, globalPointerMove } = board;
@@ -3190,8 +3221,8 @@ function withMoving(board) {
3190
3221
  const endPoint = transformPoint(board, toPoint(event.x, event.y, host));
3191
3222
  offsetX = endPoint[0] - startPoint[0];
3192
3223
  offsetY = endPoint[1] - startPoint[1];
3193
- const offsetBuffer = 5;
3194
- if (Math.abs(offsetX) > offsetBuffer || Math.abs(offsetY) > offsetBuffer || getMovingElements(board).length > 0) {
3224
+ const tolerance = 5;
3225
+ if (Math.abs(offsetX) > tolerance || Math.abs(offsetY) > tolerance || getMovingElements(board).length > 0) {
3195
3226
  throttleRAF(() => {
3196
3227
  const activeElementsRectangle = getRectangleByElements(board, activeElements, true);
3197
3228
  activeElementsRectangle.x += offsetX;