Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple pasting formats #63

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@editorjs/nested-list",
"version": "1.4.2",
"version": "1.5.0",
"keywords": [
"codex editor",
"nested-list",
Expand Down
62 changes: 49 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,25 +227,61 @@ export default class NestedList {
items: [],
};

// Pasted Case 1.
// <ul>
// <li>editor</li>
// <ul>
// <li>nested-list</li>
// </ul>
// </ul>
// Pasted Case 2
// <ul>
// <li>
// editor
// <ul>
// <li>nested-list</li>
// </ul>
// </li>
// </ul>
// get pasted items from the html.
const getPastedItems = (parent) => {
let responseData = [];
// get first level li elements.
const children = Array.from(parent.querySelectorAll(`:scope > li`));
const children = Array.from(parent.querySelectorAll(`:scope > li, :scope > ${tagToSearch}`));

children.map((child) => {
if (child.tagName === tag) {
const nestedListGroup = getNestedListGroup(parent);
const previousListItem = responseData.pop();
previousListItem.items = nestedListGroup;
responseData = responseData.concat(previousListItem);
} else {
const listItem = getListItem(child);
responseData = responseData.concat(listItem);
}
});

return children.map((child) => {
// get subitems if they exist.
const subItemsWrapper = child.querySelector(`:scope > ${tagToSearch}`);
// get subitems.
const subItems = subItemsWrapper ? getPastedItems(subItemsWrapper) : [];
// get text content of the li element.
const content = child?.firstChild?.textContent || '';
return responseData;
};

return {
content,
items: subItems,
};
const getListItem = (list) => {
const nestedItems = getNestedListGroup(list);
return {
content: list?.firstChild?.textContent || '',
items: nestedItems
};
}

const getNestedListGroup = (parent) => {
let responseData = [];
const nestedListGroups = Array.from(parent.querySelectorAll(`:scope > ${tagToSearch}`))

nestedListGroups.map((nestedListGroup) => {
responseData = responseData.concat(getPastedItems(nestedListGroup));
});
};

return responseData;
}

// get pasted items.
data.items = getPastedItems(element);
Expand Down
Loading