From afc7c7bf7afcc8a11353260a44a556e002b5763f Mon Sep 17 00:00:00 2001 From: Majd_dk Date: Sun, 28 Sep 2025 11:34:46 +0300 Subject: [PATCH] add shorcut --- src/Hooks/useHandlePasteKeyDown.ts | 24 ++++++++++++++---------- src/utils/keyDownUtils.ts | 30 +++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/Hooks/useHandlePasteKeyDown.ts b/src/Hooks/useHandlePasteKeyDown.ts index 913240b..afd98a5 100644 --- a/src/Hooks/useHandlePasteKeyDown.ts +++ b/src/Hooks/useHandlePasteKeyDown.ts @@ -1,5 +1,5 @@ -import { useFormikContext } from "formik"; import { useCallback } from "react"; +import { useFormikContext } from "formik"; import { getLatexOnCustomPased } from "../utils/keyDownUtils"; export function useHandlePasteKeyDown() { @@ -7,18 +7,22 @@ export function useHandlePasteKeyDown() { const handleCustomPasteKeyDown = useCallback( async (e: React.KeyboardEvent, fieldName: string) => { - const latex = await getLatexOnCustomPased(e); - if (!latex) return; + const latexCtlrShiftV = await getLatexOnCustomPased(e,!e.ctrlKey || !e.shiftKey || e.code !== "KeyV" ); + const latexCtlrCheftBackquote = await getLatexOnCustomPased(e,!e.ctrlKey || !e.shiftKey || e.code !== "Backquote" ); + if (!latexCtlrShiftV && !latexCtlrCheftBackquote) return; - // set fieldValue - const currentValue = (e.target as HTMLInputElement).value; - const final = currentValue - ? `${currentValue} $$ ${latex} $$` - : `$$ ${latex} $$`; + const input = e.target as HTMLInputElement; + const toInsert = !!latexCtlrShiftV ? `$$ ${latexCtlrShiftV} $$` : `$$$ ${latexCtlrCheftBackquote} $$$` + + e.preventDefault?.(); + + input.focus(); + document.execCommand('insertText', false, toInsert); + + input.dispatchEvent(new Event("input", { bubbles: true })); - formik.setFieldValue(fieldName, final); }, - [formik] + [] ); return handleCustomPasteKeyDown; diff --git a/src/utils/keyDownUtils.ts b/src/utils/keyDownUtils.ts index ab04f99..7bc191c 100644 --- a/src/utils/keyDownUtils.ts +++ b/src/utils/keyDownUtils.ts @@ -2,9 +2,10 @@ import { getTextFromClipboard } from "./clipboardUtils"; import { convertMathMLToLaTeX } from "./convertMathMLToLaTeX"; export async function getLatexOnCustomPased( - e: React.KeyboardEvent + e: React.KeyboardEvent, + shortCut:boolean ): Promise { - if (!e.ctrlKey || !e.shiftKey || e.code !== "KeyV" ) return null; + if (shortCut) return null; e.preventDefault(); const copyedValue = await getTextFromClipboard(); @@ -13,7 +14,22 @@ export async function getLatexOnCustomPased( return latex; } -export function getLatexOnCustomShortCut( +export function getLatexOnCtrlShiftThKeyDown( + e: React.KeyboardEvent +): string | null { + if (!e.ctrlKey || !e.shiftKey || e.code !== "Backquote") return null; + + e.preventDefault(); + + const selectedText = getSelectedText(e) + if (!selectedText) return null; + + const latex = convertMathMLToLaTeX(selectedText); + const result = replaceSelectedTo(e,latex,"$$$"); + + return result; +} +export function getLatexOnCtrlShiftQkeyDown( e: React.KeyboardEvent ): string | null { if (!e.ctrlKey || !e.shiftKey || e.code !== "KeyQ") return null; @@ -24,7 +40,7 @@ export function getLatexOnCustomShortCut( if (!selectedText) return null; const latex = convertMathMLToLaTeX(selectedText); - const result = replaceSelectedTo(e,latex); + const result = replaceSelectedTo(e,latex,"$$"); return result; } @@ -36,13 +52,13 @@ export function getSelectedText(e:React.KeyboardEvent){ return selectedText } -export function replaceSelectedTo(e: React.KeyboardEvent,newValue:string) { +export function replaceSelectedTo(e: React.KeyboardEvent,newValue:string,encabsolation?:string) { const {start,end,input} = getIndexesOfSelectionValue(e) const before = input.value.substring(0, start); const after = input.value.substring(end); - - const result = `${before}$$ ${newValue} $$${after}`; + const trimValue = newValue.trim() + const result = `${encabsolation} ${trimValue.slice(2,trimValue.length-2)} ${encabsolation}`; return result;