From 0aa39f93cb1ea328bcf3c1b8da9b0ff1b6a00486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BChn?= Date: Tue, 25 Jan 2022 10:35:39 +0100 Subject: [PATCH] feat: add selectTextblockStart and selectTextblockEnd commands --- docs/api/commands/select-textblock-end.md | 5 +++++ docs/api/commands/select-textblock-start.md | 5 +++++ docs/links.yaml | 6 ++++++ .../core/src/commands/selectTextblockEnd.ts | 19 +++++++++++++++++++ .../core/src/commands/selectTextblockStart.ts | 19 +++++++++++++++++++ packages/core/src/extensions/commands.ts | 6 ++++++ packages/core/src/extensions/keymap.ts | 4 ++++ 7 files changed, 64 insertions(+) create mode 100644 docs/api/commands/select-textblock-end.md create mode 100644 docs/api/commands/select-textblock-start.md create mode 100644 packages/core/src/commands/selectTextblockEnd.ts create mode 100644 packages/core/src/commands/selectTextblockStart.ts diff --git a/docs/api/commands/select-textblock-end.md b/docs/api/commands/select-textblock-end.md new file mode 100644 index 00000000..f27799b1 --- /dev/null +++ b/docs/api/commands/select-textblock-end.md @@ -0,0 +1,5 @@ +# selectTextblockEnd + +:::warning +Oops, we didn’t find time to fill this page. Writing documentation needs attention to detail, a great understanding of the project and time to write. Though Tiptap is used by thousands of developers all around the world, it’s still a side project for us. Let’s change that and make open source our full-time job! [Become a sponsor!](https://github.com/sponsors/ueberdosis) +::: diff --git a/docs/api/commands/select-textblock-start.md b/docs/api/commands/select-textblock-start.md new file mode 100644 index 00000000..5e99588a --- /dev/null +++ b/docs/api/commands/select-textblock-start.md @@ -0,0 +1,5 @@ +# selectTextblockStart + +:::warning +Oops, we didn’t find time to fill this page. Writing documentation needs attention to detail, a great understanding of the project and time to write. Though Tiptap is used by thousands of developers all around the world, it’s still a side project for us. Let’s change that and make open source our full-time job! [Become a sponsor!](https://github.com/sponsors/ueberdosis) +::: diff --git a/docs/links.yaml b/docs/links.yaml index 4103b70b..1640e089 100644 --- a/docs/links.yaml +++ b/docs/links.yaml @@ -223,6 +223,12 @@ - title: selectParentNode link: /api/commands/select-parent-node type: draft + - title: selectTextblockEnd + link: /api/commands/select-textblock-end + type: draft + - title: selectTextblockStart + link: /api/commands/select-textblock-start + type: draft - title: setContent link: /api/commands/set-content - title: setMark diff --git a/packages/core/src/commands/selectTextblockEnd.ts b/packages/core/src/commands/selectTextblockEnd.ts new file mode 100644 index 00000000..007e8c69 --- /dev/null +++ b/packages/core/src/commands/selectTextblockEnd.ts @@ -0,0 +1,19 @@ +// @ts-ignore +// TODO: add types to @types/prosemirror-commands +import { selectTextblockEnd as originalSelectTextblockEnd } from 'prosemirror-commands' +import { RawCommands } from '../types' + +declare module '@tiptap/core' { + interface Commands { + selectTextblockEnd: { + /** + * Moves the cursor to the end of current text block. + */ + selectTextblockEnd: () => ReturnType, + } + } +} + +export const selectTextblockEnd: RawCommands['selectTextblockEnd'] = () => ({ state, dispatch }) => { + return originalSelectTextblockEnd(state, dispatch) +} diff --git a/packages/core/src/commands/selectTextblockStart.ts b/packages/core/src/commands/selectTextblockStart.ts new file mode 100644 index 00000000..9205c04b --- /dev/null +++ b/packages/core/src/commands/selectTextblockStart.ts @@ -0,0 +1,19 @@ +// @ts-ignore +// TODO: add types to @types/prosemirror-commands +import { selectTextblockStart as originalSelectTextblockStart } from 'prosemirror-commands' +import { RawCommands } from '../types' + +declare module '@tiptap/core' { + interface Commands { + selectTextblockStart: { + /** + * Moves the cursor to the start of current text block. + */ + selectTextblockStart: () => ReturnType, + } + } +} + +export const selectTextblockStart: RawCommands['selectTextblockStart'] = () => ({ state, dispatch }) => { + return originalSelectTextblockStart(state, dispatch) +} diff --git a/packages/core/src/extensions/commands.ts b/packages/core/src/extensions/commands.ts index 11b5a02b..fa316335 100644 --- a/packages/core/src/extensions/commands.ts +++ b/packages/core/src/extensions/commands.ts @@ -28,6 +28,8 @@ import * as selectAll from '../commands/selectAll' import * as selectNodeBackward from '../commands/selectNodeBackward' import * as selectNodeForward from '../commands/selectNodeForward' import * as selectParentNode from '../commands/selectParentNode' +import * as selectTextblockEnd from '../commands/selectTextblockEnd' +import * as selectTextblockStart from '../commands/selectTextblockStart' import * as setContent from '../commands/setContent' import * as setMark from '../commands/setMark' import * as setMeta from '../commands/setMeta' @@ -77,6 +79,8 @@ export { selectAll } export { selectNodeBackward } export { selectNodeForward } export { selectParentNode } +export { selectTextblockEnd } +export { selectTextblockStart } export { setContent } export { setMark } export { setMeta } @@ -131,6 +135,8 @@ export const Commands = Extension.create({ ...selectNodeBackward, ...selectNodeForward, ...selectParentNode, + ...selectTextblockEnd, + ...selectTextblockStart, ...setContent, ...setMark, ...setMeta, diff --git a/packages/core/src/extensions/keymap.ts b/packages/core/src/extensions/keymap.ts index 51cdd276..36808748 100644 --- a/packages/core/src/extensions/keymap.ts +++ b/packages/core/src/extensions/keymap.ts @@ -52,6 +52,10 @@ export const Keymap = Extension.create({ Delete: handleDelete, 'Mod-Delete': handleDelete, 'Mod-a': () => this.editor.commands.selectAll(), + 'Ctrl-a': () => this.editor.commands.selectTextblockStart(), + 'Ctrl-e': () => this.editor.commands.selectTextblockEnd(), + Home: () => this.editor.commands.selectTextblockStart(), + End: () => this.editor.commands.selectTextblockStart(), } },