Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: paragraph infinite reset selection #9034

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 36 additions & 23 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 All @@ -189,7 +202,7 @@ export class ParagraphBlockComponent extends CaptionedBlockComponent<
nearestHeading.collapsed &&
!this.doc.readonly
) {
this.model.collapsed = false;
nearestHeading.collapsed = false;
}
}
beforeType = type;
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
Loading