feat: dallin's feedback
This commit is contained in:
parent
6eddab18c5
commit
e88485348c
16 changed files with 227 additions and 343 deletions
|
|
@ -1,3 +0,0 @@
|
|||
Always use Tailwind classes, not styled components.
|
||||
|
||||
If a component is using styled components, it should be converted to use Tailwind classes.
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
import { SlashCommandDescription } from "core";
|
||||
import { useState } from "react";
|
||||
import { useBookmarkedSlashCommands } from "../../hooks/useBookmarkedSlashCommands";
|
||||
import { MAIN_EDITOR_INPUT_ID } from "../../pages/gui/Chat";
|
||||
import { useAppDispatch, useAppSelector } from "../../redux/hooks";
|
||||
import { setNewestToolbarPreviewForInput } from "../../redux/slices/sessionSlice";
|
||||
import { useMainEditor } from "../mainInput/TipTapEditor";
|
||||
import { ConversationStarterCard } from "./ConversationStarterCard";
|
||||
|
||||
|
|
@ -26,14 +24,7 @@ export function ConversationStarterCards() {
|
|||
);
|
||||
|
||||
function onClick(command: SlashCommandDescription) {
|
||||
dispatch(
|
||||
setNewestToolbarPreviewForInput({
|
||||
inputId: MAIN_EDITOR_INPUT_ID,
|
||||
contextItemId: command.name,
|
||||
}),
|
||||
);
|
||||
|
||||
mainEditor?.commands.insertSlashCommand(command);
|
||||
mainEditor?.commands.insertPrompt(command);
|
||||
}
|
||||
|
||||
if (bookmarkedSlashCommands.length === 0) {
|
||||
|
|
|
|||
51
gui/src/components/mainInput/Lump/LumpContext.tsx
Normal file
51
gui/src/components/mainInput/Lump/LumpContext.tsx
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/**
|
||||
* Provides context for controlling the Lump component's visibility state
|
||||
*/
|
||||
import { createContext, ReactNode, useContext } from "react";
|
||||
|
||||
interface LumpContextType {
|
||||
isLumpVisible: boolean;
|
||||
selectedSection: string | null;
|
||||
hideLump: () => void;
|
||||
setSelectedSection: (section: string | null) => void;
|
||||
}
|
||||
|
||||
const LumpContext = createContext<LumpContextType | undefined>(undefined);
|
||||
|
||||
interface LumpProviderProps {
|
||||
children: ReactNode;
|
||||
isLumpVisible: boolean;
|
||||
selectedSection: string | null;
|
||||
hideLump: () => void;
|
||||
setSelectedSection: (section: string | null) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider component that makes Lump state available to any child component
|
||||
*/
|
||||
export function LumpProvider({
|
||||
children,
|
||||
isLumpVisible,
|
||||
selectedSection,
|
||||
hideLump,
|
||||
setSelectedSection,
|
||||
}: LumpProviderProps) {
|
||||
return (
|
||||
<LumpContext.Provider
|
||||
value={{ isLumpVisible, selectedSection, hideLump, setSelectedSection }}
|
||||
>
|
||||
{children}
|
||||
</LumpContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook that lets any component access the Lump context
|
||||
*/
|
||||
export function useLump() {
|
||||
const context = useContext(LumpContext);
|
||||
if (context === undefined) {
|
||||
throw new Error("useLump must be used within a LumpProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { useEffect, useState } from "react";
|
||||
import { useCallback, useState } from "react";
|
||||
import styled from "styled-components";
|
||||
import {
|
||||
defaultBorderRadius,
|
||||
|
|
@ -6,6 +6,7 @@ import {
|
|||
vscInputBackground,
|
||||
} from "../..";
|
||||
import { useAppSelector } from "../../../redux/hooks";
|
||||
import { LumpProvider } from "./LumpContext";
|
||||
import { LumpToolbar } from "./LumpToolbar";
|
||||
import { SelectedSection } from "./sections/SelectedSection";
|
||||
|
||||
|
|
@ -30,64 +31,78 @@ const ContentDiv = styled.div<{ hasSection: boolean; isVisible: boolean }>`
|
|||
overflow-y: auto;
|
||||
`;
|
||||
|
||||
/**
|
||||
* Main component that displays the toolbar and selected content section
|
||||
*/
|
||||
export function Lump() {
|
||||
const [selectedSection, setSelectedSection] = useState<string | null>(null);
|
||||
const [displayedSection, setDisplayedSection] = useState<string | null>(null);
|
||||
const [isVisible, setIsVisible] = useState(false);
|
||||
const isStreaming = useAppSelector((state) => state.session.isStreaming);
|
||||
|
||||
useEffect(() => {
|
||||
if (!selectedSection) {
|
||||
return;
|
||||
}
|
||||
const handleKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.key === "Escape") {
|
||||
// Handle keyboard escape
|
||||
const handleKeyDown = useCallback(
|
||||
(event: KeyboardEvent) => {
|
||||
if (event.key === "Escape" && selectedSection) {
|
||||
setSelectedSection(null);
|
||||
}
|
||||
};
|
||||
document.addEventListener("keydown", handleKeyDown);
|
||||
return () => {
|
||||
document.removeEventListener("keydown", handleKeyDown);
|
||||
};
|
||||
}, [selectedSection]);
|
||||
},
|
||||
[selectedSection],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (isStreaming) {
|
||||
setSelectedSection(null);
|
||||
}
|
||||
}, [isStreaming]);
|
||||
// Function to hide the lump
|
||||
const hideLump = useCallback(() => {
|
||||
setSelectedSection(null);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedSection) {
|
||||
// Reset when streaming starts
|
||||
if (isStreaming && selectedSection) {
|
||||
setSelectedSection(null);
|
||||
}
|
||||
|
||||
// Update displayedSection and visibility when selectedSection changes
|
||||
if (selectedSection) {
|
||||
if (displayedSection !== selectedSection || !isVisible) {
|
||||
setDisplayedSection(selectedSection);
|
||||
setIsVisible(true);
|
||||
} else {
|
||||
setIsVisible(false);
|
||||
const timeout = setTimeout(() => {
|
||||
setDisplayedSection(null);
|
||||
}, 300);
|
||||
return () => {
|
||||
clearTimeout(timeout);
|
||||
};
|
||||
}
|
||||
}, [selectedSection]);
|
||||
} else if (isVisible) {
|
||||
setIsVisible(false);
|
||||
setTimeout(() => {
|
||||
setDisplayedSection(null);
|
||||
}, 300);
|
||||
}
|
||||
|
||||
// Set up keyboard listener
|
||||
if (selectedSection) {
|
||||
document.addEventListener("keydown", handleKeyDown);
|
||||
} else {
|
||||
document.removeEventListener("keydown", handleKeyDown);
|
||||
}
|
||||
|
||||
return (
|
||||
<LumpDiv>
|
||||
<div className="mt-0.5 px-2">
|
||||
<LumpToolbar
|
||||
selectedSection={selectedSection}
|
||||
setSelectedSection={setSelectedSection}
|
||||
/>
|
||||
<LumpProvider
|
||||
isLumpVisible={isVisible}
|
||||
selectedSection={selectedSection}
|
||||
hideLump={hideLump}
|
||||
setSelectedSection={setSelectedSection}
|
||||
>
|
||||
<LumpDiv>
|
||||
<div className="mt-0.5 px-2">
|
||||
<LumpToolbar
|
||||
selectedSection={selectedSection}
|
||||
setSelectedSection={setSelectedSection}
|
||||
/>
|
||||
|
||||
<ContentDiv
|
||||
className="no-scrollbar pr-0.5"
|
||||
hasSection={!!selectedSection}
|
||||
isVisible={isVisible}
|
||||
>
|
||||
<SelectedSection selectedSection={displayedSection} />
|
||||
</ContentDiv>
|
||||
</div>
|
||||
</LumpDiv>
|
||||
<ContentDiv
|
||||
className="no-scrollbar pr-0.5"
|
||||
hasSection={!!selectedSection}
|
||||
isVisible={isVisible}
|
||||
>
|
||||
<SelectedSection selectedSection={displayedSection} />
|
||||
</ContentDiv>
|
||||
</div>
|
||||
</LumpDiv>
|
||||
</LumpProvider>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ export function ExploreBlocksButton(props: { blockType: string }) {
|
|||
|
||||
return (
|
||||
<GhostButton
|
||||
className="w-full cursor-pointer rounded px-2 text-center text-gray-400 hover:text-gray-300"
|
||||
className="w-full cursor-pointer rounded px-2 py-0.5 text-center text-gray-400 hover:text-gray-300"
|
||||
style={{
|
||||
fontSize: fontSize(-3),
|
||||
}}
|
||||
|
|
@ -60,7 +60,8 @@ export function ExploreBlocksButton(props: { blockType: string }) {
|
|||
}}
|
||||
>
|
||||
<div className="flex items-center justify-center gap-1">
|
||||
<Icon className="h-3 w-3" /> {text}
|
||||
<Icon className="h-2.5 w-2.5 pr-1" />
|
||||
<span className="text-[11px]">{text}</span>
|
||||
</div>
|
||||
</GhostButton>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -3,44 +3,70 @@ import {
|
|||
PencilIcon,
|
||||
} from "@heroicons/react/24/outline";
|
||||
import { BookmarkIcon as BookmarkSolid } from "@heroicons/react/24/solid";
|
||||
import { SlashCommandDescription } from "core";
|
||||
import { useBookmarkedSlashCommands } from "../../../../hooks/useBookmarkedSlashCommands";
|
||||
import { useAppSelector } from "../../../../redux/hooks";
|
||||
import { fontSize } from "../../../../util";
|
||||
import { useMainEditor } from "../../TipTapEditor";
|
||||
import { useLump } from "../LumpContext";
|
||||
import { ExploreBlocksButton } from "./ExploreBlocksButton";
|
||||
|
||||
interface PromptRowProps {
|
||||
command: string;
|
||||
description: string;
|
||||
prompt: SlashCommandDescription;
|
||||
isBookmarked: boolean;
|
||||
setIsBookmarked: (isBookmarked: boolean) => void;
|
||||
onEdit?: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a single prompt row with bookmark and edit controls
|
||||
*/
|
||||
function PromptRow({
|
||||
command,
|
||||
description,
|
||||
prompt,
|
||||
isBookmarked,
|
||||
setIsBookmarked,
|
||||
onEdit,
|
||||
}: PromptRowProps) {
|
||||
const { mainEditor } = useMainEditor();
|
||||
const { hideLump } = useLump();
|
||||
|
||||
const handlePromptClick = (e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
mainEditor?.commands.insertPrompt(prompt);
|
||||
hideLump();
|
||||
};
|
||||
|
||||
const handleBookmarkClick = (e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
setIsBookmarked(!isBookmarked);
|
||||
};
|
||||
|
||||
const handleEditClick = (e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
if (onEdit) {
|
||||
onEdit();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
className="flex items-center justify-between gap-3"
|
||||
className="hover:bg-list-active hover:text-list-active-foreground flex items-center justify-between gap-3 rounded-md px-2 hover:cursor-pointer"
|
||||
onClick={handlePromptClick}
|
||||
style={{
|
||||
fontSize: fontSize(-3),
|
||||
}}
|
||||
>
|
||||
<div className="flex min-w-0 gap-2">
|
||||
<span className="text-vscForeground shrink-0">{command}</span>
|
||||
<span className="truncate text-gray-400">{description}</span>
|
||||
<span className="text-vscForeground shrink-0">{prompt.name}</span>
|
||||
<span className="truncate text-gray-400">{prompt.description}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<PencilIcon
|
||||
className="h-3 w-3 cursor-pointer text-gray-400 hover:brightness-125"
|
||||
onClick={onEdit}
|
||||
onClick={handleEditClick}
|
||||
/>
|
||||
<div
|
||||
onClick={() => setIsBookmarked(!isBookmarked)}
|
||||
onClick={handleBookmarkClick}
|
||||
className="cursor-pointer pt-0.5 text-gray-400 hover:brightness-125"
|
||||
>
|
||||
{isBookmarked ? (
|
||||
|
|
@ -54,8 +80,12 @@ function PromptRow({
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Section that displays all available prompts with bookmarking functionality
|
||||
*/
|
||||
export function PromptsSection() {
|
||||
const { isCommandBookmarked, toggleBookmark } = useBookmarkedSlashCommands();
|
||||
|
||||
const slashCommands = useAppSelector(
|
||||
(state) => state.config.config.slashCommands ?? [],
|
||||
);
|
||||
|
|
@ -78,8 +108,7 @@ export function PromptsSection() {
|
|||
{sortedCommands.map((prompt) => (
|
||||
<PromptRow
|
||||
key={prompt.name}
|
||||
command={prompt.name}
|
||||
description={prompt.description}
|
||||
prompt={prompt}
|
||||
isBookmarked={isCommandBookmarked(prompt.name)}
|
||||
setIsBookmarked={() => toggleBookmark(prompt)}
|
||||
onEdit={() => handleEdit(prompt)}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import {
|
|||
import { RootState } from "../../../../redux/store";
|
||||
import { fontSize } from "../../../../util";
|
||||
import HeaderButtonWithToolTip from "../../../gui/HeaderButtonWithToolTip";
|
||||
import { useLump } from "../LumpContext";
|
||||
import { ExploreBlocksButton } from "./ExploreBlocksButton";
|
||||
|
||||
interface RuleCardProps {
|
||||
|
|
@ -82,6 +83,7 @@ const RuleCard: React.FC<RuleCardProps> = ({ index, rule, onClick, title }) => {
|
|||
export function RulesSection() {
|
||||
const ideMessenger = useContext(IdeMessengerContext);
|
||||
const { selectedProfile } = useAuth();
|
||||
const { hideLump } = useLump();
|
||||
|
||||
const rules = useSelector(
|
||||
(state: RootState) => state.config.config.rules ?? [],
|
||||
|
|
@ -97,11 +99,13 @@ export function RulesSection() {
|
|||
}));
|
||||
}, [rules]);
|
||||
|
||||
const openUrl = (path: string) =>
|
||||
const openUrl = (path: string) => {
|
||||
ideMessenger.request("controlPlane/openUrl", {
|
||||
path,
|
||||
orgSlug: undefined,
|
||||
});
|
||||
hideLump();
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
|
|
@ -113,11 +117,12 @@ export function RulesSection() {
|
|||
key={index}
|
||||
index={index}
|
||||
rule={rule.unrolledRule}
|
||||
onClick={() =>
|
||||
onClick={() => {
|
||||
ideMessenger.post("config/openProfile", {
|
||||
profileId: undefined,
|
||||
})
|
||||
}
|
||||
});
|
||||
hideLump();
|
||||
}}
|
||||
title="Locally Defined Rule"
|
||||
/>
|
||||
);
|
||||
|
|
@ -158,6 +163,3 @@ export function RulesSection() {
|
|||
</div>
|
||||
);
|
||||
}
|
||||
function useTypedDispatch() {
|
||||
throw new Error("Function not implemented.");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import {
|
|||
import { fontSize } from "../../../../../util";
|
||||
import ConfirmationDialog from "../../../../dialogs/ConfirmationDialog";
|
||||
import EditBlockButton from "../../EditBlockButton";
|
||||
import { useLump } from "../../LumpContext";
|
||||
import { StatusIndicator } from "./StatusIndicator";
|
||||
interface IndexingStatusViewerProps {
|
||||
docConfig: SiteIndexingConfig;
|
||||
|
|
@ -24,6 +25,7 @@ function DocsIndexingStatus({
|
|||
}: IndexingStatusViewerProps) {
|
||||
const ideMessenger = useContext(IdeMessengerContext);
|
||||
const dispatch = useAppDispatch();
|
||||
const { hideLump } = useLump();
|
||||
|
||||
const status = useAppSelector(
|
||||
(store) => store.indexing.indexing.statuses[docConfig.startUrl],
|
||||
|
|
@ -87,6 +89,7 @@ function DocsIndexingStatus({
|
|||
onClick={() => {
|
||||
if (status?.url) {
|
||||
ideMessenger.post("openUrl", status.url);
|
||||
hideLump();
|
||||
}
|
||||
}}
|
||||
>
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ function DocsIndexingStatuses() {
|
|||
(store) => store.indexing.indexing.statuses,
|
||||
);
|
||||
const { selectedProfile } = useAuth();
|
||||
|
||||
const mergedDocs = useMemo(() => {
|
||||
const parsed = selectedProfile?.rawYaml
|
||||
? parseConfigYaml(selectedProfile?.rawYaml ?? "")
|
||||
|
|
|
|||
|
|
@ -102,6 +102,12 @@ export function ExpandableToolbarPreview(props: ExpandableToolbarPreviewProps) {
|
|||
);
|
||||
|
||||
const calculatedInitiallyHidden = useMemo(() => {
|
||||
// If initiallyHidden is explicitly set to false, always start visible
|
||||
if (props.initiallyHidden === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Otherwise, use the inputId/itemId logic
|
||||
if (props.inputId && props.itemId) {
|
||||
return newestCodeblockForInputId !== props.itemId;
|
||||
}
|
||||
|
|
@ -116,13 +122,19 @@ export function ExpandableToolbarPreview(props: ExpandableToolbarPreviewProps) {
|
|||
const [hidden, setHidden] = useState(calculatedInitiallyHidden);
|
||||
const [isExpanded, setIsExpanded] = useState(false);
|
||||
|
||||
// Track if user has manually toggled visibility
|
||||
const [userToggled, setUserToggled] = useState(false);
|
||||
|
||||
const contentRef = useRef<HTMLDivElement>(null);
|
||||
const [contentDims, setContentDims] = useState({ width: 0, height: 0 });
|
||||
|
||||
useEffect(() => {
|
||||
setHidden(calculatedInitiallyHidden);
|
||||
}, [calculatedInitiallyHidden]);
|
||||
|
||||
// Only update hidden state from props if user hasn't manually toggled
|
||||
// or if initiallyHidden isn't explicitly set to false
|
||||
if (!userToggled || props.initiallyHidden !== false) {
|
||||
setHidden(calculatedInitiallyHidden);
|
||||
}
|
||||
}, [calculatedInitiallyHidden, userToggled, props.initiallyHidden]);
|
||||
useEffect(() => {
|
||||
const resizeObserver = new ResizeObserver(() => {
|
||||
setContentDims({
|
||||
|
|
@ -146,7 +158,7 @@ export function ExpandableToolbarPreview(props: ExpandableToolbarPreviewProps) {
|
|||
borderColor={
|
||||
props.isSelected ? vscBadgeBackground : vscCommandCenterInactiveBorder
|
||||
}
|
||||
className="find-widget-skip"
|
||||
className="find-widget-skip !my-0"
|
||||
>
|
||||
<div
|
||||
className="border-b-vsc-commandCenter-inactiveBorder m-0 flex cursor-pointer items-center justify-between break-all border-0 border-b-[1px] border-solid px-[5px] py-1.5 hover:opacity-90"
|
||||
|
|
@ -154,6 +166,7 @@ export function ExpandableToolbarPreview(props: ExpandableToolbarPreviewProps) {
|
|||
fontSize: getFontSize() - 3,
|
||||
}}
|
||||
onClick={() => {
|
||||
setUserToggled(true);
|
||||
setHidden(!hidden);
|
||||
}}
|
||||
>
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ export const NodeViewWrapper: React.FC<NodeViewWrapperProps> = ({
|
|||
const nodeViewWrapperTag: TiptapNodeViewWrapperProps["as"] = "p";
|
||||
|
||||
return (
|
||||
<TiptapNodeViewWrapper className={`mt-0`} as={nodeViewWrapperTag}>
|
||||
<TiptapNodeViewWrapper className="my-1.5" as={nodeViewWrapperTag}>
|
||||
{children}
|
||||
</TiptapNodeViewWrapper>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import { mergeAttributes, Node } from "@tiptap/core";
|
||||
import { Node as ProseMirrorNode } from "@tiptap/pm/model";
|
||||
import { ReactNodeViewRenderer } from "@tiptap/react";
|
||||
import { ContextItemWithId } from "core";
|
||||
import { ContextItemWithId, SlashCommandDescription } from "core";
|
||||
import { MAIN_EDITOR_INPUT_ID } from "../../../../../pages/gui/Chat";
|
||||
import { PromptBlockPreview } from "./PromptBlockPreview";
|
||||
|
||||
export interface PromptBlockOptions {
|
||||
|
|
@ -19,7 +20,7 @@ declare module "@tiptap/core" {
|
|||
/**
|
||||
* Insert a prompt block into the editor
|
||||
*/
|
||||
insertPrompt: (item: ContextItemWithId, inputId: string) => ReturnType;
|
||||
insertPrompt: (item: SlashCommandDescription) => ReturnType;
|
||||
|
||||
/**
|
||||
* Clear all prompt blocks from the editor
|
||||
|
|
@ -35,6 +36,7 @@ declare module "@tiptap/core" {
|
|||
export const PromptBlock = Node.create<PromptBlockOptions>({
|
||||
name: "prompt-block",
|
||||
group: "block",
|
||||
content: "inline*",
|
||||
|
||||
addOptions() {
|
||||
return {
|
||||
|
|
@ -74,15 +76,28 @@ export const PromptBlock = Node.create<PromptBlockOptions>({
|
|||
addCommands() {
|
||||
return {
|
||||
insertPrompt:
|
||||
(item: ContextItemWithId, inputId: string) =>
|
||||
({ commands }) => {
|
||||
return commands.insertContent({
|
||||
type: this.name,
|
||||
attrs: {
|
||||
item,
|
||||
inputId,
|
||||
(prompt: SlashCommandDescription) =>
|
||||
({ chain }) => {
|
||||
const item: ContextItemWithId = {
|
||||
content: prompt.prompt ?? "",
|
||||
name: prompt.name,
|
||||
description: prompt.description,
|
||||
id: {
|
||||
providerTitle: "prompt",
|
||||
itemId: prompt.name,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
return chain()
|
||||
.insertContentAt(0, {
|
||||
type: this.name,
|
||||
attrs: {
|
||||
item,
|
||||
inputId: MAIN_EDITOR_INPUT_ID,
|
||||
},
|
||||
})
|
||||
.focus("end")
|
||||
.run();
|
||||
},
|
||||
|
||||
// TODO: This could probably be greatly simplified with something along the lines of
|
||||
|
|
|
|||
|
|
@ -27,12 +27,13 @@ export const PromptBlockPreview = ({
|
|||
};
|
||||
|
||||
const handleDelete = () => {
|
||||
editor.commands.clearSlashCommand();
|
||||
editor.commands.clearPrompt();
|
||||
};
|
||||
|
||||
return (
|
||||
<NodeViewWrapper>
|
||||
<ExpandableToolbarPreview
|
||||
initiallyHidden={false}
|
||||
isSelected={selected}
|
||||
title={item.name}
|
||||
icon={<ChatBubbleLeftIcon className="h-3 w-3 pl-1 pr-0.5" />}
|
||||
|
|
|
|||
|
|
@ -1,191 +1,50 @@
|
|||
// Adapted from SlashCommand extension (@tiptap/extension-mention/src/mention.ts)
|
||||
|
||||
import { mergeAttributes, Node } from "@tiptap/core";
|
||||
import Document from "@tiptap/extension-document";
|
||||
import Paragraph from "@tiptap/extension-paragraph";
|
||||
import { Node as ProseMirrorNode } from "@tiptap/pm/model";
|
||||
import { Node } from "@tiptap/core";
|
||||
import { PluginKey } from "@tiptap/pm/state";
|
||||
import Suggestion, { SuggestionOptions } from "@tiptap/suggestion";
|
||||
import { ContextItemWithId, SlashCommandDescription } from "core";
|
||||
import { MAIN_EDITOR_INPUT_ID } from "../../../../pages/gui/Chat";
|
||||
import { ComboBoxItem } from "../../types";
|
||||
import { PromptBlock } from "./Prompt/PromptBlock";
|
||||
|
||||
export type SlashCommandOptions = {
|
||||
HTMLAttributes: Record<string, any>;
|
||||
renderText: (props: { node: ProseMirrorNode }) => string;
|
||||
suggestion: Omit<SuggestionOptions<ComboBoxItem, ComboBoxItem>, "editor">;
|
||||
};
|
||||
|
||||
declare module "@tiptap/core" {
|
||||
interface Commands<ReturnType> {
|
||||
slashCommand: {
|
||||
/**
|
||||
* Insert a slash command and its associated prompt block
|
||||
* @param command The slash command description
|
||||
*/
|
||||
insertSlashCommand: (command: SlashCommandDescription) => ReturnType;
|
||||
|
||||
/**
|
||||
* Clear the slash command content from the editor
|
||||
*/
|
||||
clearSlashCommand: () => ReturnType;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const SlashCommand = Node.create<SlashCommandOptions>({
|
||||
name: "slash-command",
|
||||
|
||||
addOptions() {
|
||||
return {
|
||||
HTMLAttributes: {
|
||||
class: "mention",
|
||||
},
|
||||
renderText({ node }) {
|
||||
return node.attrs.label ?? node.attrs.id;
|
||||
},
|
||||
suggestion: {
|
||||
char: "/",
|
||||
pluginKey: new PluginKey(this.name),
|
||||
startOfLine: true,
|
||||
command: ({ editor, range, props }) => {
|
||||
// increase range.to by one when the next node is of type "text"
|
||||
// and starts with a space character
|
||||
const nodeAfter = editor.view.state.selection.$to.nodeAfter;
|
||||
const overrideSpace = nodeAfter?.text?.startsWith(" ");
|
||||
// First delete the slash character and any text after it
|
||||
editor.chain().focus().deleteRange(range).run();
|
||||
|
||||
if (overrideSpace) {
|
||||
range.to += 1;
|
||||
}
|
||||
|
||||
editor.commands.insertSlashCommand({
|
||||
editor.commands.insertPrompt({
|
||||
name: props.title,
|
||||
description: props.description,
|
||||
prompt: props.content,
|
||||
});
|
||||
|
||||
window.getSelection()?.collapseToEnd();
|
||||
},
|
||||
allow: ({ state, range }) => {
|
||||
const $from = state.doc.resolve(range.from);
|
||||
const type = state.schema.nodes[this.name];
|
||||
const allow = !!$from.parent.type.contentMatch.matchType(type);
|
||||
|
||||
return allow;
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
group: "inline",
|
||||
|
||||
inline: true,
|
||||
|
||||
selectable: false,
|
||||
|
||||
atom: true,
|
||||
|
||||
// We don't need attributes since we won't be rendering any node
|
||||
addAttributes() {
|
||||
return {
|
||||
id: {
|
||||
default: null,
|
||||
parseHTML: (element) => element.getAttribute("data-id"),
|
||||
renderHTML: (attributes) => {
|
||||
if (!attributes.id) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
"data-id": attributes.id,
|
||||
};
|
||||
},
|
||||
},
|
||||
|
||||
label: {
|
||||
default: null,
|
||||
parseHTML: (element) => element.getAttribute("data-label"),
|
||||
renderHTML: (attributes) => {
|
||||
if (!attributes.label) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
"data-label": attributes.label,
|
||||
};
|
||||
},
|
||||
},
|
||||
};
|
||||
return {};
|
||||
},
|
||||
|
||||
// No need to parse HTML since we're not rendering anything
|
||||
parseHTML() {
|
||||
return [
|
||||
{
|
||||
tag: `span[data-type="${this.name}"]`,
|
||||
},
|
||||
];
|
||||
},
|
||||
|
||||
renderHTML({ node, HTMLAttributes }) {
|
||||
return [
|
||||
"span",
|
||||
mergeAttributes(
|
||||
{ "data-type": this.name },
|
||||
this.options.HTMLAttributes,
|
||||
HTMLAttributes,
|
||||
),
|
||||
this.options.renderText({
|
||||
node,
|
||||
}),
|
||||
];
|
||||
},
|
||||
|
||||
addKeyboardShortcuts() {
|
||||
return {
|
||||
Backspace: () => {
|
||||
// Get a reference to the editor
|
||||
const editor = this.editor;
|
||||
|
||||
// First try to find if we're at a slash command
|
||||
const result = editor.commands.command(({ tr, state }) => {
|
||||
let isSlashCommand = false;
|
||||
const { selection } = state;
|
||||
const { empty, anchor } = selection;
|
||||
|
||||
if (!empty) {
|
||||
return false;
|
||||
}
|
||||
|
||||
state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {
|
||||
if (node.type.name === this.name) {
|
||||
isSlashCommand = true;
|
||||
tr.insertText(
|
||||
this.options.suggestion.char || "",
|
||||
pos,
|
||||
pos + node.nodeSize,
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
return isSlashCommand;
|
||||
});
|
||||
|
||||
// If we found and processed a slash command, also call clearSlashCommand
|
||||
// to remove the prompt block
|
||||
if (result) {
|
||||
// We need to do this with a small delay to ensure the transaction
|
||||
// above completes first
|
||||
setTimeout(() => {
|
||||
editor.commands.clearSlashCommand();
|
||||
}, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
};
|
||||
return [];
|
||||
},
|
||||
|
||||
addProseMirrorPlugins() {
|
||||
|
|
@ -196,90 +55,4 @@ export const SlashCommand = Node.create<SlashCommandOptions>({
|
|||
}),
|
||||
];
|
||||
},
|
||||
|
||||
addCommands() {
|
||||
return {
|
||||
insertSlashCommand:
|
||||
(
|
||||
command: SlashCommandDescription,
|
||||
inputId: string = MAIN_EDITOR_INPUT_ID,
|
||||
) =>
|
||||
({ chain }) => {
|
||||
if (!command.prompt) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const contextItem: ContextItemWithId = {
|
||||
content: command.prompt,
|
||||
name: command.name,
|
||||
description: command.description || "",
|
||||
id: {
|
||||
providerTitle: "prompt",
|
||||
itemId: command.name,
|
||||
},
|
||||
};
|
||||
|
||||
// Right now this inserts at the beginning of the document, which removes codeblocks.
|
||||
// This is the current behavior of slash commands, eg codeblocks aren't sent.
|
||||
// Once this bug if fixed, we should instead insert prompt block at the beginning of the document
|
||||
// without clearing the codeblocks.
|
||||
return chain()
|
||||
.setContent({
|
||||
type: Document.name,
|
||||
content: [
|
||||
// First insert the prompt using the PromptExtension command
|
||||
{
|
||||
type: PromptBlock.name,
|
||||
attrs: {
|
||||
item: contextItem,
|
||||
inputId,
|
||||
},
|
||||
},
|
||||
// Add the paragraph with the slash command
|
||||
{
|
||||
type: Paragraph.name,
|
||||
content: [
|
||||
{
|
||||
type: this.name,
|
||||
attrs: {
|
||||
id: command.name,
|
||||
label: command.name,
|
||||
},
|
||||
},
|
||||
{
|
||||
type: "text",
|
||||
text: " ",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
.focus()
|
||||
.run();
|
||||
},
|
||||
|
||||
clearSlashCommand:
|
||||
() =>
|
||||
({ editor }) => {
|
||||
editor.commands.clearPrompt();
|
||||
|
||||
editor.commands.command(({ tr, state }) => {
|
||||
let hasDeleted = false;
|
||||
|
||||
// Find all slash command nodes in the document
|
||||
state.doc.descendants((node, pos) => {
|
||||
if (node.type.name === this.name) {
|
||||
// Delete the node at the found position
|
||||
tr.delete(pos, pos + node.nodeSize);
|
||||
hasDeleted = true;
|
||||
}
|
||||
});
|
||||
|
||||
return hasDeleted;
|
||||
});
|
||||
|
||||
return true;
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import {
|
|||
} from "../../../redux/slices/sessionSlice";
|
||||
import { AppDispatch } from "../../../redux/store";
|
||||
import { loadSession, saveCurrentSession } from "../../../redux/thunks/session";
|
||||
import { CodeBlock } from "./extensions";
|
||||
import { CodeBlock, PromptBlock } from "./extensions";
|
||||
|
||||
/**
|
||||
* Hook for setting up main editor specific webview listeners
|
||||
|
|
@ -111,16 +111,20 @@ export function useMainEditorWebviewListeners({
|
|||
|
||||
let index = 0;
|
||||
for (const el of editor.getJSON()?.content ?? []) {
|
||||
// Prevent exact duplicate code blocks
|
||||
if (el.attrs?.item?.name === contextItem.name) {
|
||||
return; // Prevent exact duplicate code blocks
|
||||
return;
|
||||
}
|
||||
if (el.type === CodeBlock.name) {
|
||||
|
||||
if (el.type === CodeBlock.name || el.type === PromptBlock.name) {
|
||||
index += 2;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
debugger;
|
||||
|
||||
editor
|
||||
.chain()
|
||||
.insertContentAt(index, {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import {
|
|||
import { MutableRefObject } from "react";
|
||||
import tippy from "tippy.js";
|
||||
import { IIdeMessenger } from "../../../../context/IdeMessenger";
|
||||
import { setNewestToolbarPreviewForInput } from "../../../../redux/slices/sessionSlice";
|
||||
import { AppDispatch } from "../../../../redux/store";
|
||||
import AtMentionDropdown from "../../AtMentionDropdown";
|
||||
import { ComboBoxItem, ComboBoxItemType, ComboBoxSubAction } from "../../types";
|
||||
|
|
@ -222,16 +221,7 @@ export function getSlashCommandDropdownOptions(
|
|||
label: provider.title,
|
||||
type: (provider.type ?? SlashCommand.name) as ComboBoxItemType,
|
||||
content: provider.content,
|
||||
action: () => {
|
||||
dispatch(
|
||||
setNewestToolbarPreviewForInput({
|
||||
inputId,
|
||||
contextItemId: provider.title,
|
||||
}),
|
||||
);
|
||||
|
||||
provider.action?.();
|
||||
},
|
||||
action: provider.action,
|
||||
}));
|
||||
|
||||
if (query.length === 0 && commandItems.length === 0) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue