From 3c571c9a3f2fdbf1d70e9efa27294fbdf6bcbd3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BChn?= Date: Mon, 22 Nov 2021 10:17:06 +0100 Subject: [PATCH] feat: add 'all' option to focus command, fix #2181 --- docs/api/commands/focus.md | 5 ++++- docs/api/editor.md | 1 + packages/core/src/commands/focus.ts | 11 +++++++++-- packages/core/src/types.ts | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/api/commands/focus.md b/docs/api/commands/focus.md index c512c3a78b..f75a19e946 100644 --- a/docs/api/commands/focus.md +++ b/docs/api/commands/focus.md @@ -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) ## 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. @@ -21,6 +21,9 @@ editor.commands.focus('start') // Set the cursor to the last position editor.commands.focus('end') +// Selects the whole document +editor.commands.focus('all') + // Set the cursor to position 10 editor.commands.focus(10) ``` diff --git a/docs/api/editor.md b/docs/api/editor.md index c181633b26..17eb64e7d9 100644 --- a/docs/api/editor.md +++ b/docs/api/editor.md @@ -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. | | `'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. | | `true` | Enables autofocus. | | `false` | Disables autofocus. | diff --git a/packages/core/src/commands/focus.ts b/packages/core/src/commands/focus.ts index 00b078c5ba..291817ca4e 100644 --- a/packages/core/src/commands/focus.ts +++ b/packages/core/src/commands/focus.ts @@ -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 { from: size, to: size, } } + if (position === 'all') { + return { + from: 0, + to: size, + } + } + return { from: position, to: position, diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 68684db06e..ac5942b620 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -212,7 +212,7 @@ export type 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 = { from: number,