Skip to content

Commit

Permalink
Display correct shortcut for Strikethrough, depending on extension ve…
Browse files Browse the repository at this point in the history
…rsion

This ensures the correct mod+Shift+S shortcut is displayed by default
(used in newer versions of the strike extension package), but uses
mod+Shift+X if the extension is configured to use that shortcut, for
users still on Tiptap <2.1.0 (for backwards compatibility).
  • Loading branch information
sjdemartini committed Dec 28, 2024
1 parent b87074c commit ca3e906
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/controls/MenuButtonStrikethrough.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
/// <reference types="@tiptap/extension-strike" />
import StrikethroughS from "@mui/icons-material/StrikethroughS";
import { getExtensionField, type AnyConfig } from "@tiptap/core";
import { useMemo } from "react";
import { useRichTextEditorContext } from "../context";
import MenuButton, { type MenuButtonProps } from "./MenuButton";

export type MenuButtonStrikethroughProps = Partial<MenuButtonProps>;

// "mod+Shift+S" is used as the shortcut for strike in Tiptap 2.1.0+
// (https://github.com/ueberdosis/tiptap/releases/tag/v2.1.0), whereas in
// earlier versions, the shortcut was "mod+Shift+X".
const DEFAULT_SHORTCUT_KEYS = ["mod", "Shift", "S"];

export default function MenuButtonStrikethrough(
props: MenuButtonStrikethroughProps
) {
const editor = useRichTextEditorContext();

// Determine the correct shortcut keys to display based on the
// installed/configured strike extension, for backwards compatibility with
// versions of `@tiptap/extension-strike` prior to 2.1.0 that used
// "mod+Shift+X".
const shortcutKeys: string[] = useMemo(() => {
const strikeExtension = editor?.extensionManager.extensions.find(
(extension) => extension.name == "strike"
);
if (!strikeExtension) {
return DEFAULT_SHORTCUT_KEYS;
}
const addKeyboardShortcuts = getExtensionField<
AnyConfig["addKeyboardShortcuts"]
>(strikeExtension, "addKeyboardShortcuts");
if (!addKeyboardShortcuts) {
return DEFAULT_SHORTCUT_KEYS;
}

const configuredKeyboardShortcuts = addKeyboardShortcuts();
return "Mod-Shift-x" in configuredKeyboardShortcuts
? ["mod", "Shift", "X"]
: DEFAULT_SHORTCUT_KEYS;
}, [editor]);

return (
<MenuButton
tooltipLabel="Strikethrough"
tooltipShortcutKeys={["mod", "Shift", "X"]}
tooltipShortcutKeys={shortcutKeys}
IconComponent={StrikethroughS}
selected={editor?.isActive("strike") ?? false}
disabled={!editor?.isEditable || !editor.can().toggleStrike()}
Expand Down

0 comments on commit ca3e906

Please sign in to comment.