Skip to content
Open
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
32 changes: 32 additions & 0 deletions src/test/system/list_formatting_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
30 changes: 24 additions & 6 deletions src/trix/models/composition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
blshkv marked this conversation as resolved.
}

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() {
Expand Down