refactor to separate file
This commit is contained in:
parent
65445b6fe7
commit
01cee687c1
2 changed files with 48 additions and 21 deletions
|
|
@ -3,11 +3,13 @@ import { InputModifiers } from "core";
|
|||
import { rifWithContentsToContextItem } from "core/commands/util";
|
||||
import { MutableRefObject } from "react";
|
||||
import { useWebviewListener } from "../../../hooks/useWebviewListener";
|
||||
import { useAppSelector } from "../../../redux/hooks";
|
||||
import { clearCodeToEdit } from "../../../redux/slices/editModeState";
|
||||
import { setNewestToolbarPreviewForInput } from "../../../redux/slices/sessionSlice";
|
||||
import { AppDispatch } from "../../../redux/store";
|
||||
import { loadSession, saveCurrentSession } from "../../../redux/thunks/session";
|
||||
import { CodeBlock, Mention, PromptBlock } from "./extensions";
|
||||
import { CodeBlock, PromptBlock } from "./extensions";
|
||||
import { insertCurrentFileContextMention } from "./utils/insertContent";
|
||||
|
||||
/**
|
||||
* Hook for setting up main editor specific webview listeners
|
||||
|
|
@ -179,27 +181,18 @@ export function useMainEditorWebviewListeners({
|
|||
[],
|
||||
);
|
||||
|
||||
useWebviewListener(
|
||||
"configUpdate",
|
||||
async (data) => {
|
||||
const currentFileProvider = data.result.config?.contextProviders.find(
|
||||
(provider) => provider.title === "currentFile",
|
||||
);
|
||||
|
||||
if (currentFileProvider && editor) {
|
||||
const node = editor.schema.nodes[Mention.name].create({
|
||||
name: currentFileProvider.displayTitle,
|
||||
description: currentFileProvider.description,
|
||||
id: currentFileProvider.title,
|
||||
label: currentFileProvider.displayTitle,
|
||||
renderInlineAs: currentFileProvider.renderInlineAs,
|
||||
type: currentFileProvider.type,
|
||||
itemType: 'contextProvider'
|
||||
});
|
||||
const activeContextProviders = useAppSelector(
|
||||
(state) => state.config.config.contextProviders,
|
||||
);
|
||||
const mode = useAppSelector(state=>state.session.mode);
|
||||
|
||||
editor.chain().insertContent(node.toJSON()).run();
|
||||
}
|
||||
useWebviewListener(
|
||||
"newSession",
|
||||
async () => {
|
||||
// do not insert current file context mention if we are in edit mode
|
||||
if(!editor || mode === 'edit') return;
|
||||
insertCurrentFileContextMention(editor, activeContextProviders);
|
||||
},
|
||||
[editor],
|
||||
[editor, activeContextProviders, mode],
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
import { Editor } from "@tiptap/core";
|
||||
import { ContextProviderDescription } from "core";
|
||||
import * as ContinueExtensions from "../extensions";
|
||||
|
||||
/**
|
||||
* inserts the current file context as a removable mention in the editor area
|
||||
*
|
||||
* uses tiptap's `create` method to create a new node under the hood
|
||||
*/
|
||||
export function insertCurrentFileContextMention(
|
||||
editor: Editor,
|
||||
contextProviders: ContextProviderDescription[],
|
||||
) {
|
||||
const foundCurrentFileProvider = contextProviders.find(
|
||||
(provider) => provider.title === "currentFile",
|
||||
);
|
||||
|
||||
if (foundCurrentFileProvider) {
|
||||
const node = editor.schema.nodes[ContinueExtensions.Mention.name].create({
|
||||
name: foundCurrentFileProvider.displayTitle,
|
||||
description: foundCurrentFileProvider.description,
|
||||
id: foundCurrentFileProvider.title,
|
||||
label: foundCurrentFileProvider.displayTitle,
|
||||
renderInlineAs: foundCurrentFileProvider.renderInlineAs,
|
||||
type: foundCurrentFileProvider.type,
|
||||
itemType: "contextProvider",
|
||||
});
|
||||
|
||||
editor
|
||||
.chain()
|
||||
.insertContent([node.toJSON(), { type: "text", text: " " }]) // add a space after the mention
|
||||
.run();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue