Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ exports[`Lift nested lists > Lifts multiple bullet lists 1`] = `
<li>
Nested Bullet List Item 2
</li>
</ul><ul>
</ul>
<ul>
<li>
Nested Bullet List Item 3
</li>
<li>
Nested Bullet List Item 4
</li>
</ul></div></div>
</ul>
</div></div>
<li>
Bullet List Item 2
</li>
Expand All @@ -36,16 +38,17 @@ exports[`Lift nested lists > Lifts multiple bullet lists with content in between
<li>
Nested Bullet List Item 2
</li>
</ul></div></div><div><li>
</ul>
In between content
</li><div data-node-type="blockGroup"><ul>
<ul>
<li>
Nested Bullet List Item 3
</li>
<li>
Nested Bullet List Item 4
</li>
</ul></div></div>
</ul>
</div></div>
<li>
Bullet List Item 2
</li>
Expand All @@ -63,7 +66,8 @@ exports[`Lift nested lists > Lifts nested bullet lists 1`] = `
<li>
Nested Bullet List Item 2
</li>
</ul></div></div>
</ul>
</div></div>
<li>
Bullet List Item 2
</li>
Expand All @@ -81,9 +85,9 @@ exports[`Lift nested lists > Lifts nested bullet lists with content after nested
<li>
Nested Bullet List Item 2
</li>
</ul></div></div><li>
</ul>
More content in list item 1
</li>
</div></div>
<li>
Bullet List Item 2
</li>
Expand Down Expand Up @@ -118,7 +122,8 @@ exports[`Lift nested lists > Lifts nested mixed lists 1`] = `
<li>
Bullet List Item 2
</li>
</ul></div></div>
</ul>
</div></div>
<li>
Numbered List Item 2
</li>
Expand All @@ -136,7 +141,8 @@ exports[`Lift nested lists > Lifts nested numbered lists 1`] = `
<li>
Nested Numbered List Item 2
</li>
</ol></div></div>
</ol>
</div></div>
<li>
Numbered List Item 2
</li>
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/api/parsers/html/util/nestedLists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
});

describe("content after a nested list", () => {
const append = Element.prototype.append;

Check warning on line 168 in packages/core/src/api/parsers/html/util/nestedLists.test.ts

View workflow job for this annotation

GitHub Actions / Build

typescript(unbound-method)

packages/core/src/api/parsers/html/util/nestedLists.test.ts:168:36: Avoid referencing unbound methods which may cause unintentional scoping of `this`.

Check warning on line 168 in packages/core/src/api/parsers/html/util/nestedLists.test.ts

View workflow job for this annotation

GitHub Actions / Build

typescript(unbound-method)

packages/core/src/api/parsers/html/util/nestedLists.test.ts:168:36: Avoid referencing unbound methods which may cause unintentional scoping of `this`.

Check warning on line 168 in packages/core/src/api/parsers/html/util/nestedLists.test.ts

View workflow job for this annotation

GitHub Actions / Build

typescript(unbound-method)

packages/core/src/api/parsers/html/util/nestedLists.test.ts:168:36: Avoid referencing unbound methods which may cause unintentional scoping of `this`.

afterEach(() => {
Element.prototype.append = append;
Expand Down Expand Up @@ -196,9 +196,10 @@
`<ul><li>Item<ul><li>Nested item</li></ul><img src="image.png"></li></ul>`,
);

// The content after the nested list is in a list item of its own.
// The content after the nested list is a child of the list item: it is in
// the block group, after the nested list.
expect(htmlNode.innerHTML).toMatchInlineSnapshot(
`"<ul><div><li>Item</li><div data-node-type="blockGroup"><ul><li>Nested item</li></ul></div></div><li><img src="image.png"></li></ul>"`,
`"<ul><div><li>Item</li><div data-node-type="blockGroup"><ul><li>Nested item</li></ul><img src="image.png"></div></div></ul>"`,
);
expect(inActiveDocument).toEqual([]);
});
Expand Down
142 changes: 113 additions & 29 deletions packages/core/src/api/parsers/html/util/nestedLists.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,76 @@
import { detachedDoc } from "../../../../util/detachedDoc.js";

function getChildIndex(node: Element) {
return Array.prototype.indexOf.call(node.parentElement!.childNodes, node);
function isElement(node: Node): node is Element {
return node.nodeType === 1;
}

function isWhitespaceNode(node: Node) {
return node.nodeType === 3 && !/\S/.test(node.nodeValue || "");
function isList(node: Node) {
return node.nodeName === "UL" || node.nodeName === "OL";
}

// The elements that end a paragraph in the parse. These are the block elements
// of the ProseMirror DOM parser, and the elements that BlockNote parses as a
// block (the last line). The first three lines are a copy of `blockTags`, which
// prosemirror-model does not export:
// https://github.com/ProseMirror/prosemirror-model/blob/master/src/from_dom.ts
const BLOCK_ELEMENTS = [
"address, article, aside, blockquote, body, canvas, dd, div, dl, fieldset",
"figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup",
"hr, li, noscript, ol, output, p, pre, section, table, tfoot, ul",
"audio, details, embed, img, video",
].join(", ");

/** True for a block element, and for an element that has a block in it. */
function isBlock(node: Node) {
return (
isElement(node) &&
(node.matches(BLOCK_ELEMENTS) ||
node.querySelector(BLOCK_ELEMENTS) !== null)
);
}

/**
* True for text and for an element with text. These nodes give no content in
* the parse: white space, a comment (node type 8), and an element without text
* (for example an anchor, or the check box of a check list item).
*/
function hasContent(node: Node) {
return node.nodeType !== 8 && /\S/.test(node.textContent || "");
}

/**
* True for an element that the parse reads as one block with text: a
* paragraph, a heading, a code block or a quote.
*/
function isTextBlock(node: Node) {
return /^(P|H[1-6]|PRE|BLOCKQUOTE)$/.test(node.nodeName) && hasContent(node);
}

/**
* Returns the index of the first child in the nodes of a list item that has a
* nested list. The nodes before it are the own content of the list item: its
* text and inline elements up to the first block, or else that first block, if
* it is a block with text.
*
* If the first block is not a block with text (for example an image, or the
* nested list), the nodes before the nested list stay in the list item. The
* parse of the list item (`getListItemContent`) then finds its content.
*/
function childrenStart(nodes: Node[]) {
const firstBlock = nodes.findIndex(isBlock);
if (nodes.slice(0, firstBlock).some(hasContent)) {
return firstBlock;
}
if (isTextBlock(nodes[firstBlock])) {
return firstBlock + 1;
}
return nodes.findIndex(isList);
}

function createBlockGroup() {
const blockGroup = detachedDoc().createElement("div");
blockGroup.setAttribute("data-node-type", "blockGroup");
return blockGroup;
}

/**
Expand All @@ -29,32 +94,48 @@ function isWhitespaceNode(node: Node) {
* </ul>
* </ul>
*
* A list item with content of its own keeps that content. All the nodes after
* it are the children of the list item. They move out of the list item into a
* block group, in order:
*
* <li>
* <p>item</p>
* <pre>...</pre>
* <ul>...</ul>
* text
* </li>
*
* Into:
* <li><p>item</p></li>
* <div data-node-type="blockGroup">
* <pre>...</pre>
* <ul>...</ul>
* text
* </div>
*
*/
function liftNestedListsToParent(element: HTMLElement) {
element.querySelectorAll("li > ul, li > ol").forEach((list) => {
const index = getChildIndex(list);
const parentListItem = list.parentElement!;
const siblingsAfter = Array.from(parentListItem.childNodes).slice(
index + 1,
);
list.remove();
siblingsAfter.forEach((sibling) => {
sibling.remove();
});

parentListItem.insertAdjacentElement("afterend", list);

siblingsAfter.reverse().forEach((sibling) => {
if (isWhitespaceNode(sibling)) {
return;
}
const siblingContainer = detachedDoc().createElement("li");
siblingContainer.append(sibling);
list.insertAdjacentElement("afterend", siblingContainer);
});
if (parentListItem.childNodes.length === 0) {
parentListItem.remove();
// An earlier list of the same list item moved this list out already.
if (parentListItem.nodeName !== "LI") {
return;
}

const nodes = Array.from(parentListItem.childNodes);
const start = childrenStart(nodes);
const children = nodes.slice(start);

if (start === 0) {
// The list item starts with the nested list. Its nodes take its place,
// and step 2 gives the nested list to the list item before it.
parentListItem.replaceWith(...children);
return;
}

const blockGroup = createBlockGroup();
parentListItem.after(blockGroup);
blockGroup.append(...children);
});
}

Expand All @@ -78,18 +159,21 @@ function liftNestedListsToParent(element: HTMLElement) {
* </div>
* </div>
*
* If step 1 made a block group for the list item, that block group is used.
*
* This resulting format is parsed
*/
function createGroups(element: HTMLElement) {
element.querySelectorAll("li + ul, li + ol").forEach((list) => {
const listItem = list.previousElementSibling as HTMLElement;
const firstChildren =
'li + ul, li + ol, li + div[data-node-type="blockGroup"]';
element.querySelectorAll(firstChildren).forEach((firstChild) => {
const listItem = firstChild.previousElementSibling as HTMLElement;
const blockContainer = detachedDoc().createElement("div");

listItem.insertAdjacentElement("afterend", blockContainer);
blockContainer.append(listItem);

const blockGroup = detachedDoc().createElement("div");
blockGroup.setAttribute("data-node-type", "blockGroup");
const blockGroup = isList(firstChild) ? createBlockGroup() : firstChild;
blockContainer.append(blockGroup);

while (
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/blocks/ListItem/NumberedListItem/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,18 @@ export const createNumberedListItemBlockSpec = createBlockSpec(
parent.tagName === "OL" ||
(parent.tagName === "DIV" && parent.parentElement?.tagName === "OL")
) {
const startIndex = parseInt(parent.getAttribute("start") || "1");
// The parser puts an item with nested blocks into a `div`, together
// with the block group of these blocks (see `createGroups` in
// `nestedLists.ts`). Then that `div` has the place of the item in the
// list.
const item = parent.tagName === "DIV" ? parent : element;
const startIndex = parseInt(
item.parentElement?.getAttribute("start") || "1",
);

const defaultProps = parseDefaultProps(element);

if (element.previousElementSibling || startIndex === 1) {
if (item.previousElementSibling || startIndex === 1) {
return defaultProps;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[
{
"children": [
{
"children": [],
"content": [
{
"styles": {},
"text": "Nested",
"type": "text",
},
],
"id": "2",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "bulletListItem",
},
{
"children": [],
"content": [
{
"styles": {},
"text": "After",
"type": "text",
},
],
"id": "3",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
],
"content": [
{
"styles": {},
"text": "Item 1",
"type": "text",
},
],
"id": "1",
"props": {
"backgroundColor": "default",
"checked": true,
"textAlignment": "left",
"textColor": "default",
},
"type": "checkListItem",
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
},
"type": "checkListItem",
},
],
"content": [],
"id": "1",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "bulletListItem",
},
{
"children": [],
"content": [
{
"styles": {},
"text": "Paragraph",
"type": "text",
"children": [],
"content": [
{
"styles": {},
"text": "Paragraph",
"type": "text",
},
],
"id": "3",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
"textColor": "default",
},
"type": "paragraph",
},
],
"id": "3",
"content": [],
"id": "1",
"props": {
"backgroundColor": "default",
"textAlignment": "left",
Expand Down
Loading
Loading