Skip to content

Commit

Permalink
Add property "isEmpty" (ianstormtaylor#863)
Browse files Browse the repository at this point in the history
* Add property isEmpty to State

* Update hovering menu example

* Document isEmpty

* Improve perf of isEmpty with @Soreine 's suggestion

* Fix return of isEmpty
  • Loading branch information
SamyPesse authored and XuHaoJun committed Jul 23, 2017
1 parent d6456e2 commit 99ec161
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/reference/models/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/hovering-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions src/models/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 99ec161

Please sign in to comment.