✨ input box history

This commit is contained in:
Nate Sesti 2024-04-24 17:16:56 -07:00
parent b5b81b66dd
commit 88966412eb
3 changed files with 110 additions and 0 deletions

View file

@ -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);
}
},

View file

@ -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<JSONContent[]>(
getLocalStorage("inputHistory") ?? [],
);
const [pendingInput, setPendingInput] = useState<JSONContent>(
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 };
}

View file

@ -1,7 +1,10 @@
import { JSONContent } from "@tiptap/react";
type LocalStorageTypes = {
onboardingComplete: boolean;
mainTextEntryCounter: number;
lastSessionId: string | undefined;
inputHistory: JSONContent[];
};
export function getLocalStorage<T extends keyof LocalStorageTypes>(