[lexical-playground] Feature: Add keyboard shortcut for comments (#7464)

This commit is contained in:
Kiran Dash
2025-04-14 16:34:51 +08:00
committed by GitHub
parent 5f5181b146
commit ac49ee32fc
2 changed files with 13 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import {Dispatch, useEffect} from 'react';
import {useToolbarState} from '../../context/ToolbarContext';
import {sanitizeUrl} from '../../utils/url';
import {INSERT_INLINE_COMMAND} from '../CommentPlugin';
import {
clearFormatting,
formatBulletList,
@ -34,6 +35,7 @@ import {
UpdateFontSizeType,
} from '../ToolbarPlugin/utils';
import {
isAddComment,
isCapitalize,
isCenterAlign,
isClearFormatting,
@ -158,6 +160,9 @@ export default function ShortcutsPlugin({
setIsLinkEditMode(!toolbarState.isLink);
editor.dispatchCommand(TOGGLE_LINK_COMMAND, url);
} else if (isAddComment(event)) {
event.preventDefault();
editor.dispatchCommand(INSERT_INLINE_COMMAND, undefined);
}
return false;

View File

@ -21,6 +21,7 @@ export const SHORTCUTS = Object.freeze({
CHECK_LIST: IS_APPLE ? '⌘+Opt+6' : 'Ctrl+Alt+6',
CODE_BLOCK: IS_APPLE ? '⌘+Opt+C' : 'Ctrl+Alt+C',
QUOTE: IS_APPLE ? '⌘+Opt+Q' : 'Ctrl+Alt+Q',
ADD_COMMENT: IS_APPLE ? '⌘+Opt+M' : 'Ctrl+Alt+M',
// (Ctrl|⌘) + Shift + <key> shortcuts
INCREASE_FONT_SIZE: IS_APPLE ? '⌘+Shift+.' : 'Ctrl+Shift+.',
@ -256,3 +257,10 @@ export function isInsertLink(event: KeyboardEvent): boolean {
code === 'KeyK' && !shiftKey && !altKey && controlOrMeta(metaKey, ctrlKey)
);
}
export function isAddComment(event: KeyboardEvent): boolean {
const {code, shiftKey, altKey, metaKey, ctrlKey} = event;
return (
code === 'KeyM' && !shiftKey && altKey && controlOrMeta(metaKey, ctrlKey)
);
}