mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-30 00:29:46 +08:00
refactor(editor): refactor page note empty checker (#9570)
Close [BS-2320](https://linear.app/affine-design/issue/BS-2320/内容为空的状态判断)
This commit is contained in:
@@ -111,6 +111,17 @@ export class NoteBlockModel
|
||||
if (!this._isSelectable()) return false;
|
||||
return super.intersectsBound(bound);
|
||||
}
|
||||
|
||||
override isEmpty(): boolean {
|
||||
if (this.children.length === 0) return true;
|
||||
if (this.children.length === 1) {
|
||||
const firstChild = this.children[0];
|
||||
if (firstChild.flavour === 'affine:paragraph') {
|
||||
return firstChild.isEmpty();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
|
||||
@@ -41,6 +41,10 @@ export class ParagraphBlockModel extends BlockModel<ParagraphProps> {
|
||||
override flavour!: 'affine:paragraph';
|
||||
|
||||
override text!: Text;
|
||||
|
||||
override isEmpty(): boolean {
|
||||
return this.text$.value.length === 0 && this.children.length === 0;
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
|
||||
@@ -22,6 +22,22 @@ export class RootBlockModel extends BlockModel<RootBlockProps> {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* A page is empty if it only contains one empty note and the canvas is empty
|
||||
*/
|
||||
override isEmpty() {
|
||||
let numNotes = 0;
|
||||
let empty = true;
|
||||
for (const child of this.children) {
|
||||
empty = empty && child.isEmpty();
|
||||
|
||||
if (child.flavour === 'affine:note') numNotes++;
|
||||
if (numNotes > 1) return false;
|
||||
}
|
||||
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
|
||||
export const RootBlockSchema = defineBlockSchema({
|
||||
|
||||
Reference in New Issue
Block a user