mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-07 17:43:49 +08:00
feat: add 'all' option to focus command, fix #2181
This commit is contained in:
parent
cd14731612
commit
3c571c9a3f
@ -6,7 +6,7 @@ When a user clicks on a button outside the editor, the browser sets the focus to
|
|||||||
See also: [setTextSelection](/api/commands/set-text-selection), [blur](/api/commands/blur)
|
See also: [setTextSelection](/api/commands/set-text-selection), [blur](/api/commands/blur)
|
||||||
|
|
||||||
## Parameters
|
## Parameters
|
||||||
`position: 'start' | 'end' | number | boolean | null (false)`
|
`position: 'start' | 'end' | 'all' | number | boolean | null (false)`
|
||||||
|
|
||||||
By default, it’s restoring the cursor position (and text selection). Pass a position to move the cursor too.
|
By default, it’s restoring the cursor position (and text selection). Pass a position to move the cursor too.
|
||||||
|
|
||||||
@ -21,6 +21,9 @@ editor.commands.focus('start')
|
|||||||
// Set the cursor to the last position
|
// Set the cursor to the last position
|
||||||
editor.commands.focus('end')
|
editor.commands.focus('end')
|
||||||
|
|
||||||
|
// Selects the whole document
|
||||||
|
editor.commands.focus('all')
|
||||||
|
|
||||||
// Set the cursor to position 10
|
// Set the cursor to position 10
|
||||||
editor.commands.focus(10)
|
editor.commands.focus(10)
|
||||||
```
|
```
|
||||||
|
@ -241,6 +241,7 @@ With `autofocus` you can force the cursor to jump in the editor on initializatio
|
|||||||
| --------- | ------------------------------------------------------ |
|
| --------- | ------------------------------------------------------ |
|
||||||
| `'start'` | Sets the focus to the beginning of the document. |
|
| `'start'` | Sets the focus to the beginning of the document. |
|
||||||
| `'end'` | Sets the focus to the end of the document. |
|
| `'end'` | Sets the focus to the end of the document. |
|
||||||
|
| `'all'` | Selects the whole document. |
|
||||||
| `Number` | Sets the focus to a specific position in the document. |
|
| `Number` | Sets the focus to a specific position in the document. |
|
||||||
| `true` | Enables autofocus. |
|
| `true` | Enables autofocus. |
|
||||||
| `false` | Disables autofocus. |
|
| `false` | Disables autofocus. |
|
||||||
|
@ -16,15 +16,22 @@ function resolveSelection(state: EditorState, position: FocusPosition = null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (position === 'end') {
|
const { size } = state.doc.content
|
||||||
const { size } = state.doc.content
|
|
||||||
|
|
||||||
|
if (position === 'end') {
|
||||||
return {
|
return {
|
||||||
from: size,
|
from: size,
|
||||||
to: size,
|
to: size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (position === 'all') {
|
||||||
|
return {
|
||||||
|
from: 0,
|
||||||
|
to: size,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from: position,
|
from: position,
|
||||||
to: position,
|
to: position,
|
||||||
|
@ -212,7 +212,7 @@ export type ChainedCommands = {
|
|||||||
|
|
||||||
export type CanCommands = SingleCommands & { chain: () => ChainedCommands }
|
export type CanCommands = SingleCommands & { chain: () => ChainedCommands }
|
||||||
|
|
||||||
export type FocusPosition = 'start' | 'end' | number | boolean | null
|
export type FocusPosition = 'start' | 'end' | 'all' | number | boolean | null
|
||||||
|
|
||||||
export type Range = {
|
export type Range = {
|
||||||
from: number,
|
from: number,
|
||||||
|
Loading…
Reference in New Issue
Block a user