How to tell if currently inside a list

How can I tell if the current selection or position is inside a list ?

I tried to take code from liftList Item like this:

	function isInList(state) {
   		let {$from, $to} = state.selection
    	let range = $from.blockRange($to, node => node.childCount && node.firstChild.type == schema.nodes.list_item)
    	let notlist=(!range || range.depth < 2 || $from.node(range.depth - 1).type != schema.nodes.list_item);
    	return !notlist

But this does not work even on a first level bullet.

Any help ?

Depends entirely on what you mean by the selection being inside of a list. You could check whether the selection’s $from and $to both have a parent node that’s a list, or, if you want to make sure they are in the same list, scan parent nodes between 0 and $from.sharedDepth($to) only. But there’s various other ways to define this – maybe you want them to be in the same list item, etc.

Sorry I wasn’t clear…

I have a menu item that I want to be different (have a different title) depends on whether the current position or selection is or is not in a list. So I don’t care if the selection is in the same list etc. The more important case is when there is no selection at all, otherwise it’s enough for me to know if the selection start is a list.

I tried your suggestion, if I have an empty editor and just insert a bullet list then do:

{$from, $to} = state.selection
fromParentNodeType=$from.parent.type, toParentNodeType=$to.parent.type

then both fromParentNodeType and toParentNodeType are paragraphs, not lists

Any help ?


Look higher up the tree (lower depths) using the .node method on resolved positions.