🐛 fix repeated paste bug

This commit is contained in:
Nate Sesti 2024-05-14 23:00:09 -07:00
parent e877f8ab7a
commit c3fd16bd9d

View file

@ -420,7 +420,7 @@ function TipTapEditor(props: TipTapEditorProps) {
},
});
const shortcutTimeoutRef = useRef<NodeJS.Timeout | undefined>(undefined);
const editorFocusedRef = useUpdatingRef(editor?.isFocused, [editor]);
useEffect(() => {
if (isJetBrains()) {
@ -429,35 +429,18 @@ function TipTapEditor(props: TipTapEditorProps) {
}
const handleKeyDown = async (event: KeyboardEvent) => {
if (!editor) return;
const shouldSkip = () => {
const skip = shortcutTimeoutRef.current !== undefined;
if (!skip) {
shortcutTimeoutRef.current = setTimeout(async () => {
shortcutTimeoutRef.current = undefined;
}, 100);
}
return skip
}
if (!editor || !editorFocusedRef.current) return;
if (event.metaKey && event.key === "x") {
if (!shouldSkip()) {
document.execCommand("cut");
}
document.execCommand("cut");
event.stopPropagation();
event.preventDefault();
} else if (event.metaKey && event.key === "v") {
if (!shouldSkip()) {
document.execCommand("paste");
}
document.execCommand("paste");
event.stopPropagation();
event.preventDefault();
} else if (event.metaKey && event.key === "c") {
if (!shouldSkip()) {
document.execCommand("copy");
}
document.execCommand("copy");
event.stopPropagation();
event.preventDefault();
}
@ -468,7 +451,7 @@ function TipTapEditor(props: TipTapEditorProps) {
return () => {
document.removeEventListener("keydown", handleKeyDown);
};
}, [editor, shortcutTimeoutRef]);
}, [editor, editorFocusedRef]);
useEffect(() => {
if (mainEditorContent && editor) {
@ -525,7 +508,7 @@ function TipTapEditor(props: TipTapEditorProps) {
// Re-focus main input after done generating
useEffect(() => {
if (editor && !active && props.isMainInput && document.hasFocus()) {
editor.commands.focus(undefined, {scrollIntoView: false});
editor.commands.focus(undefined, { scrollIntoView: false });
}
}, [props.isMainInput, active, editor]);