Skip to content

fix: Tab handling (indentation and toolbar focus) #1285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ export type BlockNoteEditorOptions<
setIdAttribute?: boolean;

dropCursor?: (opts: any) => Plugin;

/**
Select desired behavior when pressing `Tab` (or `Shift-Tab`). Specifically,
what should happen when a user has selected multiple blocks while a toolbar
is open:
- `"prefer-navigate-ui"`: Change focus to the toolbar. The user needs to
first press `Escape` to close the toolbar, and can then indent multiple
blocks. Better for keyboard accessibility.
- `"prefer-indent"`: Regardless of whether toolbars are open, indent the
selection of blocks. In this case, it's not possible to navigate toolbars
with the keyboard.

@default "prefer-navigate-ui"
*/
tabBehavior: "prefer-navigate-ui" | "prefer-indent";
};

const blockNoteTipTapOptions = {
Expand Down Expand Up @@ -395,6 +410,7 @@ export class BlockNoteEditor<
tableHandles: checkDefaultBlockTypeInSchema("table", this),
dropCursor: this.options.dropCursor ?? dropCursor,
placeholders: newOptions.placeholders,
tabBehavior: newOptions.tabBehavior,
});

// add extensions from _tiptapOptions
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/editor/BlockNoteExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type ExtensionOptions<
tableHandles: boolean;
dropCursor: (opts: any) => Plugin;
placeholders: Record<string | "default", string>;
tabBehavior?: "prefer-navigate-ui" | "prefer-indent";
};

/**
Expand Down Expand Up @@ -200,6 +201,7 @@ const getTipTapExtensions = <
}),
KeyboardShortcutsExtension.configure({
editor: opts.editor,
tabBehavior: opts.tabBehavior,
}),
BlockGroup.configure({
domAttributes: opts.domAttributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ export class FormattingToolbarView implements PluginView {
// Wrapping in a setTimeout gives enough time to wait for the blur event to
// occur before updating the toolbar.
const { state, composing } = view;
const { doc, selection } = state;
const { selection } = state;
const isSame =
oldState && oldState.doc.eq(doc) && oldState.selection.eq(selection);
oldState &&
oldState.selection.from === state.selection.from &&
oldState.selection.to === state.selection.to;

if (composing || isSame) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";

export const KeyboardShortcutsExtension = Extension.create<{
editor: BlockNoteEditor<any, any, any>;
tabBehavior: "prefer-navigate-ui" | "prefer-indent";
}>({
priority: 50,

Expand Down Expand Up @@ -479,9 +480,10 @@ export const KeyboardShortcutsExtension = Extension.create<{
// editor since the browser will try to use tab for keyboard navigation.
Tab: () => {
if (
this.options.editor.formattingToolbar?.shown ||
this.options.editor.linkToolbar?.shown ||
this.options.editor.filePanel?.shown
this.options.tabBehavior !== "prefer-indent" &&
(this.options.editor.formattingToolbar?.shown ||
this.options.editor.linkToolbar?.shown ||
this.options.editor.filePanel?.shown)
) {
// don't handle tabs if a toolbar is shown, so we can tab into / out of it
return false;
Expand All @@ -491,9 +493,10 @@ export const KeyboardShortcutsExtension = Extension.create<{
},
"Shift-Tab": () => {
if (
this.options.editor.formattingToolbar?.shown ||
this.options.editor.linkToolbar?.shown ||
this.options.editor.filePanel?.shown
this.options.tabBehavior !== "prefer-indent" &&
(this.options.editor.formattingToolbar?.shown ||
this.options.editor.linkToolbar?.shown ||
this.options.editor.filePanel?.shown)
) {
// don't handle tabs if a toolbar is shown, so we can tab into / out of it
return false;
Expand Down
15 changes: 11 additions & 4 deletions packages/core/src/extensions/LinkToolbar/LinkToolbarPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getMarkRange, posToDOMRect, Range } from "@tiptap/core";

import { EditorView } from "@tiptap/pm/view";
import { Mark } from "prosemirror-model";
import { Plugin, PluginKey, PluginView } from "prosemirror-state";
import { EditorState, Plugin, PluginKey, PluginView } from "prosemirror-state";

import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
import { UiElementPosition } from "../../extensions-shared/UiElementPosition.js";
Expand Down Expand Up @@ -52,7 +52,7 @@ class LinkToolbarView implements PluginView {

this.startMenuUpdateTimer = () => {
this.menuUpdateTimer = setTimeout(() => {
this.update();
this.update(this.pmView);
}, 250);
};

Expand Down Expand Up @@ -190,8 +190,15 @@ class LinkToolbarView implements PluginView {
}
}

update() {
if (!this.pmView.hasFocus()) {
update(view: EditorView, oldState?: EditorState) {
const { state } = view;

const isSame =
oldState &&
oldState.selection.from === state.selection.from &&
oldState.selection.to === state.selection.to;

if (isSame || !this.pmView.hasFocus()) {
return;
}

Expand Down
Loading