You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Better selections and moving multiple blocks (#1276)
* Made `getSelection` only return blocks at the spanned by the selection at the depth of the shallowest spanned block
* Fixed bug where moving blocks across a `columnList` using Cmd+ArrowUp/ArrowDown wouldn't work
* Revert "Fixed bug where moving blocks across a `columnList` using Cmd+ArrowUp/ArrowDown wouldn't work"
This reverts commit 8c4ddd3.
* Renamed `moveBlock` -> `moveBlocks`
* Small fixes
* - Made selections & moving blocks up/down more closely mimic Notion
- Moved `prevBlock`, `nextBlock`, and `parentBlock` from `Selection` fields to getters on the editor which take a `BlockIdentifier`
- Fixed table handles error being thrown sometimes when removing blocks (was triggering on `moveBlocks`)
* Fixed small bug with getting blocks
* Made `moveBlocks` work with columns
* Added `setSelection` and unit tests
* Cleaned up block getters
* Implemented PR feedback
* Made `getNodeById` return undefined instead of throwing error
* Updated docs
`startBlock:` The [identifier](/docs/editor-api/manipulating-blocks#block-identifiers) of the block where the selection should start.
102
+
103
+
`endBlock:` The [identifier](/docs/editor-api/manipulating-blocks#block-identifiers) of the block where the selection should end.
104
+
105
+
Both `startBlock` and `endBlock` must point to a block with content. The updated selection will span from the start of the first block to the end of the last block.
106
+
90
107
## Getting Selected Blocks
91
108
92
109
The demo below displays the blocks in the current [selection](/docs/editor-api/cursor-selections#selections) as JSON below the editor. If a selection isn't active, it displays the block containing the [text cursor](/docs/editor-api/cursor-selections#text-cursor) instead.
`blockIdentifier:` The [identifier](/docs/editor-api/manipulating-blocks#block-identifiers) of an existing block for which the previous sibling should be retrieved.
107
+
108
+
`returns:` The previous sibling of the block that matches the identifier, or `undefined` if no matching block was found. Also `undefined` when the matching block is the first in the document, or the first child of a parent block.
109
+
110
+
#### Next Block
111
+
112
+
Use `getNextBlock` to retrieve a snapshot of a block's next sibling in the editor:
`blockIdentifier:` The [identifier](/docs/editor-api/manipulating-blocks#block-identifiers) of an existing block for which the next sibling should be retrieved.
122
+
123
+
`returns:` The next sibling of the block that matches the identifier, or `undefined` if no matching block was found. Also `undefined` when the matching block is the last in the document, or the last child of a parent block.
124
+
125
+
#### Parent Block
126
+
127
+
Use `getParentBlock` to retrieve a snapshot of a block's parent in the editor:
`blockIdentifier:` The [identifier](/docs/editor-api/manipulating-blocks#block-identifiers) of an existing block for which the parent should be retrieved.
137
+
138
+
`returns:` The parent of the block that matches the identifier, or `undefined` if no matching block was found. Also `undefined` when the matching block is not nested in a parent block.
139
+
88
140
### Traversing All Blocks
89
141
90
142
Use `forEachBlock` to traverse all blocks in the editor depth-first, and execute a callback for each block:
@@ -193,6 +245,30 @@ If the blocks that should be removed are not adjacent or are at different nestin
193
245
194
246
Throws an error if any of the blocks to remove could not be found.
195
247
248
+
## Moving Blocks Up/Down
249
+
250
+
### Moving Up
251
+
252
+
Use `moveBlocksUp` to move the selected blocks up:
253
+
254
+
```typescript
255
+
moveBlocksUp(): void;
256
+
257
+
// Usage
258
+
editor.moveBlocksUp();
259
+
```
260
+
261
+
### Moving Down
262
+
263
+
Use `moveBlocksDown` to move the selected blocks down:
264
+
265
+
```typescript
266
+
moveBlocksDown(): void;
267
+
268
+
// Usage
269
+
editor.moveBlocksDown();
270
+
```
271
+
196
272
## Nesting & Un-nesting Blocks
197
273
198
274
BlockNote also provides functions to nest & un-nest the block containing the [Text Cursor](/docs/editor-api/cursor-selections#text-cursor).
0 commit comments