mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-24 05:48:59 +08:00
55651503df
Closes: [BS-2207](https://linear.app/affine-design/issue/BS-2207/move-root-block-to-affineblock-root)
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import type { SerializedElement } from '@blocksuite/block-std/gfx';
|
|
import { Bound, getBoundWithRotation } from '@blocksuite/global/utils';
|
|
import { type BlockSnapshot, BlockSnapshotSchema } from '@blocksuite/store';
|
|
|
|
export function getBoundFromSerializedElement(element: SerializedElement) {
|
|
return Bound.from(
|
|
getBoundWithRotation({
|
|
...Bound.deserialize(element.xywh),
|
|
rotate: typeof element.rotate === 'number' ? element.rotate : 0,
|
|
})
|
|
);
|
|
}
|
|
|
|
export function getBoundFromGfxBlockSnapshot(snapshot: BlockSnapshot) {
|
|
if (typeof snapshot.props.xywh !== 'string') return null;
|
|
return Bound.deserialize(snapshot.props.xywh);
|
|
}
|
|
|
|
export function edgelessElementsBoundFromRawData(
|
|
elementsRawData: (SerializedElement | BlockSnapshot)[]
|
|
) {
|
|
if (elementsRawData.length === 0) return new Bound();
|
|
|
|
let prev: Bound | null = null;
|
|
|
|
for (const data of elementsRawData) {
|
|
const { data: blockSnapshot } = BlockSnapshotSchema.safeParse(data);
|
|
const bound = blockSnapshot
|
|
? getBoundFromGfxBlockSnapshot(blockSnapshot)
|
|
: getBoundFromSerializedElement(data as SerializedElement);
|
|
|
|
if (!bound) continue;
|
|
if (!prev) prev = bound;
|
|
else prev = prev.unite(bound);
|
|
}
|
|
|
|
return prev ?? new Bound();
|
|
}
|