microboard-temp 0.5.75 → 0.5.77

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/esm/node.js CHANGED
@@ -7686,7 +7686,8 @@ var conf = {
7686
7686
  DECK_VERTICAL_OFFSET: 2,
7687
7687
  CARD_DIMENSIONS: { width: 250, height: 400 },
7688
7688
  DEFAULT_GAME_ITEM_DIMENSIONS: { width: 200, height: 200 },
7689
- MAX_CARD_SIZE: 500
7689
+ MAX_CARD_SIZE: 500,
7690
+ CONNECTOR_ITEM_OFFSET: 20
7690
7691
  };
7691
7692
  initDefaultI18N();
7692
7693
 
@@ -38975,7 +38976,6 @@ function radiansBetweenPoints(point1, point22) {
38975
38976
  }
38976
38977
 
38977
38978
  // src/Items/Connector/getLine/findOrthogonalPath.ts
38978
- var ITEM_OFFSET = 1;
38979
38979
  function getDirection(from, to) {
38980
38980
  if (!to) {
38981
38981
  return null;
@@ -39016,9 +39016,9 @@ function getNeighbors(node2, grid, obstacles) {
39016
39016
  { x: node2.xGrid, y: node2.yGrid + 1 }
39017
39017
  ];
39018
39018
  for (const pos of potentialNeighbors) {
39019
- if (pos.x >= 0 && pos.x < grid.length && pos.y >= 0) {
39019
+ if (pos.x >= 0 && pos.x < grid.length && pos.y >= 0 && grid[pos.x] && grid[pos.x][pos.y]) {
39020
39020
  const newPoint = grid[pos.x][pos.y];
39021
- if (newPoint && !obstacles.some((obstacle) => obstacle.isAlmostInside(newPoint, ITEM_OFFSET - 1))) {
39021
+ if (newPoint && !obstacles.some((obstacle) => obstacle.isAlmostInside(newPoint, conf.CONNECTOR_ITEM_OFFSET - 1))) {
39022
39022
  neighbors.push({
39023
39023
  point: newPoint,
39024
39024
  costSoFar: 0,
@@ -39046,16 +39046,20 @@ function findCenterLine(grid, start, end, middle) {
39046
39046
  }
39047
39047
  if (width > height) {
39048
39048
  const centerIdx = grid.findIndex((row2) => row2[0].x === middlePoint.x || Math.abs(row2[0].x - middlePoint.x) < 0.01);
39049
- for (let y = 0;y < grid[0].length && centerIdx !== -1; y++) {
39050
- if (grid[centerIdx][y] && grid[centerIdx][y].x >= min2.x - 0.01 && grid[centerIdx][y].x <= max2.x + 0.01 && grid[centerIdx][y].y >= min2.y - 0.01 && grid[centerIdx][y].y <= max2.y + 0.01) {
39051
- centerLine.push(grid[centerIdx][y]);
39049
+ if (centerIdx !== -1) {
39050
+ for (let y = 0;y < grid[0].length; y++) {
39051
+ if (grid[centerIdx][y] && grid[centerIdx][y].x >= min2.x - 0.01 && grid[centerIdx][y].x <= max2.x + 0.01 && grid[centerIdx][y].y >= min2.y - 0.01 && grid[centerIdx][y].y <= max2.y + 0.01) {
39052
+ centerLine.push(grid[centerIdx][y]);
39053
+ }
39052
39054
  }
39053
39055
  }
39054
39056
  } else {
39055
39057
  const centerIdx = grid[0].findIndex((point5) => point5.y === middlePoint.y || Math.abs(point5.y - middlePoint.y) < 0.01);
39056
- for (let x = 0;x < grid.length && centerIdx !== -1; x++) {
39057
- if (grid[x][centerIdx].x >= min2.x - 0.01 && grid[x][centerIdx].x <= max2.x + 0.01 && grid[x][centerIdx].y >= min2.y - 0.01 && grid[x][centerIdx].y <= max2.y + 0.01) {
39058
- centerLine.push(grid[x][centerIdx]);
39058
+ if (centerIdx !== -1) {
39059
+ for (let x = 0;x < grid.length; x++) {
39060
+ if (grid[x][centerIdx] && grid[x][centerIdx].x >= min2.x - 0.01 && grid[x][centerIdx].x <= max2.x + 0.01 && grid[x][centerIdx].y >= min2.y - 0.01 && grid[x][centerIdx].y <= max2.y + 0.01) {
39061
+ centerLine.push(grid[x][centerIdx]);
39062
+ }
39059
39063
  }
39060
39064
  }
39061
39065
  }
@@ -39066,69 +39070,24 @@ function createGrid(start, end, toVisitPoints = []) {
39066
39070
  const endDir = getPointerDirection(end);
39067
39071
  const revertMapDir = { top: 0, bottom: 1, right: 2, left: 3 };
39068
39072
  const offsetMap = {
39069
- top: { x: 0, y: -ITEM_OFFSET },
39070
- bottom: { x: 0, y: ITEM_OFFSET },
39071
- right: { x: ITEM_OFFSET, y: 0 },
39072
- left: { x: -ITEM_OFFSET, y: 0 }
39073
+ top: { x: 0, y: -conf.CONNECTOR_ITEM_OFFSET },
39074
+ bottom: { x: 0, y: conf.CONNECTOR_ITEM_OFFSET },
39075
+ right: { x: conf.CONNECTOR_ITEM_OFFSET, y: 0 },
39076
+ left: { x: -conf.CONNECTOR_ITEM_OFFSET, y: 0 }
39073
39077
  };
39074
39078
  const horizontalLines = [];
39075
39079
  const verticalLines = [];
39076
- if (start.pointType !== "Board") {
39077
- const itemMbr = start.item.getMbr();
39078
- verticalLines.push(itemMbr.left - ITEM_OFFSET, itemMbr.left, itemMbr.right, itemMbr.right + ITEM_OFFSET);
39079
- horizontalLines.push(itemMbr.top - ITEM_OFFSET, itemMbr.top, itemMbr.bottom, itemMbr.bottom + ITEM_OFFSET);
39080
- }
39081
- if (end.pointType !== "Board") {
39082
- const itemMbr = end.item.getMbr();
39083
- verticalLines.push(itemMbr.left - ITEM_OFFSET, itemMbr.left, itemMbr.right, itemMbr.right + ITEM_OFFSET);
39084
- horizontalLines.push(itemMbr.top - ITEM_OFFSET, itemMbr.top, itemMbr.bottom, itemMbr.bottom + ITEM_OFFSET);
39085
- }
39086
- const tempStart = start;
39087
- const tempEnd = end;
39088
- const middle = new Point((tempStart.x + tempEnd.x) / 2, (tempStart.y + tempEnd.y) / 2);
39089
- horizontalLines.push(middle.y, tempStart.y, tempEnd.y);
39090
- verticalLines.push(middle.x, tempStart.x, tempEnd.x);
39091
- toVisitPoints.forEach((p3) => {
39092
- horizontalLines.push(p3.y);
39093
- verticalLines.push(p3.x);
39094
- });
39095
- const uniqueHorizontalLines = Array.from(new Set(horizontalLines)).sort((a2, b) => a2 - b);
39096
- const uniqueVerticalLines = Array.from(new Set(verticalLines)).sort((a2, b) => a2 - b);
39097
39080
  let newStart;
39098
39081
  let newEnd;
39099
39082
  const processPoint = (point5, dir2) => {
39100
39083
  const itemMbr = point5.item.getMbr();
39101
- const pointOnMbr = itemMbr.getLines()[revertMapDir[dir2]].getNearestPointOnLineSegment(point5);
39084
+ const mbrFloored = new Mbr(Math.floor(itemMbr.left), Math.floor(itemMbr.top), Math.floor(itemMbr.right), Math.floor(itemMbr.bottom));
39085
+ const pointOnMbr = mbrFloored.getLines()[revertMapDir[dir2]].getNearestPointOnLineSegment(point5);
39102
39086
  const newPoint = Object.create(Object.getPrototypeOf(point5), Object.getOwnPropertyDescriptors(point5));
39103
- if (dir2 === "top") {
39104
- const currentYIndex = uniqueHorizontalLines.findIndex((y) => Math.abs(y - pointOnMbr.y) < 0.01);
39105
- const nextYIndex = currentYIndex - 1;
39106
- if (nextYIndex >= 0) {
39107
- newPoint.y = uniqueHorizontalLines[nextYIndex];
39108
- newPoint.x = pointOnMbr.x;
39109
- }
39110
- } else if (dir2 === "bottom") {
39111
- const currentYIndex = uniqueHorizontalLines.findIndex((y) => Math.abs(y - pointOnMbr.y) < 0.01);
39112
- const nextYIndex = currentYIndex + 1;
39113
- if (nextYIndex < uniqueHorizontalLines.length) {
39114
- newPoint.y = uniqueHorizontalLines[nextYIndex];
39115
- newPoint.x = pointOnMbr.x;
39116
- }
39117
- } else if (dir2 === "left") {
39118
- const currentXIndex = uniqueVerticalLines.findIndex((x) => Math.abs(x - pointOnMbr.x) < 0.01);
39119
- const nextXIndex = currentXIndex - 1;
39120
- if (nextXIndex >= 0) {
39121
- newPoint.x = uniqueVerticalLines[nextXIndex];
39122
- newPoint.y = pointOnMbr.y;
39123
- }
39124
- } else if (dir2 === "right") {
39125
- const currentXIndex = uniqueVerticalLines.findIndex((x) => Math.abs(x - pointOnMbr.x) < 0.01);
39126
- const nextXIndex = currentXIndex + 1;
39127
- if (nextXIndex < uniqueVerticalLines.length) {
39128
- newPoint.x = uniqueVerticalLines[nextXIndex];
39129
- newPoint.y = pointOnMbr.y;
39130
- }
39131
- }
39087
+ newPoint.x = pointOnMbr.x + offsetMap[dir2].x;
39088
+ newPoint.y = pointOnMbr.y + offsetMap[dir2].y;
39089
+ verticalLines.push(mbrFloored.left - conf.CONNECTOR_ITEM_OFFSET, mbrFloored.left, pointOnMbr.x, mbrFloored.right, mbrFloored.right + conf.CONNECTOR_ITEM_OFFSET);
39090
+ horizontalLines.push(mbrFloored.top - conf.CONNECTOR_ITEM_OFFSET, mbrFloored.top, pointOnMbr.y, mbrFloored.bottom, mbrFloored.bottom + conf.CONNECTOR_ITEM_OFFSET);
39132
39091
  return newPoint;
39133
39092
  };
39134
39093
  if (start.pointType !== "Board" && startDir) {
@@ -39137,6 +39096,17 @@ function createGrid(start, end, toVisitPoints = []) {
39137
39096
  if (end.pointType !== "Board" && endDir) {
39138
39097
  newEnd = processPoint(end, endDir);
39139
39098
  }
39099
+ const finalStart = newStart || start;
39100
+ const finalEnd = newEnd || end;
39101
+ const middle = new Point((finalStart.x + finalEnd.x) / 2, (finalStart.y + finalEnd.y) / 2);
39102
+ horizontalLines.push(middle.y, finalStart.y, finalEnd.y);
39103
+ verticalLines.push(middle.x, finalStart.x, finalEnd.x);
39104
+ toVisitPoints.forEach((p3) => {
39105
+ horizontalLines.push(p3.y);
39106
+ verticalLines.push(p3.x);
39107
+ });
39108
+ const uniqueHorizontalLines = Array.from(new Set(horizontalLines)).sort((a2, b) => a2 - b);
39109
+ const uniqueVerticalLines = Array.from(new Set(verticalLines)).sort((a2, b) => a2 - b);
39140
39110
  const grid = uniqueVerticalLines.map((x) => uniqueHorizontalLines.map((y) => new Point(x, y)));
39141
39111
  return {
39142
39112
  grid,
@@ -39145,18 +39115,27 @@ function createGrid(start, end, toVisitPoints = []) {
39145
39115
  middlePoint: middle
39146
39116
  };
39147
39117
  }
39148
- function findPath(start, end, grid, obstacles, newStart, newEnd) {
39149
- const startIdx = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(start)));
39150
- const endIdx = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(end)));
39151
- if (startIdx === -1 || endIdx === -1) {
39152
- throw new Error("Start or end point not found in the grid");
39118
+ function findPath(start, end, grid, obstacles, existingPath, newStart, newEnd) {
39119
+ const startRowIndex = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(start)));
39120
+ if (startRowIndex === -1) {
39121
+ throw new Error("Start point not found in the grid row");
39122
+ }
39123
+ const startPointIndex = grid[startRowIndex].findIndex((point5) => point5.barelyEqual(start));
39124
+ if (startPointIndex === -1) {
39125
+ throw new Error("Start point not found in the grid column");
39126
+ }
39127
+ const endRowIndex = grid.findIndex((row2) => row2.some((point5) => point5.barelyEqual(end)));
39128
+ if (endRowIndex === -1) {
39129
+ throw new Error("End point not found in the grid row");
39130
+ }
39131
+ const endPointIndex = grid[endRowIndex].findIndex((point5) => point5.barelyEqual(end));
39132
+ if (endPointIndex === -1) {
39133
+ throw new Error("End point not found in the grid column");
39153
39134
  }
39154
- const startPointIdx = grid[startIdx].findIndex((point5) => point5.barelyEqual(start));
39155
- const endPointIdx = grid[endIdx].findIndex((point5) => point5.barelyEqual(end));
39156
39135
  const endNode = {
39157
39136
  point: end,
39158
- xGrid: endIdx,
39159
- yGrid: endPointIdx,
39137
+ xGrid: endRowIndex,
39138
+ yGrid: endPointIndex,
39160
39139
  costSoFar: 0,
39161
39140
  heuristic: 0,
39162
39141
  toFinish: 0
@@ -39164,53 +39143,72 @@ function findPath(start, end, grid, obstacles, newStart, newEnd) {
39164
39143
  const startNode = {
39165
39144
  point: start,
39166
39145
  costSoFar: 0,
39167
- heuristic: heuristic({ point: start, xGrid: startIdx, yGrid: startPointIdx }, endNode),
39168
- toFinish: heuristic({ point: start, xGrid: startIdx, yGrid: startPointIdx }, endNode),
39169
- xGrid: startIdx,
39170
- yGrid: startPointIdx
39146
+ heuristic: heuristic({ point: start, xGrid: startRowIndex, yGrid: startPointIndex }, endNode),
39147
+ toFinish: heuristic({ point: start, xGrid: startRowIndex, yGrid: startPointIndex }, endNode),
39148
+ xGrid: startRowIndex,
39149
+ yGrid: startPointIndex
39171
39150
  };
39172
39151
  const openSet = [startNode];
39173
39152
  const closedSet = new Set;
39174
39153
  while (openSet.length > 0) {
39175
39154
  openSet.sort((aa, bb) => aa.toFinish - bb.toFinish);
39176
39155
  const current = openSet.shift();
39156
+ const currentKey = `${current.point.x},${current.point.y}`;
39177
39157
  if (current.point.barelyEqual(end)) {
39178
39158
  const path2 = reconstructPath(current);
39179
39159
  return path2;
39180
39160
  }
39181
- closedSet.add(current.point);
39161
+ closedSet.add(currentKey);
39182
39162
  const neighbors = getNeighbors(current, grid, obstacles);
39183
39163
  for (const neighbor of neighbors) {
39184
- if (closedSet.has(neighbor.point)) {
39164
+ const neighborKey = `${neighbor.point.x},${neighbor.point.y}`;
39165
+ if (closedSet.has(neighborKey) || existingPath.has(neighborKey) && !neighbor.point.barelyEqual(end)) {
39185
39166
  continue;
39186
39167
  }
39187
39168
  const extraCost = isChangingDirection(current, neighbor, newStart, newEnd);
39188
- const tentativeCost = current.costSoFar + 1;
39189
- if (!openSet.some((node2) => node2.point.barelyEqual(neighbor.point) && node2.costSoFar <= tentativeCost)) {
39190
- neighbor.costSoFar = tentativeCost + extraCost;
39191
- neighbor.heuristic = heuristic(neighbor, endNode);
39192
- neighbor.toFinish = neighbor.costSoFar + neighbor.heuristic;
39193
- openSet.push(neighbor);
39169
+ const pathOverlapCost = existingPath.has(neighborKey) ? 1000 : 0;
39170
+ const tentativeCost = current.costSoFar + 1 + pathOverlapCost;
39171
+ let existingNodeInOpenSet = openSet.find((node2) => node2.point.barelyEqual(neighbor.point));
39172
+ if (!existingNodeInOpenSet || tentativeCost < existingNodeInOpenSet.costSoFar) {
39173
+ if (existingNodeInOpenSet) {
39174
+ existingNodeInOpenSet.costSoFar = tentativeCost + extraCost;
39175
+ existingNodeInOpenSet.heuristic = heuristic(neighbor, endNode);
39176
+ existingNodeInOpenSet.toFinish = existingNodeInOpenSet.costSoFar + existingNodeInOpenSet.heuristic;
39177
+ existingNodeInOpenSet.parent = current;
39178
+ } else {
39179
+ neighbor.costSoFar = tentativeCost + extraCost;
39180
+ neighbor.heuristic = heuristic(neighbor, endNode);
39181
+ neighbor.toFinish = neighbor.costSoFar + neighbor.heuristic;
39182
+ openSet.push(neighbor);
39183
+ }
39194
39184
  }
39195
39185
  }
39196
39186
  }
39197
39187
  return;
39198
39188
  }
39199
39189
  function findPathPoints(points, grid, obstacles, newStart, newEnd) {
39200
- const pathPoints = [];
39190
+ const finalPath = [];
39191
+ const existingPathSegments = new Set;
39192
+ if (points.length > 0) {
39193
+ finalPath.push(points[0]);
39194
+ const startKey = `${points[0].x},${points[0].y}`;
39195
+ existingPathSegments.add(startKey);
39196
+ }
39201
39197
  for (let i = 0;i < points.length - 1; i += 1) {
39202
- const segmentPath = findPath(points[i], points[i + 1], grid, obstacles, newStart, newEnd);
39203
- if (segmentPath) {
39204
- pathPoints.push(...segmentPath.slice(0, -1));
39198
+ const segmentPath = findPath(points[i], points[i + 1], grid, obstacles, existingPathSegments, newStart, newEnd);
39199
+ if (segmentPath && segmentPath.length > 0) {
39200
+ for (let j = 1;j < segmentPath.length; j++) {
39201
+ const point5 = segmentPath[j];
39202
+ const key = `${point5.x},${point5.y}`;
39203
+ finalPath.push(point5);
39204
+ existingPathSegments.add(key);
39205
+ }
39205
39206
  } else {
39206
39207
  points.splice(i + 1, 1);
39207
39208
  i--;
39208
39209
  }
39209
39210
  }
39210
- if (pathPoints.length !== 0) {
39211
- pathPoints.push(points[points.length - 1]);
39212
- }
39213
- return pathPoints;
39211
+ return finalPath;
39214
39212
  }
39215
39213
  function reducePoints(points) {
39216
39214
  const uniquePoints = new Map;
@@ -39221,6 +39219,12 @@ function reducePoints(points) {
39221
39219
  if (uniquePoints.has(key)) {
39222
39220
  const loopStartIndex = uniquePoints.get(key);
39223
39221
  result.splice(loopStartIndex + 1);
39222
+ const removedPoints = points.slice(loopStartIndex + 1, i + 1);
39223
+ removedPoints.forEach((p3) => {
39224
+ uniquePoints.delete(`${p3.x},${p3.y}`);
39225
+ });
39226
+ uniquePoints.set(key, result.length);
39227
+ result.push(point5);
39224
39228
  } else {
39225
39229
  uniquePoints.set(key, result.length);
39226
39230
  result.push(point5);
@@ -39230,19 +39234,32 @@ function reducePoints(points) {
39230
39234
  }
39231
39235
  function getLines(pathPoints) {
39232
39236
  const lines = [];
39237
+ if (pathPoints.length < 2) {
39238
+ return [];
39239
+ }
39233
39240
  const reducedPoints = reducePoints(pathPoints);
39241
+ if (reducedPoints.length < 2) {
39242
+ return [];
39243
+ }
39234
39244
  let startPoint = reducedPoints[0];
39235
- for (let i = 1;i < reducedPoints.length - 1; i += 1) {
39245
+ for (let i = 1;i < reducedPoints.length; i += 1) {
39236
39246
  const prevPoint = reducedPoints[i - 1];
39237
39247
  const currPoint = reducedPoints[i];
39238
- const nextPoint = reducedPoints[i + 1];
39239
- if (prevPoint.x !== nextPoint.x && prevPoint.y !== nextPoint.y) {
39248
+ const nextPoint = i + 1 < reducedPoints.length ? reducedPoints[i + 1] : null;
39249
+ if (!nextPoint || prevPoint.x !== nextPoint.x && prevPoint.y !== nextPoint.y) {
39240
39250
  lines.push(new Line(startPoint, currPoint));
39241
39251
  startPoint = currPoint;
39242
39252
  }
39243
39253
  }
39244
- if (lines.length > 0) {
39245
- lines.push(new Line(startPoint, pathPoints[pathPoints.length - 1]));
39254
+ if (lines.length === 0 && reducedPoints.length > 1) {
39255
+ lines.push(new Line(reducedPoints[0], reducedPoints[reducedPoints.length - 1]));
39256
+ } else if (lines.length > 0) {
39257
+ const lastLine = lines[lines.length - 1];
39258
+ const lastPointInLines = lastLine.getEndPoint();
39259
+ const lastPointInReduced = reducedPoints[reducedPoints.length - 1];
39260
+ if (!lastPointInLines.barelyEqual(lastPointInReduced)) {
39261
+ lines.push(new Line(lastPointInLines, lastPointInReduced));
39262
+ }
39246
39263
  }
39247
39264
  return lines;
39248
39265
  }
@@ -241,6 +241,7 @@ export declare const conf: {
241
241
  height: number;
242
242
  };
243
243
  MAX_CARD_SIZE: number;
244
+ CONNECTOR_ITEM_OFFSET: number;
244
245
  };
245
246
  export type Settings = typeof conf;
246
247
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "microboard-temp",
3
- "version": "0.5.75",
3
+ "version": "0.5.77",
4
4
  "description": "A flexible interactive whiteboard library",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",