-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
@@ -31,6 +40,21 @@ export const insertFragmentList = (editor: PlateEditor) => { | |||
return [Node.get(root, ancestor), ancestor]; | |||
}; | |||
|
|||
const findListItemsWithContent = <T extends TDescendant>(first: T): T[] => { |
There was a problem hiding this comment.
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: '' }]); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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, { |
There was a problem hiding this comment.
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.
d19567e
to
2cb28c7
Compare
…f directly manipulating the children
bdb3f53
to
5efbb5e
Compare
editor.normalizeNode would do one pass through the normalizers, which is not what the editor does.
test |
Description
Issue
Fixes:
Example
Checklist
yarn test
.yarn lint
. (Fix errors withyarn lint --fix
.)yarn docs
.)