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

List insert text #1

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open

List insert text #1

wants to merge 15 commits into from

Conversation

alex-vladut
Copy link

Description

Issue

Fixes:

Example

Checklist

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn lint --fix.)
  • The relevant examples still work: (Run examples with yarn docs.)

@@ -31,6 +40,21 @@ export const insertFragmentList = (editor: PlateEditor) => {
return [Node.get(root, ancestor), ancestor];
};

const findListItemsWithContent = <T extends TDescendant>(first: T): T[] => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is added to handle a case when a sublist is selected from a different level, so it will normalize it and move all the LIs to the same level. e.g. let's say from this list

- a
  - b
    - c
    - d
  - e
    - f

the following part is selected:

    - c
    - d
  - e
    - f

With this method the LIs c and d will be at the same level as e so that will be properly inserted at the right level.

if (liEntry) {
const [, liPath] = liEntry;
// delete selection (if necessary) so that it can check if needs to insert into an empty block
Transforms.insertFragment(editor, [{ text: '' }]);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible that the selection spans multiple LIs so there is no way to know at this point if the insert will happen inside an empty LIC. We need to check if the paste is to an empty block in order to decide if we want to update the content of the current block instead of inserting it as a new LI and leaving the empty LI.

nodes = rest;
} else if (isEmptyNode) {
// FIXME: is there a more direct way to set this?
const li = Node.get(editor, liPath) as Element;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic for pasting inside an empty list item is very convoluted, and as I noticed it is not working properly after the update to the latest Slate and Plate version. I'll have to revisit that anyway, so if you have any suggestions what would be a better way to address this case let me know please as I am not very familiar with Slate's API.

One particular case I try to address here is something like this, let's say we want to copy a simple list such as this one (a LI z with a UL consisting of 2 other LIs y and w):

- z
  - y
  - w
- v

This will be pasted into a list such as this:

- a
  - b
  - <paste here>
    - d
    - e

I'm expecting the result of this operation will look like this:

- a
  - b
  - z
    - d
    - e
    - y
    - w
  - v

So, the first node will replace the empty LI and, in addition to that, it should keep the existing children of the empty LI and add the children of the newly pasted fragment. The rest of the LIs will just be inserted as siblings of the (previously) empty LI, just as it is the case now.

firstText = first;
nodes = rest;
}
Transforms.insertFragment(editor, [firstText]); // insert text if needed
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using insertFragment for inserting the element as simple text appended to the current selection.

}
Transforms.insertFragment(editor, [firstText]); // insert text if needed

return Transforms.insertNodes(editor, nodes, {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the LIs will be inserted as siblings of the selected LI, just as it works now.

@nemanja-tosic
Copy link

test

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.

3 participants