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

Reintroduce non leaf inline and block accessors to value #2937

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/reference/slate/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Get all of the bottom-most [`Block`](./block.md) node descendants.

`getLeafBlocksAtRange(range: Range) => List`

Get all of the bottom-most [`Block`](./block.md) nodes in a `range`.
Get all of the leaf [`Block`](./block.md) nodes in a `range`.

### `getBlocksByType`

Expand Down Expand Up @@ -186,7 +186,7 @@ Get all of the top-most [`Inline`](./inline.md) nodes in a node.

`getLeafInlinesAtRange(range: Range) => List`

Get all of the bottom-most [`Inline`](./inline.md) nodes in a `range`.
Get all of the leaf [`Inline`](./inline.md) nodes in a `range`.

### `getInlinesByType`

Expand Down
16 changes: 14 additions & 2 deletions docs/reference/slate/value.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ Get a subset of the [`Marks`](./mark.md) that are present in _all_ the character

`List`

Get a list of the lowest-depth [`Block`](./block.md) nodes in the current selection.
Get a list of the [`Block`](./block.md) nodes in the current selection.

### `leafBlocks`

`List`

Get a list of any leaf [`Block`](./block.md) nodes in the current selection.

### `fragment`

Expand All @@ -91,7 +97,13 @@ Get a [`Document`](./document.md) fragment of the current selection.

`List`

Get a list of the lowest-depth [`Inline`](./inline.md) nodes in the current selection.
Get a list of the [`Inline`](./inline.md) nodes in the current selection.

### `leafInlines`

`List`

Get a list of any leaf [`Inline`](./inline.md) nodes in the current selection.

### `texts`

Expand Down
20 changes: 8 additions & 12 deletions packages/slate/src/interfaces/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -1887,12 +1887,10 @@ class ElementInterface {
*/

getBlocksAtRange(range) {
warning(
false,
'As of [email protected] the `node.getBlocksAtRange` method has been renamed to `getLeafBlocksAtRange`.'
)

return this.getLeafBlocksAtRange(range)
const iterable = this.blocks({ range })
const array = Array.from(iterable, ([node]) => node)
const list = List(array)
return list
}

getBlocksAtRangeAsArray(range) {
Expand All @@ -1905,12 +1903,10 @@ class ElementInterface {
}

getInlinesAtRange(range) {
warning(
false,
'As of [email protected] the `node.getInlinesAtRange` method has been renamed to `getLeafInlinesAtRange`.'
)

return this.getLeafInlinesAtRange(range)
const iterable = this.inlines({ range })
const array = Array.from(iterable, ([node]) => node)
const list = List(array)
return list
}

getInlinesAtRangeAsArray(range) {
Expand Down
26 changes: 25 additions & 1 deletion packages/slate/src/models/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,18 @@ class Value extends Record(DEFAULTS) {
*/

get blocks() {
return this.selection.isUnset
? new List()
: this.document.getBlocksAtRange(this.selection)
}

/**
* Get the bottom-most block nodes in the current selection.
*
* @return {List<Block>}
*/

get leafBlocks() {
return this.selection.isUnset
? new List()
: this.document.getLeafBlocksAtRange(this.selection)
Expand All @@ -395,12 +407,24 @@ class Value extends Record(DEFAULTS) {
}

/**
* Get the bottom-most inline nodes in the current selection.
* Get the inline nodes in the current selection.
*
* @return {List<Inline>}
*/

get inlines() {
return this.selection.isUnset
? new List()
: this.document.getInlinesAtRange(this.selection)
}

/**
* Get the bottom-most inline nodes in the current selection.
*
* @return {List<Inline>}
*/

get leafInlines() {
return this.selection.isUnset
? new List()
: this.document.getLeafInlinesAtRange(this.selection)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/** @jsx h */

import { List } from 'immutable'
import h from '../../../helpers/h'

export const input = (
<value>
<document>
<paragraph key="a">
<text key="b">one</text>
</paragraph>
<paragraph key="c">
<text key="d">
tw<anchor />o
</text>
</paragraph>
<image key="e" src="https://example.com/image2.png">
<text key="f" />
</image>
<paragraph key="g">
<inline type="link" key="h">
<text key="i">three</text>
</inline>
<text key="j">
<focus />four
</text>
</paragraph>
</document>
</value>
)

export default function({ document, selection }) {
return document.getBlocksAtRange(selection).map(n => n.key)
}

export const output = List(['c', 'e', 'g'])
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/** @jsx h */

import { List } from 'immutable'
import h from '../../../helpers/h'

export const input = (
<value>
<document>
<quote key="a">
<quote key="b">
<paragraph key="c">
<text key="d">
on<cursor />e
</text>
</paragraph>
</quote>
</quote>
<paragraph key="e">
<text key="f">two</text>
</paragraph>
</document>
</value>
)

export default function({ document, selection }) {
return document.getBlocksAtRange(selection).map(n => n.key)
}

export const output = List(['a', 'b', 'c'])
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** @jsx h */

import { List } from 'immutable'
import h from '../../../helpers/h'

export const input = (
<value>
<document>
<quote key="a">
<quote key="b">
<paragraph key="c">
<text key="d">one</text>
</paragraph>
<paragraph key="e">
<text key="f">two</text>
</paragraph>
</quote>
<quote key="g">
<paragraph key="h">
<text key="i">
three
<cursor />
</text>
</paragraph>
</quote>
</quote>
</document>
</value>
)

export default function({ document, selection }) {
return document.getBlocksAtRange(selection).map(n => n.key)
}

export const output = List(['a', 'g', 'h'])
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/** @jsx h */

import { List } from 'immutable'
import h from '../../../helpers/h'

export const input = (
<value>
<document>
<quote key="a">
<quote key="b">
<paragraph key="c">
<text key="d">one</text>
</paragraph>
<paragraph key="e">
<text key="f">
<cursor />
two
</text>
</paragraph>
</quote>
</quote>
<paragraph key="g">
<text key="h">three</text>
</paragraph>
</document>
</value>
)

export default function({ document, selection }) {
return document.getBlocksAtRange(selection).map(n => n.key)
}

export const output = List(['a', 'b', 'e'])
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/** @jsx h */

import h from '../../../helpers/h'

export const input = (
<value>
<document key="zz">
<paragraph key="a">
<text key="b">
<focus />
one
</text>
</paragraph>
<quote key="c">
<quote key="d">
<paragraph key="e">
<text key="f">two</text>
</paragraph>
</quote>
<quote key="g">
<paragraph key="h">
<text key="i">three</text>
</paragraph>
<paragraph key="j">
<text key="k">
<anchor />
four
</text>
</paragraph>
<paragraph key="l">
<text key="m">five</text>
</paragraph>
</quote>
</quote>
</document>
</value>
)

export default function({ document, selection }) {
return document
.getBlocksAtRange(selection)
.map(n => n.key)
.toArray()
}

export const output = ['a', 'c', 'd', 'e', 'g', 'h', 'j']
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/** @jsx h */

import { List } from 'immutable'
import h from '../../../helpers/h'

export const input = (
<value>
<document>
<quote key="a">
<quote key="b">
<paragraph key="c">
<text key="d">one</text>
</paragraph>
</quote>
<quote key="e">
<paragraph key="f">
<text key="g">
<anchor />two
</text>
</paragraph>
<paragraph key="h">
<text key="i">three</text>
</paragraph>
<paragraph key="j">
<text key="k">
f<focus />our
</text>
</paragraph>
</quote>
</quote>
<paragraph key="l">
<text key="m">five</text>
</paragraph>
</document>
</value>
)

export default function({ document, selection }) {
return document.getBlocksAtRange(selection).map(n => n.key)
}

export const output = List(['a', 'e', 'f', 'h', 'j'])
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/** @jsx h */

import { List } from 'immutable'
import h from '../../../helpers/h'

export const input = (
<value>
<document>
<quote key="a">
<quote key="b">
<paragraph key="c">
<text key="d">
<anchor />one<focus />
</text>
</paragraph>
</quote>
</quote>
<paragraph key="e">
<text key="f">two</text>
</paragraph>
</document>
</value>
)

export default function({ document, selection }) {
return document.getBlocksAtRange(selection).map(n => n.key)
}

export const output = List(['a', 'b', 'c'])
Loading