From 367544bcc544192f1e42fb54047a9714e466db44 Mon Sep 17 00:00:00 2001 From: doouding Date: Thu, 27 Mar 2025 13:33:20 +0000 Subject: [PATCH] fix: mindmap connectors disappear (#11234) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes [BS-2868](https://linear.app/affine-design/issue/BS-2868/mindmap-bug-布局四的线不见了) --- .../blocks/block-surface/src/utils/a-star.ts | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/blocksuite/affine/blocks/block-surface/src/utils/a-star.ts b/blocksuite/affine/blocks/block-surface/src/utils/a-star.ts index 4548442c73..eea1e38815 100644 --- a/blocksuite/affine/blocks/block-surface/src/utils/a-star.ts +++ b/blocksuite/affine/blocks/block-surface/src/utils/a-star.ts @@ -1,9 +1,12 @@ -import type { Bound, IVec3 } from '@blocksuite/global/gfx'; -import { almostEqual } from '@blocksuite/global/gfx'; +import { almostEqual, type Bound, type IVec3 } from '@blocksuite/global/gfx'; import { Graph } from './graph.js'; import { PriorityQueue } from './priority-queue.js'; +function isNullish(val: T | null | undefined): val is null | undefined { + return val === null || val === undefined; +} + function cost(point: IVec3, point2: IVec3) { return Math.abs(point[0] - point2[0]) + Math.abs(point[1] - point2[1]); } @@ -66,10 +69,11 @@ export class AStarRunner { const froms = this._cameFrom.get(current); if (!froms) return result; const index = nextIndexs.shift(); - if (index !== undefined && index !== null) { - nextIndexs.push(froms.indexs[index]); - current = froms.from[index]; + if (isNullish(index)) { + break; } + nextIndexs.push(froms.indexs[index]); + current = froms.from[index]; } return result; } @@ -107,16 +111,16 @@ export class AStarRunner { private _neighbors(cur: IVec3) { const neighbors = this._graph.neighbors(cur); const cameFroms = this._cameFrom.get(cur); - if (!cameFroms) { - return []; + + if (cameFroms) { + cameFroms.from.forEach(from => { + const index = neighbors.findIndex(n => pointAlmostEqual(n, from)); + if (index >= 0) { + neighbors.splice(index, 1); + } + }); } - cameFroms.from.forEach(from => { - const index = neighbors.findIndex(n => pointAlmostEqual(n, from)); - if (index >= 0) { - neighbors.splice(index, 1); - } - }); if (cur === this._ep) neighbors.push(this._originalEp); return neighbors; } @@ -165,7 +169,7 @@ export class AStarRunner { (count, index) => count + getDiagonalCount(next, current, cameFroms.from[index]) ); - if (!next[2]) { + if (isNullish(next[2])) { continue; } const newPointPrioritys = curPointPrioritys.map(