Skip to content

Commit

Permalink
feat: add setNodeSelection and setTextSelection commands
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Apr 27, 2021
1 parent fe61a6e commit 811bf69
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/src/docPages/api/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ Have a look at all of the core commands listed below. They should give you a goo
| .selectNodeBackward() | Select a node backward. | [More](/api/commands/select-node-backward) |
| .selectNodeForward() | Select a node forward. | [More](/api/commands/select-node-forward) |
| .selectParentNode() | Select the parent node. | [More](/api/commands/select-parent-node) |
| .setNodeSelection() | Creates a NodeSelection. | [More](/api/commands/set-node-selection) |
| .setTextSelection() | Creates a TextSelection. | [More](/api/commands/set-text-selection) |

<!-- ## Example use cases
Expand Down
3 changes: 3 additions & 0 deletions docs/src/docPages/api/commands/set-node-selection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# setNodeSelection

TODO
3 changes: 3 additions & 0 deletions docs/src/docPages/api/commands/set-text-selection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# setTextSelection

TODO
4 changes: 4 additions & 0 deletions docs/src/links.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@
link: /api/commands/set-mark
- title: setNode
link: /api/commands/set-node
- title: setNodeSelection
link: /api/commands/set-node-selection
- title: setTextSelection
link: /api/commands/set-text-selection
- title: sinkListItem
link: /api/commands/sink-list-item
- title: splitBlock
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/commands/setNodeSelection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { NodeSelection } from 'prosemirror-state'
import { Command, RawCommands } from '../types'

declare module '@tiptap/core' {
interface Commands {
setNodeSelection: {
/**
* Creates a NodeSelection.
*/
setNodeSelection: (position: number) => Command,
}
}
}

export const setNodeSelection: RawCommands['setNodeSelection'] = position => ({ tr, dispatch }) => {
if (dispatch) {
const selection = NodeSelection.create(tr.doc, position)

tr.setSelection(selection)
}

return true
}
23 changes: 23 additions & 0 deletions packages/core/src/commands/setTextSelection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TextSelection } from 'prosemirror-state'
import { Command, RawCommands, Range } from '../types'

declare module '@tiptap/core' {
interface Commands {
setTextSelection: {
/**
* Creates a TextSelection.
*/
setTextSelection: (range: Range) => Command,
}
}
}

export const setTextSelection: RawCommands['setTextSelection'] = range => ({ tr, dispatch }) => {
if (dispatch) {
const selection = TextSelection.create(tr.doc, range.from, range.to)

tr.setSelection(selection)
}

return true
}
6 changes: 6 additions & 0 deletions packages/core/src/extensions/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import * as selectParentNode from '../commands/selectParentNode'
import * as setContent from '../commands/setContent'
import * as setMark from '../commands/setMark'
import * as setNode from '../commands/setNode'
import * as setNodeSelection from '../commands/setNodeSelection'
import * as setTextSelection from '../commands/setTextSelection'
import * as sinkListItem from '../commands/sinkListItem'
import * as splitBlock from '../commands/splitBlock'
import * as splitListItem from '../commands/splitListItem'
Expand Down Expand Up @@ -84,6 +86,8 @@ export { selectParentNode }
export { setContent }
export { setMark }
export { setNode }
export { setNodeSelection }
export { setTextSelection }
export { sinkListItem }
export { splitBlock }
export { splitListItem }
Expand Down Expand Up @@ -139,6 +143,8 @@ export const Commands = Extension.create({
...setContent,
...setMark,
...setNode,
...setNodeSelection,
...setTextSelection,
...sinkListItem,
...splitBlock,
...splitListItem,
Expand Down

0 comments on commit 811bf69

Please sign in to comment.