diff --git a/src/test/system/list_formatting_test.js b/src/test/system/list_formatting_test.js index 3d3ab7c05..749110548 100644 --- a/src/test/system/list_formatting_test.js +++ b/src/test/system/list_formatting_test.js @@ -96,6 +96,38 @@ testGroup("List formatting", { template: "editor_empty" }, () => { expectDocument("ab\nc\n") }) + test("increasing nesting level applies to all selected items", async () => { + await clickToolbarButton({ attribute: "bullet" }) + await typeCharacters("a\nb\nc") + getSelectionManager().setLocationRange([ + { index: 0, offset: 0 }, + { index: 2, offset: 1 }, + ]) + await clickToolbarButton({ action: "increaseNestingLevel" }) + assert.blockAttributes([ 0, 2 ], [ "bulletList", "bullet", "bulletList", "bullet" ]) + assert.blockAttributes([ 2, 4 ], [ "bulletList", "bullet", "bulletList", "bullet" ]) + assert.blockAttributes([ 4, 6 ], [ "bulletList", "bullet", "bulletList", "bullet" ]) + expectDocument("a\nb\nc\n") + }) + + test("decreasing nesting level applies to all selected items", async () => { + await clickToolbarButton({ attribute: "bullet" }) + await typeCharacters("a\n") + await clickToolbarButton({ action: "increaseNestingLevel" }) + await typeCharacters("b\n") + await clickToolbarButton({ action: "increaseNestingLevel" }) + await typeCharacters("c") + getSelectionManager().setLocationRange([ + { index: 1, offset: 0 }, + { index: 2, offset: 1 }, + ]) + await clickToolbarButton({ action: "decreaseNestingLevel" }) + assert.blockAttributes([ 0, 2 ], [ "bulletList", "bullet" ]) + assert.blockAttributes([ 2, 4 ], [ "bulletList", "bullet" ]) + assert.blockAttributes([ 4, 6 ], [ "bulletList", "bullet", "bulletList", "bullet" ]) + expectDocument("a\nb\nc\n") + }) + test("decreasing list item's level decreases its nested items level too", async () => { await clickToolbarButton({ attribute: "bullet" }) await typeCharacters("a\n") diff --git a/src/trix/models/composition.js b/src/trix/models/composition.js index ad61bb358..1bc760a5d 100644 --- a/src/trix/models/composition.js +++ b/src/trix/models/composition.js @@ -415,15 +415,33 @@ export default class Composition extends BasicObject { } decreaseNestingLevel() { - const block = this.getBlock() - if (!block) return - return this.setDocument(this.document.replaceBlock(block, block.decreaseNestingLevel())) + const locationRange = this.getLocationRange() + if (!locationRange) return + const startIndex = locationRange[0].index + const endIndex = locationRange[1].offset === 0 && locationRange[1].index > startIndex + ? locationRange[1].index - 1 + : locationRange[1].index + let document = this.document + for (let i = startIndex; i <= endIndex; i++) { + const block = document.getBlockAtIndex(i) + if (block) document = document.replaceBlock(block, block.decreaseNestingLevel()) + } + return this.setDocument(document) } increaseNestingLevel() { - const block = this.getBlock() - if (!block) return - return this.setDocument(this.document.replaceBlock(block, block.increaseNestingLevel())) + const locationRange = this.getLocationRange() + if (!locationRange) return + const startIndex = locationRange[0].index + const endIndex = locationRange[1].offset === 0 && locationRange[1].index > startIndex + ? locationRange[1].index - 1 + : locationRange[1].index + let document = this.document + for (let i = startIndex; i <= endIndex; i++) { + const block = document.getBlockAtIndex(i) + if (block) document = document.replaceBlock(block, block.increaseNestingLevel()) + } + return this.setDocument(document) } canDecreaseBlockAttributeLevel() {