Skip to content

Build your personal knowledge base with Trilium Notes

fix(text editor): relocate range selections hidden by collapse

matt wilkie091843d290be
diff --git a/packages/ckeditor5/src/plugins/collapsible_list_items.spec.ts b/packages/ckeditor5/src/plugins/collapsible_list_items.spec.ts
index e0ba2b7168..1ae134ff7f 100644
--- a/packages/ckeditor5/src/plugins/collapsible_list_items.spec.ts
+++ b/packages/ckeditor5/src/plugins/collapsible_list_items.spec.ts
@@ -137,6 +137,24 @@ describe("CollapsibleListItems", () => {
expect(position?.offset).toBe(parentBlock.maxOffset);
});
+ it("moves a selection that ends in a hidden child when collapsing (#11256)", () => {
+ editor.model.change((writer) => {
+ writer.setSelection(writer.createRange(
+ writer.createPositionAt(getBlock(editor, 0), 2),
+ writer.createPositionAt(getBlock(editor, 1), 3)
+ ));
+ });
+
+ editor.execute("toggleListCollapse");
+
+ const parentBlock = getBlock(editor, 0);
+ const selection = editor.model.document.selection;
+ expect(parentBlock.getAttribute(LIST_COLLAPSED_ATTRIBUTE)).toBe(true);
+ expect(selection.isCollapsed).toBe(true);
+ expect(selection.getFirstPosition()?.parent).toBe(parentBlock);
+ expect(selection.getFirstPosition()?.offset).toBe(parentBlock.maxOffset);
+ });
+
it("expands automatically when an item is indented under a collapsed parent", () => {
editor.execute("toggleListCollapse");
setSelectionIn(editor, 3); // "Sibling", still visible at indent 0
diff --git a/packages/ckeditor5/src/plugins/collapsible_list_items.ts b/packages/ckeditor5/src/plugins/collapsible_list_items.ts
index 59198aee0c..6baf1be0cc 100644
--- a/packages/ckeditor5/src/plugins/collapsible_list_items.ts
+++ b/packages/ckeditor5/src/plugins/collapsible_list_items.ts
@@ -486,11 +486,9 @@ function getArrowBlockAtPointer(editor: Editor, data: ViewDocumentDomEventData<M
function toggleCollapsed(editor: Editor, block: ModelElement): void {
const collapse = !block.getAttribute(LIST_COLLAPSED_ATTRIBUTE);
const itemBlocks = getItemBlocks(block);
- const selectionBlock = editor.model.document.selection.getFirstPosition()?.parent;
- const hidesSelection = collapse
- && !!selectionBlock
- && selectionBlock.is("element")
- && getNestedBlocks(block).includes(selectionBlock);
+ const nestedBlocks = getNestedBlocks(block);
+ const hidesSelection = collapse && [ ...editor.model.document.selection.getSelectedBlocks() ]
+ .some((selectedBlock) => nestedBlocks.includes(selectedBlock));
editor.model.change((writer) => {
for (const itemBlock of itemBlocks) {