Revert "feat(editor): implement up/down arrow key history navigation for edit mode"
This reverts commit fc92d55c8c.
This commit is contained in:
parent
12fb378afb
commit
e4860171c7
2 changed files with 33 additions and 89 deletions
|
|
@ -301,9 +301,7 @@ function TipTapEditor(props: TipTapEditorProps) {
|
|||
}
|
||||
}
|
||||
|
||||
// Ensure we're using the correct history key - chat for main input, edit for edit mode
|
||||
const historyKey = props.historyKey === 'edit' ? 'edit' : 'chat';
|
||||
const { prevRef, nextRef, addRef } = useInputHistory(historyKey);
|
||||
const { prevRef, nextRef, addRef } = useInputHistory(props.historyKey);
|
||||
|
||||
const editor: Editor | null = useEditor({
|
||||
extensions: [
|
||||
|
|
@ -662,9 +660,9 @@ function TipTapEditor(props: TipTapEditorProps) {
|
|||
return;
|
||||
}
|
||||
|
||||
// Always add to input history regardless of input type
|
||||
// This ensures that both chat and edit modes maintain their own history
|
||||
addRef.current(json);
|
||||
if (props.isMainInput) {
|
||||
addRef.current(json);
|
||||
}
|
||||
|
||||
props.onEnter(json, modifiers, editor);
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { JSONContent } from "@tiptap/react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useState } from "react";
|
||||
import { getLocalStorage, setLocalStorage } from "../util/localStorage";
|
||||
import useUpdatingRef from "./useUpdatingRef";
|
||||
|
||||
|
|
@ -10,112 +10,58 @@ const emptyJsonContent = () => ({
|
|||
|
||||
const MAX_HISTORY_LENGTH = 100;
|
||||
|
||||
// Completely isolated memory stores for different history types
|
||||
// This ensures no crossover between edit and chat histories
|
||||
const CHAT_HISTORY_STORE: JSONContent[] = [];
|
||||
const EDIT_HISTORY_STORE: JSONContent[] = [];
|
||||
|
||||
// Initialize the history stores from localStorage (only do this once)
|
||||
let storesInitialized = false;
|
||||
function initializeStores() {
|
||||
|
||||
if (!storesInitialized) {
|
||||
// Initialize chat history
|
||||
const chatHistory = getLocalStorage('inputHistory_chat');
|
||||
if (chatHistory) {
|
||||
CHAT_HISTORY_STORE.push(...chatHistory.slice(-MAX_HISTORY_LENGTH));
|
||||
}
|
||||
|
||||
// Initialize edit history
|
||||
const editHistory = getLocalStorage('inputHistory_edit');
|
||||
if (editHistory) {
|
||||
EDIT_HISTORY_STORE.push(...editHistory.slice(-MAX_HISTORY_LENGTH));
|
||||
}
|
||||
|
||||
storesInitialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Initialize stores immediately
|
||||
initializeStores();
|
||||
|
||||
/**
|
||||
* Custom hook for managing input history with complete separation between history types
|
||||
*/
|
||||
export function useInputHistory(historyKey: string) {
|
||||
// Determine which history store to use
|
||||
const historyStore = historyKey === 'edit' ? EDIT_HISTORY_STORE : CHAT_HISTORY_STORE;
|
||||
const storageKey = historyKey === 'edit' ? 'inputHistory_edit' as const : 'inputHistory_chat' as const;
|
||||
|
||||
// Use a reference to the appropriate history store
|
||||
const [inputHistory, setInputHistory] = useState<JSONContent[]>(historyStore);
|
||||
const [pendingInput, setPendingInput] = useState<JSONContent>(emptyJsonContent());
|
||||
const [inputHistory, setInputHistory] = useState<JSONContent[]>(
|
||||
getLocalStorage(`inputHistory_${historyKey}`)?.slice(-MAX_HISTORY_LENGTH) ??
|
||||
[],
|
||||
);
|
||||
const [pendingInput, setPendingInput] =
|
||||
useState<JSONContent>(emptyJsonContent());
|
||||
const [currentIndex, setCurrentIndex] = useState(inputHistory.length);
|
||||
|
||||
// Keep the view of history updated when the underlying store changes
|
||||
useEffect(() => {
|
||||
setInputHistory([...historyStore]);
|
||||
setCurrentIndex(historyStore.length);
|
||||
}, [historyStore.length]);
|
||||
|
||||
function prev(currentInput: JSONContent) {
|
||||
if (currentIndex === inputHistory.length) {
|
||||
let index = currentIndex;
|
||||
|
||||
if (index === inputHistory.length) {
|
||||
setPendingInput(currentInput);
|
||||
}
|
||||
|
||||
if (currentIndex > 0) {
|
||||
setCurrentIndex(currentIndex - 1);
|
||||
return inputHistory[currentIndex - 1];
|
||||
if (index > 0 && index <= inputHistory.length) {
|
||||
setCurrentIndex((prevState) => prevState - 1);
|
||||
return inputHistory[index - 1];
|
||||
}
|
||||
|
||||
// Stay at the first history item if we're already there
|
||||
if (currentIndex === 0 && inputHistory.length > 0) {
|
||||
return inputHistory[0];
|
||||
}
|
||||
|
||||
return currentInput;
|
||||
}
|
||||
|
||||
function next() {
|
||||
if (currentIndex < inputHistory.length) {
|
||||
setCurrentIndex(currentIndex + 1);
|
||||
|
||||
if (currentIndex === inputHistory.length - 1) {
|
||||
let index = currentIndex;
|
||||
if (index >= 0 && index < inputHistory.length) {
|
||||
setCurrentIndex((prevState) => prevState + 1);
|
||||
if (index === inputHistory.length - 1) {
|
||||
return pendingInput;
|
||||
}
|
||||
|
||||
return inputHistory[currentIndex + 1];
|
||||
return inputHistory[index + 1];
|
||||
}
|
||||
|
||||
// Stay at pending input if we're already at the end
|
||||
return pendingInput;
|
||||
}
|
||||
|
||||
function add(inputValue: JSONContent) {
|
||||
setPendingInput(emptyJsonContent());
|
||||
|
||||
// Don't add duplicates
|
||||
if (
|
||||
historyStore.length > 0 &&
|
||||
JSON.stringify(historyStore[historyStore.length - 1]) === JSON.stringify(inputValue)
|
||||
JSON.stringify(inputHistory[inputHistory.length - 1]) ===
|
||||
JSON.stringify(inputValue)
|
||||
) {
|
||||
setCurrentIndex(historyStore.length);
|
||||
setCurrentIndex(inputHistory.length);
|
||||
return;
|
||||
}
|
||||
|
||||
// Update the appropriate history store
|
||||
while (historyStore.length >= MAX_HISTORY_LENGTH) {
|
||||
historyStore.shift(); // Remove oldest items if we exceed the limit
|
||||
}
|
||||
historyStore.push(inputValue);
|
||||
|
||||
// Update the view of history
|
||||
setInputHistory([...historyStore]);
|
||||
setCurrentIndex(historyStore.length);
|
||||
|
||||
// Update localStorage
|
||||
setLocalStorage(storageKey, [...historyStore]);
|
||||
setCurrentIndex(inputHistory.length + 1);
|
||||
setInputHistory((prev) => {
|
||||
return [...prev, inputValue].slice(-MAX_HISTORY_LENGTH);
|
||||
});
|
||||
setLocalStorage(
|
||||
`inputHistory_${historyKey}`,
|
||||
[...inputHistory, inputValue].slice(-MAX_HISTORY_LENGTH),
|
||||
);
|
||||
}
|
||||
|
||||
const prevRef = useUpdatingRef(prev, [inputHistory]);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue