diff --git a/gui/src/components/mainInput/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor.tsx index 9d2a49e91..3cbf6c218 100644 --- a/gui/src/components/mainInput/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor.tsx @@ -27,6 +27,7 @@ import { } from ".."; import { SubmenuContextProvidersContext } from "../../App"; import useHistory from "../../hooks/useHistory"; +import { useInputHistory } from "../../hooks/useInputHistory"; import useUpdatingRef from "../../hooks/useUpdatingRef"; import { useWebviewListener } from "../../hooks/useWebviewListener"; import { defaultModelSelector } from "../../redux/selectors/modelSelectors"; @@ -238,6 +239,8 @@ function TipTapEditor(props: TipTapEditorProps) { (store: RootState) => store.state.mainEditorContent, ); + const { prevRef, nextRef, addRef } = useInputHistory(); + const editor: Editor = useEditor({ extensions: [ Document, @@ -273,6 +276,41 @@ function TipTapEditor(props: TipTapEditorProps) { () => commands.liftEmptyBlock(), () => commands.splitBlock(), ]), + + ArrowUp: () => { + if (this.editor.state.selection.anchor > 1) { + return false; + } + + const previousInput = prevRef.current( + this.editor.state.toJSON().doc, + ); + if (previousInput) { + this.editor.commands.setContent(previousInput); + setTimeout(() => { + this.editor.commands.blur(); + this.editor.commands.focus("start"); + }, 0); + return true; + } + }, + ArrowDown: () => { + if ( + this.editor.state.selection.anchor < + this.editor.state.doc.content.size - 1 + ) { + return false; + } + const nextInput = nextRef.current(); + if (nextInput) { + this.editor.commands.setContent(nextInput); + setTimeout(() => { + this.editor.commands.blur(); + this.editor.commands.focus("end"); + }, 0); + return true; + } + }, }; }, }).configure({ @@ -376,6 +414,8 @@ function TipTapEditor(props: TipTapEditorProps) { props.onEnter(json, modifiers); if (props.isMainInput) { + const content = editor.state.toJSON().doc; + addRef.current(content); editor.commands.clearContent(true); } }, diff --git a/gui/src/hooks/useInputHistory.ts b/gui/src/hooks/useInputHistory.ts new file mode 100644 index 000000000..cac897d8d --- /dev/null +++ b/gui/src/hooks/useInputHistory.ts @@ -0,0 +1,67 @@ +import { JSONContent } from "@tiptap/react"; +import { useState } from "react"; +import { getLocalStorage, setLocalStorage } from "../util/localStorage"; +import useUpdatingRef from "./useUpdatingRef"; + +const emptyJsonContent = () => ({ + type: "doc", + content: [{ type: "paragraph", content: [{ type: "text", text: "" }] }], +}); + +export function useInputHistory() { + const [inputHistory, setInputHistory] = useState( + getLocalStorage("inputHistory") ?? [], + ); + const [pendingInput, setPendingInput] = useState( + emptyJsonContent(), + ); + const [currentIndex, setCurrentIndex] = useState(inputHistory.length); + + function prev(currentInput: JSONContent) { + let index = currentIndex; + + if (index === inputHistory.length) { + setPendingInput(currentInput); + } + + if (index > 0 && index <= inputHistory.length) { + setCurrentIndex((prevState) => prevState - 1); + return inputHistory[index - 1]; + } + } + + function next() { + let index = currentIndex; + if (index >= 0 && index < inputHistory.length) { + setCurrentIndex((prevState) => prevState + 1); + if (index === inputHistory.length - 1) { + return pendingInput; + } + return inputHistory[index + 1]; + } + } + + function add(inputValue: JSONContent) { + setPendingInput(emptyJsonContent()); + + if ( + JSON.stringify(inputHistory[inputHistory.length - 1]) === + JSON.stringify(inputValue) + ) { + setCurrentIndex(inputHistory.length); + return; + } else { + setCurrentIndex(inputHistory.length + 1); + } + setInputHistory((prev) => { + return [...prev, inputValue]; + }); + setLocalStorage("inputHistory", [...inputHistory, inputValue]); + } + + const prevRef = useUpdatingRef(prev, [inputHistory]); + const nextRef = useUpdatingRef(next, [inputHistory]); + const addRef = useUpdatingRef(add, [inputHistory]); + + return { prevRef, nextRef, addRef }; +} diff --git a/gui/src/util/localStorage.ts b/gui/src/util/localStorage.ts index 2ad952bd3..84658a5df 100644 --- a/gui/src/util/localStorage.ts +++ b/gui/src/util/localStorage.ts @@ -1,7 +1,10 @@ +import { JSONContent } from "@tiptap/react"; + type LocalStorageTypes = { onboardingComplete: boolean; mainTextEntryCounter: number; lastSessionId: string | undefined; + inputHistory: JSONContent[]; }; export function getLocalStorage(