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

fix: Pasting list with inline formats #65

Closed
wants to merge 1 commit into from

Conversation

bettysteger
Copy link

#26

@neSpecc
Copy link
Contributor

neSpecc commented Nov 30, 2023

Please, describe your change with more details

@bettysteger
Copy link
Author

bettysteger commented Nov 30, 2023

@neSpecc

When pasting the following tags:

<ul>
  <li><b>Bold text</b> non bold</li>
 </ul>

the b-tag is removed in the data:

{ 
  style: 'unordered',
  items: [{ 
    content: 'Bold text non bold', 
    items: [] 
  }]
}

using innerHTML (and removing a nested ul/ol) instead of textContent for content solves this problem!

@skovy
Copy link

skovy commented Mar 26, 2024

This also happens with inline links, and confirmed this also fixes those!

@skovy
Copy link

skovy commented Mar 28, 2024

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),
    };
  }
}

@bettysteger
Copy link
Author

@skovy i think you linked the wrong PR #25 ?

What are your changes and what are you trying to solve?

@skovy
Copy link

skovy commented Mar 28, 2024

d'oh, yes 🤦 i meant #63 (updated)

@CodyPChristian
Copy link

@skovy Any updates on this being merged?

@e11sy
Copy link
Contributor

e11sy commented Nov 21, 2024

This PR is outdated since List 2.0.0 release

@e11sy e11sy closed this Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants