Files
AFFiNE-Mirror/blocksuite/framework/std/src/selection/surface.ts
T
Saul-Mirone 7eb6b268a6 fix(editor): auto focus between tab switch (#12572)
Closes: BS-2290

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

## Summary by CodeRabbit

- **Bug Fixes**
  - Improved focus behavior when switching between tabs to prevent unwanted automatic focusing of the content-editable area.
  - Enhanced selection clearing to avoid unnecessary blurring when the main editable element is already focused.
  - Refined focus checks in tests to specifically target contenteditable elements, ensuring more accurate validation of focus behavior.
  - Adjusted test assertions for block selection to be less strict and removed redundant blur operations for smoother test execution.
  - Updated toolbar dropdown closing method to use keyboard interaction for better reliability.
- **New Features**
  - Added a recoverable property to selection types, improving selection state management and recovery.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-05-27 13:38:02 +00:00

73 lines
1.7 KiB
TypeScript

import { BaseSelection, SelectionExtension } from '@blocksuite/store';
import z from 'zod';
const SurfaceSelectionSchema = z.object({
blockId: z.string(),
elements: z.array(z.string()),
editing: z.boolean(),
inoperable: z.boolean().optional(),
});
export class SurfaceSelection extends BaseSelection {
static override group = 'gfx';
static override type = 'surface';
static override recoverable = true;
readonly editing: boolean;
readonly elements: string[];
readonly inoperable: boolean;
constructor(
blockId: string,
elements: string[],
editing: boolean,
inoperable = false
) {
super({ blockId });
this.elements = elements;
this.editing = editing;
this.inoperable = inoperable;
}
static override fromJSON(json: Record<string, unknown>): SurfaceSelection {
const { blockId, elements, editing, inoperable } =
SurfaceSelectionSchema.parse(json);
return new SurfaceSelection(blockId, elements, editing, inoperable);
}
override equals(other: BaseSelection): boolean {
if (other instanceof SurfaceSelection) {
return (
this.blockId === other.blockId &&
this.editing === other.editing &&
this.inoperable === other.inoperable &&
this.elements.length === other.elements.length &&
this.elements.every((id, idx) => id === other.elements[idx])
);
}
return false;
}
isEmpty() {
return this.elements.length === 0 && !this.editing;
}
override toJSON(): Record<string, unknown> {
return {
type: 'surface',
blockId: this.blockId,
elements: this.elements,
editing: this.editing,
inoperable: this.inoperable,
};
}
}
export const SurfaceSelectionExtension = SelectionExtension(SurfaceSelection);