Skip to content
Open
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
1 change: 1 addition & 0 deletions packages/xl-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"lodash.isequal": "^4.5.0",
"lodash.merge": "^4.6.2",
"prosemirror-changeset": "^2.4.1",
"prosemirror-keymap": "^1.2.3",
"prosemirror-model": "^1.25.11",
"prosemirror-state": "^1.4.4",
"prosemirror-tables": "^1.8.5",
Expand Down
2 changes: 2 additions & 0 deletions packages/xl-ai/src/AIExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Plugin, PluginKey } from "prosemirror-state";
import { fixTablesKey } from "prosemirror-tables";
import { buildAIRequest, sendMessageWithAIRequest } from "./api/index.js";
import { createAgentCursorPlugin } from "./plugins/AgentCursorPlugin.js";
import { createShortcutPlugin } from "./plugins/ShortcutPlugin.js";
import { AIRequestHelpers, InvokeAIOptions } from "./types.js";
import { AttributionMarksExtension } from "./prosemirror/AttributionMarks.js";

Expand Down Expand Up @@ -137,6 +138,7 @@ export const AIExtension = createExtension(
createAgentCursorPlugin(
editorOptions?.agentCursor || { name: "AI", color: "#8bc6ff" },
),
createShortcutPlugin(editor),
],

/**
Expand Down
46 changes: 46 additions & 0 deletions packages/xl-ai/src/plugins/ShortcutPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { BlockNoteEditor } from "@blocknote/core";
import { keymap } from "prosemirror-keymap";
import { Command } from "prosemirror-state";

type AIMenuExtension = {
store: { state: { aiMenuState: unknown } };
openAIMenuAtBlock: (blockId: string) => void;
};

// The command that will be executed when the shortcut is pressed.
const openAIMenuCommand = (editor: BlockNoteEditor): Command => {
return () => {
const ai = editor.getExtension<AIMenuExtension>("ai");
if (!ai) {
return false;
}

// Check if the AI Menu is already open. If so, do nothing.
if (ai.store.state.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),
});
};