refactor(editor): remove assertExists (#10615)

This commit is contained in:
Saul-Mirone
2025-03-05 00:13:08 +00:00
parent a6692f70aa
commit b8ecfbdae6
106 changed files with 863 additions and 517 deletions

View File

@@ -12,7 +12,6 @@ import {
toDegree,
toRadian,
} from '@blocksuite/global/gfx';
import { assertExists } from '@blocksuite/global/utils';
import { describe, expect, it } from 'vitest';
describe('Line', () => {
@@ -43,7 +42,7 @@ describe('Line', () => {
[0, 1],
[0, -1],
];
assertExists(rst);
if (!rst) throw new Error('Failed to get line ellipse intersects');
expect(
rst.every((point, index) => pointAlmostEqual(point, expected[index]))
).toBeTruthy();
@@ -76,7 +75,7 @@ describe('Line', () => {
[0, 10],
]
);
assertExists(rst);
if (!rst) throw new Error('Failed to get line polygon intersects');
expect(pointAlmostEqual(rst[0], [10, 5])).toBeTruthy();
});

View File

@@ -12,6 +12,7 @@ import type {
GfxLocalElementModel,
GfxModel,
} from '@blocksuite/block-std/gfx';
import { BlockSuiteError } from '@blocksuite/global/exceptions';
import type { IBound, IVec, IVec3 } from '@blocksuite/global/gfx';
import {
almostEqual,
@@ -31,12 +32,7 @@ import {
toRadian,
Vec,
} from '@blocksuite/global/gfx';
import {
assertEquals,
assertExists,
assertType,
last,
} from '@blocksuite/global/utils';
import { assertEquals, assertType, last } from '@blocksuite/global/utils';
import { effect } from '@preact/signals-core';
import { Overlay } from '../renderer/overlay.js';
@@ -153,7 +149,10 @@ export function getAnchors(ele: GfxModel) {
)
.forEach(vec => {
const rst = ele.getLineIntersections(bound.center as IVec, vec as IVec);
assertExists(rst);
if (!rst) {
console.error(`Failed to get line intersections for ${ele.id}`);
return;
}
const originPoint = getPointFromBoundsWithRotation(
{ ...bound, rotate: -rotate },
rst[0]
@@ -497,9 +496,7 @@ function getConnectablePoints(
pushOuterPoints(points, expandStartBound, expandEndBound, outerBound);
}
if (startBound && endBound) {
assertExists(expandStartBound);
assertExists(expandEndBound);
if (startBound && endBound && expandStartBound && expandEndBound) {
pushGapMidPoint(
points,
startPoint,
@@ -564,8 +561,12 @@ function getConnectablePoints(
almostEqual(item[1], point[1], 0.02)
);
}) as IVec3[];
assertExists(startEnds[0]);
assertExists(startEnds[1]);
if (!startEnds[0] || !startEnds[1]) {
throw new BlockSuiteError(
BlockSuiteError.ErrorCode.ValueNotExists,
'Failed to get start and end points when getting connectable points'
);
}
return { points, nextStartPoint: startEnds[0], lastEndPoint: startEnds[1] };
}
@@ -709,7 +710,12 @@ function getNextPoint(
result,
[bound.maxX + 10, result[1]]
);
assertExists(intersects);
if (!intersects) {
throw new BlockSuiteError(
BlockSuiteError.ErrorCode.ValueNotExists,
'Failed to get line intersections for getNextPoint'
);
}
result[0] = intersects[0] + offsetX;
} else {
const intersects = lineIntersects(
@@ -718,7 +724,12 @@ function getNextPoint(
result,
[bound.x - 10, result[1]]
);
assertExists(intersects);
if (!intersects) {
throw new BlockSuiteError(
BlockSuiteError.ErrorCode.ValueNotExists,
'Failed to get line intersections for getNextPoint'
);
}
result[0] = intersects[0] - offsetX;
}
} else {
@@ -729,7 +740,12 @@ function getNextPoint(
result,
[result[0], bound.maxY + 10]
);
assertExists(intersects);
if (!intersects) {
throw new BlockSuiteError(
BlockSuiteError.ErrorCode.ValueNotExists,
'Failed to get line intersections for getNextPoint'
);
}
result[1] = intersects[1] + offsetY;
} else {
const intersects = lineIntersects(
@@ -738,7 +754,12 @@ function getNextPoint(
result,
[result[0], bound.y - 10]
);
assertExists(intersects);
if (!intersects) {
throw new BlockSuiteError(
BlockSuiteError.ErrorCode.ValueNotExists,
'Failed to get line intersections for getNextPoint'
);
}
result[1] = intersects[1] - offsetY;
}
}
@@ -1204,9 +1225,14 @@ export class ConnectorPathGenerator extends PathGenerator {
let startPoint: PointLocation | null = null;
let endPoint: PointLocation | null = null;
if (source.id && !source.position && target.id && !target.position) {
assertExists(start);
assertExists(end);
if (
source.id &&
!source.position &&
target.id &&
!target.position &&
start &&
end
) {
const startAnchors = getAnchors(start);
const endAnchors = getAnchors(end);
let minDist = Infinity;

View File

@@ -1,6 +1,5 @@
import type { Bound, IVec3 } from '@blocksuite/global/gfx';
import { almostEqual } from '@blocksuite/global/gfx';
import { assertExists } from '@blocksuite/global/utils';
import { Graph } from './graph.js';
import { PriorityQueue } from './priority-queue.js';
@@ -67,9 +66,10 @@ export class AStarRunner {
const froms = this._cameFrom.get(current);
if (!froms) return result;
const index = nextIndexs.shift();
assertExists(index);
nextIndexs.push(froms.indexs[index]);
current = froms.from[index];
if (index !== undefined && index !== null) {
nextIndexs.push(froms.indexs[index]);
current = froms.from[index];
}
}
return result;
}
@@ -107,7 +107,9 @@ export class AStarRunner {
private _neighbors(cur: IVec3) {
const neighbors = this._graph.neighbors(cur);
const cameFroms = this._cameFrom.get(cur);
assertExists(cameFroms);
if (!cameFroms) {
return [];
}
cameFroms.from.forEach(from => {
const index = neighbors.findIndex(n => pointAlmostEqual(n, from));
@@ -154,17 +156,18 @@ export class AStarRunner {
const curDiagoalCounts = this._diagonalCount.get(current);
const curPointPrioritys = this._pointPriority.get(current);
const cameFroms = this._cameFrom.get(current);
assertExists(curCosts);
assertExists(curDiagoalCounts);
assertExists(curPointPrioritys);
assertExists(cameFroms);
if (!curCosts || !curDiagoalCounts || !curPointPrioritys || !cameFroms) {
continue;
}
const newCosts = curCosts.map(co => co + cost(current, next));
const newDiagonalCounts = curDiagoalCounts.map(
(count, index) =>
count + getDiagonalCount(next, current, cameFroms.from[index])
);
assertExists(next[2]);
if (!next[2]) {
continue;
}
const newPointPrioritys = curPointPrioritys.map(
pointPriority => pointPriority + next[2]
);