Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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" ])
Comment thread
blshkv marked this conversation as resolved.
Outdated
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
22 changes: 16 additions & 6 deletions src/trix/models/composition.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,25 @@ 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
let document = this.document
for (let i = locationRange[0].index; i <= locationRange[1].index; i++) {
Comment thread
blshkv marked this conversation as resolved.
Outdated
const block = document.getBlockAtIndex(i)
if (block) document = document.replaceBlock(block, block.decreaseNestingLevel())
}
Comment thread
blshkv marked this conversation as resolved.
Outdated
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
let document = this.document
for (let i = locationRange[0].index; i <= locationRange[1].index; i++) {
const block = document.getBlockAtIndex(i)
if (block) document = document.replaceBlock(block, block.increaseNestingLevel())
}
return this.setDocument(document)
}

canDecreaseBlockAttributeLevel() {
Expand Down