fix: Pasting list with inline formats#65
Closed
bettysteger wants to merge 1 commit intoeditor-js:mainfrom
Closed
Conversation
Contributor
|
Please, describe your change with more details |
Author
|
When pasting the following tags: the b-tag is removed in the data: using |
|
This also happens with inline links, and confirmed this also fixes those! |
|
I also ran into this same exact issue, but also the issue faced in #63. Here is a monkey patch of this PR and that PR, plus with some additional bug fixes on top. Would be great to incorporate this in this PR or another to see these fixes made upstream. Thanks and hopefully this is helpful to others! import NestedList from "@editorjs/nested-list";
// This is a patched version of the NestedList plugin from EditorJS.
export default class PatchedNestedList extends NestedList {
pasteHandler(element) {
const { tagName: tag } = element;
let style;
let tagToSearch;
// Find list style we're working with and tag to search based on the pasted input.
switch (tag) {
case "OL":
style = "ordered";
tagToSearch = "ol";
break;
case "UL":
case "LI":
style = "unordered";
tagToSearch = "ul";
}
const getPastedItems = (parent) => {
let items = [];
// Get all of the list items (`li`), or nested lists (`ul`/`ol`) that are siblings.
// Technically, nested lists should be within the list items, but
// it seems both variants exist in the wild when copy/pasting.
const children = Array.from(
parent.querySelectorAll(`:scope > li, :scope > ${tagToSearch}`)
);
children.map((childItem) => {
if (childItem.tagName === tag) {
// Recursively handle a nested list (`ul`/`ol`). When a sibling nested list is found,
// it is appended to the previous list item to nest properly.
const previousListItem = items[items.length - 1];
previousListItem.items = getPastedItems(childItem);
} else {
// Handle a list item (`li`)
items = items.concat(getListItem(childItem));
}
});
return items;
};
const getListItem = (list) => {
// Also support properly formatted nested lists (`ul`/`ol`) within a list item (`li`).
const nestedListGroups = Array.from(
list.querySelectorAll(`:scope > ${tagToSearch}`)
);
// Include all of the `innerHTML` of the list instead of only `textContent`.
// Then allow Editor.js to sanitize the content as needed.
let content = list?.innerHTML || "";
// However, we don't want any of the nested list groups to be included,
// so strip out all of their HTML from the content.
nestedListGroups.forEach((nestedListGroup) => {
content = content.replace(nestedListGroup.outerHTML, "");
});
return {
content,
items: getNestedListGroups(nestedListGroups),
};
};
const getNestedListGroups = (groups) => {
let lists = [];
groups.map((nestedListGroup) => {
lists = lists.concat(getPastedItems(nestedListGroup));
});
return lists;
};
return {
style,
items: getPastedItems(element),
};
}
} |
Author
|
d'oh, yes 🤦 i meant #63 (updated) |
|
@skovy Any updates on this being merged? |
Contributor
|
This PR is outdated since List 2.0.0 release |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#26