mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-16 12:14:14 +02:00
Add node.isNodeInRange
(#2401)
* Add `node.isNodeInRange` * Add tests for `node.isNodeInRange`
This commit is contained in:
committed by
Ian Storm Taylor
parent
13107b0c43
commit
4df88cfaeb
@@ -364,3 +364,10 @@ Check whether the node has a descendant node by `path` or `key`.
|
||||
`hasNode(key: String) => Boolean`
|
||||
|
||||
Check whether a node exists in the tree by `path` or `key`.
|
||||
|
||||
### `isNodeInRange`
|
||||
|
||||
`isNodeInRange(path: List|Array) => Boolean`
|
||||
`isNodeInRange(key: String) => Boolean`
|
||||
|
||||
Check whether a node is inside a `range`. This will return true for all [`Text`](./text.md) nodes inside the range and all ancestors of those [`Text`](./text.md) nodes up to this node.
|
||||
|
@@ -1636,6 +1636,31 @@ class ElementInterface {
|
||||
return object === 'inline' && first.object !== 'inline'
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a descendant node is inside a range. This will return true for all
|
||||
* text nodes inside the range and all ancestors of those text nodes up to this node.
|
||||
*
|
||||
* @param {List|Key} path
|
||||
* @param {Range} range
|
||||
* @return {Node}
|
||||
*/
|
||||
|
||||
isNodeInRange(path, range) {
|
||||
this.assertDescendant(path)
|
||||
path = this.resolvePath(path)
|
||||
range = this.resolveRange(range)
|
||||
if (range.isUnset) return false
|
||||
|
||||
const toStart = PathUtils.compare(path, range.start.path)
|
||||
const toEnd =
|
||||
range.start.key === range.end.key
|
||||
? toStart
|
||||
: PathUtils.compare(path, range.end.path)
|
||||
|
||||
const is = toStart !== -1 && toEnd !== 1
|
||||
return is
|
||||
}
|
||||
|
||||
/**
|
||||
* Map all child nodes, updating them in their parents. This method is
|
||||
* optimized to not return a new node if no changes are made.
|
||||
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange('a', selection)
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange([0], selection)
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,38 @@
|
||||
/** @jsx h */
|
||||
|
||||
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>
|
||||
<paragraph key="k">
|
||||
<text key="l">five</text>
|
||||
</paragraph>
|
||||
</document>
|
||||
</value>
|
||||
)
|
||||
|
||||
export default function({ document, selection }) {
|
||||
return document.isNodeInRange('k', selection)
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,38 @@
|
||||
/** @jsx h */
|
||||
|
||||
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>
|
||||
<paragraph key="k">
|
||||
<text key="l">five</text>
|
||||
</paragraph>
|
||||
</document>
|
||||
</value>
|
||||
)
|
||||
|
||||
export default function({ document, selection }) {
|
||||
return document.isNodeInRange([4], selection)
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange('c', selection)
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange([1], selection)
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange('d', selection)
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange([1, 0], selection)
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange('g', selection)
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange([3], selection)
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange('j', selection)
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange([3, 1], selection)
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange('b', selection)
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange([0, 0], selection)
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,38 @@
|
||||
/** @jsx h */
|
||||
|
||||
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>
|
||||
<paragraph key="k">
|
||||
<text key="l">five</text>
|
||||
</paragraph>
|
||||
</document>
|
||||
</value>
|
||||
)
|
||||
|
||||
export default function({ document, selection }) {
|
||||
return document.isNodeInRange('l', selection)
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,38 @@
|
||||
/** @jsx h */
|
||||
|
||||
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>
|
||||
<paragraph key="k">
|
||||
<text key="l">five</text>
|
||||
</paragraph>
|
||||
</document>
|
||||
</value>
|
||||
)
|
||||
|
||||
export default function({ document, selection }) {
|
||||
return document.isNodeInRange([4, 0], selection)
|
||||
}
|
||||
|
||||
export const output = false
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange('f', selection)
|
||||
}
|
||||
|
||||
export const output = true
|
@@ -0,0 +1,35 @@
|
||||
/** @jsx h */
|
||||
|
||||
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.isNodeInRange([2, 0], selection)
|
||||
}
|
||||
|
||||
export const output = true
|
Reference in New Issue
Block a user