-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(editor): query methods in edgeless api (#9407)
- Loading branch information
1 parent
dc92d78
commit 1e4b180
Showing
35 changed files
with
296 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { FrameBlockModel, GroupElementModel } from '@blocksuite/affine-model'; | ||
import type { GfxBlockElementModel } from '@blocksuite/block-std/gfx'; | ||
import { | ||
deserializeXYWH, | ||
getQuadBoundWithRotation, | ||
} from '@blocksuite/global/utils'; | ||
import type { BlockModel } from '@blocksuite/store'; | ||
|
||
export function getSelectedRect(selected: BlockSuite.EdgelessModel[]): DOMRect { | ||
if (selected.length === 0) { | ||
return new DOMRect(); | ||
} | ||
|
||
const lockedElementsByFrame = selected | ||
.map(selectable => { | ||
if (selectable instanceof FrameBlockModel && selectable.isLocked()) { | ||
return selectable.descendantElements; | ||
} | ||
return []; | ||
}) | ||
.flat(); | ||
|
||
selected = [...new Set([...selected, ...lockedElementsByFrame])]; | ||
|
||
if (selected.length === 1) { | ||
const [x, y, w, h] = deserializeXYWH(selected[0].xywh); | ||
return new DOMRect(x, y, w, h); | ||
} | ||
|
||
return getElementsWithoutGroup(selected).reduce( | ||
(bounds, selectable, index) => { | ||
const rotate = isTopLevelBlock(selectable) ? 0 : selectable.rotate; | ||
const [x, y, w, h] = deserializeXYWH(selectable.xywh); | ||
let { left, top, right, bottom } = getQuadBoundWithRotation({ | ||
x, | ||
y, | ||
w, | ||
h, | ||
rotate, | ||
}); | ||
|
||
if (index !== 0) { | ||
left = Math.min(left, bounds.left); | ||
top = Math.min(top, bounds.top); | ||
right = Math.max(right, bounds.right); | ||
bottom = Math.max(bottom, bounds.bottom); | ||
} | ||
|
||
bounds.x = left; | ||
bounds.y = top; | ||
bounds.width = right - left; | ||
bounds.height = bottom - top; | ||
|
||
return bounds; | ||
}, | ||
new DOMRect() | ||
); | ||
} | ||
|
||
export function getElementsWithoutGroup(elements: BlockSuite.EdgelessModel[]) { | ||
const set = new Set<BlockSuite.EdgelessModel>(); | ||
|
||
elements.forEach(element => { | ||
if (element instanceof GroupElementModel) { | ||
element.descendantElements | ||
.filter(descendant => !(descendant instanceof GroupElementModel)) | ||
.forEach(descendant => set.add(descendant)); | ||
} else { | ||
set.add(element); | ||
} | ||
}); | ||
return Array.from(set); | ||
} | ||
|
||
export function isTopLevelBlock( | ||
selectable: BlockModel | BlockSuite.EdgelessModel | null | ||
): selectable is GfxBlockElementModel { | ||
return !!selectable && 'flavour' in selectable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.