fix(markdown-widget): apply list item style on each block in a selection (#5676)

This commit is contained in:
Trang Le 2021-08-04 21:54:52 +07:00 committed by GitHub
parent d930fb422a
commit 04e53054ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 8 deletions

View File

@ -164,6 +164,46 @@ describe('Markdown widget', () => {
`);
});
it('wrap each bottom-most block in a selection with a list item block', () => {
cy.focused()
.type('foo')
.enter()
.type('bar')
.enter()
.type('baz')
.setSelection('foo', 'baz')
.clickUnorderedListButton()
.confirmMarkdownEditorContent(`
<ul>
<li>
<p>foo</p>
</li>
<li>
<p>bar</p>
</li>
<li>
<p>baz</p>
</li>
</ul>
`)
})
it('unwraps list item block from each selected list item and unwraps all of them from the outer list block', () => {
cy.clickUnorderedListButton()
.type('foo')
.enter()
.type('bar')
.enter()
.type('baz')
.setSelection('foo', 'baz')
.clickUnorderedListButton()
.confirmMarkdownEditorContent(`
<p>foo</p>
<p>bar</p>
<p>baz</p>
`)
})
it('combines adjacent same-typed lists, not differently typed lists', () => {
cy.focused()
.type('foo')
@ -321,6 +361,8 @@ describe('Markdown widget', () => {
</li>
<li>
<p>bar</p>
</li>
<li>
<p>baz</p>
</li>
</ul>
@ -334,14 +376,12 @@ describe('Markdown widget', () => {
</li>
<li>
<p>bar</p>
<ul>
<li>
<p>baz</p>
</li>
</ul>
</li>
</ul>
<p>baz</p>
`)
.clickUnorderedListButton()
.tabkey()
.setCursorAfter('baz')
.enter()
.tabkey()

View File

@ -152,7 +152,7 @@ function ListPlugin({ defaultType, unorderedListType, orderedListType }) {
if (!LIST_TYPES.includes(type)) {
throw Error(`${type} is not a valid list type, must be one of: ${LIST_TYPES}`);
}
const { startBlock } = editor.value;
const { startBlock, blocks } = editor.value;
const target = editor.getBlockContainer();
switch (get(target, 'type')) {
@ -191,7 +191,18 @@ function ListPlugin({ defaultType, unorderedListType, orderedListType }) {
}
default: {
editor.wrapInList(type);
if (blocks.size > 1) {
const listItems = blocks.map(block =>
Block.create({ type: 'list-item', nodes: [block] }),
);
const listBlock = Block.create({ type, nodes: listItems });
editor
.delete()
.replaceNodeByKey(startBlock.key, listBlock)
.moveToRangeOfNode(listBlock);
} else {
editor.wrapInList(type);
}
break;
}
}