mirror of
https://github.com/docmost/docmost.git
synced 2026-05-08 15:23:07 +08:00
a3c1c6cccd
The slash command menu (/) and emoji menu (:) were incorrectly triggering when typing inside code blocks, breaking keyboard navigation and confusing users who type paths like /work or symbols like := in their code. Added an `allow` function to both SlashCommand and EmojiCommand extensions that checks if the cursor is inside a code block and disables the menu accordingly. Closes #1730
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { Extension } from '@tiptap/core';
|
|
import { PluginKey } from '@tiptap/pm/state';
|
|
import Suggestion, { SuggestionOptions } from '@tiptap/suggestion';
|
|
import renderItems from '@/features/editor/components/slash-menu/render-items';
|
|
import getSuggestionItems from '@/features/editor/components/slash-menu/menu-items';
|
|
|
|
export const slashMenuPluginKey = new PluginKey('slash-command');
|
|
|
|
// @ts-ignore
|
|
const Command = Extension.create({
|
|
name: 'slash-command',
|
|
|
|
addOptions() {
|
|
return {
|
|
suggestion: {
|
|
char: '/',
|
|
command: ({ editor, range, props }) => {
|
|
props.command({ editor, range, props });
|
|
},
|
|
allow: ({ state, range }) => {
|
|
const $from = state.doc.resolve(range.from);
|
|
// Disable slash menu inside code blocks
|
|
if ($from.parent.type.name === 'codeBlock') {
|
|
return false;
|
|
}
|
|
return true;
|
|
},
|
|
} as Partial<SuggestionOptions>,
|
|
};
|
|
},
|
|
|
|
addProseMirrorPlugins() {
|
|
return [
|
|
Suggestion({
|
|
pluginKey: slashMenuPluginKey,
|
|
...this.options.suggestion,
|
|
editor: this.editor,
|
|
}),
|
|
];
|
|
},
|
|
});
|
|
|
|
const SlashCommand = Command.configure({
|
|
suggestion: {
|
|
items: getSuggestionItems,
|
|
render: renderItems,
|
|
},
|
|
});
|
|
|
|
export default SlashCommand;
|