Skip to content
This repository has been archived by the owner on Feb 21, 2019. It is now read-only.

How to know whether we are at first "li" #79

Open
PrimeObjects opened this issue Aug 11, 2018 · 1 comment
Open

How to know whether we are at first "li" #79

PrimeObjects opened this issue Aug 11, 2018 · 1 comment
Labels

Comments

@PrimeObjects
Copy link

PrimeObjects commented Aug 11, 2018

<button className={inList ? '' : 'disabled'} onClick={() => {this.call(increaseItemDepth)}}>

In the sample code, we can disabled the button by checking inList. If we are at the first "li", we should also disable the button because it will do nothing to increaseItemDepth, how can we know whether we are at the first "li"?

Thanks lot
Gary

@Soreine
Copy link
Contributor

Soreine commented Aug 19, 2018

Here is a dumb solution: check for value.document === value.change.call(increaseItemDepth ...).document.


Or maybe the plugin could expose a function:

// If the given node is inside a list, returns all the ancestor lists as well as the 
// item index sequence in each of these list that leads to the given node.
function getPositionInList(document: Slate.Document, key: string): ?ListPosition

type ListPosition = {
  // The conceptual sequence of item index that leads to the current node
  path: List<number>
  ancestors: List<Slate.Block>
}

So you could

const listPosition = listPlugin.getPositionInList(document, key)
const isFirstItem = listPosition && listPosition.path.last() === 0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants