From 18918762e1fb9e6188667e7596c6c1e0f00b65c9 Mon Sep 17 00:00:00 2001 From: Yuzhong Zhang <141388234+BetterAndBetterII@users.noreply.github.com> Date: Fri, 27 Jun 2025 22:39:43 +0800 Subject: [PATCH 1/2] add ai menu shortcut (align with copilot) --- packages/xl-ai/package.json | 4 +- packages/xl-ai/src/AIExtension.ts | 2 + packages/xl-ai/src/plugins/ShortcutPlugin.ts | 39 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 packages/xl-ai/src/plugins/ShortcutPlugin.ts diff --git a/packages/xl-ai/package.json b/packages/xl-ai/package.json index 3e29b2156a..5536deee93 100644 --- a/packages/xl-ai/package.json +++ b/packages/xl-ai/package.json @@ -73,6 +73,7 @@ "ai": "^4.3.15", "lodash.isequal": "^4.5.0", "prosemirror-changeset": "^2.3.0", + "prosemirror-keymap": "^1.2.2", "prosemirror-model": "^1.24.1", "prosemirror-state": "^1.4.3", "prosemirror-tables": "^1.6.4", @@ -88,16 +89,17 @@ "zustand": "^5.0.3" }, "devDependencies": { + "@ai-sdk/anthropic": "^1.2.12", "@ai-sdk/groq": "^1.2.9", "@ai-sdk/mistral": "^1.2.8", "@ai-sdk/openai": "^1.3.22", "@ai-sdk/openai-compatible": "^0.2.14", - "@ai-sdk/anthropic": "^1.2.12", "@mswjs/interceptors": "^0.37.5", "@types/diff": "^6.0.0", "@types/json-diff": "^1.0.3", "@types/json-schema": "^7.0.15", "@types/lodash.isequal": "^4.5.8", + "@types/prosemirror-keymap": "^1.2.0", "@types/react": "^18.0.25", "@types/react-dom": "^18.0.9", "@vitejs/plugin-react": "^4.3.1", diff --git a/packages/xl-ai/src/AIExtension.ts b/packages/xl-ai/src/AIExtension.ts index e4ba5d6840..a5aff1ee02 100644 --- a/packages/xl-ai/src/AIExtension.ts +++ b/packages/xl-ai/src/AIExtension.ts @@ -17,6 +17,7 @@ import { LLMResponse } from "./api/LLMResponse.js"; import { PromptBuilder } from "./api/formats/PromptBuilder.js"; import { LLMFormat, llmFormats } from "./api/index.js"; import { createAgentCursorPlugin } from "./plugins/AgentCursorPlugin.js"; +import { createShortcutPlugin } from "./plugins/ShortcutPlugin.js"; import { Fragment, Slice } from "prosemirror-model"; type MakeOptional = Omit & Partial>; @@ -141,6 +142,7 @@ export class AIExtension extends BlockNoteExtension { ...options, })); + this.addProsemirrorPlugin(createShortcutPlugin(this.editor)); this.addProsemirrorPlugin( new Plugin({ key: PLUGIN_KEY, diff --git a/packages/xl-ai/src/plugins/ShortcutPlugin.ts b/packages/xl-ai/src/plugins/ShortcutPlugin.ts new file mode 100644 index 0000000000..e141e51184 --- /dev/null +++ b/packages/xl-ai/src/plugins/ShortcutPlugin.ts @@ -0,0 +1,39 @@ +import { BlockNoteEditor } from "@blocknote/core"; +import { getAIExtension } from "@blocknote/xl-ai"; +import { Command } from "prosemirror-state"; +import { keymap } from "prosemirror-keymap"; + +// The command that will be executed when the shortcut is pressed. +const openAIMenuCommand = (editor: BlockNoteEditor): Command => { + return () => { + const ai = getAIExtension(editor); + + // Check if the AI Menu is already open. If so, do nothing. + if (ai.store.getState().aiMenuState !== "closed") { + return false; // Return false to indicate the command did nothing. + } + + const cursor = editor.getTextCursorPosition(); + if ( + cursor.block.content && + Array.isArray(cursor.block.content) && // isarray check not ideal + cursor.block.content.length === 0 && + cursor.prevBlock + ) { + ai.openAIMenuAtBlock(cursor.prevBlock.id); + } else { + ai.openAIMenuAtBlock(cursor.block.id); + } + + // Return true to indicate that the key event has been handled. + return true; + }; +}; + +// A factory function to create the shortcut plugin. +export const createShortcutPlugin = (editor: BlockNoteEditor) => { + return keymap({ + // "Mod" maps to "Cmd" on Mac and "Ctrl" on Windows/Linux. + "Mod-i": openAIMenuCommand(editor), + }); +}; From 6befedf7aabc2a5bb464fa877e14c2e745b24eb9 Mon Sep 17 00:00:00 2001 From: Yuzhong Zhang <141388234+BetterAndBetterII@users.noreply.github.com> Date: Fri, 27 Jun 2025 22:47:53 +0800 Subject: [PATCH 2/2] alter prosemirror-keymap ^1.2.0 --- packages/xl-ai/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/xl-ai/package.json b/packages/xl-ai/package.json index 5536deee93..349605b193 100644 --- a/packages/xl-ai/package.json +++ b/packages/xl-ai/package.json @@ -73,7 +73,7 @@ "ai": "^4.3.15", "lodash.isequal": "^4.5.0", "prosemirror-changeset": "^2.3.0", - "prosemirror-keymap": "^1.2.2", + "prosemirror-keymap": "^1.2.0", "prosemirror-model": "^1.24.1", "prosemirror-state": "^1.4.3", "prosemirror-tables": "^1.6.4",