A Slate plugin to handle keyboard events in lists. List items can contain blocks.
Demo: gitbookio.github.io/slate-edit-list/
npm install slate-edit-list
Natural keybindings:
- Pressing Enter insert a new list item
- Pressing Shift+Enter split the block in the list item
- Pressing Tab increase the depth of the item (creates a sub-list)
- Pressing Shift+Tab decrease the depth of the item
- Pressing Delete (OSX) or Backspace at the start, remove the list item (or the list)
Simple validation/normalization (see assumptions about the schema):
- Lists can contain only list items, and at least one.
- List items can only be the direct children of a list.
- List items must always contain blocks.
Useful transforms: see Utilities and Transform.
import EditList from 'slate-edit-list'
const plugins = [
EditList()
]
This plugin accepts options to redefine the following block types:
types: string = ["ol_list", "ul_list"]
— the array of possible types for list containers. First value will be used as default.typeItem: string = "list_item"
— type for list items.typeDefault: string = "paragraph"
— type for default block in list items.canMerge: (Node, Node) => boolean
— controls which list can be merged automatically (for example when they are adjacent). Defaults to merging list with identical types
You can use this plugins with custom list block types (using plugin arguments). But your lists structure should still conform to a few rules. These rules are implemented as schema.
Here is what a minimal list would look like:
nodes:
- kind: block
type: ul_list # Default type for bulleted lists container
nodes:
- kind: block
type: list_item # List containers can only contain list items
nodes:
# List items contain blocks. They cannot be the direct container of text.
- kind: block
type: paragraph # Default type of blocks in a list item
nodes:
- kind: text
text: Hello World
And here is an example of a multi-level list:
nodes:
- kind: block
type: ol_list
nodes:
- kind: block
type: list_item
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: Item 1
- kind: block
type: ol_list
nodes:
- kind: block
type: list_item
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: Item 1.1
- kind: block
type: list_item
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: Item 1.2
slate-edit-list
exports utilities and transforms:
Return true if selection is inside a list (and it can be unwrap). Optional param type
can be supplied to deduce whether list is of specified type.
Return true if the node is one of the list type.
Returns the depth of the current item (or the depth of the given block) in a list. 0 means not in a list.
Returns the current item at selection (or at the given block).
Returns the current list at selection (or at the given block).
Return the list of items at the given range. The returned items are the highest list of of successive items that cover the given range.
The returned list is empty if no such list can be found.
Increase the depth of the current item.
Decrease the depth of the current item.
Wrap the current blocks in list items of a list container of the given type. You can pass optional data for the created list container.
Unwrap all items at range from their list.
Split current block into a new list item.