mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-21 22:45:18 +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:
committed by
Ian Storm Taylor
parent
4e01f80b1b
commit
74bf684ec9
@@ -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
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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.
|
||||
*
|
||||
|
Reference in New Issue
Block a user