1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 06:53:25 +02:00

Add property "isEmpty" (#863)

* Add property isEmpty to State

* Update hovering menu example

* Document isEmpty

* Improve perf of isEmpty with @Soreine 's suggestion

* Fix return of isEmpty
This commit is contained in:
Samy Pessé
2017-07-11 22:34:02 +02:00
committed by Ian Storm Taylor
parent 4e01f80b1b
commit 74bf684ec9
3 changed files with 26 additions and 1 deletions

View File

@@ -33,6 +33,7 @@ For convenience, in addition to transforms, many of the [`Selection`](./selectio
- [`isExpanded`](#isExpanded)
- [`isFocused`](#isfocused)
- [`isForward`](#isForward)
- [`isEmpty`](#isEmpty)
- [Static Methods](#static-methods)
- [`State.create`](#statecreate)
- [Methods](#methods)
@@ -153,6 +154,10 @@ Whether the current selection is focused.
Whether the current selection is forward.
### `isEmpty`
`Boolean`
Whether the current selection is empty.
## Static Methods

View File

@@ -179,7 +179,7 @@ class HoveringMenu extends React.Component {
const { menu, state } = this.state
if (!menu) return
if (state.isBlurred || state.isCollapsed) {
if (state.isBlurred || state.isEmpty) {
menu.removeAttribute('style')
return
}

View File

@@ -425,6 +425,26 @@ class State extends new Record(DEFAULTS) {
: this.document.getTextsAtRange(this.selection)
}
/**
* Check whether the selection is empty.
*
* @return {Boolean}
*/
get isEmpty() {
const { startOffset, endOffset } = this
if (this.isCollapsed) {
return true
}
if (endOffset != 0 && startOffset != 0) {
return false
}
return this.fragment.text.length == 0
}
/**
* Return a new `Transform` with the current state as a starting point.
*