diff --git a/packages/affine/block-paragraph/src/paragraph-block.ts b/packages/affine/block-paragraph/src/paragraph-block.ts index ca4178f30a2c..95bf18e725ad 100644 --- a/packages/affine/block-paragraph/src/paragraph-block.ts +++ b/packages/affine/block-paragraph/src/paragraph-block.ts @@ -144,39 +144,52 @@ export class ParagraphBlockComponent extends CaptionedBlockComponent< }) ); - // collapsed change effect + this.disposables.add( + effect(() => { + const type = this.model.type$.value; + if (!type.startsWith('h') && this.model.collapsed) { + this.model.collapsed = false; + } + }) + ); + this.disposables.add( effect(() => { const collapsed = this.model.collapsed$.value; this._readonlyCollapsed = collapsed; - if (!collapsed) return; + // reset text selection when selected block is collapsed + if (this.model.type.startsWith('h') && collapsed) { + const collapsedSiblings = this.collapsedSiblings; + const textSelection = this.host.selection.find('text'); + const blockSelections = this.host.selection.filter('block'); - const collapsedSiblings = this.collapsedSiblings; - const textSelection = this.host.selection.find('text'); - const blockSelection = this.host.selection.find('block'); - - if ( - textSelection && - collapsedSiblings.some( - sibling => sibling.id === textSelection.blockId - ) - ) { - this.host.selection.clear(['text']); - } + if ( + textSelection && + collapsedSiblings.some( + sibling => sibling.id === textSelection.blockId + ) + ) { + this.host.selection.clear(['text']); + } - if ( - blockSelection && - collapsedSiblings.some( - sibling => sibling.id === blockSelection.blockId - ) - ) { - this.host.selection.clear(['block']); + if ( + blockSelections.some(selection => + collapsedSiblings.some( + sibling => sibling.id === selection.blockId + ) + ) + ) { + this.host.selection.clear(['block']); + } } }) ); - // type change effect + // > # 123 + // # 456 + // + // we need to update collapsed state of 123 when 456 converted to text let beforeType = this.model.type; this.disposables.add( effect(() => { @@ -189,7 +202,7 @@ export class ParagraphBlockComponent extends CaptionedBlockComponent< nearestHeading.collapsed && !this.doc.readonly ) { - this.model.collapsed = false; + nearestHeading.collapsed = false; } } beforeType = type; diff --git a/packages/affine/model/src/blocks/paragraph/paragraph-model.ts b/packages/affine/model/src/blocks/paragraph/paragraph-model.ts index f8d37b6caaa1..c467c48bcacc 100644 --- a/packages/affine/model/src/blocks/paragraph/paragraph-model.ts +++ b/packages/affine/model/src/blocks/paragraph/paragraph-model.ts @@ -41,18 +41,6 @@ export class ParagraphBlockModel extends BlockModel { override flavour!: 'affine:paragraph'; override text!: Text; - - constructor() { - super(); - - this.propsUpdated.on(({ key }) => { - if (key === 'type') { - if (!this.type.startsWith('h')) { - this.collapsed = false; - } - } - }); - } } declare global {