Skip to content

Commit

Permalink
fix: paragraph infinite reset selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Flrande committed Dec 19, 2024
1 parent 2213c04 commit 623e717
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
57 changes: 35 additions & 22 deletions packages/affine/block-paragraph/src/paragraph-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
12 changes: 0 additions & 12 deletions packages/affine/model/src/blocks/paragraph/paragraph-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@ export class ParagraphBlockModel extends BlockModel<ParagraphProps> {
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 {
Expand Down

0 comments on commit 623e717

Please sign in to comment.